-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathrpc_server_plugins.e2e.test.ts
More file actions
320 lines (280 loc) · 12.2 KB
/
Copy pathrpc_server_plugins.e2e.test.ts
File metadata and controls
320 lines (280 loc) · 12.2 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { randomUUID } from "node:crypto";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { CopilotClient, RuntimeConnection } from "../../src/index.js";
import { createSdkTestContext, DEFAULT_GITHUB_TOKEN } from "./harness/sdkTestContext.js";
const MARKETPLACE_NAME = "csharp-e2e-marketplace";
const PLUGIN_NAME = "csharp-e2e-plugin";
const DIRECT_PLUGIN_NAME = "csharp-e2e-direct";
describe("Server-scoped plugin RPC", async () => {
const { env, workDir } = await createSdkTestContext();
function createUniqueDirectory(prefix: string): string {
const directory = join(workDir, `${prefix}-${randomUUID()}`);
mkdirSync(directory, { recursive: true });
return directory;
}
function createClient(home: string): CopilotClient {
return new CopilotClient({
workingDirectory: workDir,
env: {
...env,
COPILOT_HOME: home,
GH_CONFIG_DIR: home,
XDG_CONFIG_HOME: home,
XDG_STATE_HOME: home,
},
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-home");
const client = createClient(home);
try {
await client.start();
return { client, home };
} catch (error) {
await disposeIsolated(client, home);
throw error;
}
}
async function disposeIsolated(
client: CopilotClient,
home: string,
fixtureDir?: string
): Promise<void> {
try {
await client.forceStop();
} catch {
// Best-effort cleanup.
}
tryRemoveDirectory(home);
if (fixtureDir) {
tryRemoveDirectory(fixtureDir);
}
}
function tryRemoveDirectory(directory: string): void {
try {
rmSync(directory, { recursive: true, force: true });
} catch {
// Temp directories are reclaimed by the harness/OS.
}
}
function createLocalMarketplaceFixture(): string {
const directory = createUniqueDirectory("copilot-e2e-mp");
const manifest = `{
"name": "${MARKETPLACE_NAME}",
"owner": { "name": "Copilot SDK E2E" },
"metadata": { "description": "Local marketplace fixture for SDK E2E tests." },
"plugins": [
{
"name": "${PLUGIN_NAME}",
"source": "./${PLUGIN_NAME}",
"description": "E2E demo plugin advertised by the local marketplace.",
"version": "1.0.0"
}
]
}
`;
writeFileSync(join(directory, "marketplace.json"), manifest);
const pluginDir = join(directory, PLUGIN_NAME);
mkdirSync(pluginDir, { recursive: true });
writeSkillFile(pluginDir);
return directory;
}
function createDirectPluginFixture(): string {
const directory = createUniqueDirectory("copilot-e2e-plugin");
const manifest = `{
"name": "${DIRECT_PLUGIN_NAME}",
"description": "E2E demo plugin installed directly from a local path.",
"version": "1.0.0"
}
`;
writeFileSync(join(directory, "plugin.json"), manifest);
writeSkillFile(directory);
return directory;
}
function writeSkillFile(pluginDir: string): void {
const skill = `---
name: csharp-e2e-skill
description: A demo skill contributed by the E2E test plugin.
---
# Demo Skill
This skill exists so the plugin reports at least one installed skill.
`;
writeFileSync(join(pluginDir, "SKILL.md"), skill);
}
it("should install and list plugin from local marketplace", { timeout: 120_000 }, async () => {
const marketplaceDir = createLocalMarketplaceFixture();
const { client, home } = await createIsolatedStartedClient();
try {
await client.rpc.plugins.marketplaces.add({ source: marketplaceDir });
const spec = `${PLUGIN_NAME}@${MARKETPLACE_NAME}`;
const install = await client.rpc.plugins.install({ source: spec });
expect(install.plugin.name).toBe(PLUGIN_NAME);
expect(install.plugin.marketplace).toBe(MARKETPLACE_NAME);
expect(install.plugin.enabled).toBe(true);
expect(install.skillsInstalled).toBeGreaterThanOrEqual(1);
expect(install.deprecationWarning ?? null).toBeNull();
const afterInstall = await client.rpc.plugins.list();
const listed = afterInstall.plugins.filter(
(plugin) => plugin.name === PLUGIN_NAME && plugin.marketplace === MARKETPLACE_NAME
);
expect(listed).toHaveLength(1);
expect(listed[0].enabled).toBe(true);
} finally {
await disposeIsolated(client, home, marketplaceDir);
}
});
it("should enable and disable marketplace plugin", { timeout: 120_000 }, async () => {
const marketplaceDir = createLocalMarketplaceFixture();
const { client, home } = await createIsolatedStartedClient();
try {
const spec = `${PLUGIN_NAME}@${MARKETPLACE_NAME}`;
await client.rpc.plugins.marketplaces.add({ source: marketplaceDir });
await client.rpc.plugins.install({ source: spec });
await client.rpc.plugins.disable({ names: [spec] });
expect(getPlugin(await client.rpc.plugins.list()).enabled).toBe(false);
await client.rpc.plugins.enable({ names: [spec] });
expect(getPlugin(await client.rpc.plugins.list()).enabled).toBe(true);
} finally {
await disposeIsolated(client, home, marketplaceDir);
}
});
it("should update single marketplace plugin", { timeout: 120_000 }, async () => {
const marketplaceDir = createLocalMarketplaceFixture();
const { client, home } = await createIsolatedStartedClient();
try {
const spec = `${PLUGIN_NAME}@${MARKETPLACE_NAME}`;
await client.rpc.plugins.marketplaces.add({ source: marketplaceDir });
await client.rpc.plugins.install({ source: spec });
const update = await client.rpc.plugins.update({ name: spec });
expect(update.skillsInstalled).toBeGreaterThanOrEqual(1);
expect(update.previousVersion).toBe("1.0.0");
expect(update.newVersion).toBe("1.0.0");
} finally {
await disposeIsolated(client, home, marketplaceDir);
}
});
it("should update all installed plugins", { timeout: 120_000 }, async () => {
const marketplaceDir = createLocalMarketplaceFixture();
const { client, home } = await createIsolatedStartedClient();
try {
const spec = `${PLUGIN_NAME}@${MARKETPLACE_NAME}`;
await client.rpc.plugins.marketplaces.add({ source: marketplaceDir });
await client.rpc.plugins.install({ source: spec });
const result = await client.rpc.plugins.updateAll();
const entries = result.results.filter(
(entry) => entry.name === PLUGIN_NAME && entry.marketplace === MARKETPLACE_NAME
);
expect(entries).toHaveLength(1);
expect(entries[0].success).toBe(true);
expect(entries[0].skillsInstalled).toBeGreaterThanOrEqual(1);
} finally {
await disposeIsolated(client, home, marketplaceDir);
}
});
it(
"should install direct local plugin with deprecation warning",
{ timeout: 120_000 },
async () => {
const pluginDir = createDirectPluginFixture();
const { client, home } = await createIsolatedStartedClient();
try {
const install = await client.rpc.plugins.install({ source: pluginDir });
expect(install.plugin.name).toBe(DIRECT_PLUGIN_NAME);
expect(install.plugin.marketplace).toBe("");
expect(install.deprecationWarning).toBeTruthy();
expect(install.deprecationWarning?.toLowerCase()).toContain("deprecated");
expect(install.skillsInstalled).toBeGreaterThanOrEqual(1);
const afterInstall = await client.rpc.plugins.list();
expect(
afterInstall.plugins.filter((plugin) => plugin.name === DIRECT_PLUGIN_NAME)
).toHaveLength(1);
expect(install.plugin.directSourceId).toBeTruthy();
await client.rpc.plugins.uninstall({
name: DIRECT_PLUGIN_NAME,
directSourceId: install.plugin.directSourceId,
});
const afterUninstall = await client.rpc.plugins.list();
expect(
afterUninstall.plugins.some((plugin) => plugin.name === DIRECT_PLUGIN_NAME)
).toBe(false);
} finally {
await disposeIsolated(client, home, pluginDir);
}
}
);
it(
"should list browse refresh and remove local marketplace",
{ timeout: 120_000 },
async () => {
const marketplaceDir = createLocalMarketplaceFixture();
const { client, home } = await createIsolatedStartedClient();
try {
const add = await client.rpc.plugins.marketplaces.add({ source: marketplaceDir });
expect(add.name).toBe(MARKETPLACE_NAME);
const list = await client.rpc.plugins.marketplaces.list();
const mine = list.marketplaces.filter(
(marketplace) => marketplace.name === MARKETPLACE_NAME
);
expect(mine).toHaveLength(1);
expect(mine[0].isDefault).not.toBe(true);
expect(
list.marketplaces.some((marketplace) => marketplace.isDefault === true)
).toBe(true);
const browse = await client.rpc.plugins.marketplaces.browse({
name: MARKETPLACE_NAME,
});
const advertised = browse.plugins.filter((plugin) => plugin.name === PLUGIN_NAME);
expect(advertised).toHaveLength(1);
expect(advertised[0].description).toBeTruthy();
const refresh = await client.rpc.plugins.marketplaces.refresh({
name: MARKETPLACE_NAME,
});
const refreshed = refresh.results.filter(
(result) => result.name === MARKETPLACE_NAME
);
expect(refreshed).toHaveLength(1);
expect(refreshed[0].success).toBe(true);
const remove = await client.rpc.plugins.marketplaces.remove({
name: MARKETPLACE_NAME,
});
expect(remove.removed).toBe(true);
const afterRemove = await client.rpc.plugins.marketplaces.list();
expect(
afterRemove.marketplaces.some(
(marketplace) => marketplace.name === MARKETPLACE_NAME
)
).toBe(false);
} finally {
await disposeIsolated(client, home, marketplaceDir);
}
}
);
it("should reload mcp config cache", { timeout: 120_000 }, async () => {
const { client, home } = await createIsolatedStartedClient();
try {
await client.rpc.mcp.config.reload();
} finally {
await disposeIsolated(client, home);
}
});
function getPlugin(list: {
plugins: Array<{ name: string; marketplace: string; enabled: boolean }>;
}) {
const plugins = list.plugins.filter(
(plugin) => plugin.name === PLUGIN_NAME && plugin.marketplace === MARKETPLACE_NAME
);
expect(plugins).toHaveLength(1);
return plugins[0];
}
});