-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathclient.test.ts
More file actions
177 lines (146 loc) · 6.27 KB
/
Copy pathclient.test.ts
File metadata and controls
177 lines (146 loc) · 6.27 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, expect, it, onTestFinished } from "vitest";
import { CopilotClient, parseCliUrl } from "../src/index.js";
import { TcpTransport } from "../src/tcp-transport.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();
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 result = parseCliUrl("8080");
expect(result.port).toBe(8080);
expect(result.host).toBe("localhost");
});
it("should parse host:port URL format", () => {
const result = parseCliUrl("127.0.0.1:9000");
expect(result.port).toBe(9000);
expect(result.host).toBe("127.0.0.1");
});
it("should parse http://host:port URL format", () => {
const result = parseCliUrl("http://localhost:7000");
expect(result.port).toBe(7000);
expect(result.host).toBe("localhost");
});
it("should parse https://host:port URL format", () => {
const result = parseCliUrl("https://example.com:443");
expect(result.port).toBe(443);
expect(result.host).toBe("example.com");
});
it("should throw error for invalid URL format", () => {
expect(() => parseCliUrl("invalid-url")).toThrow(/Invalid cliUrl format/);
});
it("should throw error for invalid port - too high", () => {
expect(() => parseCliUrl("localhost:99999")).toThrow(/Invalid port in cliUrl/);
});
it("should throw error for invalid port - zero", () => {
expect(() => parseCliUrl("localhost:0")).toThrow(/Invalid port in cliUrl/);
});
it("should throw error for invalid port - negative", () => {
expect(() => parseCliUrl("localhost:-1")).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 create TcpTransport when cliUrl is provided", () => {
const client = new CopilotClient({
cliUrl: "localhost:8080",
logLevel: "error",
});
expect((client as any).transport).toBeInstanceOf(TcpTransport);
});
});
describe("Auth options", () => {
it("should accept githubToken option", () => {
const client = new CopilotClient({
githubToken: "gho_test_token",
logLevel: "error",
});
expect((client as any).options.githubToken).toBe("gho_test_token");
});
it("should default useLoggedInUser to false (secure by default)", () => {
const client = new CopilotClient({
logLevel: "error",
});
expect((client as any).options.useLoggedInUser).toBe(false);
});
it("should default useLoggedInUser to false when githubToken is provided", () => {
const client = new CopilotClient({
githubToken: "gho_test_token",
logLevel: "error",
});
expect((client as any).options.useLoggedInUser).toBe(false);
});
it("should allow explicit useLoggedInUser: true with githubToken", () => {
const client = new CopilotClient({
githubToken: "gho_test_token",
useLoggedInUser: true,
logLevel: "error",
});
expect((client as any).options.useLoggedInUser).toBe(true);
});
it("should allow explicit useLoggedInUser: false without githubToken", () => {
const client = new CopilotClient({
useLoggedInUser: false,
logLevel: "error",
});
expect((client as any).options.useLoggedInUser).toBe(false);
});
it("should throw error when githubToken is used with cliUrl", () => {
expect(() => {
new CopilotClient({
cliUrl: "localhost:8080",
githubToken: "gho_test_token",
logLevel: "error",
});
}).toThrow(/githubToken and useLoggedInUser cannot be used with cliUrl/);
});
it("should throw error when useLoggedInUser is used with cliUrl", () => {
expect(() => {
new CopilotClient({
cliUrl: "localhost:8080",
useLoggedInUser: false,
logLevel: "error",
});
}).toThrow(/githubToken and useLoggedInUser cannot be used with cliUrl/);
});
});
});