|
| 1 | +import { mkdir, readFile, readdir, writeFile } from "node:fs/promises"; |
| 2 | +import { dirname, join } from "node:path"; |
| 3 | +import { expect } from "vitest"; |
| 4 | + |
| 5 | +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; |
| 6 | +import { cliTest } from "../../support/cli/cliFixture.js"; |
| 7 | + |
| 8 | +const cases = [ |
| 9 | + { |
| 10 | + client: "cursor", |
| 11 | + path: ".cursor/mcp.json", |
| 12 | + text: "[]", |
| 13 | + diagnostic: "The configuration root must be an object", |
| 14 | + }, |
| 15 | + { |
| 16 | + client: "cursor", |
| 17 | + path: ".cursor/mcp.json", |
| 18 | + text: "null", |
| 19 | + diagnostic: "The configuration root must be an object", |
| 20 | + }, |
| 21 | + { |
| 22 | + client: "cursor", |
| 23 | + path: ".cursor/mcp.json", |
| 24 | + text: '{"mcpServers":null,"keep":1}', |
| 25 | + diagnostic: '"mcpServers" must be an object', |
| 26 | + }, |
| 27 | + { |
| 28 | + client: "cursor", |
| 29 | + path: ".cursor/mcp.json", |
| 30 | + text: '{"mcpServers":"x","keep":1}', |
| 31 | + diagnostic: '"mcpServers" must be an object', |
| 32 | + }, |
| 33 | + { |
| 34 | + client: "pi", |
| 35 | + path: ".pi/agent/mcp.json", |
| 36 | + text: '{"mcpServers":[]}', |
| 37 | + diagnostic: '"mcpServers" must be an object', |
| 38 | + }, |
| 39 | + { |
| 40 | + client: "codex", |
| 41 | + path: ".codex/config.toml", |
| 42 | + text: 'mcp_servers = "x"\n', |
| 43 | + diagnostic: '"mcp_servers" must be an object', |
| 44 | + }, |
| 45 | + { |
| 46 | + client: "opencode", |
| 47 | + path: ".config/opencode/opencode.json", |
| 48 | + text: '{"mcp":{"servers":null}}', |
| 49 | + diagnostic: '"mcp.servers" must be an object', |
| 50 | + }, |
| 51 | + { |
| 52 | + client: "hermes", |
| 53 | + path: ".hermes/config.yaml", |
| 54 | + text: "- item\n", |
| 55 | + diagnostic: "The configuration root must be an object", |
| 56 | + }, |
| 57 | + { |
| 58 | + client: "hermes", |
| 59 | + path: ".hermes/config.yaml", |
| 60 | + text: "mcp_servers: null\nkeep: 1\n", |
| 61 | + diagnostic: '"mcp_servers" must be an object', |
| 62 | + }, |
| 63 | + { |
| 64 | + client: "cursor", |
| 65 | + path: ".cursor/mcp.json", |
| 66 | + text: "not-json", |
| 67 | + diagnostic: "Configuration is not valid JSON", |
| 68 | + syntax: "JSON", |
| 69 | + }, |
| 70 | + { |
| 71 | + client: "hermes", |
| 72 | + path: ".hermes/config.yaml", |
| 73 | + text: "mcp_servers: [\n", |
| 74 | + diagnostic: "Configuration is not valid YAML", |
| 75 | + syntax: "YAML", |
| 76 | + }, |
| 77 | + { |
| 78 | + client: "dsh", |
| 79 | + path: ".dsh/cordis.patch.yml", |
| 80 | + text: "- insert: [\n", |
| 81 | + diagnostic: "Configuration is not valid YAML", |
| 82 | + syntax: "YAML", |
| 83 | + }, |
| 84 | +] as const; |
| 85 | + |
| 86 | +for (const scenario of cases) |
| 87 | + cliTest( |
| 88 | + `setup and uninstall explain ${scenario.client} configuration ${scenario.text}`, |
| 89 | + async ({ cli }) => { |
| 90 | + const home = await createTestTempDirectory( |
| 91 | + "rea-client-config-diagnostic-", |
| 92 | + ); |
| 93 | + const config = join(home, scenario.path); |
| 94 | + await mkdir(dirname(config), { recursive: true }); |
| 95 | + await writeFile(config, scenario.text); |
| 96 | + const environment = { |
| 97 | + HOME: home, |
| 98 | + USERPROFILE: home, |
| 99 | + XDG_CONFIG_HOME: join(home, ".config"), |
| 100 | + HERMES_HOME: join(home, ".hermes"), |
| 101 | + DSH_HOME: join(home, ".dsh"), |
| 102 | + }; |
| 103 | + const setup = await cli.run({ |
| 104 | + arguments: [ |
| 105 | + "setup", |
| 106 | + "--client", |
| 107 | + scenario.client, |
| 108 | + "--skill=false", |
| 109 | + "--yes", |
| 110 | + "--json", |
| 111 | + ], |
| 112 | + cwd: home, |
| 113 | + environment, |
| 114 | + }); |
| 115 | + expect(setup.exitCode).toBe(1); |
| 116 | + expect(setup.json).toMatchObject({ |
| 117 | + status: "needs_human", |
| 118 | + plannedActions: [], |
| 119 | + appliedActions: [], |
| 120 | + }); |
| 121 | + if ("syntax" in scenario) { |
| 122 | + expect(setup.json).toMatchObject({ remediation: expect.any(String) }); |
| 123 | + if (scenario.syntax === "JSON") |
| 124 | + expect(setup.stdout).toContain("Invalid JSON/JSONC"); |
| 125 | + } else { |
| 126 | + expect(setup.json).toMatchObject({ |
| 127 | + remediation: expect.stringContaining(scenario.diagnostic), |
| 128 | + }); |
| 129 | + expect(setup.stdout).not.toContain("invalid_type"); |
| 130 | + } |
| 131 | + const uninstall = await cli.run({ |
| 132 | + arguments: ["uninstall", "--json"], |
| 133 | + cwd: home, |
| 134 | + environment, |
| 135 | + }); |
| 136 | + expect(uninstall.exitCode).toBe(1); |
| 137 | + expect(uninstall.json).toMatchObject({ |
| 138 | + status: "failed", |
| 139 | + items: expect.arrayContaining([ |
| 140 | + expect.objectContaining({ |
| 141 | + name: scenario.client, |
| 142 | + status: "failed", |
| 143 | + detail: expect.stringContaining(scenario.diagnostic), |
| 144 | + }), |
| 145 | + ]), |
| 146 | + }); |
| 147 | + if (!("syntax" in scenario)) |
| 148 | + expect(uninstall.stdout).not.toContain("not valid JSON"); |
| 149 | + expect(await readFile(config, "utf8")).toBe(scenario.text); |
| 150 | + expect(await readdir(dirname(config))).toEqual([ |
| 151 | + scenario.path.split("/").at(-1), |
| 152 | + ]); |
| 153 | + }, |
| 154 | + ); |
0 commit comments