From 4b1066501b379107b2375e270f386fc9855a85c1 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 9 Oct 2026 00:20:50 +0800 Subject: [PATCH] refactor(setup): validate formats through canonical configuration --- src/application/SetupClientConfiguration.ts | 47 +++++--- .../clientConfigurationTransactions.test.ts | 53 +++++++++- .../filesystem/clientLifecycle.test.ts | 16 +-- tests/boundary/filesystem/setupConfig.test.ts | 100 ++++++++++++++---- 4 files changed, 168 insertions(+), 48 deletions(-) diff --git a/src/application/SetupClientConfiguration.ts b/src/application/SetupClientConfiguration.ts index d59b06f17..707d6ae9f 100644 --- a/src/application/SetupClientConfiguration.ts +++ b/src/application/SetupClientConfiguration.ts @@ -27,22 +27,6 @@ import type { SetupClient } from "./SupportedClients.js"; const defaultCommand = (): readonly string[] => npxRegistrationCommand(); -/** Back up, atomically update, and semantically read back one JSON MCP configuration. */ -export const configureJsonClient = ( - client: SetupClient, - environment: SetupProviderEnvironment = {}, - command: readonly string[] = defaultCommand(), -): Promise => - configureClientDocument(client, environment, command, "json"); - -/** Back up, atomically update, and semantically read back one TOML MCP configuration. */ -export const configureTomlClient = ( - client: SetupClient, - environment: SetupProviderEnvironment = {}, - command: readonly string[] = defaultCommand(), -): Promise => - configureClientDocument(client, environment, command, "toml"); - /** Configure one supported client's native stdio MCP registration shape. */ export const configureClientConfiguration = ( client: SetupClient, @@ -54,6 +38,37 @@ export const configureClientConfiguration = ( return configureClientDocument(client, environment, command, client.format); }; +/** @deprecated Use configureClientConfiguration with the client's declared format. */ +export const configureJsonClient = ( + client: SetupClient, + environment: SetupProviderEnvironment = {}, + command: readonly string[] = defaultCommand(), +): Promise => + configureClientWithFormat(client, "json", environment, command); + +/** @deprecated Use configureClientConfiguration with the client's declared format. */ +export const configureTomlClient = ( + client: SetupClient, + environment: SetupProviderEnvironment = {}, + command: readonly string[] = defaultCommand(), +): Promise => + configureClientWithFormat(client, "toml", environment, command); + +const configureClientWithFormat = ( + client: SetupClient, + format: "json" | "toml", + environment: SetupProviderEnvironment, + command: readonly string[], +): Promise => { + if (client.format !== undefined && client.format !== format) + return Promise.resolve({ status: "failed", reason: "readback" }); + return configureClientConfiguration( + { ...client, format: client.format ?? format }, + environment, + command, + ); +}; + const configureClientDocument = async ( client: SetupClient, environment: SetupProviderEnvironment, diff --git a/tests/boundary/filesystem/clientConfigurationTransactions.test.ts b/tests/boundary/filesystem/clientConfigurationTransactions.test.ts index 7f803cad8..ceed2b33b 100644 --- a/tests/boundary/filesystem/clientConfigurationTransactions.test.ts +++ b/tests/boundary/filesystem/clientConfigurationTransactions.test.ts @@ -4,6 +4,7 @@ import { join } from "node:path"; import { describe, expect, it } from "vitest"; import { + configureClientConfiguration, configureJsonClient, configureTomlClient, } from "../../../src/application/SetupClientConfiguration.js"; @@ -11,8 +12,8 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; describe("client configuration write failures", () => { it.skipIf(process.platform === "win32" || process.getuid?.() === 0).each([ - ["json", configureJsonClient], - ["toml", configureTomlClient], + ["json", configureClientConfiguration], + ["toml", configureClientConfiguration], ] as const)( "classifies a denied %s write as a write failure", async (format, configure) => { @@ -41,7 +42,9 @@ describe("TOML client configuration comparison", () => { const original = `unrelated = ${value}\n[mcp_servers.rea]\ncommand = "old"\nstartup_timeout_sec = ${value}\n`; await writeFile(configPath, original); const client = { name: "codex", format: "toml", configPath } as const; - expect(await configureTomlClient(client, {}, ["rea", "mcp"])).toEqual({ + expect( + await configureClientConfiguration(client, {}, ["rea", "mcp"]), + ).toEqual({ status: "configured", backupPath: `${configPath}.rea.backup`, }); @@ -49,8 +52,50 @@ describe("TOML client configuration comparison", () => { expect(await readFile(configPath, "utf8")).toContain( `unrelated = ${value}`, ); - expect(await configureTomlClient(client, {}, ["rea", "mcp"])).toEqual({ + expect( + await configureClientConfiguration(client, {}, ["rea", "mcp"]), + ).toEqual({ status: "unchanged", }); }); }); + +describe("format-specific compatibility entrypoints", () => { + it("defaults a missing client format for legacy JSON and TOML calls", async () => { + const root = await createTestTempDirectory("rea-client-compat-format-"); + const jsonPath = join(root, "config.json"); + const tomlPath = join(root, "config.toml"); + + expect( + await configureJsonClient({ name: "cursor", configPath: jsonPath }), + ).toMatchObject({ + status: "configured", + }); + expect(await readFile(jsonPath, "utf8")).toContain('"mcpServers"'); + expect( + await configureTomlClient({ name: "codex", configPath: tomlPath }), + ).toMatchObject({ + status: "configured", + }); + expect(await readFile(tomlPath, "utf8")).toContain("[mcp_servers.rea]"); + }); + + it.each([ + ["JSON", configureJsonClient, "toml"], + ["TOML", configureTomlClient, "json"], + ] as const)( + "rejects a mismatched %s compatibility format", + async (_name, configure, format) => { + const root = await createTestTempDirectory("rea-client-compat-mismatch-"); + const configPath = join(root, "config"); + const original = "keep this file unchanged\n"; + await writeFile(configPath, original); + + expect(await configure({ name: "fixture", configPath, format })).toEqual({ + status: "failed", + reason: "readback", + }); + expect(await readFile(configPath, "utf8")).toBe(original); + }, + ); +}); diff --git a/tests/boundary/filesystem/clientLifecycle.test.ts b/tests/boundary/filesystem/clientLifecycle.test.ts index ed60c4f86..1a25e8100 100644 --- a/tests/boundary/filesystem/clientLifecycle.test.ts +++ b/tests/boundary/filesystem/clientLifecycle.test.ts @@ -14,7 +14,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { supportedClients } from "../../../src/application/SupportedClients.js"; -import { configureTomlClient } from "../../../src/application/SetupClientConfiguration.js"; +import { configureClientConfiguration } from "../../../src/application/SetupClientConfiguration.js"; import { detectClients } from "../../../src/application/SetupHost.js"; import { runUninstall, @@ -82,7 +82,7 @@ describe("client configuration filesystem lifecycle", () => { 'model = "gpt-5"\n[mcp_servers.other]\ncommand = "other"\n', ); expect( - await configureTomlClient( + await configureClientConfiguration( { name: "codex", configPath, format: "toml" }, { HOPPER_LAUNCHER_PATH: "/Hopper Path", @@ -100,7 +100,7 @@ describe("client configuration filesystem lifecycle", () => { expect(configured).toContain('GHIDRA_INSTALL_DIR = "/opt/ghidra"'); expect(configured).toContain('JAVA_HOME = "/opt/jdk-21"'); expect( - await configureTomlClient( + await configureClientConfiguration( { name: "codex", configPath, format: "toml" }, { HOPPER_LAUNCHER_PATH: "/Hopper Path", @@ -124,7 +124,7 @@ describe("client configuration filesystem lifecycle", () => { const original = await readFile(configPath, "utf8"); expect( - await configureTomlClient( + await configureClientConfiguration( { name: "codex", configPath, format: "toml" }, { HOPPER_LAUNCHER_PATH: "/Hopper Path", @@ -151,7 +151,7 @@ describe("client configuration filesystem lifecycle", () => { await symlink(targetPath, configPath); expect( - await configureTomlClient( + await configureClientConfiguration( { name: "codex", configPath, format: "toml" }, undefined, ["rea", "mcp"], @@ -170,7 +170,11 @@ describe("client configuration filesystem lifecycle", () => { await symlink(join(home, "missing-config.toml"), configPath); expect( - await configureTomlClient({ name: "codex", configPath, format: "toml" }), + await configureClientConfiguration({ + name: "codex", + configPath, + format: "toml", + }), ).toEqual({ status: "failed", reason: "path" }); expect((await lstat(configPath)).isSymbolicLink()).toBe(true); await expect( diff --git a/tests/boundary/filesystem/setupConfig.test.ts b/tests/boundary/filesystem/setupConfig.test.ts index e5e3ce25a..6819b69cd 100644 --- a/tests/boundary/filesystem/setupConfig.test.ts +++ b/tests/boundary/filesystem/setupConfig.test.ts @@ -5,12 +5,12 @@ import { describe, expect, it } from "vitest"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { resolveClientConfigTransactionPath } from "../../../src/application/ClientConfigPath.js"; -import { configureJsonClient } from "../../../src/application/SetupClientConfiguration.js"; +import { configureClientConfiguration } from "../../../src/application/SetupClientConfiguration.js"; import { PRODUCT_IDENTITY } from "../../../src/identity.js"; const pinnedPackage = PRODUCT_IDENTITY.registrationPackageSpecifier; -describe("JSON client configuration transaction", () => { +describe("JSON client configuration transaction: writing configuration through safe paths", () => { it.skipIf(typeof process.getuid !== "function")( "rejects a symlink target not owned by the current user", async () => { @@ -40,7 +40,11 @@ describe("JSON client configuration transaction", () => { const original = '{"theme":"dark","mcpServers":{"other":{"command":"other"}}}\n'; await writeFile(configPath, original); - const result = await configureJsonClient({ name: "cursor", configPath }); + const result = await configureClientConfiguration({ + name: "cursor", + configPath, + format: "json", + }); expect(result.status).toBe("configured"); expect(await readFile(`${configPath}.rea.backup`, "utf8")).toBe(original); expect(JSON.parse(await readFile(configPath, "utf8"))).toMatchObject({ @@ -64,7 +68,11 @@ describe("JSON client configuration transaction", () => { await symlink(targetPath, configPath); expect( - await configureJsonClient({ name: "cursor", configPath }), + await configureClientConfiguration({ + name: "cursor", + configPath, + format: "json", + }), ).toMatchObject({ status: "configured" }); expect((await lstat(configPath)).isSymbolicLink()).toBe(true); expect(await readFile(`${configPath}.rea.backup`, "utf8")).toBe(original); @@ -77,7 +85,13 @@ describe("JSON client configuration transaction", () => { }, }, }); - expect(await configureJsonClient({ name: "cursor", configPath })).toEqual({ + expect( + await configureClientConfiguration({ + name: "cursor", + configPath, + format: "json", + }), + ).toEqual({ status: "unchanged", }); }); @@ -87,7 +101,13 @@ describe("JSON client configuration transaction", () => { const configPath = join(directory, "mcp.json"); await symlink(join(directory, "missing.json"), configPath); - expect(await configureJsonClient({ name: "cursor", configPath })).toEqual({ + expect( + await configureClientConfiguration({ + name: "cursor", + configPath, + format: "json", + }), + ).toEqual({ status: "failed", reason: "path", }); @@ -96,7 +116,9 @@ describe("JSON client configuration transaction", () => { readFile(`${configPath}.rea.backup`, "utf8"), ).rejects.toThrow(); }); +}); +describe("JSON client configuration transaction: keeping matching configuration unchanged", () => { it("performs no write or second backup when configuration already matches", async () => { const directory = await createTestTempDirectory("rea-setup-"); const configPath = join(directory, "mcp.json"); @@ -104,7 +126,13 @@ describe("JSON client configuration transaction", () => { configPath, `${JSON.stringify({ mcpServers: { rea: { command: "npx", args: ["-y", pinnedPackage, "mcp"] } } })}\n`, ); - expect(await configureJsonClient({ name: "cursor", configPath })).toEqual({ + expect( + await configureClientConfiguration({ + name: "cursor", + configPath, + format: "json", + }), + ).toEqual({ status: "unchanged", }); await expect( @@ -132,8 +160,8 @@ describe("JSON client configuration transaction", () => { ); expect( - await configureJsonClient( - { name: "cursor", configPath }, + await configureClientConfiguration( + { name: "cursor", configPath, format: "json" }, { GHIDRA_INSTALL_DIR: "/opt/ghidra", JAVA_HOME: "/usr/lib/jvm/jdk-21", @@ -154,9 +182,13 @@ describe("JSON client configuration migration and provider paths", () => { '{"theme":"dark","mcpServers":{"rea":{"command":"npx","args":["-y","rea-agents","mcp"]},"other":{"command":"other"}}}\n'; await writeFile(configPath, original); - expect(await configureJsonClient({ name: "cursor", configPath })).toEqual( - expect.objectContaining({ status: "configured" }), - ); + expect( + await configureClientConfiguration({ + name: "cursor", + configPath, + format: "json", + }), + ).toEqual(expect.objectContaining({ status: "configured" })); expect(JSON.parse(await readFile(configPath, "utf8"))).toEqual({ theme: "dark", @@ -175,11 +207,13 @@ describe("JSON client configuration migration and provider paths", () => { const directory = await createTestTempDirectory("rea-setup-"); const configPath = join(directory, "mcp.json"); const hopperPath = "/Applications/Hopper v6.app/Contents/MacOS/hopper"; - const client = { name: "cursor", configPath }; + const client = { name: "cursor", configPath, format: "json" } as const; const original = "{}\n"; await writeFile(configPath, original); expect( - await configureJsonClient(client, { HOPPER_LAUNCHER_PATH: hopperPath }), + await configureClientConfiguration(client, { + HOPPER_LAUNCHER_PATH: hopperPath, + }), ).toMatchObject({ status: "configured", }); @@ -193,12 +227,14 @@ describe("JSON client configuration migration and provider paths", () => { }, }); expect( - await configureJsonClient(client, { HOPPER_LAUNCHER_PATH: hopperPath }), + await configureClientConfiguration(client, { + HOPPER_LAUNCHER_PATH: hopperPath, + }), ).toEqual({ status: "unchanged", }); expect( - await configureJsonClient(client, { + await configureClientConfiguration(client, { HOPPER_LAUNCHER_PATH: "/opt/hopper/bin/Hopper", }), ).toMatchObject({ status: "configured" }); @@ -208,13 +244,15 @@ describe("JSON client configuration migration and provider paths", () => { it("adds exact BYO Ghidra and Java paths without installing dependencies", async () => { const directory = await createTestTempDirectory("rea-setup-"); const configPath = join(directory, "mcp.json"); - const client = { name: "cursor", configPath }; + const client = { name: "cursor", configPath, format: "json" } as const; const environment = { GHIDRA_INSTALL_DIR: "/opt/ghidra_12.1.4_PUBLIC", JAVA_HOME: "/usr/lib/jvm/jdk-21", }; - expect(await configureJsonClient(client, environment)).toMatchObject({ + expect( + await configureClientConfiguration(client, environment), + ).toMatchObject({ status: "configured", }); expect(JSON.parse(await readFile(configPath, "utf8"))).toMatchObject({ @@ -227,7 +265,7 @@ describe("JSON client configuration migration and provider paths", () => { }, }, }); - expect(await configureJsonClient(client, environment)).toEqual({ + expect(await configureClientConfiguration(client, environment)).toEqual({ status: "unchanged", }); }); @@ -236,7 +274,13 @@ describe("JSON client configuration migration and provider paths", () => { const directory = await createTestTempDirectory("rea-setup-"); const configPath = join(directory, "mcp.json"); await writeFile(configPath, "not-json"); - expect(await configureJsonClient({ name: "cursor", configPath })).toEqual({ + expect( + await configureClientConfiguration({ + name: "cursor", + configPath, + format: "json", + }), + ).toEqual({ status: "failed", reason: "readback", }); @@ -248,7 +292,13 @@ describe("JSON client configuration migration and provider paths", () => { const directory = await createTestTempDirectory("rea-setup-"); const configPath = join(directory, "mcp.json"); await writeFile(configPath, original); - expect(await configureJsonClient({ name: "cursor", configPath })).toEqual({ + expect( + await configureClientConfiguration({ + name: "cursor", + configPath, + format: "json", + }), + ).toEqual({ status: "failed", reason: "readback", }); @@ -264,7 +314,13 @@ describe("JSON client configuration migration and provider paths", () => { const configPath = join(directory, "mcp.json"); const original = `{"mcpServers":${servers}}`; await writeFile(configPath, original); - expect(await configureJsonClient({ name: "cursor", configPath })).toEqual({ + expect( + await configureClientConfiguration({ + name: "cursor", + configPath, + format: "json", + }), + ).toEqual({ status: "failed", reason: "readback", });