From fea7648f68f4a09cfcb1938def5c4edd2ec0f6a6 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:08:17 -0500 Subject: [PATCH] perf(tests): share CLI session across read-only JSON/view boundaries and mcp subcommands --- tests/boundary/cli/analysisViewCli.test.ts | 37 --------- tests/boundary/cli/jsonInputStatus.test.ts | 95 ++++++---------------- tests/support/cli/cliFixture.ts | 25 +++++- 3 files changed, 45 insertions(+), 112 deletions(-) diff --git a/tests/boundary/cli/analysisViewCli.test.ts b/tests/boundary/cli/analysisViewCli.test.ts index 7af30119ce..f532778fb2 100644 --- a/tests/boundary/cli/analysisViewCli.test.ts +++ b/tests/boundary/cli/analysisViewCli.test.ts @@ -777,7 +777,6 @@ cliTest( JSON.stringify({ path, ...options }), "--json", ], - environment: { HOME: root, XDG_CONFIG_HOME: root, XDG_CACHE_HOME: root }, }); expect(result.exitCode).toBe(0); const expected = inspectEvidenceBundle(bundle, options); @@ -790,7 +789,6 @@ cliTest( JSON.stringify({ path, ...options, detail: "records", limit: 1 }), "--json", ], - environment: { HOME: root, XDG_CONFIG_HOME: root, XDG_CACHE_HOME: root }, }); expect(records.exitCode).toBe(0); expect(records.json).toMatchObject({ @@ -807,7 +805,6 @@ cliTest( JSON.stringify({ path }), "--json", ], - environment: { HOME: root, XDG_CONFIG_HOME: root, XDG_CACHE_HOME: root }, }); expect(complete.exitCode).toBe(0); expect(complete.json).toEqual(bundle); @@ -842,7 +839,6 @@ cliTest( JSON.stringify({ path, ...options }), "--json", ], - environment: { HOME: root, XDG_CONFIG_HOME: root, XDG_CACHE_HOME: root }, }); expect(result.exitCode).toBe(0); const expected = inspectEvidenceBundle(bundle, options); @@ -965,11 +961,6 @@ cliTest( await writeFile(input, JSON.stringify(request)); const result = await cli.run({ arguments: ["inspect-analysis-view", input, "--json"], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, }); expect(result.exitCode).toBe(0); expect(result.json).toMatchObject({ @@ -986,7 +977,6 @@ cliTest( cliTest( "reports empty retained layout collections without invented index bounds", async ({ cli }) => { - const root = await createTestTempDirectory("rea-empty-layout-view-cli-"); const empty = analysisViewLayoutEvidence(binaryLayoutFixture()); for (const collection of ["sections", "symbols"] as const) { const result = await cli.run({ @@ -998,11 +988,6 @@ cliTest( }), "--json", ], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ @@ -1033,11 +1018,6 @@ cliTest( }), "--json", ], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, }); expect(populated.exitCode).toBe(1); expect(populated.json).toMatchObject({ @@ -1079,7 +1059,6 @@ cliTest( ); const result = await cli.run({ arguments: ["inspect-analysis-view", input, "--json"], - environment: { HOME: root, XDG_CONFIG_HOME: root, XDG_CACHE_HOME: root }, }); expect(result.exitCode).toBe(0); expect(result.json).toMatchObject({ @@ -1141,14 +1120,8 @@ cliTest( ); cliTest("rejects malformed JSON before projection", async ({ cli }) => { - const root = await createTestTempDirectory("rea-analysis-view-cli-bad-"); const result = await cli.run({ arguments: ["inspect-analysis-view", "{", "--json"], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ @@ -1164,11 +1137,6 @@ cliTest("rejects a request that omits source", async ({ cli }) => { await writeFile(input, JSON.stringify({ view: { kind: "summary" } })); const result = await cli.run({ arguments: ["inspect-analysis-view", input, "--json"], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ @@ -1199,11 +1167,6 @@ for (const [view, reason] of [ ); const result = await cli.run({ arguments: ["inspect-analysis-view", input, "--json"], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ diff --git a/tests/boundary/cli/jsonInputStatus.test.ts b/tests/boundary/cli/jsonInputStatus.test.ts index 678df87054..e172c03afc 100644 --- a/tests/boundary/cli/jsonInputStatus.test.ts +++ b/tests/boundary/cli/jsonInputStatus.test.ts @@ -6,6 +6,16 @@ import { describe, expect } from "vitest"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; +let isolatedHomeEnvironment: Promise | undefined; +const sharedIsolatedHome = (): Promise => + (isolatedHomeEnvironment ??= createTestTempDirectory( + "rea-cli-json-isolated-home-", + ).then((home) => ({ + HOME: home, + XDG_CONFIG_HOME: home, + XDG_CACHE_HOME: home, + }))); + const JSON_COMMANDS = [ ["inspect-analysis-view", "inspect_analysis_view"], ["trace-application-feature", "trace_application_feature"], @@ -33,15 +43,10 @@ describe("compiled CLI JSON input failure status", () => { cliTest( `${command} rejects malformed inline input before dispatch`, async ({ cli }) => { - const root = await createTestTempDirectory("rea-cli-json-status-"); const input = "{"; const result = await cli.run({ arguments: [command, input, "--json"], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, + environment: await sharedIsolatedHome(), }); expect(result.json).toMatchObject({ @@ -67,11 +72,7 @@ describe("compiled CLI JSON input failure status", () => { const result = await cli.run({ arguments: ["compare-javascript-export-shapes", input, "--json"], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, + environment: await sharedIsolatedHome(), }); expect(result.json).toMatchObject({ @@ -97,18 +98,13 @@ describe("compiled CLI JSON document omissions", () => { ["trace-application-feature", "trace_application_feature"], ] as const) cliTest(`${command} refuses an omitted view field`, async ({ cli }) => { - const root = await createTestTempDirectory("rea-cli-json-omitted-"); const result = await cli.run({ arguments: [ command, JSON.stringify({ view: { kind: "summary" } }), "--json", ], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, + environment: await sharedIsolatedHome(), }); expect(result.json).toMatchObject({ code: "invalid_request", @@ -157,7 +153,6 @@ describe("compiled CLI JSON document omissions", () => { cliTest( `${command} refuses ${field} inside the document`, async ({ cli }) => { - const root = await createTestTempDirectory("rea-cli-json-flagged-"); const result = await cli.run({ arguments: [ command, @@ -166,11 +161,7 @@ describe("compiled CLI JSON document omissions", () => { }), "--json", ], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, + environment: await sharedIsolatedHome(), }); expect(result.json).toMatchObject({ code: "invalid_request", @@ -202,11 +193,7 @@ describe("compiled CLI JSON document admission order", () => { join(root, "missing.bundle.json"), "--json", ], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, + environment: await sharedIsolatedHome(), }); expect(result.json).toMatchObject({ code: "invalid_request", @@ -230,7 +217,6 @@ describe("compiled CLI JSON document caller paths", () => { cliTest( "compare-source-to-bundle refuses a file URL reference path", async ({ cli }) => { - const root = await createTestTempDirectory("rea-cli-json-url-path-"); const result = await cli.run({ arguments: [ "compare-source-to-bundle", @@ -240,11 +226,7 @@ describe("compiled CLI JSON document caller paths", () => { }), "--json", ], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, + environment: await sharedIsolatedHome(), }); expect(result.json).toMatchObject({ code: "invalid_request", @@ -270,11 +252,7 @@ describe("compiled CLI session-target JSON documents", () => { JSON.stringify({ breakpoints: "main" }), "--json", ], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, + environment: await sharedIsolatedHome(), }); expect(result.json).toMatchObject({ error: "Analysis failed", @@ -305,11 +283,7 @@ describe("compiled CLI JSON input diagnostic preservation", () => { const input = join(root, "missing.json"); const result = await cli.run({ arguments: ["compare-javascript-export-shapes", input, ...flags], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, + environment: await sharedIsolatedHome(), }); for (const diagnostic of [ "invalid_request", @@ -326,14 +300,9 @@ describe("compiled CLI JSON input diagnostic preservation", () => { cliTest( "rejects an empty file path without losing its diagnostic", async ({ cli }) => { - const root = await createTestTempDirectory("rea-cli-json-empty-"); const result = await cli.run({ arguments: ["compare-javascript-export-shapes", "", "--json"], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, + environment: await sharedIsolatedHome(), }); expect(JSON.parse(result.stdout.trim())).toMatchObject({ code: "invalid_request", @@ -347,15 +316,10 @@ describe("compiled CLI JSON input diagnostic preservation", () => { cliTest( "classifies an overlong malformed inline value before dispatch", async ({ cli }) => { - const root = await createTestTempDirectory("rea-cli-json-overlong-"); const input = `[${"x".repeat(3_000)}`; const result = await cli.run({ arguments: ["compare-javascript-export-shapes", input, "--json"], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, + environment: await sharedIsolatedHome(), }); expect(result.json).toMatchObject({ @@ -376,15 +340,10 @@ describe("compiled CLI JSON input diagnostic preservation", () => { cliTest( "preserves explicit overlong JSON path diagnostics", async ({ cli }) => { - const root = await createTestTempDirectory("rea-cli-json-overlong-path-"); const input = `[${"x".repeat(3_000)}.json`; const result = await cli.run({ arguments: ["compare-javascript-export-shapes", input, "--json"], - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, + environment: await sharedIsolatedHome(), }); expect(result.json).toMatchObject({ @@ -407,11 +366,7 @@ describe("compiled CLI JSON path ambiguity", () => { const result = await cli.run({ arguments: ["capture-browser-scenario", input, "--json"], cwd: root, - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, + environment: await sharedIsolatedHome(), }); expect(result.json).toMatchObject({ @@ -444,11 +399,7 @@ describe("compiled CLI JSON path ambiguity", () => { const result = await cli.run({ arguments: ["capture-browser-scenario", input, "--json"], cwd: root, - environment: { - HOME: root, - XDG_CONFIG_HOME: root, - XDG_CACHE_HOME: root, - }, + environment: await sharedIsolatedHome(), }); expect(result.json).toMatchObject({ diff --git a/tests/support/cli/cliFixture.ts b/tests/support/cli/cliFixture.ts index d79a8d95cc..ad60ac5a9e 100644 --- a/tests/support/cli/cliFixture.ts +++ b/tests/support/cli/cliFixture.ts @@ -1,6 +1,7 @@ import { resolve } from "node:path"; import { tmpdir } from "node:os"; import { + cliCompletionShell, productionMcpDoctorArguments, parseCliGlobalArguments, } from "../../../src/cliBuiltinArguments.js"; @@ -272,6 +273,26 @@ export const cliCompileCacheEnvironment = ( ...environment, }); +/** + * Mirror `scripts/rea.mjs` entry routing for invocations handled outside + * `runOneShotCli` (`rea mcp` / `rea --mcp`, `rea mcp ... --http`, and + * production `rea mcp doctor ...`). Subcommands such as `rea mcp add` and + * `rea mcp --help` still dispatch through `runOneShotCli`. + */ +const isDirectMcpLauncherEntry = ( + arguments_: readonly string[], + environment: NodeJS.ProcessEnv | undefined, +): boolean => { + if (cliCompletionShell(environment ?? {}) !== undefined) return false; + if (productionMcpDoctorArguments([...arguments_]) !== undefined) return true; + if (arguments_[0] !== "mcp" && arguments_[0] !== "--mcp") return false; + return ( + arguments_.length === 1 || + ((arguments_[1]?.startsWith("-") ?? false) && + arguments_.slice(1).includes("--http")) + ); +}; + /** * Whether the file's shared process reports this command exactly as a fresh * `rea` process would. Stdin and deadlines belong to a process; the launcher @@ -292,9 +313,7 @@ const sharedProcessCanServe = ({ terminal !== true && input === undefined && timeoutMs === undefined && - arguments_[0] !== "mcp" && - arguments_[0] !== "--mcp" && - productionMcpDoctorArguments([...arguments_]) === undefined && + !isDirectMcpLauncherEntry(arguments_, environment) && environment?.NODE_OPTIONS === undefined && !enablesLogging(environment?.REA_LOG_LEVEL) && !logsByDefault(arguments_, environment?.REA_LOG_LEVEL);