-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMetadataApiTest.java
More file actions
333 lines (281 loc) · 11.8 KB
/
MetadataApiTest.java
File metadata and controls
333 lines (281 loc) · 11.8 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.copilot.sdk.events.SessionEventParser;
import com.github.copilot.sdk.events.ToolExecutionProgressEvent;
import com.github.copilot.sdk.json.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/**
* Tests for the new metadata APIs (getStatus, getAuthStatus, listModels) and
* the ToolExecutionProgressEvent.
*/
public class MetadataApiTest {
private static String cliPath;
private static final ObjectMapper MAPPER = new ObjectMapper();
@BeforeAll
static void setup() {
cliPath = getCliPath();
}
private static String getCliPath() {
// First, try to find 'copilot' in PATH
String copilotInPath = findCopilotInPath();
if (copilotInPath != null) {
return copilotInPath;
}
// Fall back to COPILOT_CLI_PATH environment variable
String envPath = System.getenv("COPILOT_CLI_PATH");
if (envPath != null && !envPath.isEmpty()) {
return envPath;
}
// Search for the CLI in the parent directories (nodejs module)
Path current = Paths.get(System.getProperty("user.dir"));
while (current != null) {
Path cliPath = current.resolve("nodejs/node_modules/@github/copilot/index.js");
if (cliPath.toFile().exists()) {
return cliPath.toString();
}
current = current.getParent();
}
return null;
}
private static String findCopilotInPath() {
try {
String command = System.getProperty("os.name").toLowerCase().contains("win") ? "where" : "which";
ProcessBuilder pb = new ProcessBuilder(command, "copilot");
pb.redirectErrorStream(true);
Process process = pb.start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line = reader.readLine();
int exitCode = process.waitFor();
if (exitCode == 0 && line != null && !line.isEmpty()) {
return line.trim();
}
}
} catch (Exception e) {
// Ignore - copilot not found in PATH
}
return null;
}
// ===== ToolExecutionProgressEvent Tests =====
@Test
void testToolExecutionProgressEventParsing() {
String json = """
{
"type": "tool.execution_progress",
"id": "550e8400-e29b-41d4-a716-446655440000",
"timestamp": "2026-01-22T10:00:00Z",
"data": {
"toolCallId": "call-123",
"progressMessage": "Processing file 1 of 10..."
}
}
""";
var event = SessionEventParser.parse(json);
assertNotNull(event);
assertInstanceOf(ToolExecutionProgressEvent.class, event);
ToolExecutionProgressEvent progressEvent = (ToolExecutionProgressEvent) event;
assertEquals("tool.execution_progress", progressEvent.getType());
assertNotNull(progressEvent.getData());
assertEquals("call-123", progressEvent.getData().getToolCallId());
assertEquals("Processing file 1 of 10...", progressEvent.getData().getProgressMessage());
}
@Test
void testToolExecutionProgressEventType() {
assertEquals("tool.execution_progress", ToolExecutionProgressEvent.TYPE);
}
// ===== Response Type Deserialization Tests =====
@Test
void testGetStatusResponseDeserialization() throws Exception {
String json = """
{
"version": "1.2.3",
"protocolVersion": 2
}
""";
GetStatusResponse response = MAPPER.readValue(json, GetStatusResponse.class);
assertEquals("1.2.3", response.getVersion());
assertEquals(2, response.getProtocolVersion());
}
@Test
void testGetAuthStatusResponseDeserialization() throws Exception {
String json = """
{
"isAuthenticated": true,
"authType": "user",
"host": "github.com",
"login": "testuser",
"statusMessage": "Authenticated successfully"
}
""";
GetAuthStatusResponse response = MAPPER.readValue(json, GetAuthStatusResponse.class);
assertTrue(response.isAuthenticated());
assertEquals("user", response.getAuthType());
assertEquals("github.com", response.getHost());
assertEquals("testuser", response.getLogin());
assertEquals("Authenticated successfully", response.getStatusMessage());
}
@Test
void testGetAuthStatusResponseNotAuthenticated() throws Exception {
String json = """
{
"isAuthenticated": false,
"statusMessage": "Not authenticated"
}
""";
GetAuthStatusResponse response = MAPPER.readValue(json, GetAuthStatusResponse.class);
assertFalse(response.isAuthenticated());
assertNull(response.getAuthType());
assertNull(response.getHost());
assertNull(response.getLogin());
assertEquals("Not authenticated", response.getStatusMessage());
}
@Test
void testModelInfoDeserialization() throws Exception {
String json = """
{
"id": "gpt-4",
"name": "GPT-4",
"capabilities": {
"supports": {
"vision": true
},
"limits": {
"max_prompt_tokens": 8192,
"max_context_window_tokens": 128000,
"vision": {
"supported_media_types": ["image/png", "image/jpeg"],
"max_prompt_images": 10,
"max_prompt_image_size": 20971520
}
}
},
"policy": {
"state": "active",
"terms": "https://example.com/terms"
},
"billing": {
"multiplier": 1.5
}
}
""";
ModelInfo model = MAPPER.readValue(json, ModelInfo.class);
assertEquals("gpt-4", model.getId());
assertEquals("GPT-4", model.getName());
// Capabilities
assertNotNull(model.getCapabilities());
assertTrue(model.getCapabilities().getSupports().isVision());
assertEquals(8192, model.getCapabilities().getLimits().getMaxPromptTokens());
assertEquals(128000, model.getCapabilities().getLimits().getMaxContextWindowTokens());
// Vision limits
ModelVisionLimits visionLimits = model.getCapabilities().getLimits().getVision();
assertNotNull(visionLimits);
assertEquals(List.of("image/png", "image/jpeg"), visionLimits.getSupportedMediaTypes());
assertEquals(10, visionLimits.getMaxPromptImages());
assertEquals(20971520, visionLimits.getMaxPromptImageSize());
// Policy
assertNotNull(model.getPolicy());
assertEquals("active", model.getPolicy().getState());
assertEquals("https://example.com/terms", model.getPolicy().getTerms());
// Billing
assertNotNull(model.getBilling());
assertEquals(1.5, model.getBilling().getMultiplier());
}
@Test
void testGetModelsResponseDeserialization() throws Exception {
String json = """
{
"models": [
{
"id": "gpt-4",
"name": "GPT-4",
"capabilities": {
"supports": { "vision": false },
"limits": { "max_context_window_tokens": 8192 }
}
},
{
"id": "claude-3",
"name": "Claude 3",
"capabilities": {
"supports": { "vision": true },
"limits": { "max_context_window_tokens": 200000 }
}
}
]
}
""";
GetModelsResponse response = MAPPER.readValue(json, GetModelsResponse.class);
assertNotNull(response.getModels());
assertEquals(2, response.getModels().size());
assertEquals("gpt-4", response.getModels().get(0).getId());
assertEquals("claude-3", response.getModels().get(1).getId());
}
// ===== Integration Tests (require CLI) =====
@Test
void testGetStatus() throws Exception {
if (cliPath == null) {
System.out.println("Skipping test: CLI not found");
return;
}
try (var client = new CopilotClient(new CopilotClientOptions().setCliPath(cliPath).setUseStdio(true))) {
client.start().get();
GetStatusResponse status = client.getStatus().get();
assertNotNull(status);
assertNotNull(status.getVersion());
assertFalse(status.getVersion().isEmpty());
assertEquals(SdkProtocolVersion.get(), status.getProtocolVersion());
}
}
@Test
void testGetAuthStatus() throws Exception {
if (cliPath == null) {
System.out.println("Skipping test: CLI not found");
return;
}
try (var client = new CopilotClient(new CopilotClientOptions().setCliPath(cliPath).setUseStdio(true))) {
client.start().get();
GetAuthStatusResponse authStatus = client.getAuthStatus().get();
assertNotNull(authStatus);
// The response should have a status message regardless of auth state
// We can't guarantee the user is authenticated in tests
}
}
@Test
void testListModels() throws Exception {
if (cliPath == null) {
System.out.println("Skipping test: CLI not found");
return;
}
try (var client = new CopilotClient(new CopilotClientOptions().setCliPath(cliPath).setUseStdio(true))) {
client.start().get();
// Note: listModels may require authentication
// This test verifies the method exists and can be called
try {
List<ModelInfo> models = client.listModels().get();
assertNotNull(models);
// If we got models, verify they have expected fields
for (ModelInfo model : models) {
assertNotNull(model.getId());
assertNotNull(model.getName());
}
} catch (Exception e) {
// May fail if not authenticated, which is acceptable in tests
System.out.println("listModels failed (may require auth): " + e.getMessage());
}
}
}
// ===== Protocol Version Test =====
@Test
void testProtocolVersionIsTwo() {
assertEquals(2, SdkProtocolVersion.get());
}
}