forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.test.ts
More file actions
361 lines (304 loc) · 13.2 KB
/
client.test.ts
File metadata and controls
361 lines (304 loc) · 13.2 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, expect, it, onTestFinished, vi } from "vitest";
import { approveAll, CopilotClient } from "../src/index.js";
// This file is for unit tests. Where relevant, prefer to add e2e tests in e2e/*.test.ts instead
describe("CopilotClient", () => {
it("throws when createSession is called without onPermissionRequest", async () => {
const client = new CopilotClient();
await client.start();
onTestFinished(() => client.forceStop());
await expect((client as any).createSession({})).rejects.toThrow(
/onPermissionRequest.*is required/
);
});
it("throws when resumeSession is called without onPermissionRequest", async () => {
const client = new CopilotClient();
await client.start();
onTestFinished(() => client.forceStop());
const session = await client.createSession({ onPermissionRequest: approveAll });
await expect((client as any).resumeSession(session.sessionId, {})).rejects.toThrow(
/onPermissionRequest.*is required/
);
});
it("returns a standardized failure result when a tool is not registered", async () => {
const client = new CopilotClient();
await client.start();
onTestFinished(() => client.forceStop());
const session = await client.createSession({ onPermissionRequest: approveAll });
const response = await (
client as unknown as { handleToolCallRequest: (typeof client)["handleToolCallRequest"] }
).handleToolCallRequest({
sessionId: session.sessionId,
toolCallId: "123",
toolName: "missing_tool",
arguments: {},
});
expect(response.result).toMatchObject({
resultType: "failure",
error: "tool 'missing_tool' not supported",
});
});
it("forwards clientName in session.create request", async () => {
const client = new CopilotClient();
await client.start();
onTestFinished(() => client.forceStop());
const spy = vi.spyOn((client as any).connection!, "sendRequest");
await client.createSession({ clientName: "my-app", onPermissionRequest: approveAll });
expect(spy).toHaveBeenCalledWith(
"session.create",
expect.objectContaining({ clientName: "my-app" })
);
});
it("forwards clientName in session.resume request", async () => {
const client = new CopilotClient();
await client.start();
onTestFinished(() => client.forceStop());
const session = await client.createSession({ onPermissionRequest: approveAll });
// Mock sendRequest to capture the call without hitting the runtime
const spy = vi
.spyOn((client as any).connection!, "sendRequest")
.mockImplementation(async (method: string, params: any) => {
if (method === "session.resume") return { sessionId: params.sessionId };
throw new Error(`Unexpected method: ${method}`);
});
await client.resumeSession(session.sessionId, {
clientName: "my-app",
onPermissionRequest: approveAll,
});
expect(spy).toHaveBeenCalledWith(
"session.resume",
expect.objectContaining({ clientName: "my-app", sessionId: session.sessionId })
);
spy.mockRestore();
});
it("sends session.model.switchTo RPC with correct params", async () => {
const client = new CopilotClient();
await client.start();
onTestFinished(() => client.forceStop());
const session = await client.createSession({ onPermissionRequest: approveAll });
// Mock sendRequest to capture the call without hitting the runtime
const spy = vi
.spyOn((client as any).connection!, "sendRequest")
.mockImplementation(async (method: string, _params: any) => {
if (method === "session.model.switchTo") return {};
// Fall through for other methods (shouldn't be called)
throw new Error(`Unexpected method: ${method}`);
});
await session.setModel("gpt-4.1");
expect(spy).toHaveBeenCalledWith("session.model.switchTo", {
sessionId: session.sessionId,
modelId: "gpt-4.1",
});
spy.mockRestore();
});
describe("URL parsing", () => {
it("should parse port-only URL format", () => {
const client = new CopilotClient({
cliUrl: "8080",
logLevel: "error",
});
// Verify internal state
expect((client as any).actualPort).toBe(8080);
expect((client as any).actualHost).toBe("localhost");
expect((client as any).isExternalServer).toBe(true);
});
it("should parse host:port URL format", () => {
const client = new CopilotClient({
cliUrl: "127.0.0.1:9000",
logLevel: "error",
});
expect((client as any).actualPort).toBe(9000);
expect((client as any).actualHost).toBe("127.0.0.1");
expect((client as any).isExternalServer).toBe(true);
});
it("should parse http://host:port URL format", () => {
const client = new CopilotClient({
cliUrl: "http://localhost:7000",
logLevel: "error",
});
expect((client as any).actualPort).toBe(7000);
expect((client as any).actualHost).toBe("localhost");
expect((client as any).isExternalServer).toBe(true);
});
it("should parse https://host:port URL format", () => {
const client = new CopilotClient({
cliUrl: "https://example.com:443",
logLevel: "error",
});
expect((client as any).actualPort).toBe(443);
expect((client as any).actualHost).toBe("example.com");
expect((client as any).isExternalServer).toBe(true);
});
it("should throw error for invalid URL format", () => {
expect(() => {
new CopilotClient({
cliUrl: "invalid-url",
logLevel: "error",
});
}).toThrow(/Invalid cliUrl format/);
});
it("should throw error for invalid port - too high", () => {
expect(() => {
new CopilotClient({
cliUrl: "localhost:99999",
logLevel: "error",
});
}).toThrow(/Invalid port in cliUrl/);
});
it("should throw error for invalid port - zero", () => {
expect(() => {
new CopilotClient({
cliUrl: "localhost:0",
logLevel: "error",
});
}).toThrow(/Invalid port in cliUrl/);
});
it("should throw error for invalid port - negative", () => {
expect(() => {
new CopilotClient({
cliUrl: "localhost:-1",
logLevel: "error",
});
}).toThrow(/Invalid port in cliUrl/);
});
it("should throw error when cliUrl is used with useStdio", () => {
expect(() => {
new CopilotClient({
cliUrl: "localhost:8080",
useStdio: true,
logLevel: "error",
});
}).toThrow(/cliUrl is mutually exclusive/);
});
it("should throw error when cliUrl is used with cliPath", () => {
expect(() => {
new CopilotClient({
cliUrl: "localhost:8080",
cliPath: "/path/to/cli",
logLevel: "error",
});
}).toThrow(/cliUrl is mutually exclusive/);
});
it("should set useStdio to false when cliUrl is provided", () => {
const client = new CopilotClient({
cliUrl: "8080",
logLevel: "error",
});
expect(client["options"].useStdio).toBe(false);
});
it("should mark client as using external server", () => {
const client = new CopilotClient({
cliUrl: "localhost:8080",
logLevel: "error",
});
expect((client as any).isExternalServer).toBe(true);
});
});
describe("Auth options", () => {
it("should accept githubToken option", () => {
const client = new CopilotClient({
githubToken: "gho_test_token",
logLevel: "error",
});
expect((client as any).options.githubToken).toBe("gho_test_token");
});
it("should default useLoggedInUser to true when no githubToken", () => {
const client = new CopilotClient({
logLevel: "error",
});
expect((client as any).options.useLoggedInUser).toBe(true);
});
it("should default useLoggedInUser to false when githubToken is provided", () => {
const client = new CopilotClient({
githubToken: "gho_test_token",
logLevel: "error",
});
expect((client as any).options.useLoggedInUser).toBe(false);
});
it("should allow explicit useLoggedInUser: true with githubToken", () => {
const client = new CopilotClient({
githubToken: "gho_test_token",
useLoggedInUser: true,
logLevel: "error",
});
expect((client as any).options.useLoggedInUser).toBe(true);
});
it("should allow explicit useLoggedInUser: false without githubToken", () => {
const client = new CopilotClient({
useLoggedInUser: false,
logLevel: "error",
});
expect((client as any).options.useLoggedInUser).toBe(false);
});
it("should throw error when githubToken is used with cliUrl", () => {
expect(() => {
new CopilotClient({
cliUrl: "localhost:8080",
githubToken: "gho_test_token",
logLevel: "error",
});
}).toThrow(/githubToken and useLoggedInUser cannot be used with cliUrl/);
});
it("should throw error when useLoggedInUser is used with cliUrl", () => {
expect(() => {
new CopilotClient({
cliUrl: "localhost:8080",
useLoggedInUser: false,
logLevel: "error",
});
}).toThrow(/githubToken and useLoggedInUser cannot be used with cliUrl/);
});
});
describe("overridesBuiltInTool in tool definitions", () => {
it("sends overridesBuiltInTool in tool definition on session.create", async () => {
const client = new CopilotClient();
await client.start();
onTestFinished(() => client.forceStop());
const spy = vi.spyOn((client as any).connection!, "sendRequest");
await client.createSession({
onPermissionRequest: approveAll,
tools: [
{
name: "grep",
description: "custom grep",
handler: async () => "ok",
overridesBuiltInTool: true,
},
],
});
const payload = spy.mock.calls.find((c) => c[0] === "session.create")![1] as any;
expect(payload.tools).toEqual([
expect.objectContaining({ name: "grep", overridesBuiltInTool: true }),
]);
});
it("sends overridesBuiltInTool in tool definition on session.resume", async () => {
const client = new CopilotClient();
await client.start();
onTestFinished(() => client.forceStop());
const session = await client.createSession({ onPermissionRequest: approveAll });
// Mock sendRequest to capture the call without hitting the runtime
const spy = vi
.spyOn((client as any).connection!, "sendRequest")
.mockImplementation(async (method: string, params: any) => {
if (method === "session.resume") return { sessionId: params.sessionId };
throw new Error(`Unexpected method: ${method}`);
});
await client.resumeSession(session.sessionId, {
onPermissionRequest: approveAll,
tools: [
{
name: "grep",
description: "custom grep",
handler: async () => "ok",
overridesBuiltInTool: true,
},
],
});
const payload = spy.mock.calls.find((c) => c[0] === "session.resume")![1] as any;
expect(payload.tools).toEqual([
expect.objectContaining({ name: "grep", overridesBuiltInTool: true }),
]);
spy.mockRestore();
});
});
});