-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathclient_options.e2e.test.ts
More file actions
332 lines (285 loc) · 11.5 KB
/
client_options.e2e.test.ts
File metadata and controls
332 lines (285 loc) · 11.5 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import * as fs from "fs";
import * as net from "net";
import * as path from "path";
import { describe, expect, it, onTestFinished } from "vitest";
import { approveAll, CopilotClient, RuntimeConnection } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext.js";
const FAKE_STDIO_CLI_SCRIPT = `const fs = require("fs");
const captureIndex = process.argv.indexOf("--capture-file");
const captureFile = captureIndex >= 0 ? process.argv[captureIndex + 1] : undefined;
const requests = [];
function saveCapture() {
if (!captureFile) {
return;
}
fs.writeFileSync(captureFile, JSON.stringify({
args: process.argv.slice(2),
cwd: process.cwd(),
requests,
env: {
COPILOT_HOME: process.env.COPILOT_HOME,
COPILOT_SDK_AUTH_TOKEN: process.env.COPILOT_SDK_AUTH_TOKEN,
COPILOT_OTEL_ENABLED: process.env.COPILOT_OTEL_ENABLED,
OTEL_EXPORTER_OTLP_ENDPOINT: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
COPILOT_OTEL_FILE_EXPORTER_PATH: process.env.COPILOT_OTEL_FILE_EXPORTER_PATH,
COPILOT_OTEL_EXPORTER_TYPE: process.env.COPILOT_OTEL_EXPORTER_TYPE,
COPILOT_OTEL_SOURCE_NAME: process.env.COPILOT_OTEL_SOURCE_NAME,
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT: process.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT
}
}));
}
saveCapture();
let buffer = Buffer.alloc(0);
process.stdin.on("data", chunk => {
buffer = Buffer.concat([buffer, chunk]);
processBuffer();
});
process.stdin.resume();
function processBuffer() {
while (true) {
const headerEnd = buffer.indexOf("\\r\\n\\r\\n");
if (headerEnd < 0) {
return;
}
const header = buffer.subarray(0, headerEnd).toString("utf8");
const match = /Content-Length:\\s*(\\d+)/i.exec(header);
if (!match) {
throw new Error("Missing Content-Length header");
}
const length = Number(match[1]);
const bodyStart = headerEnd + 4;
const bodyEnd = bodyStart + length;
if (buffer.length < bodyEnd) {
return;
}
const body = buffer.subarray(bodyStart, bodyEnd).toString("utf8");
buffer = buffer.subarray(bodyEnd);
handleMessage(JSON.parse(body));
}
}
function handleMessage(message) {
if (!Object.prototype.hasOwnProperty.call(message, "id")) {
return;
}
requests.push({ method: message.method, params: message.params });
saveCapture();
if (message.method === "connect") {
writeResponse(message.id, { ok: true, protocolVersion: 3, version: "fake" });
return;
}
if (message.method === "ping") {
writeResponse(message.id, { message: "pong", protocolVersion: 3 });
return;
}
if (message.method === "session.create") {
const sessionId = message.params?.sessionId ?? message.params?.[0]?.sessionId ?? "fake-session";
writeResponse(message.id, { sessionId, workspacePath: null, capabilities: null });
return;
}
writeResponse(message.id, {});
}
function writeResponse(id, result) {
const body = JSON.stringify({ jsonrpc: "2.0", id, result });
process.stdout.write(\`Content-Length: \${Buffer.byteLength(body, "utf8")}\\r\\n\\r\\n\${body}\`);
}
`;
async function getAvailableTcpPort(): Promise<number> {
return new Promise((resolve, reject) => {
const server = net.createServer();
server.once("error", reject);
server.listen(0, "127.0.0.1", () => {
const address = server.address();
if (typeof address === "object" && address !== null) {
const port = address.port;
server.close(() => resolve(port));
} else {
server.close(() => reject(new Error("Failed to get available TCP port")));
}
});
});
}
function assertArgumentValue(
args: (string | undefined)[],
name: string,
expectedValue: string
): void {
const index = args.indexOf(name);
expect(
index,
`Expected argument '${name}' was not present. Args: ${args.join(" ")}`
).toBeGreaterThanOrEqual(0);
expect(index + 1).toBeLessThan(args.length);
expect(args[index + 1]).toBe(expectedValue);
}
describe("Client options", async () => {
const { copilotClient: defaultClient, env, workDir } = await createSdkTestContext();
it("createSession starts the client lazily", async () => {
const client = new CopilotClient({
workingDirectory: workDir,
env,
connection: RuntimeConnection.forStdio({ path: process.env.COPILOT_CLI_PATH }),
});
onTestFinished(async () => {
try {
await client.forceStop();
} catch {
// Ignore cleanup errors
}
});
const session = await client.createSession({ onPermissionRequest: approveAll });
expect(session.sessionId).toMatch(/^[a-f0-9-]+$/);
await session.disconnect();
});
it("should listen on configured tcp port", async () => {
const port = await getAvailableTcpPort();
const client = new CopilotClient({
workingDirectory: workDir,
env,
connection: RuntimeConnection.forTcp({
path: process.env.COPILOT_CLI_PATH,
port,
}),
});
onTestFinished(async () => {
try {
await client.forceStop();
} catch {
// Ignore cleanup errors
}
});
await client.start();
expect((client as unknown as { runtimePort: number }).runtimePort).toBe(port);
const response = await client.ping("fixed-port");
expect(response.message).toBe("pong: fixed-port");
});
it("should use client cwd for default workingdirectory", async () => {
const clientCwd = path.join(workDir, "client-cwd");
fs.mkdirSync(clientCwd, { recursive: true });
fs.writeFileSync(path.join(clientCwd, "marker.txt"), "I am in the client cwd");
// Reference defaultClient to keep the shared test context (and its CAPI proxy/env)
// alive for the duration of this test; we deliberately spin up a fresh client with
// a custom cwd to assert that the custom cwd is honored.
void defaultClient;
const client = new CopilotClient({
workingDirectory: clientCwd,
env,
connection: RuntimeConnection.forStdio({ path: process.env.COPILOT_CLI_PATH }),
gitHubToken: process.env.CI ? "fake-token-for-e2e-tests" : undefined,
});
onTestFinished(async () => {
try {
await client.forceStop();
} catch {
// Ignore cleanup errors
}
});
const session = await client.createSession({ onPermissionRequest: approveAll });
const message = await session.sendAndWait({
prompt: "Read the file marker.txt and tell me what it says",
});
expect(message?.data.content ?? "").toContain("client cwd");
await session.disconnect();
});
it("should propagate process options to spawned cli", async () => {
const cliPath = path.join(
workDir,
`fake-cli-${Date.now()}-${Math.random().toString(36).slice(2)}.js`
);
const capturePath = path.join(
workDir,
`fake-cli-capture-${Date.now()}-${Math.random().toString(36).slice(2)}.json`
);
const telemetryPath = path.join(workDir, "telemetry.jsonl");
const copilotHomeFromEnv = path.join(workDir, "copilot-home-from-env");
const copilotHomeFromOption = path.join(workDir, "copilot-home-from-option");
fs.writeFileSync(cliPath, FAKE_STDIO_CLI_SCRIPT);
const client = new CopilotClient({
workingDirectory: workDir,
env: { ...env, COPILOT_HOME: copilotHomeFromEnv },
connection: RuntimeConnection.forStdio({
path: cliPath,
args: ["--capture-file", capturePath],
}),
baseDirectory: copilotHomeFromOption,
gitHubToken: "process-option-token",
logLevel: "debug",
sessionIdleTimeoutSeconds: 17,
telemetry: {
otlpEndpoint: "http://127.0.0.1:4318",
filePath: telemetryPath,
exporterType: "file",
sourceName: "ts-sdk-e2e",
captureContent: true,
},
useLoggedInUser: false,
});
onTestFinished(async () => {
try {
await client.forceStop();
} catch {
// Ignore cleanup errors
}
});
await client.start();
const captureRaw = fs.readFileSync(capturePath, "utf8");
const capture = JSON.parse(captureRaw) as {
args: string[];
cwd: string;
env: Record<string, string | undefined>;
requests: { method: string; params: unknown }[];
};
assertArgumentValue(capture.args, "--log-level", "debug");
expect(capture.args).toContain("--stdio");
assertArgumentValue(capture.args, "--auth-token-env", "COPILOT_SDK_AUTH_TOKEN");
expect(capture.args).toContain("--no-auto-login");
assertArgumentValue(capture.args, "--session-idle-timeout", "17");
expect(path.resolve(capture.cwd)).toBe(path.resolve(workDir));
expect(capture.env.COPILOT_HOME).toBe(copilotHomeFromOption);
expect(capture.env.COPILOT_SDK_AUTH_TOKEN).toBe("process-option-token");
expect(capture.env.COPILOT_OTEL_ENABLED).toBe("true");
expect(capture.env.OTEL_EXPORTER_OTLP_ENDPOINT).toBe("http://127.0.0.1:4318");
expect(capture.env.COPILOT_OTEL_FILE_EXPORTER_PATH).toBe(telemetryPath);
expect(capture.env.COPILOT_OTEL_EXPORTER_TYPE).toBe("file");
expect(capture.env.COPILOT_OTEL_SOURCE_NAME).toBe("ts-sdk-e2e");
expect(capture.env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT).toBe("true");
const session = await client.createSession({
onPermissionRequest: approveAll,
enableConfigDiscovery: true,
includeSubAgentStreamingEvents: false,
});
const updatedRaw = fs.readFileSync(capturePath, "utf8");
const updated = JSON.parse(updatedRaw) as {
requests: {
method: string;
params: {
enableConfigDiscovery?: boolean;
includeSubAgentStreamingEvents?: boolean;
};
}[];
};
const createRequests = updated.requests.filter((r) => r.method === "session.create");
expect(createRequests).toHaveLength(1);
expect(createRequests[0].params.enableConfigDiscovery).toBe(true);
expect(createRequests[0].params.includeSubAgentStreamingEvents).toBe(false);
await session.disconnect();
});
it("should throw when gitHubToken used with forUri", () => {
expect(() => {
new CopilotClient({
connection: RuntimeConnection.forUri("localhost:8080"),
gitHubToken: "gho_test_token",
});
}).toThrow();
});
it("should throw when useLoggedInUser used with forUri", () => {
expect(() => {
new CopilotClient({
connection: RuntimeConnection.forUri("localhost:8080"),
useLoggedInUser: false,
});
}).toThrow();
});
});