-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathToolInvocationTest.java
More file actions
177 lines (143 loc) · 5.46 KB
/
ToolInvocationTest.java
File metadata and controls
177 lines (143 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.copilot.sdk.json.ToolInvocation;
/**
* Unit tests for {@link ToolInvocation}.
* <p>
* Tests getter methods, type-safe deserialization, and null handling to improve
* coverage beyond what E2E tests exercise.
*/
public class ToolInvocationTest {
/**
* Test all basic getters return values set via setters.
*/
@Test
void testGettersReturnSetValues() {
ToolInvocation invocation = new ToolInvocation().setSessionId("test-session-123").setToolCallId("call_abc123")
.setToolName("test_tool");
assertEquals("test-session-123", invocation.getSessionId());
assertEquals("call_abc123", invocation.getToolCallId());
assertEquals("test_tool", invocation.getToolName());
}
/**
* Test getArguments returns null when no arguments are set.
*/
@Test
void testGetArgumentsWhenNull() {
ToolInvocation invocation = new ToolInvocation();
assertNull(invocation.getArguments(), "getArguments should return null when argumentsNode is null");
}
/**
* Test getArguments returns a Map when arguments are set.
*/
@Test
void testGetArgumentsReturnsMap() {
ToolInvocation invocation = new ToolInvocation();
// Create a JsonNode with some arguments
ObjectNode argsNode = JsonNodeFactory.instance.objectNode();
argsNode.put("location", "San Francisco");
argsNode.put("units", "celsius");
invocation.setArguments(argsNode);
var args = invocation.getArguments();
assertNotNull(args);
assertEquals("San Francisco", args.get("location"));
assertEquals("celsius", args.get("units"));
}
/**
* Test getArgumentsAs deserializes to a record type.
*/
@Test
void testGetArgumentsAsWithRecord() {
ToolInvocation invocation = new ToolInvocation();
// Create a JsonNode with weather arguments
ObjectNode argsNode = JsonNodeFactory.instance.objectNode();
argsNode.put("city", "Paris");
argsNode.put("units", "metric");
invocation.setArguments(argsNode);
// Deserialize to record
WeatherArgs args = invocation.getArgumentsAs(WeatherArgs.class);
assertNotNull(args);
assertEquals("Paris", args.city());
assertEquals("metric", args.units());
}
/**
* Test getArgumentsAs deserializes to a POJO.
*/
@Test
void testGetArgumentsAsWithPojo() {
ToolInvocation invocation = new ToolInvocation();
// Create a JsonNode with user data
ObjectNode argsNode = JsonNodeFactory.instance.objectNode();
argsNode.put("username", "alice");
argsNode.put("age", 30);
invocation.setArguments(argsNode);
// Deserialize to POJO
UserData userData = invocation.getArgumentsAs(UserData.class);
assertNotNull(userData);
assertEquals("alice", userData.getUsername());
assertEquals(30, userData.getAge());
}
/**
* Test getArgumentsAs throws IllegalArgumentException on deserialization
* failure.
*/
@Test
void testGetArgumentsAsThrowsOnInvalidType() {
ToolInvocation invocation = new ToolInvocation();
// Create invalid JSON for the target type (missing required field)
ObjectNode argsNode = JsonNodeFactory.instance.objectNode();
argsNode.put("invalid_field", "value");
invocation.setArguments(argsNode);
// Try to deserialize to a type that doesn't match
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> invocation.getArgumentsAs(StrictType.class),
"Should throw IllegalArgumentException for invalid deserialization");
assertTrue(exception.getMessage().contains("Failed to deserialize arguments"));
assertTrue(exception.getMessage().contains("StrictType"));
}
/**
* Record for testing type-safe argument deserialization.
*/
record WeatherArgs(String city, String units) {
}
/**
* POJO for testing type-safe argument deserialization.
*/
public static class UserData {
private String username;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
/**
* Strict type with constructor that throws, for testing error handling.
*/
public static class StrictType {
private final String requiredField;
public StrictType(String requiredField) {
if (requiredField == null) {
throw new IllegalArgumentException("requiredField cannot be null");
}
this.requiredField = requiredField;
}
public String getRequiredField() {
return requiredField;
}
}
}