import { expectResultValue } from "../../tests/support/expectResultValue.js"; import { HOST_PLATFORM } from "../platform/HostPlatform.js"; import { describe, expect, it } from "vitest"; import { parse as parseJsonc, type ParseError } from "jsonc-parser"; import { clientServerPath, parseClientConfiguration, serializeClientConfiguration, withClientServers, type ClientConfigurationEditOptions, type ClientConfigurationFormat, } from "./ClientConfigurationDocument.js"; const owned = { command: "rea", args: ["mcp"] }; const foreign = { command: "/usr/bin/false" }; const removeRea = ( text: string, format: ClientConfigurationFormat, options?: ClientConfigurationEditOptions, ) => { const parsed = expectResultValue(parseClientConfiguration(text, format)); const { rea: _removed, ...remaining } = parsed.servers; return serializeClientConfiguration( withClientServers(parsed, remaining, HOST_PLATFORM.platform), format, text, [clientServerPath(parsed, "rea")], options, ); }; describe("JSON client configuration repeated-member edits", () => { it("keeps the default refusal for repeated deletions", () => { const text = `{"mcpServers":{"rea":${JSON.stringify(foreign)},` + `"rea":${JSON.stringify(owned)}}}`; expect(removeRea(text, "claude_code")).toEqual({ ok: false, error: { kind: "shadowed_member", path: ["mcpServers", "rea"], }, }); }); it("removes all authorized repeats and preserves peer values in strict JSON", () => { const text = `{"mcpServers":{"rea":${JSON.stringify(foreign)},` + `"peer":{"command":"peer"},` + `"rea":${JSON.stringify(owned)},` + `"other":{"args":[1,2]},` + `"rea":${JSON.stringify(owned)}}}`; const parsed = expectResultValue( parseClientConfiguration(text, "claude_code"), ); const path = clientServerPath(parsed, "rea"); const result = removeRea(text, "claude_code", { removeShadowedMembersAt: [path], }); if (!result.ok) throw new Error("authorized repeats were refused"); expect(JSON.parse(result.value)).toEqual({ mcpServers: { peer: { command: "peer" }, other: { args: [1, 2] }, }, }); expect(result.value).not.toContain('"rea"'); }); it("does not apply repeat-removal authority to a different path", () => { const text = `{"mcpServers":{"rea":1,"rea":2,"peer":3,"peer":4}}`; const parsed = expectResultValue(parseClientConfiguration(text, "json")); const document = withClientServers(parsed, {}, HOST_PLATFORM.platform); const result = serializeClientConfiguration( document, "json", text, [clientServerPath(parsed, "rea"), clientServerPath(parsed, "peer")], { removeShadowedMembersAt: [clientServerPath(parsed, "rea")] }, ); expect(result).toEqual({ ok: false, error: { kind: "shadowed_member", path: ["mcpServers", "peer"], }, }); }); it("removes a single registration without authority and leaves a repeat byte-identical", () => { const text = `{"mcpServers":{/* keep peer formatting */"rea":1, "peer":2,}}`; const result = removeRea(text, "json"); if (!result.ok) throw new Error("single registration removal was refused"); const afterRemoval = `${result.value}\n`; expect(removeRea(afterRemoval, "json")).toEqual({ ok: true, value: afterRemoval, }); const errors: ParseError[] = []; expect( parseJsonc(result.value, errors, { allowTrailingComma: true }), ).toEqual({ mcpServers: { peer: 2 }, }); expect(errors).toEqual([]); expect(result.value).toContain("/* keep peer formatting */"); }); it("matches escaped keys and retains a repeated inactive parent byte for byte", () => { const inactive = `{"rea":${JSON.stringify(foreign)}}`; const text = `{"mcpServers":${inactive},` + `"mcpServers":{"r\\u0065a":${JSON.stringify(foreign)},` + `"peer":2,"rea":${JSON.stringify(owned)}}}`; const result = removeRea(text, "claude_code", { removeShadowedMembersAt: [["mcpServers", "rea"]], }); if (!result.ok) throw new Error("authorized escaped repeat was refused"); expect(result.value).toContain(`"mcpServers":${inactive}`); expect(JSON.parse(result.value)).toEqual({ mcpServers: { peer: 2 }, }); }); it("preserves JSONC comments and parses BOM/CRLF output independently", () => { const text = `\uFEFF{\r\n // top comment\r\n "mcp": {\r\n` + ` "r\\u0065a": ${JSON.stringify(foreign)}, // first\r\n` + ` "peer": { "command": "peer" },\r\n` + ` /* second */ "rea": ${JSON.stringify(owned)},\r\n` + ` },\r\n}\r\n`; const result = removeRea(text, "opencode", { removeShadowedMembersAt: [["mcp", "rea"]], }); if (!result.ok) throw new Error("authorized JSONC repeats were refused"); expect(result.value).toContain("// top comment"); expect(result.value).toContain("// first"); expect(result.value).toContain("/* second */"); expect(result.value.startsWith("\uFEFF")).toBe(true); const errors: ParseError[] = []; const document = parseJsonc(result.value.replace(/^\uFEFF/u, " "), errors, { allowTrailingComma: true, }); expect(errors).toEqual([]); expect(result.value.replaceAll("\r\n", "")).not.toContain("\n"); expect(document).toEqual({ mcp: { peer: { command: "peer" } }, }); }); });