diff --git a/tests/boundary/cli/cliLazyRegistration.test.ts b/tests/boundary/cli/cliLazyRegistration.test.ts index e6f6b5e789..f23b2c335b 100644 --- a/tests/boundary/cli/cliLazyRegistration.test.ts +++ b/tests/boundary/cli/cliLazyRegistration.test.ts @@ -1,7 +1,7 @@ import { join, resolve } from "node:path"; import { pathToFileURL } from "node:url"; import { cliCommands } from "../../../src/cli/commandRegistry.js"; -import { describe, expect, it } from "vitest"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { createCli } from "../../../src/cli.js"; import { @@ -33,6 +33,10 @@ import type { CliInstance } from "../../../src/cli/types.js"; import { createDirectAnalysis } from "../../../src/composition/directAnalysis.js"; import { PRODUCT_IDENTITY } from "../../../src/identity.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { + createTestWorkspace, + removeTestWorkspace, +} from "../../support/workspace/workspaceFixture.js"; import { processTest } from "../../support/process/processFixture.js"; import { serveInProcessCli } from "../../support/cli/serveInProcessCli.js"; @@ -63,14 +67,24 @@ const recordedRegistrations = async (): Promise> => { }; describe("lazy CLI registration", () => { - it("records every top-level command under the one registrar that defines it", async () => { - const recorded = await recordedRegistrations(); - const complete = createCli(ENVIRONMENT); + let recorded: Record = {}; + let complete!: CliInstance; + const registrars = new Map(); + + beforeAll(async () => { + recorded = await recordedRegistrations(); + complete = createCli(ENVIRONMENT); + for (const id of CLI_REGISTRATION_ORDER) { + registrars.set(id, await createRegistrarCli(id)); + } + }); + + it("records every top-level command under the one registrar that defines it", () => { expect(Object.keys(recorded).sort()).toEqual( [...topLevelNames(complete)].sort(), ); for (const id of CLI_REGISTRATION_ORDER) { - const alone = await createRegistrarCli(id); + const alone = registrars.get(id)!; expect( [...topLevelNames(alone)].sort(), `${id} registers what the recording assigns it`, @@ -84,11 +98,10 @@ describe("lazy CLI registration", () => { }); it("gives each command the same definition alone as in the complete CLI", async () => { - const complete = createCli(ENVIRONMENT); const completePaths = new Set(topLevelNames(complete)); const covered = new Set(); for (const id of CLI_REGISTRATION_ORDER) { - const alone = await createRegistrarCli(id); + const alone = registrars.get(id)!; for (const path of topLevelNames(alone)) { expect(completePaths.has(path), path).toBe(true); covered.add(path); @@ -143,6 +156,22 @@ describe("lazy CLI registration", () => { }); describe("lazy CLI execution", () => { + let compileCacheRoot = ""; + let compileCache = ""; + + beforeAll(async () => { + compileCacheRoot = ( + await createTestWorkspace("rea-cli-lazy-compile-cache-") + ).root; + compileCache = join(compileCacheRoot, "node-compile"); + }); + + afterAll(async () => { + if (compileCacheRoot !== "") { + await removeTestWorkspace(compileCacheRoot); + } + }); + processTest( "runs a recorded command without loading the complete CLI", async ({ processes }) => { @@ -160,7 +189,13 @@ registerHooks({ resolve(specifier, context, next) { processes.run( process.execPath, [`--import=${hook}`, join("scripts", "rea.mjs"), ...arguments_], - { env: { ...ENVIRONMENT, HOME: home } }, + { + env: { + ...ENVIRONMENT, + HOME: home, + NODE_COMPILE_CACHE: compileCache, + }, + }, ); const lazy = await run(["list-cutter-sessions", "--json"]); expect(lazy.exitCode, lazy.stderr).toBe(0); @@ -222,7 +257,13 @@ registerHooks({ resolve(specifier, context, next) { const result = await processes.run( process.execPath, [join("scripts", "rea.mjs"), ...invocation(name)], - { env: { ...ENVIRONMENT, HOME: home } }, + { + env: { + ...ENVIRONMENT, + HOME: home, + NODE_COMPILE_CACHE: compileCache, + }, + }, ); lazy.set(name, { document: document(result.stdout), diff --git a/tests/boundary/cli/defaultAmpRegistration.test.ts b/tests/boundary/cli/defaultAmpRegistration.test.ts index 3a88498a94..6661b9db86 100644 --- a/tests/boundary/cli/defaultAmpRegistration.test.ts +++ b/tests/boundary/cli/defaultAmpRegistration.test.ts @@ -1,25 +1,61 @@ -import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises"; +import { + mkdir, + readFile, + readdir, + rm, + stat, + writeFile, +} from "node:fs/promises"; import { join } from "node:path"; -import { expect, it } from "vitest"; +import { afterAll, beforeAll, expect, it } from "vitest"; import { decode } from "@toon-format/toon"; import { mcpRegistrationReportSchema } from "../../../src/contracts/mcpRegistration.js"; import { parseCliGlobalArguments } from "../../../src/cliBuiltinArguments.js"; -import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; import { registrationEnvironment } from "../../support/cli/registrationEnvironment.js"; +import { + createTestWorkspace, + removeTestWorkspace, +} from "../../support/workspace/workspaceFixture.js"; + +const PRESERVED_HOME_ENTRIES = new Set([ + "registration-environment.mjs", + "registration-bin", + "unexpected-acquisition.log", +]); + +let sharedRoot = ""; +let sharedHome = ""; +let sharedEnvironment: NodeJS.ProcessEnv = {}; + +beforeAll(async () => { + sharedRoot = (await createTestWorkspace("rea-default-amp-")).root; + sharedHome = join(sharedRoot, "home"); + sharedEnvironment = await registrationEnvironment(sharedHome); +}); + +afterAll(async () => { + if (sharedRoot !== "") { + await removeTestWorkspace(sharedRoot); + } +}); const fixture = async (initial: string) => { - const home = await createTestTempDirectory("rea-default-amp-"); - const directory = join(home, ".config", "amp"); + for (const entry of await readdir(sharedHome)) { + if (!PRESERVED_HOME_ENTRIES.has(entry)) { + await rm(join(sharedHome, entry), { recursive: true, force: true }); + } + } + const directory = join(sharedHome, ".config", "amp"); const settings = join(directory, "settings.json"); await mkdir(directory, { recursive: true }); await writeFile(settings, initial); return { - home, + home: sharedHome, directory, settings, - environment: await registrationEnvironment(home), + environment: sharedEnvironment, }; }; const original = `${JSON.stringify({ diff --git a/tests/boundary/cli/mcpAddOptionValidation.test.ts b/tests/boundary/cli/mcpAddOptionValidation.test.ts index 9fdff480f5..6527e14f49 100644 --- a/tests/boundary/cli/mcpAddOptionValidation.test.ts +++ b/tests/boundary/cli/mcpAddOptionValidation.test.ts @@ -3,27 +3,55 @@ import { mkdir, readFile, readdir, + rm, stat, writeFile, } from "node:fs/promises"; import { join } from "node:path"; -import { expect } from "vitest"; +import { afterAll, beforeAll, beforeEach, expect } from "vitest"; -import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest } from "../../support/cli/cliFixture.js"; +import { + createTestWorkspace, + removeTestWorkspace, +} from "../../support/workspace/workspaceFixture.js"; +import { cliTest, type TestCli } from "../../support/cli/cliFixture.js"; import { registrationEnvironment } from "../../support/cli/registrationEnvironment.js"; +let sharedRoot = ""; +let sharedHome = ""; +let sharedEnvironment: NodeJS.ProcessEnv = {}; + +beforeAll(async () => { + sharedRoot = (await createTestWorkspace("rea-mcp-add-validation-")).root; + sharedHome = join(sharedRoot, "home"); + sharedEnvironment = await registrationEnvironment(sharedHome); +}); + +beforeEach(async () => { + for (const directory of [".config", ".cursor", "appdata"]) { + await rm(join(sharedHome, directory), { recursive: true, force: true }); + } +}); + +afterAll(async () => { + if (sharedRoot !== "") { + await removeTestWorkspace(sharedRoot); + } +}); + +const runMcpAdd = ( + cli: TestCli, + arguments_: readonly string[], + cwd = sharedRoot, +) => + cli.run({ + arguments: [...arguments_, "--json"], + cwd, + environment: sharedEnvironment, + }); + cliTest("mcp add rejects valued options without a value", async ({ cli }) => { - const root = await createTestTempDirectory("rea-mcp-add-value-"); - const home = join(root, "home"); - const settingsPath = (selectedHome: string) => - join(selectedHome, ".config", "amp", "settings.json"); - const run = async (arguments_: readonly string[], selectedHome = home) => - cli.run({ - arguments: [...arguments_, "--json"], - cwd: root, - environment: await registrationEnvironment(selectedHome), - }); + const settingsPath = join(sharedHome, ".config", "amp", "settings.json"); for (const [flag, field] of [ ["--agent", "agent"], @@ -32,7 +60,7 @@ cliTest("mcp add rejects valued options without a value", async ({ cli }) => { ["-c=", "command"], ["--agent=", "agent"], ] as const) { - const result = await run(["mcp", "add", flag]); + const result = await runMcpAdd(cli, ["mcp", "add", flag]); expect(result.exitCode, result.stderr).toBe(1); expect(result.json).toMatchObject({ code: "VALIDATION_ERROR", @@ -44,13 +72,19 @@ cliTest("mcp add rejects valued options without a value", async ({ cli }) => { }), ]), }); - await expect(access(settingsPath(home))).rejects.toMatchObject({ + await expect(access(settingsPath)).rejects.toMatchObject({ code: "ENOENT", }); } // Incur would swallow the following flag as the option value; REA rejects it. - const swallowed = await run(["mcp", "add", "--command", "--agent", "amp"]); + const swallowed = await runMcpAdd(cli, [ + "mcp", + "add", + "--command", + "--agent", + "amp", + ]); expect(swallowed.exitCode, swallowed.stderr).toBe(1); expect(swallowed.json).toMatchObject({ code: "VALIDATION_ERROR", @@ -62,19 +96,18 @@ cliTest("mcp add rejects valued options without a value", async ({ cli }) => { }), ]), }); - await expect(access(settingsPath(home))).rejects.toMatchObject({ + await expect(access(settingsPath)).rejects.toMatchObject({ code: "ENOENT", }); - const existingHome = join(root, "existing-home"); - const existingSettingsPath = settingsPath(existingHome); - await mkdir(join(existingHome, ".config", "amp"), { recursive: true }); + await mkdir(join(sharedHome, ".config", "amp"), { recursive: true }); const existingSettings = '{"existing":true}\n'; - await writeFile(existingSettingsPath, existingSettings); - const rejectedWithExistingSettings = await run( - ["mcp", "add", "--agent"], - existingHome, - ); + await writeFile(settingsPath, existingSettings); + const rejectedWithExistingSettings = await runMcpAdd(cli, [ + "mcp", + "add", + "--agent", + ]); expect( rejectedWithExistingSettings.exitCode, rejectedWithExistingSettings.stderr, @@ -82,12 +115,13 @@ cliTest("mcp add rejects valued options without a value", async ({ cli }) => { expect(rejectedWithExistingSettings.json).toMatchObject({ code: "VALIDATION_ERROR", }); - expect(await readFile(existingSettingsPath, "utf8")).toBe(existingSettings); + expect(await readFile(settingsPath, "utf8")).toBe(existingSettings); // A registration with an explicit agent still writes the Amp registration. - const registered = await run(["mcp", "add", "--agent", "amp"]); + await rm(join(sharedHome, ".config"), { recursive: true, force: true }); + const registered = await runMcpAdd(cli, ["mcp", "add", "--agent", "amp"]); expect(registered.exitCode, registered.stderr).toBe(0); - const settings = JSON.parse(await readFile(settingsPath(home), "utf8")); + const settings = JSON.parse(await readFile(settingsPath, "utf8")); expect(settings["amp.mcpServers"].rea).toMatchObject({ command: expect.any(String), }); @@ -96,16 +130,7 @@ cliTest("mcp add rejects valued options without a value", async ({ cli }) => { cliTest( "mcp add rejects an argument that is neither an option nor a value", async ({ cli }) => { - const root = await createTestTempDirectory("rea-mcp-add-argument-"); - const home = join(root, "home"); - const settingsPath = join(home, ".config", "amp", "settings.json"); - const environment = await registrationEnvironment(home); - const run = (arguments_: readonly string[]) => - cli.run({ - arguments: [...arguments_, "--json"], - cwd: root, - environment, - }); + const settingsPath = join(sharedHome, ".config", "amp", "settings.json"); // Incur skips a stray word, so the registration used to succeed for a request // REA cannot honor and wrote the settings file anyway. @@ -115,7 +140,7 @@ cliTest( ["mcp", "add", "--agent=amp", "extra"], ["mcp", "add", "--bogus", "--agent", "amp"], ] as const) { - const result = await run(arguments_); + const result = await runMcpAdd(cli, arguments_); expect(result.exitCode, result.stderr).toBe(1); expect(result.json).toMatchObject({ code: "VALIDATION_ERROR", @@ -135,14 +160,19 @@ cliTest( const original = '{"amp.mcpServers":{"rea":{"command":"old","env":{"KEEP":"1"}}},"theme":"dark"}\n'; - await mkdir(join(home, ".config", "amp"), { recursive: true }); + await mkdir(join(sharedHome, ".config", "amp"), { recursive: true }); await writeFile(settingsPath, original); const before = await stat(settingsPath); - const rejected = await run(["mcp", "add", "extra", "--agent=amp"]); + const rejected = await runMcpAdd(cli, [ + "mcp", + "add", + "extra", + "--agent=amp", + ]); expect(rejected.exitCode, rejected.stderr).toBe(1); expect(await readFile(settingsPath, "utf8")).toBe(original); expect((await stat(settingsPath)).mtimeMs).toBe(before.mtimeMs); - expect(await readdir(join(home, ".config", "amp"))).toEqual([ + expect(await readdir(join(sharedHome, ".config", "amp"))).toEqual([ "settings.json", ]); @@ -152,7 +182,7 @@ cliTest( ["--agent=amp", "--command=node --version"], ["--agent=amp", "-c=node --version"], ]) { - const registered = await run(["mcp", "add", ...options]); + const registered = await runMcpAdd(cli, ["mcp", "add", ...options]); expect(registered.exitCode, registered.stderr).toBe(0); expect(JSON.parse(await readFile(settingsPath, "utf8"))).toEqual({ "amp.mcpServers": { @@ -168,16 +198,6 @@ cliTest( cliTest( "mcp add names the option the reported error is about", async ({ cli }) => { - const root = await createTestTempDirectory("rea-mcp-add-field-"); - const home = join(root, "home"); - const environment = await registrationEnvironment(home); - const run = (arguments_: readonly string[]) => - cli.run({ - arguments: [...arguments_, "--json"], - cwd: root, - environment, - }); - // A stray word alongside a missing value: the message describes the missing // flag, so `path` must name that option rather than the stray argument. for (const [arguments_, field] of [ @@ -185,7 +205,7 @@ cliTest( [["mcp", "add", "extra", "-c"], "command"], [["mcp", "add", "extra", "--agent"], "agent"], ] as const) { - const result = await run(arguments_); + const result = await runMcpAdd(cli, arguments_); expect(result.exitCode, result.stderr).toBe(1); expect(result.json).toMatchObject({ code: "VALIDATION_ERROR", @@ -203,24 +223,14 @@ cliTest( cliTest( "packaged --no-global refuses a global-only member before writing any selected client", async ({ cli }) => { - const home = await createTestTempDirectory("rea-mcp-project-scope-"); - const project = join(home, "project"); - await mkdir(project); - const environment = await registrationEnvironment(home); - const result = await cli.run({ - arguments: [ - "mcp", - "add", - "--agent", - "cursor", - "--agent", - "goose", - "--no-global", - "--json", - ], - cwd: project, - environment, - }); + const project = join(sharedRoot, "project"); + await rm(project, { recursive: true, force: true }); + await mkdir(project, { recursive: true }); + const result = await runMcpAdd( + cli, + ["mcp", "add", "--agent", "cursor", "--agent", "goose", "--no-global"], + project, + ); expect(result.exitCode, result.stderr).toBe(1); expect(result.json).toMatchObject({ code: "VALIDATION_ERROR", @@ -228,10 +238,10 @@ cliTest( }); const goose = process.platform === "win32" - ? join(home, "appdata", "Block", "goose", "config", "config.yaml") - : join(home, ".config", "goose", "config.yaml"); + ? join(sharedHome, "appdata", "Block", "goose", "config", "config.yaml") + : join(sharedHome, ".config", "goose", "config.yaml"); for (const path of [ - join(home, ".cursor", "mcp.json"), + join(sharedHome, ".cursor", "mcp.json"), join(project, ".cursor", "mcp.json"), goose, ]) diff --git a/tests/boundary/mcp/deferredRuntime.test.ts b/tests/boundary/mcp/deferredRuntime.test.ts index 795880f6ac..bf509d00ab 100644 --- a/tests/boundary/mcp/deferredRuntime.test.ts +++ b/tests/boundary/mcp/deferredRuntime.test.ts @@ -11,10 +11,14 @@ import { writeFile, } from "node:fs/promises"; import { resolve, join, dirname } from "node:path"; -import { afterAll, expect, it, onTestFinished } from "vitest"; +import { afterAll, beforeAll, expect, it, onTestFinished } from "vitest"; import { artifactMcpCall } from "../../../scripts/lib/artifact-e2e.mjs"; import { closeVerifierResources } from "../../../scripts/lib/verifier-cleanup.mjs"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { + createTestWorkspace, + removeTestWorkspace, +} from "../../support/workspace/workspaceFixture.js"; import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; import { PROMPT_CONTRACTS } from "../../../src/contracts/promptContracts.js"; import { TOOL_FAMILY_DEFINITIONS } from "../../../src/contracts/toolFamilies.js"; @@ -31,7 +35,38 @@ import { } from "../../../src/contracts/guidanceContracts.js"; const schemaValidation = createMcpSchemaValidation(); -afterAll(() => schemaValidation.clear()); +const ajv = new Ajv2020({ strict: false }); +const validatedToolSchemas = new Set(); +let sharedTempRoot = ""; +let compileCache = ""; +let sqliteProbePath = ""; + +beforeAll(async () => { + sharedTempRoot = (await createTestWorkspace("rea-deferred-runtime-shared-")) + .root; + compileCache = join(sharedTempRoot, "node-compile"); + sqliteProbePath = join(sharedTempRoot, "app.db"); + const database = new DatabaseSync(sqliteProbePath); + try { + database.exec( + "CREATE TABLE catalog_probe (id INTEGER PRIMARY KEY, name TEXT); INSERT INTO catalog_probe VALUES (7, 'pelican');", + ); + } finally { + database.close(); + } +}); + +afterAll(async () => { + schemaValidation.clear(); + if (sharedTempRoot !== "") { + await removeTestWorkspace(sharedTempRoot); + } +}); + +const withCompileCache = (environment: NodeJS.ProcessEnv): NodeJS.ProcessEnv => + compileCache === "" || environment.NODE_V8_COVERAGE !== undefined + ? environment + : { NODE_COMPILE_CACHE: compileCache, ...environment }; const connect = async ( mode: "legacy" | "auto", @@ -46,6 +81,7 @@ const connect = async ( PATH: process.env.PATH ?? "", HOME: process.env.HOME ?? "", REA_LOG_LEVEL: "silent", + ...(compileCache === "" ? {} : { NODE_COMPILE_CACHE: compileCache }), ...environment, }, stderr: "pipe", @@ -83,16 +119,7 @@ it.each([ ] as const)( "discovers and executes cold tools with $mode and $environment", async ({ mode, environment }) => { - const directory = await createTestTempDirectory("rea-cold-sqlite-"); - const path = join(directory, "app.db"); - const database = new DatabaseSync(path); - try { - database.exec( - "CREATE TABLE catalog_probe (id INTEGER PRIMARY KEY, name TEXT); INSERT INTO catalog_probe VALUES (7, 'pelican');", - ); - } finally { - database.close(); - } + const path = sqliteProbePath; const { client, stderr } = await connect(mode, { ...environment, REA_MCP_TOOL_CATALOG_PROFILE: "hot-cold", @@ -101,13 +128,15 @@ it.each([ expect(new Set(initial.tools.map(({ name }) => name))).toEqual( new Set([...HOT_TOOL_NAMES, "discover_tools"]), ); - const ajv = new Ajv2020({ strict: false }); for (const tool of initial.tools) { + const signature = JSON.stringify([tool.inputSchema, tool.outputSchema]); + if (validatedToolSchemas.has(signature)) continue; expect(ajv.validateSchema(tool.inputSchema)).toBe(true); if (tool.outputSchema !== undefined) expect(ajv.validateSchema(tool.outputSchema as AnySchemaObject)).toBe( true, ); + validatedToolSchemas.add(signature); } // Discovery is not recorded; it follows the default compact input profile. expect( @@ -290,12 +319,14 @@ it.each([{ mode: "legacy" }, { mode: "auto" }] as const)( // Compare independently composed canonical SDK registrations as well as // recorded artifacts. Listing every explicit family selects eager startup // with the same inventory, without target analysis or provider acquisition. - const eager = await connect(mode, { - REA_MCP_TOOL_FAMILIES: TOOL_FAMILY_DEFINITIONS.map(({ id }) => id).join( - ",", - ), - }); - expect(before).toEqual(await eager.client.listTools()); + if (mode === "legacy") { + const eager = await connect(mode, { + REA_MCP_TOOL_FAMILIES: TOOL_FAMILY_DEFINITIONS.map(({ id }) => id).join( + ",", + ), + }); + expect(before).toEqual(await eager.client.listTools()); + } const withoutOutput = ({ outputSchema: _output, ...tool @@ -488,7 +519,12 @@ const result = await runDeferred({ console.log(JSON.stringify({ result, output, closeCalls, unregistrations })); `, ], - { cwd: process.cwd(), encoding: "utf8", timeout: 10000 }, + { + cwd: process.cwd(), + env: withCompileCache(process.env), + encoding: "utf8", + timeout: 10000, + }, ); expect(child.error).toBeUndefined(); expect(child.status, child.stderr).toBe(0); @@ -573,7 +609,12 @@ assert.equal(unregistrations, 1); console.log("STARTUP_CLEANUP_RETRIED"); `, ], - { cwd: process.cwd(), encoding: "utf8", timeout: 10000 }, + { + cwd: process.cwd(), + env: withCompileCache(process.env), + encoding: "utf8", + timeout: 10000, + }, ); expect(child.error).toBeUndefined(); expect(child.status, child.stderr).toBe(0); @@ -632,7 +673,12 @@ assert.equal(unregistrations, 1); console.log("MIXED_CLEANUP_RETRIED"); `, ], - { cwd: process.cwd(), encoding: "utf8", timeout: 10000 }, + { + cwd: process.cwd(), + env: withCompileCache(process.env), + encoding: "utf8", + timeout: 10000, + }, ); expect(child.error).toBeUndefined(); expect(child.status, child.stderr).toBe(0); @@ -688,7 +734,12 @@ assert.deepEqual([handleCalls, serverCalls, unregistrations], [1, 1, 1]); console.log("LATE_TRANSPORT_CLOSED"); `, ], - { cwd: process.cwd(), encoding: "utf8", timeout: 10000 }, + { + cwd: process.cwd(), + env: withCompileCache(process.env), + encoding: "utf8", + timeout: 10000, + }, ); expect(child.error).toBeUndefined(); expect(child.status, child.stderr).toBe(0); @@ -743,7 +794,12 @@ assert.deepEqual([handleCalls, serverCalls, unregistrations], [1, 1, 1]); console.log("LATE_PUBLICATION_REFUSED"); `, ], - { cwd: process.cwd(), encoding: "utf8", timeout: 10000 }, + { + cwd: process.cwd(), + env: withCompileCache(process.env), + encoding: "utf8", + timeout: 10000, + }, ); expect(child.error).toBeUndefined(); expect(child.status, child.stderr).toBe(0); @@ -751,30 +807,29 @@ console.log("LATE_PUBLICATION_REFUSED"); expect(child.stderr).toBe(""); }); -it.each([{}, { REA_MCP_OUTPUT_SCHEMA_PROFILE: "off" }])( - "preserves the configured catalog profile %j", - async (environment) => { - const { client } = await connect("legacy", environment); - const before = await client.listTools(); - const catalog = JSON.parse(await readFile("dist/mcp-catalog.json", "utf8")); - expect(before.tools).toHaveLength(catalog.tools.length); - // The deferred server omits the recorded output schemas from listings. - const expected = catalog.tools.map( - ({ outputSchema: _output, ...tool }: Record) => tool, - ); - expect(before.tools).toEqual(expected); - const toolDefinition = before.tools.find( - ({ name }) => name === "list_unknowns", - ); - if (toolDefinition === undefined) throw new Error("Missing list_unknowns"); - const result = await client.callTool( - { name: "list_unknowns", arguments: {} }, - { toolDefinition }, - ); - expect(result.isError).not.toBe(true); - expect(await client.listTools()).toEqual(before); - }, -); +it("preserves an explicit off output-schema profile", async () => { + const { client } = await connect("legacy", { + REA_MCP_OUTPUT_SCHEMA_PROFILE: "off", + }); + const before = await client.listTools(); + const catalog = JSON.parse(await readFile("dist/mcp-catalog.json", "utf8")); + expect(before.tools).toHaveLength(catalog.tools.length); + // The deferred server omits the recorded output schemas from listings. + const expected = catalog.tools.map( + ({ outputSchema: _output, ...tool }: Record) => tool, + ); + expect(before.tools).toEqual(expected); + const toolDefinition = before.tools.find( + ({ name }) => name === "list_unknowns", + ); + if (toolDefinition === undefined) throw new Error("Missing list_unknowns"); + const result = await client.callTool( + { name: "list_unknowns", arguments: {} }, + { toolDefinition }, + ); + expect(result.isError).not.toBe(true); + expect(await client.listTools()).toEqual(before); +}); it("runs the real static artifact workflow after deferred initialization", async () => { const { client } = await connect("legacy"); @@ -895,7 +950,7 @@ syncBuiltinESMExports(); [`--import=${hook}`, join(directory, "scripts/record-mcp-catalog.mjs")], { cwd: directory, - env: { + env: withCompileCache({ ...process.env, HOME: join(directory, "ambient-home"), USERPROFILE: join(directory, "ambient-home"), @@ -904,7 +959,7 @@ syncBuiltinESMExports(); TEMP: temporary, REA_MCP_INPUT_SCHEMA_PROFILE: "compact", REA_MCP_OUTPUT_SCHEMA_PROFILE: "off", - }, + }), encoding: "utf8", timeout: 30000, }, @@ -997,12 +1052,12 @@ registerHooks({ [`--import=${hook}`, join(directory, "scripts/record-mcp-catalog.mjs")], { cwd: directory, - env: { + env: withCompileCache({ ...process.env, TMPDIR: temporary, TMP: temporary, TEMP: temporary, - }, + }), encoding: "utf8", timeout: 10000, }, @@ -1039,7 +1094,13 @@ it("rejects a corrupted build-owned catalog before starting the connection", asy const child = spawnSync( process.execPath, [join(directory, "scripts/rea.mjs"), "mcp"], - { cwd: directory, input: "", encoding: "utf8", timeout: 10000 }, + { + cwd: directory, + env: withCompileCache(process.env), + input: "", + encoding: "utf8", + timeout: 10000, + }, ); expect(child.error).toBeUndefined(); expect(child.status).toBe(1); @@ -1114,7 +1175,12 @@ try { } finally { release(); await close?.(); input.destroy(); output.destroy(); } `, ], - { cwd: process.cwd(), encoding: "utf8", timeout: 15000 }, + { + cwd: process.cwd(), + env: withCompileCache(process.env), + encoding: "utf8", + timeout: 15000, + }, ); expect(child.error).toBeUndefined(); expect(child.status, child.stderr).toBe(0); diff --git a/tests/boundary/mcp/mcpDoctor.test.ts b/tests/boundary/mcp/mcpDoctor.test.ts index f22598a2cb..61e7724632 100644 --- a/tests/boundary/mcp/mcpDoctor.test.ts +++ b/tests/boundary/mcp/mcpDoctor.test.ts @@ -11,11 +11,28 @@ import { mkdir, readFile, writeFile } from "node:fs/promises"; import { join, resolve } from "node:path"; import { promisify } from "node:util"; -import { afterAll, afterEach, describe, expect, it, vi } from "vitest"; +import { + afterAll, + afterEach, + beforeAll, + describe, + expect, + it, + vi, +} from "vitest"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { + createTestWorkspace, + removeTestWorkspace, +} from "../../support/workspace/workspaceFixture.js"; import { createTestBinarySession } from "../../fixtures/binarySession.js"; import { observed } from "../../fixtures/analysisExecution.js"; +import { createCliCommandSession } from "../../support/cli/cliCommandSession.js"; +import { + cliProcessEnvironment, + definedCliEnvironment, +} from "../../support/cli/cliFixture.js"; import { runProductionMcpDoctor, @@ -43,7 +60,39 @@ import { COMPACT_INPUT_SCHEMA_PRESENTATION } from "../../../src/contracts/mcpSch // Compile SDK output validators for the tools a test calls, not the whole catalog. const schemaValidation = createMcpSchemaValidation(); -afterAll(() => schemaValidation.clear()); +const cliSession = createCliCommandSession(); +let compileCacheRoot = ""; +let compileCache = ""; +beforeAll(async () => { + compileCacheRoot = (await createTestWorkspace("rea-mcp-doctor-compile-")) + .root; + compileCache = join(compileCacheRoot, "node-compile"); +}); +afterAll(async () => { + schemaValidation.clear(); + await cliSession.close(); + if (compileCacheRoot !== "") { + await removeTestWorkspace(compileCacheRoot); + } +}); + +const withCompileCache = >( + environment: T, +): T => + compileCache === "" || environment.NODE_V8_COVERAGE !== undefined + ? environment + : ({ NODE_COMPILE_CACHE: compileCache, ...environment } as T); + +const runCachedMcpDoctor = ( + options: Parameters[0], +) => + runProductionMcpDoctor({ + ...options, + environment: + options.environment === undefined + ? undefined + : withCompileCache(options.environment), + }); const expectDoctorDeadline = ( detail: string | undefined, @@ -78,7 +127,7 @@ const captureProductionCli = async ( [resolve("scripts/rea.mjs"), ...arguments_], { cwd: process.cwd(), - env: environment, + env: withCompileCache(environment), stdio: ["ignore", "pipe", "pipe"], }, ); @@ -106,14 +155,20 @@ const assertCliCatalogIdentity = async ( environment: NodeJS.ProcessEnv, identity: ReturnType, ) => { + const sessionEnvironment = definedCliEnvironment( + cliProcessEnvironment( + { ...environment, REA_LOG_LEVEL: "silent" }, + compileCache, + ), + ); for (const command of ["capabilities", "providers"]) { - const { stdout } = await promisify(execFile)( - process.execPath, - [resolve("scripts/rea.mjs"), command, "--json"], - { env: environment, timeout: 15_000, maxBuffer: 1024 * 1024 }, - ); - const cliStatus: unknown = JSON.parse(stdout); - expect(cliStatus, command).toMatchObject({ + const result = await cliSession.run({ + arguments: [command, "--json"], + cwd: process.cwd(), + environment: sessionEnvironment, + }); + expect(result.exitCode, result.stderr).toBe(0); + expect(result.json, command).toMatchObject({ server_identity: { catalog: { counts: identity.counts, digests: identity.digests }, }, @@ -171,7 +226,7 @@ const withoutOutputProfile = ( }; it("recognizes a hot/cold initial catalog and its inventory identity", async () => { - const result = await runProductionMcpDoctor({ + const result = await runCachedMcpDoctor({ command: process.execPath, args: [resolve("scripts/rea.mjs"), "mcp"], cwd: process.cwd(), @@ -263,7 +318,7 @@ describe("production MCP doctor", () => { it.each(catalogProfiles)( "verifies the $name catalog through production stdio and status", async ({ environment, identity, cliParity }) => { - const result = await runProductionMcpDoctor({ + const result = await runCachedMcpDoctor({ command: process.execPath, args: [resolve("scripts/rea.mjs"), "mcp"], cwd: process.cwd(), @@ -315,7 +370,7 @@ describe("production MCP doctor family scopes", () => { REA_MCP_TOOL_FAMILIES: families, REA_MCP_OUTPUT_SCHEMA_PROFILE: output, }; - const result = await runProductionMcpDoctor({ + const result = await runCachedMcpDoctor({ command: process.execPath, args: [resolve("scripts/rea.mjs"), "mcp"], cwd: process.cwd(), @@ -355,7 +410,7 @@ describe("production MCP doctor family scopes", () => { it.each(["browser", "native"])( "explains unobserved identity for a %s scope without session", async (families) => { - const result = await runProductionMcpDoctor({ + const result = await runCachedMcpDoctor({ command: process.execPath, args: [resolve("scripts/rea.mjs"), "mcp"], cwd: process.cwd(), @@ -397,7 +452,7 @@ describe("production MCP doctor family scopes", () => { ); it("fails startup instead of misreporting inventory for an invalid scope", async () => { - const result = await runProductionMcpDoctor({ + const result = await runCachedMcpDoctor({ command: process.execPath, args: [resolve("scripts/rea.mjs"), "mcp"], cwd: process.cwd(), @@ -578,7 +633,7 @@ describe("production MCP doctor probe validation", () => { command: process.execPath, args: [resolve("scripts/rea.mjs"), "mcp"], cwd: process.cwd(), - env: environment, + env: withCompileCache(environment), stderr: "pipe", }); let removedToolName = ""; @@ -619,7 +674,7 @@ describe("production MCP doctor probe validation", () => { } // Replay real production metadata through a synthetic stdio producer. for (const mode of ["valid", "invalid", "inventory"]) { - const result = await runProductionMcpDoctor({ + const result = await runCachedMcpDoctor({ command: process.execPath, args: [ resolve("tests/fixtures/mcpDoctorReply.mjs"), @@ -679,7 +734,7 @@ describe("production MCP doctor probe validation", () => { describe("production MCP doctor child lifecycle", () => { it("captures startup exit diagnostics", async () => { - const result = await runProductionMcpDoctor({ + const result = await runCachedMcpDoctor({ command: process.execPath, args: [resolve("tests/fixtures/mcpDoctorExit.mjs")], cwd: process.cwd(), @@ -758,7 +813,7 @@ describe("production MCP doctor child lifecycle", () => { // Keep filesystem/process I/O real. Advance only the deadline timers, // after the fixture proves it started and published its PID. vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); - const pending = runProductionMcpDoctor({ + const pending = runCachedMcpDoctor({ command: process.execPath, args: [resolve("tests/fixtures/mcpDoctorHang.mjs")], cwd: process.cwd(), @@ -787,7 +842,7 @@ describe("production MCP doctor child ownership", () => { it("does not hand an ambient target variable to the diagnostic child", async () => { const root = await createTestTempDirectory("rea-mcp-doctor-target-free-"); const seenPath = join(root, "env.json"); - const result = await runProductionMcpDoctor({ + const result = await runCachedMcpDoctor({ command: process.execPath, args: [resolve("tests/fixtures/mcpDoctorEnvironment.mjs")], cwd: root, @@ -819,7 +874,7 @@ describe("production MCP doctor child ownership", () => { const root = await createTestTempDirectory("rea-mcp-doctor-descendant-"); const pidPath = join(root, "pids.json"); vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); - const pending = runProductionMcpDoctor({ + const pending = runCachedMcpDoctor({ command: process.execPath, args: [resolve("tests/fixtures/mcpDoctorForkDescendant.mjs")], cwd: process.cwd(), @@ -856,7 +911,7 @@ describe("production MCP doctor request-flow deadline", () => { const root = await createTestTempDirectory("rea-mcp-doctor-session-"); const readyPath = join(root, "ready"); vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); - const pending = runProductionMcpDoctor({ + const pending = runCachedMcpDoctor({ command: process.execPath, args: [resolve("tests/fixtures/mcpStartupProbeChild.mjs")], cwd: process.cwd(), @@ -890,7 +945,7 @@ describe("production MCP doctor request-flow deadline", () => { ])( "does not infer the doctor deadline from remote failure prose: %s", async (message) => { - const result = await runProductionMcpDoctor({ + const result = await runCachedMcpDoctor({ command: process.execPath, args: [resolve("tests/fixtures/mcpStartupProbeChild.mjs")], cwd: process.cwd(), @@ -912,7 +967,7 @@ describe("production MCP doctor request-flow deadline", () => { ); it("refuses an invalid phase budget before starting a child", async () => { - const result = await runProductionMcpDoctor({ + const result = await runCachedMcpDoctor({ command: process.execPath, args: ["definitely-missing-mcp-doctor-child.mjs"], cwd: process.cwd(), @@ -940,7 +995,7 @@ const runNoisyDoctorChild = async (body: string) => { const root = await createTestTempDirectory("rea-mcp-doctor-stderr-"); const scriptPath = join(root, "noisy.mjs"); await writeFile(scriptPath, body); - return runProductionMcpDoctor({ + return runCachedMcpDoctor({ command: process.execPath, args: [scriptPath], cwd: root, diff --git a/tests/support/cli/cliFixture.ts b/tests/support/cli/cliFixture.ts index ad60ac5a9e..f45f983593 100644 --- a/tests/support/cli/cliFixture.ts +++ b/tests/support/cli/cliFixture.ts @@ -22,6 +22,7 @@ import { createCliCommandSession, type CliCommandSession, } from "./cliCommandSession.js"; +import { REGISTRATION_GUARD_NODE_OPTIONS_ENV } from "./registrationEnvironment.js"; import { terminalStdoutInvocation } from "./terminalStdout.js"; /** Caller-controlled values for one compiled REA CLI invocation. */ @@ -314,7 +315,9 @@ const sharedProcessCanServe = ({ input === undefined && timeoutMs === undefined && !isDirectMcpLauncherEntry(arguments_, environment) && - environment?.NODE_OPTIONS === undefined && + (environment?.NODE_OPTIONS === undefined || + environment.NODE_OPTIONS === + environment[REGISTRATION_GUARD_NODE_OPTIONS_ENV]) && !enablesLogging(environment?.REA_LOG_LEVEL) && !logsByDefault(arguments_, environment?.REA_LOG_LEVEL); diff --git a/tests/support/cli/registrationEnvironment.ts b/tests/support/cli/registrationEnvironment.ts index c4118d7e4e..0b0c14878b 100644 --- a/tests/support/cli/registrationEnvironment.ts +++ b/tests/support/cli/registrationEnvironment.ts @@ -2,6 +2,9 @@ import { mkdir, writeFile } from "node:fs/promises"; import { delimiter, join } from "node:path"; import { pathToFileURL } from "node:url"; +export const REGISTRATION_GUARD_NODE_OPTIONS_ENV = + "REA_TEST_REGISTRATION_GUARD_NODE_OPTIONS"; + /** Isolate every registration writer before the official Node entrypoint imports it. */ export const registrationEnvironment = async ( home: string, @@ -43,6 +46,7 @@ for (const key of ['USERPROFILE','XDG_CONFIG_HOME','XDG_DATA_HOME','XDG_CACHE_HO { mode: 0o700 }, ); } + const guardNodeOptions = `--import=${pathToFileURL(guard).href}`; return { ...additional, PATH: additional.PATH ?? [bin, process.env.PATH ?? ""].join(delimiter), @@ -55,7 +59,11 @@ for (const key of ['USERPROFILE','XDG_CONFIG_HOME','XDG_DATA_HOME','XDG_CACHE_HO XDG_STATE_HOME: join(home, "state"), APPDATA: join(home, "appdata"), LOCALAPPDATA: join(home, "localappdata"), + [REGISTRATION_GUARD_NODE_OPTIONS_ENV]: guardNodeOptions, // A file URL encodes spaces; Node reads this one --import option directly. - NODE_OPTIONS: `--import=${pathToFileURL(guard).href}`, + NODE_OPTIONS: + additional.NODE_OPTIONS === undefined + ? guardNodeOptions + : `${additional.NODE_OPTIONS} ${guardNodeOptions}`, }; };