-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathclient.test.ts
More file actions
151 lines (128 loc) · 5.17 KB
/
client.test.ts
File metadata and controls
151 lines (128 loc) · 5.17 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
/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, expect, it, onTestFinished } from "vitest";
import { CopilotClient } from "../src/index.js";
import { CLI_PATH } from "./e2e/harness/sdkTestContext.js";
// This file is for unit tests. Where relevant, prefer to add e2e tests in e2e/*.test.ts instead
describe("CopilotClient", () => {
it("returns a standardized failure result when a tool is not registered", async () => {
const client = new CopilotClient({ cliPath: CLI_PATH });
await client.start();
onTestFinished(() => client.forceStop());
const session = await client.createSession();
const response = await (
client as unknown as { handleToolCallRequest: (typeof client)["handleToolCallRequest"] }
).handleToolCallRequest({
sessionId: session.sessionId,
toolCallId: "123",
toolName: "missing_tool",
arguments: {},
});
expect(response.result).toMatchObject({
resultType: "failure",
error: "tool 'missing_tool' not supported",
});
});
describe("URL parsing", () => {
it("should parse port-only URL format", () => {
const client = new CopilotClient({
cliUrl: "8080",
logLevel: "error",
});
// Verify internal state
expect((client as any).actualPort).toBe(8080);
expect((client as any).actualHost).toBe("localhost");
expect((client as any).isExternalServer).toBe(true);
});
it("should parse host:port URL format", () => {
const client = new CopilotClient({
cliUrl: "127.0.0.1:9000",
logLevel: "error",
});
expect((client as any).actualPort).toBe(9000);
expect((client as any).actualHost).toBe("127.0.0.1");
expect((client as any).isExternalServer).toBe(true);
});
it("should parse http://host:port URL format", () => {
const client = new CopilotClient({
cliUrl: "http://localhost:7000",
logLevel: "error",
});
expect((client as any).actualPort).toBe(7000);
expect((client as any).actualHost).toBe("localhost");
expect((client as any).isExternalServer).toBe(true);
});
it("should parse https://host:port URL format", () => {
const client = new CopilotClient({
cliUrl: "https://example.com:443",
logLevel: "error",
});
expect((client as any).actualPort).toBe(443);
expect((client as any).actualHost).toBe("example.com");
expect((client as any).isExternalServer).toBe(true);
});
it("should throw error for invalid URL format", () => {
expect(() => {
new CopilotClient({
cliUrl: "invalid-url",
logLevel: "error",
});
}).toThrow(/Invalid cliUrl format/);
});
it("should throw error for invalid port - too high", () => {
expect(() => {
new CopilotClient({
cliUrl: "localhost:99999",
logLevel: "error",
});
}).toThrow(/Invalid port in cliUrl/);
});
it("should throw error for invalid port - zero", () => {
expect(() => {
new CopilotClient({
cliUrl: "localhost:0",
logLevel: "error",
});
}).toThrow(/Invalid port in cliUrl/);
});
it("should throw error for invalid port - negative", () => {
expect(() => {
new CopilotClient({
cliUrl: "localhost:-1",
logLevel: "error",
});
}).toThrow(/Invalid port in cliUrl/);
});
it("should throw error when cliUrl is used with useStdio", () => {
expect(() => {
new CopilotClient({
cliUrl: "localhost:8080",
useStdio: true,
logLevel: "error",
});
}).toThrow(/cliUrl is mutually exclusive/);
});
it("should throw error when cliUrl is used with cliPath", () => {
expect(() => {
new CopilotClient({
cliUrl: "localhost:8080",
cliPath: "/path/to/cli",
logLevel: "error",
});
}).toThrow(/cliUrl is mutually exclusive/);
});
it("should set useStdio to false when cliUrl is provided", () => {
const client = new CopilotClient({
cliUrl: "8080",
logLevel: "error",
});
expect(client["options"].useStdio).toBe(false);
});
it("should mark client as using external server", () => {
const client = new CopilotClient({
cliUrl: "localhost:8080",
logLevel: "error",
});
expect((client as any).isExternalServer).toBe(true);
});
});
});