forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.e2e.test.ts
More file actions
114 lines (93 loc) · 4.2 KB
/
Copy pathcommands.e2e.test.ts
File metadata and controls
114 lines (93 loc) · 4.2 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { afterAll, describe, expect, it } from "vitest";
import { CopilotClient, approveAll, RuntimeConnection } from "../../src/index.js";
import type { SessionEvent } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext.js";
describe("Commands", async () => {
// Use TCP mode so a second client can connect to the same CLI process
const tcpConnectionToken = "commands-test-token";
const ctx = await createSdkTestContext({
useStdio: false,
copilotClientOptions: {
connection: RuntimeConnection.forTcp({ connectionToken: tcpConnectionToken }),
},
});
const client1 = ctx.copilotClient;
// Trigger connection so we can read the port
const initSession = await client1.createSession({ onPermissionRequest: approveAll });
await initSession.disconnect();
const { runtimePort } = client1 as unknown as { runtimePort: number };
const client2 = new CopilotClient({
connection: RuntimeConnection.forUri(`localhost:${runtimePort}`, {
connectionToken: tcpConnectionToken,
}),
});
afterAll(async () => {
await client2.stop();
});
it(
"client receives commands.changed when another client joins with commands",
{ timeout: 20_000 },
async () => {
const session1 = await client1.createSession({
onPermissionRequest: approveAll,
});
type CommandsChangedEvent = Extract<SessionEvent, { type: "commands.changed" }>;
// Wait for the commands.changed event deterministically
const commandsChangedPromise = new Promise<CommandsChangedEvent>((resolve) => {
session1.on((event) => {
if (event.type === "commands.changed") resolve(event);
});
});
// Client2 joins with commands
const session2 = await client2.resumeSession(session1.sessionId, {
onPermissionRequest: approveAll,
commands: [
{ name: "deploy", description: "Deploy the app", handler: async () => {} },
],
suppressResumeEvent: true,
});
// Rely on default vitest timeout
const commandsChanged = await commandsChangedPromise;
expect(commandsChanged.data.commands).toEqual(
expect.arrayContaining([
expect.objectContaining({ name: "deploy", description: "Deploy the app" }),
])
);
await session2.disconnect();
}
);
it("session with commands creates successfully", async () => {
const session = await client1.createSession({
onPermissionRequest: approveAll,
commands: [
{ name: "deploy", description: "Deploy the app", handler: async () => {} },
{ name: "rollback", handler: async () => {} },
],
});
expect(session).toBeDefined();
expect(session.sessionId).toMatch(/^[a-f0-9-]+$/);
await session.disconnect();
});
it("session with commands resumes successfully", async () => {
const session1 = await client1.createSession({ onPermissionRequest: approveAll });
const sessionId = session1.sessionId;
const session2 = await client1.resumeSession(sessionId, {
onPermissionRequest: approveAll,
commands: [{ name: "deploy", description: "Deploy", handler: async () => {} }],
});
expect(session2).toBeDefined();
expect(session2.sessionId).toBe(sessionId);
await session2.disconnect();
});
it("session with no commands creates successfully", async () => {
const session = await client1.createSession({
onPermissionRequest: approveAll,
});
expect(session).toBeDefined();
expect(session.sessionId).toMatch(/^[a-f0-9-]+$/);
await session.disconnect();
});
});