Repository navigation
Expand file tree
/
Copy pathclient.e2e.test.ts
More file actions
258 lines (220 loc) · 10.6 KB
/
Copy pathclient.e2e.test.ts
File metadata and controls
258 lines (220 loc) · 10.6 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
import { ChildProcess } from "child_process";
import type { Socket } from "node:net";
import { describe, expect, it, onTestFinished, vi } from "vitest";
import { approveAll, CopilotClient, RuntimeConnection } from "../../src/index.js";
import { createSdkTestContext, isInProcessTransport } from "./harness/sdkTestContext.js";
function onTestFinishedStop(client: CopilotClient) {
onTestFinished(async () => {
try {
await client.stop();
} catch {
// Ignore cleanup errors - process may already be stopped
}
});
}
describe("Client", () => {
it.each([
{ transport: "stdio", connection: () => undefined },
{ transport: "tcp", connection: () => RuntimeConnection.forTcp() },
])("allows createSession without onPermissionRequest ($transport)", async ({ connection }) => {
const client = new CopilotClient({ connection: connection() });
onTestFinishedStop(client);
await using session = await client.createSession({});
expect(session.sessionId).toMatch(/^[a-f0-9-]+$/);
});
it("allows resumeSession without onPermissionRequest", async () => {
const connectionToken = "client-e2e-resume-token";
const client = new CopilotClient({
connection: RuntimeConnection.forTcp({ connectionToken }),
});
onTestFinishedStop(client);
await using originalSession = await client.createSession({});
const port = (client as unknown as { runtimePort: number | null }).runtimePort;
if (port == null) {
throw new Error("Client must be using TCP transport to support multi-client resume.");
}
const resumeClient = new CopilotClient({
connection: RuntimeConnection.forUri(`localhost:${port}`, { connectionToken }),
});
onTestFinishedStop(resumeClient);
await using resumedSession = await resumeClient.resumeSession(
originalSession.sessionId,
{}
);
expect(resumedSession.sessionId).toBe(originalSession.sessionId);
});
it("should start and connect to server using stdio", async () => {
const client = new CopilotClient();
onTestFinishedStop(client);
await client.start();
const pong = await client.ping("test message");
expect(pong.message).toBe("pong: test message");
expect(Date.parse(pong.timestamp)).not.toBeNaN();
expect(await client.stop()).toHaveLength(0); // No errors on stop
});
it("should start and connect to server using tcp", async () => {
const client = new CopilotClient({ connection: RuntimeConnection.forTcp() });
onTestFinishedStop(client);
await client.start();
const pong = await client.ping("test message");
expect(pong.message).toBe("pong: test message");
expect(Date.parse(pong.timestamp)).not.toBeNaN();
expect(await client.stop()).toHaveLength(0); // No errors on stop
});
it.skipIf(process.platform === "darwin")(
"should stop cleanly when the server exits during cleanup",
async () => {
// Use TCP mode to avoid stdin stream destruction issues
// Without this, on macOS there are intermittent test failures
// saying "Cannot call write after a stream was destroyed"
// because the JSON-RPC logic is still trying to write to stdin after
// the process has exited.
const client = new CopilotClient({ connection: RuntimeConnection.forTcp() });
let phase = "createSession";
const startedAt = Date.now();
let phaseStartedAt = startedAt;
const phaseTimes = ["createSession=0ms"];
let stopSocket: Socket | null = null;
let stopProcess: ChildProcess | null = null;
const logPendingPhase = (reason: string) => {
const status = client as unknown as {
state: string;
connectionClosed: boolean;
connection: unknown;
socket: Socket | null;
cliProcess: ChildProcess | null;
};
const socket = stopSocket ?? status.socket;
const child = stopProcess ?? status.cliProcess;
console.warn(
`${reason}: phase=${phase} for ${Date.now() - phaseStartedAt}ms, ` +
`phases=${phaseTimes.join(", ")}, state=${status.state}, ` +
`connectionClosed=${status.connectionClosed}, connectionActive=${status.connection !== null}, ` +
`child exitCode=${child?.exitCode}, signalCode=${child?.signalCode}, ` +
`socket destroyed=${socket?.destroyed}, readyState=${socket?.readyState}`
);
};
let diagnosticTimer = setTimeout(() => logPendingPhase("Still waiting"), 20_000);
const startPhase = (nextPhase: string) => {
clearTimeout(diagnosticTimer);
phase = nextPhase;
phaseStartedAt = Date.now();
phaseTimes.push(`${phase}=${phaseStartedAt - startedAt}ms`);
diagnosticTimer = setTimeout(() => logPendingPhase("Still waiting"), 20_000);
};
let completed = false;
onTestFinished(async () => {
clearTimeout(diagnosticTimer);
if (!completed) logPendingPhase("Test ended before completion");
await client.forceStop();
});
await client.createSession({ onPermissionRequest: approveAll });
startPhase("child exit");
// Kill the server process to force cleanup to fail
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cliProcess = (client as any).cliProcess as ChildProcess;
expect(cliProcess).toBeDefined();
expect(cliProcess.kill("SIGKILL")).toBe(true);
await vi.waitFor(
() => {
const socket = (
client as unknown as {
socket: { destroyed: boolean; readyState: string } | null;
}
).socket;
expect(
(client as unknown as { state: string }).state,
`After SIGKILL: child exitCode=${cliProcess.exitCode}, signalCode=${cliProcess.signalCode}, ` +
`socket destroyed=${socket?.destroyed}, readyState=${socket?.readyState}`
).toBe("disconnected");
},
{ timeout: 10_000 }
);
stopSocket = (client as unknown as { socket: Socket | null }).socket;
stopProcess = cliProcess;
phaseTimes.push(
`socketAtStop=${stopSocket?.readyState}/${stopSocket?.destroyed}, ` +
`childAtStop=${cliProcess.exitCode}/${cliProcess.signalCode}`
);
startPhase("client.stop()");
const errors = await client.stop();
clearTimeout(diagnosticTimer);
if (errors.length > 0) {
expect(errors[0].message).toContain("Failed to disconnect session");
}
completed = true;
},
// Generous timeout: client.stop() must wait for session.detach to time out
// when the server process is dead. The default 30s can flake on slow CI under load.
60_000
);
// Skipping on in-proc:
// - It breaks the macOS E2E run (failure: EPIPE)
// - It's not clear that anyone should use forceStop in the in-proc case - there's no child process
// to terminate, so we can't be sure to leave a clean state
// - If you want to get to a clean state within your process, that's what "stop" (not "forceStop") is for
it.skipIf(isInProcessTransport)("should forceStop without cleanup", async () => {
const client = new CopilotClient({});
onTestFinishedStop(client);
await client.createSession({ onPermissionRequest: approveAll });
await client.forceStop();
});
it("should get status with version and protocol info", async () => {
const client = new CopilotClient();
onTestFinishedStop(client);
await client.start();
const status = await client.getStatus();
expect(status.version).toBeDefined();
expect(typeof status.version).toBe("string");
expect(status.protocolVersion).toBeDefined();
expect(typeof status.protocolVersion).toBe("number");
expect(status.protocolVersion).toBeGreaterThanOrEqual(1);
await client.stop();
});
it("should get auth status", async () => {
const client = new CopilotClient();
onTestFinishedStop(client);
await client.start();
const authStatus = await client.getAuthStatus();
expect(typeof authStatus.isAuthenticated).toBe("boolean");
if (authStatus.isAuthenticated) {
expect(authStatus.authType).toBeDefined();
expect(authStatus.statusMessage).toBeDefined();
}
await client.stop();
});
describe("model catalog", async () => {
const { copilotClient: client } = await createSdkTestContext();
it("should list models when authenticated", async () => {
await client.start();
const authStatus = await client.getAuthStatus();
expect(authStatus.isAuthenticated).toBe(true);
const models = await client.listModels();
expect(Array.isArray(models)).toBe(true);
expect(models.length).toBeGreaterThan(0);
const model = models[0];
expect(model.id).toBeDefined();
expect(model.name).toBeDefined();
expect(model.capabilities).toBeDefined();
expect(model.capabilities.supports).toBeDefined();
expect(model.capabilities.limits).toBeDefined();
});
});
it.skipIf(isInProcessTransport)("should report error when CLI fails to start", async () => {
const client = new CopilotClient({
connection: RuntimeConnection.forStdio({
args: ["--nonexistent-flag-for-testing"],
}),
});
onTestFinishedStop(client);
await expect(client.start()).rejects.toBeInstanceOf(Error);
// Verify subsequent calls also fail (don't hang)
try {
const session = await client.createSession({ onPermissionRequest: approveAll });
await session.send("test");
expect.fail("Expected send() to throw an error after CLI exit");
} catch (error) {
expect(error).toBeInstanceOf(Error);
}
});
});