Repository navigation
Expand file tree
/
Copy pathCapiProxy.test.ts
More file actions
342 lines (319 loc) · 13.7 KB
/
Copy pathCapiProxy.test.ts
File metadata and controls
342 lines (319 loc) · 13.7 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { spawn } from "child_process";
import { once } from "node:events";
import { existsSync } from "node:fs";
import { copyFile, mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { createInterface } from "node:readline";
import { beforeEach, describe, expect, inject, it, onTestFinished, vi } from "vitest";
import { CapiProxy } from "./CapiProxy";
import { CAPI_PROXY_BUNDLE } from "./proxyBundleContext";
import { hasChildExited, stopChildProcess, waitForChildExit } from "./sdkTestHelper";
vi.mock("child_process", async (importOriginal) => {
const actual = await importOriginal<typeof import("child_process")>();
return { ...actual, spawn: vi.fn(actual.spawn) };
});
vi.mock("./sdkTestHelper", async (importOriginal) => {
const actual = await importOriginal<typeof import("./sdkTestHelper")>();
return {
...actual,
stopChildProcess: vi.fn(actual.stopChildProcess),
waitForChildExit: vi.fn(actual.waitForChildExit),
};
});
const realSpawn = vi.mocked(spawn).getMockImplementation()!;
const realWaitForChildExit = vi.mocked(waitForChildExit).getMockImplementation()!;
async function startOwnedProxy(): Promise<CapiProxy> {
const proxy = new CapiProxy();
const starting = proxy.start();
onTestFinished(async () => {
await Promise.allSettled([starting]);
await proxy.stop(true);
});
await starting;
return proxy;
}
describe("bundled CAPI proxy", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("launches the shared bundle directly and waits for a clean shutdown", async () => {
const proxy = await startOwnedProxy();
const serverPath = inject(CAPI_PROXY_BUNDLE);
expect(serverPath).toMatch(/server\.mjs$/);
expect(existsSync(serverPath)).toBe(true);
expect(spawn).toHaveBeenCalledExactlyOnceWith(process.execPath, [serverPath], {
stdio: ["ignore", "pipe", "inherit"],
windowsHide: true,
});
const child = vi.mocked(spawn).mock.results[0].value;
expect(child.exitCode).toBeNull();
expect(proxy.getProxyEnv()).toMatchObject({
HTTPS_PROXY: expect.stringMatching(/^http:\/\/127\.0\.0\.1:/),
NODE_EXTRA_CA_CERTS: expect.any(String),
});
expect(existsSync(proxy.getProxyEnv().NODE_EXTRA_CA_CERTS)).toBe(true);
const proxyUrl = proxy.url;
await proxy.stop(true);
expect(child.exitCode).toBe(0);
expect(child.signalCode).toBeNull();
expect(() => proxy.url).toThrow("has not been started");
await expect(fetch(proxyUrl)).rejects.toThrow();
});
it("shares only the bundle, not authentication state, ports, or certificates", async () => {
const [first, second] = await Promise.all([startOwnedProxy(), startOwnedProxy()]);
expect(first.url).not.toBe(second.url);
expect(first.getProxyEnv().HTTPS_PROXY).not.toBe(second.getProxyEnv().HTTPS_PROXY);
expect(first.getProxyEnv().NODE_EXTRA_CA_CERTS).not.toBe(
second.getProxyEnv().NODE_EXTRA_CA_CERTS
);
expect(vi.mocked(spawn).mock.calls.map((call) => call[1])).toEqual([
[inject(CAPI_PROXY_BUNDLE)],
[inject(CAPI_PROXY_BUNDLE)],
]);
await first.setCopilotUserByToken("isolated-token", { login: "first-user" });
await second.setCopilotUserByToken("isolated-token", { login: "second-user" });
for (const [proxy, login] of [
[first, "first-user"],
[second, "second-user"],
] as const) {
const response = await fetch(`${proxy.url}/copilot_internal/user`, {
headers: { Authorization: "Bearer isolated-token" },
});
expect(response.ok).toBe(true);
expect(await response.json()).toMatchObject({ login });
}
});
it("runs the self-contained bundle from a path containing spaces", async () => {
const directory = await mkdtemp(join(tmpdir(), "sdk proxy bundle "));
const proxy = new CapiProxy();
let starting: Promise<string> | undefined;
onTestFinished(async () => {
await Promise.allSettled(starting ? [starting] : []);
try {
await proxy.stop(true);
} finally {
await rm(directory, { recursive: true, force: true });
}
});
const serverPath = join(directory, "server with spaces.mjs");
await copyFile(inject(CAPI_PROXY_BUNDLE), serverPath);
vi.mocked(spawn).mockImplementationOnce((command, _args, options) =>
realSpawn(command, [serverPath], options)
);
starting = proxy.start();
await starting;
await proxy.setCopilotUserByToken("spaces-token", { login: "spaces-user" });
const response = await fetch(`${proxy.url}/copilot_internal/user`, {
headers: { Authorization: "Bearer spaces-token" },
});
expect(await response.json()).toMatchObject({ login: "spaces-user" });
});
it("reports early startup exit and its output", async () => {
vi.mocked(spawn).mockImplementationOnce((_command, _args, options) =>
realSpawn(
process.execPath,
["-e", 'console.log("startup failed"); process.exit(23);'],
options
)
);
await expect(startOwnedProxy()).rejects.toThrow(
"Proxy exited before startup with code 23: startup failed"
);
expect(vi.mocked(spawn).mock.results[0].value.exitCode).toBe(23);
});
it("terminates the owned child when startup metadata is invalid", async () => {
vi.mocked(spawn).mockImplementationOnce((_command, _args, options) =>
realSpawn(
process.execPath,
[
"-e",
'console.log("Listening: http://127.0.0.1:1 {}"); setInterval(() => {}, 1000);',
],
options
)
);
await expect(startOwnedProxy()).rejects.toThrow("missing CONNECT proxy details");
const child = vi.mocked(spawn).mock.results[0].value;
expect(child.exitCode !== null || child.signalCode !== null).toBe(true);
});
it("reports spawn failures without waiting for an impossible exit", async () => {
vi.mocked(spawn).mockImplementationOnce(() =>
realSpawn(join(dirname(inject(CAPI_PROXY_BUNDLE)), "missing-node-executable"), [], {})
);
await expect(startOwnedProxy()).rejects.toMatchObject({ code: "ENOENT" });
});
it("retains a child after startup cleanup fails so shutdown can retry", async () => {
const cleanupError = new Error("Child process did not exit after SIGKILL");
vi.mocked(stopChildProcess).mockRejectedValueOnce(cleanupError);
vi.mocked(spawn).mockImplementationOnce((_command, _args, options) =>
realSpawn(
process.execPath,
[
"-e",
'console.log("Listening: http://127.0.0.1:1 {}"); setInterval(() => {}, 1000);',
],
options
)
);
const proxy = new CapiProxy();
const starting = proxy.start();
const child = vi.mocked(spawn).mock.results[0].value;
onTestFinished(async () => {
await Promise.allSettled([starting]);
try {
await proxy.stop(true);
} finally {
await stopChildProcess(child);
}
});
await expect(starting).rejects.toMatchObject({
message: "Proxy startup and cleanup failed",
errors: [
expect.objectContaining({
message: expect.stringContaining("missing CONNECT proxy details"),
}),
cleanupError,
],
});
expect(hasChildExited(child)).toBe(false);
await expect(proxy.start()).rejects.toThrow("already been started");
expect(spawn).toHaveBeenCalledTimes(1);
await proxy.stop(true);
expect(hasChildExited(child)).toBe(true);
});
it("retains a running proxy after shutdown cleanup fails so shutdown can retry", async () => {
const proxy = new CapiProxy();
const starting = proxy.start();
const child = vi.mocked(spawn).mock.results[0].value;
const fetchMock = vi.spyOn(globalThis, "fetch");
onTestFinished(async () => {
fetchMock.mockRestore();
await Promise.allSettled([starting]);
try {
await proxy.stop(true);
} finally {
await stopChildProcess(child);
}
});
const proxyUrl = await starting;
const proxyEnv = proxy.getProxyEnv();
const shutdownError = new Error("Shutdown request failed");
const cleanupError = new Error("Child process did not exit after SIGKILL");
fetchMock.mockRejectedValueOnce(shutdownError);
vi.mocked(stopChildProcess).mockRejectedValueOnce(cleanupError);
await expect(proxy.stop(true)).rejects.toMatchObject({
message: "Proxy shutdown and cleanup failed",
errors: [shutdownError, cleanupError],
});
expect(hasChildExited(child)).toBe(false);
expect(proxy.url).toBe(proxyUrl);
expect(proxy.getProxyEnv()).toEqual(proxyEnv);
await expect(proxy.start()).rejects.toThrow("already been started");
expect(spawn).toHaveBeenCalledTimes(1);
await proxy.stop(true);
expect(child.exitCode).toBe(0);
expect(child.signalCode).toBeNull();
expect(() => proxy.url).toThrow("has not been started");
expect(proxy.getProxyEnv()).toEqual({});
await expect(fetch(proxyUrl)).rejects.toThrow();
});
it("allows an acknowledged capture flush to finish beyond the old shutdown cutoff", async () => {
const directory = await mkdtemp(join(tmpdir(), "sdk proxy flush "));
const capturePath = join(directory, "capture.yaml");
vi.mocked(spawn).mockImplementationOnce((_command, _args, options) =>
realSpawn(
process.execPath,
[
"-e",
`
const { createServer } = require("node:http");
const { writeFile } = require("node:fs/promises");
let releaseFlush;
const flushGate = new Promise(resolve => { releaseFlush = resolve; });
const server = createServer(async (request, response) => {
if (request.url === "/stop") {
response.end();
await writeFile(process.argv[1], "partial capture");
console.log("Capture flush blocked");
await flushGate;
await writeFile(process.argv[1], "complete capture");
server.close();
process.exit(0);
} else if (request.url === "/release") {
response.end();
releaseFlush();
}
});
server.listen(0, "127.0.0.1", () => {
const url = "http://127.0.0.1:" + server.address().port;
console.log("Listening: " + url + " " + JSON.stringify({
connectProxyUrl: url,
caFilePath: "unused.pem"
}));
});
`,
capturePath,
],
options
)
);
const proxy = new CapiProxy();
const starting = proxy.start();
const child = vi.mocked(spawn).mock.results[0].value;
const outputLines = createInterface({ input: child.stdout! });
let stopping: Promise<unknown> | undefined;
onTestFinished(async () => {
vi.useRealTimers();
outputLines.close();
try {
await stopChildProcess(child);
await Promise.allSettled([starting, ...(stopping ? [stopping] : [])]);
} finally {
await rm(directory, { recursive: true, force: true });
}
});
const proxyUrl = await starting;
// The proxy's startup reader closes and pauses the shared stdout stream.
child.stdout!.resume();
const flushBlocked = once(outputLines, "line");
let resolveExitWait!: () => void;
const exitWaitStarted = new Promise<void>((resolve) => {
resolveExitWait = resolve;
});
vi.mocked(waitForChildExit).mockImplementationOnce((process, timeoutMs) => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
const waiting = realWaitForChildExit(process, timeoutMs);
resolveExitWait();
return waiting;
});
const kill = vi.spyOn(child, "kill");
let shutdownSettled = false;
stopping = proxy.stop().then(
() => {
shutdownSettled = true;
},
(error: unknown) => {
shutdownSettled = true;
return error;
}
);
await exitWaitStarted;
const [output] = await flushBlocked;
expect(String(output)).toContain("Capture flush blocked");
expect(await readFile(capturePath, "utf8")).toBe("partial capture");
await vi.advanceTimersByTimeAsync(10_000);
expect(shutdownSettled).toBe(false);
expect(kill).not.toHaveBeenCalled();
expect(hasChildExited(child)).toBe(false);
vi.useRealTimers();
expect((await fetch(`${proxyUrl}/release`)).ok).toBe(true);
expect(await stopping).toBeUndefined();
expect(await readFile(capturePath, "utf8")).toBe("complete capture");
expect(child.exitCode).toBe(0);
expect(child.signalCode).toBeNull();
});
});