-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathclient.e2e.test.ts
More file actions
201 lines (162 loc) · 7.4 KB
/
client.e2e.test.ts
File metadata and controls
201 lines (162 loc) · 7.4 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
import { ChildProcess } from "child_process";
import { describe, expect, it, onTestFinished } from "vitest";
import { CopilotClient, approveAll, RuntimeConnection } from "../../src/index.js";
function onTestFinishedForceStop(client: CopilotClient) {
onTestFinished(async () => {
try {
await client.forceStop();
} 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() });
onTestFinishedForceStop(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 }),
});
onTestFinishedForceStop(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 }),
});
onTestFinishedForceStop(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();
onTestFinishedForceStop(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() });
onTestFinishedForceStop(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() });
await client.createSession({ onPermissionRequest: approveAll });
// Kill the server processto force cleanup to fail
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cliProcess = (client as any).cliProcess as ChildProcess;
expect(cliProcess).toBeDefined();
cliProcess.kill("SIGKILL");
await new Promise((resolve) => setTimeout(resolve, 100));
const errors = await client.stop();
if (errors.length > 0) {
expect(errors[0].message).toContain("Failed to disconnect session");
}
},
// Generous timeout: client.stop() must wait for session.destroy to time out
// when the server process is dead. The default 30s can flake on slow CI under load.
60_000
);
it("should forceStop without cleanup", async () => {
const client = new CopilotClient({});
onTestFinishedForceStop(client);
await client.createSession({ onPermissionRequest: approveAll });
await client.forceStop();
});
it("should get status with version and protocol info", async () => {
const client = new CopilotClient();
onTestFinishedForceStop(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();
onTestFinishedForceStop(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();
});
it("should list models when authenticated", async () => {
const client = new CopilotClient();
onTestFinishedForceStop(client);
await client.start();
const authStatus = await client.getAuthStatus();
if (!authStatus.isAuthenticated) {
// Skip if not authenticated - models.list requires auth
await client.stop();
return;
}
const models = await client.listModels();
expect(Array.isArray(models)).toBe(true);
if (models.length > 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();
}
await client.stop();
});
it("should report error with stderr when CLI fails to start", async () => {
const client = new CopilotClient({
connection: RuntimeConnection.forStdio({ args: ["--nonexistent-flag-for-testing"] }),
});
onTestFinishedForceStop(client);
let initialError: Error | undefined;
try {
await client.start();
expect.fail("Expected start() to throw an error");
} catch (error) {
initialError = error as Error;
expect(initialError.message).toContain("stderr");
expect(initialError.message).toContain("nonexistent");
}
// 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 as Error).message).toContain("Connection is closed");
}
});
});