-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathrpc_server_misc.e2e.test.ts
More file actions
169 lines (152 loc) · 5.99 KB
/
Copy pathrpc_server_misc.e2e.test.ts
File metadata and controls
169 lines (152 loc) · 5.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
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
/*---------------------------------------------------------------------------------------------
* 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, 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> = {}): CopilotClient {
return new CopilotClient({
workingDirectory: workDir,
env: {
...env,
...extraEnv,
},
logLevel: "error",
connection: RuntimeConnection.forStdio({ path: process.env.COPILOT_CLI_PATH }),
gitHubToken: DEFAULT_GITHUB_TOKEN,
});
}
async function createIsolatedStartedClient(): Promise<{
client: CopilotClient;
home: string;
}> {
const home = createUniqueDirectory("copilot-e2e-misc-home");
const isolatedClient = createClient({
COPILOT_HOME: home,
GH_CONFIG_DIR: home,
XDG_CONFIG_HOME: home,
XDG_STATE_HOME: home,
});
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.forceStop();
} catch {
// Best-effort cleanup.
}
tryRemoveDirectory(home);
}
async function forceStop(target: CopilotClient): Promise<void> {
try {
await target.forceStop();
} 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 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();
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();
}
}
);
});