forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.test.ts
More file actions
77 lines (62 loc) · 2.97 KB
/
Copy pathclient.test.ts
File metadata and controls
77 lines (62 loc) · 2.97 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
import { ChildProcess } from "child_process";
import { describe, expect, it, onTestFinished } from "vitest";
import { CopilotClient } from "../../src/index.js";
import { CLI_PATH } from "./harness/sdkTestContext.js";
function onTestFinishedForceStop(client: CopilotClient) {
onTestFinished(async () => {
try {
await client.forceStop();
} catch {
// Ignore cleanup errors - process may already be stopped
}
});
}
describe("Client", () => {
it("should start and connect to server using stdio", async () => {
const client = new CopilotClient({ cliPath: CLI_PATH, useStdio: true });
onTestFinishedForceStop(client);
await client.start();
expect(client.getState()).toBe("connected");
const pong = await client.ping("test message");
expect(pong.message).toBe("pong: test message");
expect(pong.timestamp).toBeGreaterThanOrEqual(0);
expect(await client.stop()).toHaveLength(0); // No errors on stop
expect(client.getState()).toBe("disconnected");
});
it("should start and connect to server using tcp", async () => {
const client = new CopilotClient({ cliPath: CLI_PATH, useStdio: false });
onTestFinishedForceStop(client);
await client.start();
expect(client.getState()).toBe("connected");
const pong = await client.ping("test message");
expect(pong.message).toBe("pong: test message");
expect(pong.timestamp).toBeGreaterThanOrEqual(0);
expect(await client.stop()).toHaveLength(0); // No errors on stop
expect(client.getState()).toBe("disconnected");
});
it.skipIf(process.platform === "darwin")("should return errors on failed 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({ cliPath: CLI_PATH, useStdio: false });
await client.createSession();
// Kill the server process to 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();
expect(errors.length).toBeGreaterThan(0);
expect(errors[0].message).toContain("Failed to destroy session");
});
it("should forceStop without cleanup", async () => {
const client = new CopilotClient({ cliPath: CLI_PATH });
onTestFinishedForceStop(client);
await client.createSession();
await client.forceStop();
expect(client.getState()).toBe("disconnected");
});
});