Repository navigation
Expand file tree
/
Copy pathimage-generation.test.ts
More file actions
78 lines (76 loc) · 3.07 KB
/
Copy pathimage-generation.test.ts
File metadata and controls
78 lines (76 loc) · 3.07 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
// Copyright (c) Microsoft Corporation. All rights reserved.
import { once } from "node:events";
import { createServer } from "node:net";
import { describe, expect, it, onTestFinished } from "vitest";
import {
createMessageConnection,
StreamMessageReader,
StreamMessageWriter,
type MessageConnection,
} from "vscode-jsonrpc/node.js";
import {
approveAll,
CopilotClient,
RuntimeConnection,
type ImageGenerationConfig,
} from "../src/index.js";
import { getSdkProtocolVersion } from "../src/sdkProtocolVersion.js";
describe("image generation request serialization", () => {
it.each([undefined, {}, { enabled: true }, { enabled: false }] satisfies (
| ImageGenerationConfig
| undefined
)[])("forwards %j on create and resume", async (imageGeneration) => {
const requests = new Map<string, Record<string, unknown>>();
const connections: MessageConnection[] = [];
const server = createServer((socket) => {
const connection = createMessageConnection(
new StreamMessageReader(socket),
new StreamMessageWriter(socket)
);
connections.push(connection);
connection.onRequest((method, params) => {
if (method === "connect") {
return { ok: true, protocolVersion: getSdkProtocolVersion(), version: "test" };
}
if (method === "session.create" || method === "session.resume") {
const payload = params as Record<string, unknown>;
requests.set(method, payload);
return { sessionId: payload.sessionId };
}
return {};
});
connection.listen();
});
onTestFinished(async () => {
for (const connection of connections) connection.dispose();
await new Promise<void>((resolve, reject) =>
server.close((error) => (error ? reject(error) : resolve()))
);
});
server.listen(0, "127.0.0.1");
await once(server, "listening");
const address = server.address();
if (!address || typeof address === "string") throw new Error("Missing TCP address");
const client = new CopilotClient({
connection: RuntimeConnection.forUri(`127.0.0.1:${address.port}`),
});
onTestFinished(() => client.forceStop());
const session = await client.createSession({
onPermissionRequest: approveAll,
imageGeneration,
});
await client.resumeSession(session.sessionId, {
onPermissionRequest: approveAll,
imageGeneration,
});
for (const method of ["session.create", "session.resume"]) {
const payload = requests.get(method);
expect(payload).toBeDefined();
if (imageGeneration === undefined) {
expect(payload).not.toHaveProperty("imageGeneration");
} else {
expect(payload?.imageGeneration).toEqual(imageGeneration);
}
}
});
});