-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathremove.test.ts
More file actions
400 lines (345 loc) · 15.2 KB
/
Copy pathremove.test.ts
File metadata and controls
400 lines (345 loc) · 15.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
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
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { Command } from "commander";
import { mkdir, readFile, writeFile, rm, access } from "fs/promises";
import { join } from "path";
import { tmpdir } from "os";
const trackEvent = vi.fn();
const mockCheckboxWithHover = vi.fn();
let logOutput: string[];
vi.mock("../utils/tracking.js", () => ({
trackEvent: (...args: unknown[]) => trackEvent(...args),
}));
vi.mock("../utils/prompts.js", () => ({
checkboxWithHover: (...args: unknown[]) => mockCheckboxWithHover(...args),
}));
const mockSpinner = {
start: vi.fn().mockReturnThis(),
stop: vi.fn().mockReturnThis(),
succeed: vi.fn().mockReturnThis(),
fail: vi.fn().mockReturnThis(),
text: "",
};
vi.mock("ora", () => ({ default: () => mockSpinner }));
import { registerRemoveCommand } from "../commands/remove.js";
import { readYamlConfig } from "../setup/mcp-writer.js";
let tempDir: string;
let originalCwd: string;
async function exists(path: string): Promise<boolean> {
try {
await access(path);
return true;
} catch {
return false;
}
}
async function runCommand(...args: string[]): Promise<void> {
const program = new Command();
program.exitOverride();
registerRemoveCommand(program);
await program.parseAsync(["node", "test", ...args]);
}
beforeEach(async () => {
vi.clearAllMocks();
logOutput = [];
vi.spyOn(console, "log").mockImplementation((...args: unknown[]) => {
logOutput.push(args.join(" "));
});
vi.spyOn(console, "error").mockImplementation(() => {});
originalCwd = process.cwd();
tempDir = join(tmpdir(), `ctx7-uninstall-${Date.now()}`);
await mkdir(tempDir, { recursive: true });
process.chdir(tempDir);
});
afterEach(async () => {
process.chdir(originalCwd);
await rm(tempDir, { recursive: true, force: true });
vi.restoreAllMocks();
});
describe("remove command", () => {
test("removes only CLI artifacts for cursor project setup", async () => {
const rulePath = join(tempDir, ".cursor", "rules", "context7.mdc");
const cliSkillPath = join(tempDir, ".cursor", "skills", "find-docs", "SKILL.md");
const mcpSkillPath = join(tempDir, ".cursor", "skills", "context7-mcp", "SKILL.md");
await mkdir(join(tempDir, ".cursor", "rules"), { recursive: true });
await mkdir(join(tempDir, ".cursor", "skills", "find-docs"), { recursive: true });
await mkdir(join(tempDir, ".cursor", "skills", "context7-mcp"), { recursive: true });
await writeFile(rulePath, "cursor rule", "utf-8");
await writeFile(cliSkillPath, "find docs", "utf-8");
await writeFile(mcpSkillPath, "mcp skill", "utf-8");
await runCommand("remove", "--cursor", "--cli", "--project");
expect(await exists(rulePath)).toBe(false);
expect(await exists(join(tempDir, ".cursor", "skills", "find-docs"))).toBe(false);
expect(await exists(mcpSkillPath)).toBe(true);
expect(trackEvent).toHaveBeenCalledWith("command", { name: "remove" });
expect(trackEvent).toHaveBeenCalledWith("remove", {
agents: ["cursor"],
scope: "project",
modes: ["cli"],
});
});
test("removes only MCP artifacts for codex project setup", async () => {
const agentsPath = join(tempDir, "AGENTS.md");
const tomlPath = join(tempDir, ".codex", "config.toml");
const mcpSkillPath = join(tempDir, ".agents", "skills", "context7-mcp", "SKILL.md");
const cliSkillPath = join(tempDir, ".agents", "skills", "find-docs", "SKILL.md");
await mkdir(join(tempDir, ".codex"), { recursive: true });
await mkdir(join(tempDir, ".agents", "skills", "context7-mcp"), { recursive: true });
await mkdir(join(tempDir, ".agents", "skills", "find-docs"), { recursive: true });
await writeFile(
agentsPath,
"# Before\n\n<!-- context7 -->\nrule body\n<!-- context7 -->\n",
"utf-8"
);
await writeFile(
tomlPath,
'model = "gpt-5"\n\n[mcp_servers.context7]\nurl = "https://mcp.context7.com/mcp"\n\n[mcp_servers.other]\nurl = "https://other.com"\n',
"utf-8"
);
await writeFile(mcpSkillPath, "mcp skill", "utf-8");
await writeFile(cliSkillPath, "find docs", "utf-8");
await runCommand("remove", "--codex", "--mcp", "--project");
const agentsContent = await readFile(agentsPath, "utf-8");
const tomlContent = await readFile(tomlPath, "utf-8");
expect(agentsContent).not.toContain("<!-- context7 -->");
expect(tomlContent).toContain("[mcp_servers.other]");
expect(tomlContent).not.toContain("[mcp_servers.context7]");
expect(await exists(join(tempDir, ".agents", "skills", "context7-mcp"))).toBe(false);
expect(await exists(cliSkillPath)).toBe(true);
expect(trackEvent).toHaveBeenCalledWith("remove", {
agents: ["codex"],
scope: "project",
modes: ["mcp"],
});
});
test("supports uninstall alias and --all to remove both setup modes", async () => {
const agentsPath = join(tempDir, "AGENTS.md");
const tomlPath = join(tempDir, ".codex", "config.toml");
const mcpSkillPath = join(tempDir, ".agents", "skills", "context7-mcp", "SKILL.md");
const cliSkillPath = join(tempDir, ".agents", "skills", "find-docs", "SKILL.md");
await mkdir(join(tempDir, ".codex"), { recursive: true });
await mkdir(join(tempDir, ".agents", "skills", "context7-mcp"), { recursive: true });
await mkdir(join(tempDir, ".agents", "skills", "find-docs"), { recursive: true });
await writeFile(
agentsPath,
"# Before\n\n<!-- context7 -->\nrule body\n<!-- context7 -->\n",
"utf-8"
);
await writeFile(
tomlPath,
'[mcp_servers.context7]\nurl = "https://mcp.context7.com/mcp"\n',
"utf-8"
);
await writeFile(mcpSkillPath, "mcp skill", "utf-8");
await writeFile(cliSkillPath, "find docs", "utf-8");
await runCommand("uninstall", "--codex", "--all", "--project");
const agentsContent = await readFile(agentsPath, "utf-8");
expect(agentsContent).not.toContain("<!-- context7 -->");
expect(await exists(join(tempDir, ".agents", "skills", "context7-mcp"))).toBe(false);
expect(await exists(join(tempDir, ".agents", "skills", "find-docs"))).toBe(false);
expect(await readFile(tomlPath, "utf-8")).not.toContain("[mcp_servers.context7]");
expect(trackEvent).toHaveBeenCalledWith("remove", {
agents: ["codex"],
scope: "project",
modes: ["mcp", "cli"],
});
});
test("skips mode prompt when only one setup mode exists", async () => {
const rulePath = join(tempDir, ".cursor", "rules", "context7.mdc");
const cliSkillPath = join(tempDir, ".cursor", "skills", "find-docs", "SKILL.md");
const mcpSkillPath = join(tempDir, ".cursor", "skills", "context7-mcp", "SKILL.md");
await mkdir(join(tempDir, ".cursor", "rules"), { recursive: true });
await mkdir(join(tempDir, ".cursor", "skills", "find-docs"), { recursive: true });
await mkdir(join(tempDir, ".cursor", "skills", "context7-mcp"), { recursive: true });
await writeFile(rulePath, "cursor rule", "utf-8");
await writeFile(cliSkillPath, "find docs", "utf-8");
await writeFile(mcpSkillPath, "mcp skill", "utf-8");
await rm(join(tempDir, ".cursor", "skills", "context7-mcp"), { recursive: true });
await runCommand("remove", "--cursor", "--project");
expect(mockCheckboxWithHover).not.toHaveBeenCalled();
expect(await exists(rulePath)).toBe(false);
expect(await exists(join(tempDir, ".cursor", "skills", "find-docs"))).toBe(false);
expect(await exists(mcpSkillPath)).toBe(false);
expect(trackEvent).toHaveBeenCalledWith("remove", {
agents: ["cursor"],
scope: "project",
modes: ["cli"],
});
});
test("prompts for setup mode when both MCP and CLI artifacts exist", async () => {
const agentsPath = join(tempDir, "AGENTS.md");
const tomlPath = join(tempDir, ".codex", "config.toml");
const mcpSkillPath = join(tempDir, ".agents", "skills", "context7-mcp", "SKILL.md");
const cliSkillPath = join(tempDir, ".agents", "skills", "find-docs", "SKILL.md");
await mkdir(join(tempDir, ".codex"), { recursive: true });
await mkdir(join(tempDir, ".agents", "skills", "context7-mcp"), { recursive: true });
await mkdir(join(tempDir, ".agents", "skills", "find-docs"), { recursive: true });
await writeFile(
agentsPath,
"# Before\n\n<!-- context7 -->\nrule body\n<!-- context7 -->\n",
"utf-8"
);
await writeFile(
tomlPath,
'[mcp_servers.context7]\nurl = "https://mcp.context7.com/mcp"\n',
"utf-8"
);
await writeFile(mcpSkillPath, "mcp skill", "utf-8");
await writeFile(cliSkillPath, "find docs", "utf-8");
mockCheckboxWithHover.mockResolvedValueOnce(["cli"]);
await runCommand("remove", "--codex", "--project");
expect(mockCheckboxWithHover).toHaveBeenCalledTimes(1);
expect(mockCheckboxWithHover.mock.calls[0]?.[0]).toMatchObject({
message: "Which Context7 setup modes do you want to remove?",
});
expect(await exists(join(tempDir, ".agents", "skills", "find-docs"))).toBe(false);
expect(await exists(mcpSkillPath)).toBe(true);
expect(await readFile(tomlPath, "utf-8")).toContain("[mcp_servers.context7]");
expect(trackEvent).toHaveBeenCalledWith("remove", {
agents: ["codex"],
scope: "project",
modes: ["cli"],
});
});
test("does not log not found items when other artifacts were removed", async () => {
const agentsPath = join(tempDir, "AGENTS.md");
const tomlPath = join(tempDir, ".codex", "config.toml");
const mcpSkillPath = join(tempDir, ".agents", "skills", "context7-mcp", "SKILL.md");
await mkdir(join(tempDir, ".codex"), { recursive: true });
await mkdir(join(tempDir, ".agents", "skills", "context7-mcp"), { recursive: true });
await writeFile(
agentsPath,
"# Before\n\n<!-- context7 -->\nrule body\n<!-- context7 -->\n",
"utf-8"
);
await writeFile(
tomlPath,
'[mcp_servers.context7]\nurl = "https://mcp.context7.com/mcp"\n',
"utf-8"
);
await writeFile(mcpSkillPath, "mcp skill", "utf-8");
await runCommand("remove", "--codex", "--all", "--project");
expect(logOutput.some((line) => line.includes("MCP config removed"))).toBe(true);
expect(logOutput.some((line) => line.includes("Rule removed"))).toBe(true);
expect(logOutput.some((line) => line.includes("Skill context7-mcp removed"))).toBe(true);
expect(logOutput.some((line) => line.includes("not found"))).toBe(false);
});
test("removes only context7 from cursor JSON MCP config", async () => {
const mcpPath = join(tempDir, ".cursor", "mcp.json");
await mkdir(join(tempDir, ".cursor"), { recursive: true });
await writeFile(
mcpPath,
JSON.stringify(
{
theme: "dark",
mcpServers: {
alpha: { url: "https://alpha.com" },
context7: { url: "https://mcp.context7.com/mcp" },
omega: { url: "https://omega.com" },
},
telemetry: { enabled: true },
},
null,
2
),
"utf-8"
);
await runCommand("remove", "--cursor", "--mcp", "--project");
expect(JSON.parse(await readFile(mcpPath, "utf-8"))).toEqual({
theme: "dark",
mcpServers: {
alpha: { url: "https://alpha.com" },
omega: { url: "https://omega.com" },
},
telemetry: { enabled: true },
});
});
test("removes only context7 from opencode JSONC MCP config", async () => {
const configPath = join(tempDir, "opencode.jsonc");
await writeFile(
configPath,
`{
// keep this file functional after removing Context7
"theme": "night",
"mcp": {
"alpha": { "type": "remote", "url": "https://alpha.com", "enabled": true },
"context7": { "type": "remote", "url": "https://mcp.context7.com/mcp", "enabled": true },
"omega": { "type": "remote", "url": "https://omega.com", "enabled": false }
},
"telemetry": { "enabled": true }
}
`,
"utf-8"
);
await runCommand("remove", "--opencode", "--mcp", "--project");
expect(JSON.parse(await readFile(configPath, "utf-8"))).toEqual({
theme: "night",
mcp: {
alpha: { type: "remote", url: "https://alpha.com", enabled: true },
omega: { type: "remote", url: "https://omega.com", enabled: false },
},
telemetry: { enabled: true },
});
});
test("removes Hermes MCP setup from YAML config", async () => {
const previousHermesHome = process.env.HERMES_HOME;
const hermesHome = join(tempDir, "hermes-home");
const configPath = join(hermesHome, "config.yaml");
const rulePath = join(hermesHome, "rules", "context7.md");
const mcpSkillPath = join(hermesHome, "skills", "context7-mcp", "SKILL.md");
process.env.HERMES_HOME = hermesHome;
try {
await mkdir(join(hermesHome, "rules"), { recursive: true });
await mkdir(join(hermesHome, "skills", "context7-mcp"), { recursive: true });
await writeFile(
configPath,
'display:\n language: zh\nmcp_servers:\n other:\n url: https://other.com\n context7:\n url: https://mcp.context7.com/mcp\n',
"utf-8"
);
await writeFile(rulePath, "hermes rule", "utf-8");
await writeFile(mcpSkillPath, "mcp skill", "utf-8");
await runCommand("remove", "--hermes", "--mcp");
const config = await readYamlConfig(configPath);
expect(config).toEqual({
display: { language: "zh" },
mcp_servers: { other: { url: "https://other.com" } },
});
expect(await exists(rulePath)).toBe(false);
expect(await exists(join(hermesHome, "skills", "context7-mcp"))).toBe(false);
expect(trackEvent).toHaveBeenCalledWith("remove", {
agents: ["hermes"],
scope: "global",
modes: ["mcp"],
});
} finally {
if (previousHermesHome === undefined) {
delete process.env.HERMES_HOME;
} else {
process.env.HERMES_HOME = previousHermesHome;
}
}
});
test("detects only agents with Context7 artifacts, not just agent folders", async () => {
const rulePath = join(tempDir, ".cursor", "rules", "context7.mdc");
const cliSkillPath = join(tempDir, ".cursor", "skills", "find-docs", "SKILL.md");
await mkdir(join(tempDir, ".cursor", "rules"), { recursive: true });
await mkdir(join(tempDir, ".cursor", "skills", "find-docs"), { recursive: true });
await mkdir(join(tempDir, ".gemini"), { recursive: true });
await writeFile(rulePath, "cursor rule", "utf-8");
await writeFile(cliSkillPath, "find docs", "utf-8");
mockCheckboxWithHover.mockResolvedValueOnce(["cursor"]);
await runCommand("remove", "--project");
expect(logOutput.some((line) => line.includes("Detected: Cursor"))).toBe(true);
expect(logOutput.some((line) => line.includes("Gemini CLI"))).toBe(false);
expect(mockCheckboxWithHover.mock.calls[0]?.[0]).toMatchObject({
message: "Which agents do you want to remove Context7 setup from?",
choices: [{ name: "Cursor", value: "cursor" }],
});
});
test("does not prompt when no Context7 setup is detected", async () => {
await mkdir(join(tempDir, ".gemini"), { recursive: true });
await writeFile(join(tempDir, ".gemini", "settings.json"), "{}", "utf-8");
await runCommand("remove", "--project");
expect(mockCheckboxWithHover).not.toHaveBeenCalled();
expect(logOutput.some((line) => line.includes("No Context7 setup detected"))).toBe(true);
});
});