-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathclient.test.ts
More file actions
136 lines (108 loc) · 4.99 KB
/
Copy pathclient.test.ts
File metadata and controls
136 lines (108 loc) · 4.99 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
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");
});
it("should get status with version and protocol info", async () => {
const client = new CopilotClient({ cliPath: CLI_PATH, useStdio: true });
onTestFinishedForceStop(client);
await client.start();
const status = await client.getStatus();
expect(status.version).toBeDefined();
expect(typeof status.version).toBe("string");
expect(status.protocolVersion).toBeDefined();
expect(typeof status.protocolVersion).toBe("number");
expect(status.protocolVersion).toBeGreaterThanOrEqual(1);
await client.stop();
});
it("should get auth status", async () => {
const client = new CopilotClient({ cliPath: CLI_PATH, useStdio: true });
onTestFinishedForceStop(client);
await client.start();
const authStatus = await client.getAuthStatus();
expect(typeof authStatus.isAuthenticated).toBe("boolean");
if (authStatus.isAuthenticated) {
expect(authStatus.authType).toBeDefined();
expect(authStatus.statusMessage).toBeDefined();
}
await client.stop();
});
it("should list models when authenticated", async () => {
const client = new CopilotClient({ cliPath: CLI_PATH, useStdio: true });
onTestFinishedForceStop(client);
await client.start();
const authStatus = await client.getAuthStatus();
if (!authStatus.isAuthenticated) {
// Skip if not authenticated - models.list requires auth
await client.stop();
return;
}
const models = await client.listModels();
expect(Array.isArray(models)).toBe(true);
if (models.length > 0) {
const model = models[0];
expect(model.id).toBeDefined();
expect(model.name).toBeDefined();
expect(model.capabilities).toBeDefined();
expect(model.capabilities.supports).toBeDefined();
expect(model.capabilities.limits).toBeDefined();
}
await client.stop();
});
});