-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathrpc_server_misc.e2e.test.ts
More file actions
275 lines (246 loc) · 10.4 KB
/
Copy pathrpc_server_misc.e2e.test.ts
File metadata and controls
275 lines (246 loc) · 10.4 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { randomUUID } from "node:crypto";
import { mkdirSync, rmSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { approveAll, CopilotClient, RuntimeConnection } from "../../src/index.js";
import { createSdkTestContext, DEFAULT_GITHUB_TOKEN } from "./harness/sdkTestContext.js";
import { formatError, waitForCondition } from "./harness/sdkTestHelper.js";
describe("Miscellaneous server-scoped RPC", async () => {
const { copilotClient: client, env, openAiEndpoint, workDir } = await createSdkTestContext();
function createUniqueDirectory(prefix: string): string {
const directory = join(workDir, `${prefix}-${randomUUID()}`);
mkdirSync(directory, { recursive: true });
return directory;
}
function createClient(
extraEnv: Record<string, string | undefined>,
gitHubToken: string | undefined
): CopilotClient {
return new CopilotClient({
workingDirectory: workDir,
env: {
...env,
...extraEnv,
},
logLevel: "error",
connection: RuntimeConnection.forStdio({ path: process.env.COPILOT_CLI_PATH }),
gitHubToken,
useLoggedInUser: gitHubToken === undefined ? false : undefined,
});
}
async function createIsolatedStartedClient(
gitHubToken: string | null = DEFAULT_GITHUB_TOKEN
): Promise<{
client: CopilotClient;
home: string;
}> {
const home = createUniqueDirectory("copilot-e2e-misc-home");
const effectiveGitHubToken = gitHubToken === null ? undefined : gitHubToken;
const isolatedClient = createClient(
{
COPILOT_HOME: home,
GH_CONFIG_DIR: home,
XDG_CONFIG_HOME: home,
XDG_STATE_HOME: home,
COPILOT_DEBUG_GITHUB_API_URL: env.COPILOT_API_URL,
},
effectiveGitHubToken
);
try {
await isolatedClient.start();
return { client: isolatedClient, home };
} catch (error) {
await disposeIsolated(isolatedClient, home);
throw error;
}
}
async function disposeIsolated(isolatedClient: CopilotClient, home: string): Promise<void> {
try {
await isolatedClient.stop();
} catch {
// Best-effort cleanup.
}
tryRemoveDirectory(home);
}
async function forceStop(target: CopilotClient): Promise<void> {
try {
await target.stop();
} catch {
// Runtime may already be gone.
}
}
function tryRemoveDirectory(directory: string): void {
try {
rmSync(directory, { recursive: true, force: true });
} catch {
// Temp directories are reclaimed by the harness/OS.
}
}
it("should reload user settings", { timeout: 120_000 }, async () => {
await client.start();
await client.rpc.user.settings.reload();
});
it("should get set and clear user settings", { timeout: 120_000 }, async () => {
const { client: isolatedClient, home } = await createIsolatedStartedClient();
try {
const before = await isolatedClient.rpc.user.settings.get();
expect(Object.keys(before.settings).length).toBeGreaterThan(0);
for (const [key, setting] of Object.entries(before.settings)) {
expect(key.trim()).toBeTruthy();
expect(setting.value !== undefined || setting.default !== undefined).toBe(true);
}
const entry = Object.entries(before.settings).find(
([, setting]) => typeof setting.value === "boolean"
);
expect(entry).toBeDefined();
const [settingKey, setting] = entry!;
const toggledValue = setting.value !== true;
const set = await isolatedClient.rpc.user.settings.set({
settings: { [settingKey]: toggledValue },
});
expect(set.shadowedKeys).not.toContain(settingKey);
await isolatedClient.rpc.user.settings.reload();
const afterSet = await isolatedClient.rpc.user.settings.get();
expect(afterSet.settings[settingKey].isDefault).toBe(false);
expect(afterSet.settings[settingKey].value).toBe(toggledValue);
await isolatedClient.rpc.user.settings.set({
settings: { [settingKey]: null },
});
await isolatedClient.rpc.user.settings.reload();
const afterClear = await isolatedClient.rpc.user.settings.get();
expect(afterClear.settings[settingKey].isDefault).toBe(true);
} finally {
await disposeIsolated(isolatedClient, home);
}
});
it("should login list getCurrentAuth and logout account", { timeout: 120_000 }, async () => {
const login = `rpc-account-${randomUUID().replaceAll("-", "")}`;
const token = `rpc-account-token-${randomUUID().replaceAll("-", "")}`;
await openAiEndpoint.setCopilotUserByToken(token, {
login,
copilot_plan: "individual_pro",
endpoints: {
api: env.COPILOT_API_URL,
telemetry: "https://localhost:1/telemetry",
},
analytics_tracking_id: "rpc-account-tracking-id",
});
const { client: isolatedClient, home } = await createIsolatedStartedClient(null);
try {
const initial = await isolatedClient.rpc.account.getCurrentAuth();
expect(initial.authInfo).toBeUndefined();
const loginResult = await isolatedClient.rpc.account.login({
host: "https://github.com",
login,
token,
});
expect(typeof loginResult.storedInVault).toBe("boolean");
const current = await isolatedClient.rpc.account.getCurrentAuth();
expect(current.authErrors).toBeUndefined();
expect(current.authInfo).toMatchObject({
type: "user",
host: "https://github.com",
login,
});
const users = await isolatedClient.rpc.account.getAllUsers();
expect(Array.isArray(users)).toBe(true);
for (const user of users) {
expect(user.authInfo.type.trim()).toBeTruthy();
}
const account = users.find(
(user) => user.authInfo.type === "user" && user.authInfo.login === login
);
if (account) {
expect(account?.token).toBe(token);
}
const logout = await isolatedClient.rpc.account.logout({
authInfo: current.authInfo!,
});
expect(logout.hasMoreUsers).toBe(false);
const afterLogout = await isolatedClient.rpc.account.getCurrentAuth();
expect(afterLogout.authInfo).toBeUndefined();
} finally {
await disposeIsolated(isolatedClient, home);
}
});
it("should report agent registry spawn gate closed", { timeout: 120_000 }, async () => {
const { client: isolatedClient, home } = await createIsolatedStartedClient();
try {
await expect(
isolatedClient.rpc.agentRegistry.spawn({ cwd: workDir })
).rejects.toSatisfy((error: unknown) => {
const message = formatError(error);
expect(message.toLowerCase()).not.toContain("unhandled method");
expect(message.toLowerCase()).toContain("agentregistry.spawn");
expect(
message.toLowerCase().includes("not enabled") ||
message.toLowerCase().includes("no delegate")
).toBe(true);
return true;
});
} finally {
await disposeIsolated(isolatedClient, home);
}
});
it("should shut down owned runtime", { timeout: 120_000 }, async () => {
const dedicatedClient = createClient({}, DEFAULT_GITHUB_TOKEN);
try {
await dedicatedClient.start();
await dedicatedClient.rpc.user.settings.reload();
await dedicatedClient.rpc.runtime.shutdown();
await waitForCondition(
async () => {
try {
await dedicatedClient.rpc.user.settings.reload();
return false;
} catch {
return true;
}
},
{
timeoutMs: 15_000,
intervalMs: 100,
timeoutMessage: "Runtime kept serving RPCs after a graceful shutdown.",
}
);
} finally {
await forceStop(dedicatedClient);
}
});
it(
"should report not found when opening session without context",
{ timeout: 120_000 },
async () => {
const { client: isolatedClient, home } = await createIsolatedStartedClient();
try {
const result = await isolatedClient.rpc.sessions.open({ kind: "resumeLast" });
expect(result.status).toBe("not_found");
expect(result.sessionId ?? null).toBeNull();
} finally {
await disposeIsolated(isolatedClient, home);
}
}
);
it(
"should reject send attachments from non extension connection",
{ timeout: 120_000 },
async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
try {
await expect(
session.rpc.extensions.sendAttachmentsToMessage({ attachments: [] })
).rejects.toSatisfy((error: unknown) => {
const message = formatError(error);
expect(message.toLowerCase()).not.toContain("unhandled method");
expect(message.toLowerCase()).toContain("extension");
return true;
});
} finally {
await session.disconnect();
}
}
);
});