-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathrpc_server.e2e.test.ts
More file actions
441 lines (386 loc) · 16.9 KB
/
rpc_server.e2e.test.ts
File metadata and controls
441 lines (386 loc) · 16.9 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import * as fs from "fs";
import * as path from "path";
import { randomUUID } from "node:crypto";
import { describe, expect, it, onTestFinished } from "vitest";
import { CopilotClient, RuntimeConnection } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext.js";
import { waitForCondition } from "./harness/sdkTestHelper.js";
describe("Server-scoped RPC", async () => {
const { copilotClient: client, openAiEndpoint, env, workDir } = await createSdkTestContext();
function createAuthenticatedClient(token: string): CopilotClient {
return createClientWithEnv(
{
COPILOT_DEBUG_GITHUB_API_URL: env.COPILOT_API_URL,
},
token
);
}
function createClientWithEnv(
extraEnv: Record<string, string | undefined>,
token?: string
): CopilotClient {
const childEnv = {
...env,
...extraEnv,
};
const extraClient = new CopilotClient({
workingDirectory: workDir,
env: childEnv,
logLevel: "error",
connection: RuntimeConnection.forStdio({ path: process.env.COPILOT_CLI_PATH }),
gitHubToken: token,
});
onTestFinished(async () => {
try {
await extraClient.forceStop();
} catch {
// Ignore cleanup errors
}
});
return extraClient;
}
async function configureAuthenticatedUser(
token: string,
quotaSnapshots?: Record<
string,
{
entitlement?: number;
overage_count?: number;
overage_permitted?: boolean;
percent_remaining?: number;
timestamp_utc?: string;
unlimited?: boolean;
}
>
): Promise<void> {
await openAiEndpoint.setCopilotUserByToken(token, {
login: "rpc-user",
copilot_plan: "individual_pro",
endpoints: {
api: env.COPILOT_API_URL,
telemetry: "https://localhost:1/telemetry",
},
analytics_tracking_id: "rpc-user-tracking-id",
quota_snapshots: quotaSnapshots,
});
}
function createSkillDirectory(skillName: string, description: string): string {
const skillsDir = path.join(
workDir,
"server-rpc-skills",
`dir-${Date.now()}-${Math.random().toString(36).slice(2)}`
);
const skillSubdir = path.join(skillsDir, skillName);
fs.mkdirSync(skillSubdir, { recursive: true });
const skillContent = `---\nname: ${skillName}\ndescription: ${description}\n---\n\n# ${skillName}\n\nThis skill is used by RPC E2E tests.\n`;
fs.writeFileSync(path.join(skillSubdir, "SKILL.md"), skillContent);
return skillsDir;
}
function createUniqueWorkDirectory(prefix: string): string {
const directory = path.join(workDir, `${prefix}-${randomUUID()}`);
fs.mkdirSync(directory, { recursive: true });
return directory;
}
async function saveAndGetEventFilePath(
targetClient: CopilotClient,
sessionId: string
): Promise<string> {
await expect(targetClient.rpc.sessions.save({ sessionId })).resolves.toBeDefined();
const pathResult = await targetClient.rpc.sessions.getEventFilePath({ sessionId });
expect(pathResult.filePath.trim()).toBeTruthy();
expect(path.isAbsolute(pathResult.filePath)).toBe(true);
expect(path.basename(pathResult.filePath)).toBe("events.jsonl");
return pathResult.filePath;
}
it("should call rpc ping with typed params and result", async () => {
await client.start();
const result = await client.ping("typed rpc test");
expect(result.message).toBe("pong: typed rpc test");
expect(Date.parse(result.timestamp)).not.toBeNaN();
});
it("should call rpc models list with typed result", async () => {
const token = "rpc-models-token";
await configureAuthenticatedUser(token);
const authClient = createAuthenticatedClient(token);
await authClient.start();
const result = await authClient.listModels();
expect(Array.isArray(result)).toBe(true);
expect(result.some((m) => m.id === "claude-sonnet-4.5")).toBe(true);
for (const model of result) {
expect(model.name).toBeTruthy();
}
});
it("should call rpc account getquota when authenticated", async () => {
const token = "rpc-quota-token";
await configureAuthenticatedUser(token, {
chat: {
entitlement: 100,
overage_count: 2,
overage_permitted: true,
percent_remaining: 75,
timestamp_utc: "2026-04-30T00:00:00Z",
},
});
const authClient = createAuthenticatedClient(token);
await authClient.start();
const result = await authClient.rpc.account.getQuota({ gitHubToken: token });
expect(result.quotaSnapshots).toHaveProperty("chat");
const chatQuota = result.quotaSnapshots.chat;
expect(chatQuota.entitlementRequests).toBe(100);
expect(chatQuota.usedRequests).toBe(25);
expect(chatQuota.remainingPercentage).toBe(75);
expect(chatQuota.overage).toBe(2);
expect(chatQuota.usageAllowedWithExhaustedQuota).toBe(true);
expect(chatQuota.overageAllowedWithExhaustedQuota).toBe(true);
expect(chatQuota.resetDate).toBe("2026-04-30T00:00:00Z");
});
it("should call rpc tools list with typed result", async () => {
await client.start();
const result = await client.rpc.tools.list();
expect(result.tools).toBeDefined();
expect(result.tools.length).toBeGreaterThan(0);
for (const tool of result.tools) {
expect(tool.name).toBeTruthy();
}
});
it("should call rpc sessionFs setProvider with typed result", async () => {
const fsClient = createClientWithEnv({});
await fsClient.start();
const result = await fsClient.rpc.sessionFs.setProvider({
initialCwd: "/",
sessionStatePath: "/session-state",
conventions: "posix",
capabilities: { sqlite: true },
});
expect(result.success).toBe(true);
});
it("should add secret filter values", async () => {
const secretClient = createClientWithEnv({ COPILOT_ENABLE_SECRET_FILTERING: "true" });
await secretClient.start();
const result = await secretClient.rpc.secrets.addFilterValues({
values: [`rpc-secret-${randomUUID()}`],
});
expect(result.ok).toBe(true);
});
it("should list, find, and inspect persisted session state", async () => {
const sessionId = randomUUID();
const missingTaskId = `missing-task-${randomUUID()}`;
const missingSessionId = randomUUID();
const workingDirectory = createUniqueWorkDirectory("server-rpc-list");
let closed = false;
const session = await client.createSession({
sessionId,
workingDirectory,
});
try {
await session.log("SERVER_RPC_LIST_READY");
const eventFilePath = await saveAndGetEventFilePath(client, sessionId);
expect(eventFilePath.toLowerCase()).toContain(sessionId.toLowerCase());
await client.rpc.sessions.close({ sessionId });
closed = true;
const listed = await client.rpc.sessions.list({
metadataLimit: 0,
filter: { cwd: workingDirectory },
});
expect(Array.isArray(listed.sessions)).toBe(true);
expect(
listed.sessions.every(
(session) =>
session.context?.cwd === undefined ||
pathsEqual(session.context.cwd, workingDirectory)
)
).toBe(true);
const byPrefix = await client.rpc.sessions.findByPrefix({
prefix: missingSessionId.slice(0, 8),
});
expect(byPrefix.sessionId).toBeUndefined();
const byTaskId = await client.rpc.sessions.findByTaskId({ taskId: missingTaskId });
expect(byTaskId.sessionId).toBeUndefined();
const lastForContext = await client.rpc.sessions.getLastForContext({
context: { cwd: workingDirectory },
});
expect(
lastForContext.sessionId === undefined || lastForContext.sessionId === sessionId
).toBe(true);
const sizes = await client.rpc.sessions.getSizes();
if (sizes.sizes[sessionId] !== undefined) {
expect(sizes.sizes[sessionId]).toBeGreaterThanOrEqual(0);
}
const inUse = await client.rpc.sessions.checkInUse({
sessionIds: [sessionId, missingSessionId],
});
expect(inUse.inUse).not.toContain(missingSessionId);
const remoteSteerable = await client.rpc.sessions.getPersistedRemoteSteerable({
sessionId,
});
expect(remoteSteerable.remoteSteerable).toBeUndefined();
} finally {
if (closed) {
await client.rpc.sessions.bulkDelete({ sessionIds: [sessionId] });
} else {
await session.disconnect();
}
}
}, 60_000);
it("should enrich basic session metadata", async () => {
const sessionId = randomUUID();
const workingDirectory = createUniqueWorkDirectory("server-rpc-enrich");
const session = await client.createSession({
sessionId,
workingDirectory,
onPermissionRequest: () => ({ kind: "approve-once" }),
});
try {
await saveAndGetEventFilePath(client, sessionId);
const now = new Date().toISOString();
const result = await client.rpc.sessions.enrichMetadata({
sessions: [
{
sessionId,
startTime: now,
modifiedTime: now,
isRemote: false,
name: "Basic metadata",
context: { cwd: workingDirectory },
},
],
});
const enriched = result.sessions[0];
expect(enriched.sessionId).toBe(sessionId);
expect(pathsEqual(enriched.context?.cwd ?? "", workingDirectory)).toBe(true);
expect(enriched.isRemote).toBe(false);
} finally {
await session.disconnect();
}
});
it("should close active session and release lock", async () => {
const sessionId = randomUUID();
const workingDirectory = createUniqueWorkDirectory("server-rpc-close");
const session = await client.createSession({
sessionId,
workingDirectory,
onPermissionRequest: () => ({ kind: "approve-once" }),
});
await session.log("SERVER_RPC_CLOSE_READY");
await saveAndGetEventFilePath(client, sessionId);
await expect(client.rpc.sessions.close({ sessionId })).resolves.toBeDefined();
await expect(client.rpc.sessions.releaseLock({ sessionId })).resolves.toBeDefined();
const inUse = await client.rpc.sessions.checkInUse({ sessionIds: [sessionId] });
expect(inUse.inUse).not.toContain(sessionId);
// The server-side close disposes the session; do not call session.disconnect().
});
it("should prune dry-run and bulkDelete persisted session", async () => {
const sessionId = randomUUID();
const missingSessionId = randomUUID();
const workingDirectory = createUniqueWorkDirectory("server-rpc-delete");
const session = await client.createSession({
sessionId,
workingDirectory,
onPermissionRequest: () => ({ kind: "approve-once" }),
});
await saveAndGetEventFilePath(client, sessionId);
await client.rpc.sessions.close({ sessionId });
const prune = await client.rpc.sessions.pruneOld({
olderThanDays: 0,
dryRun: true,
includeNamed: true,
excludeSessionIds: [],
});
expect(prune.dryRun).toBe(true);
expect(prune.candidates).not.toContain(missingSessionId);
expect(prune.deleted).not.toContain(sessionId);
expect(prune.freedBytes).toBeGreaterThanOrEqual(0);
const deleted = await client.rpc.sessions.bulkDelete({
sessionIds: [sessionId, missingSessionId],
});
expect(deleted.freedBytes[sessionId]).toBeGreaterThanOrEqual(0);
if (deleted.freedBytes[missingSessionId] !== undefined) {
expect(deleted.freedBytes[missingSessionId]).toBe(0);
}
await waitForCondition(
async () =>
!(await client.rpc.sessions.list({})).sessions.some(
(session) => session.sessionId === sessionId
),
{ timeoutMessage: `Timed out waiting for sessions.bulkDelete to remove ${sessionId}.` }
);
// The server-side close/deletion disposes the session; do not call session.disconnect().
expect(session.sessionId).toBe(sessionId);
});
it("should set additional plugins and reload deferred hooks", async () => {
await client.start();
await expect(
client.rpc.sessions.setAdditionalPlugins({ plugins: [] })
).resolves.toBeDefined();
const sessionId = randomUUID();
const workingDirectory = createUniqueWorkDirectory("server-rpc-hooks");
const session = await client.createSession({
sessionId,
workingDirectory,
enableConfigDiscovery: false,
});
try {
await expect(
client.rpc.sessions.reloadPluginHooks({ sessionId, deferRepoHooks: true })
).resolves.toBeDefined();
const loaded = await client.rpc.sessions.loadDeferredRepoHooks({ sessionId });
expect(loaded.startupPrompts).toEqual([]);
expect(loaded.hookCount).toBe(0);
} finally {
await client.rpc.sessions.setAdditionalPlugins({ plugins: [] });
await session.disconnect();
}
});
it("should report implemented error when connecting unknown remote session", async () => {
await client.start();
const remoteSessionId = `remote-${randomUUID()}`;
await expect(client.rpc.sessions.connect({ sessionId: remoteSessionId })).rejects.toSatisfy(
(err: unknown) => {
const text =
err instanceof Error ? `${err.message}\n${err.stack ?? ""}` : String(err);
expect(text.toLowerCase()).not.toContain("unhandled method sessions.connect");
expect(text.toLowerCase()).toContain("session");
return true;
}
);
});
it("should discover server mcp and skills", async () => {
await client.start();
const skillName = `server-rpc-skill-${Date.now()}-${Math.random().toString(36).slice(2)}`;
const skillDirectory = createSkillDirectory(
skillName,
"Skill discovered by server-scoped RPC tests."
);
const mcp = await client.rpc.mcp.discover({ workingDirectory: workDir });
expect(mcp.servers).toBeDefined();
const skills = await client.rpc.skills.discover({ skillDirectories: [skillDirectory] });
const discovered = skills.skills.filter((s) => s.name === skillName);
expect(discovered).toHaveLength(1);
expect(discovered[0].description).toBe("Skill discovered by server-scoped RPC tests.");
expect(discovered[0].enabled).toBe(true);
expect(discovered[0].path.endsWith(path.join(skillName, "SKILL.md"))).toBe(true);
try {
await client.rpc.skills.config.setDisabledSkills({ disabledSkills: [skillName] });
const disabled = await client.rpc.skills.discover({
skillDirectories: [skillDirectory],
});
const disabledMatches = disabled.skills.filter((s) => s.name === skillName);
expect(disabledMatches).toHaveLength(1);
expect(disabledMatches[0].enabled).toBe(false);
} finally {
await client.rpc.skills.config.setDisabledSkills({ disabledSkills: [] });
}
});
});
function pathsEqual(left: string, right: string): boolean {
return normalizePath(left) === normalizePath(right);
}
function normalizePath(value: string): string {
return path
.resolve(value)
.replace(/[\\/]+$/g, "")
.toLowerCase();
}