Files
rea/tests/support/cli/javascriptApplication.ts
T
40428da1dd perf(test): reuse one stdio MCP server per JavaScript application file (#5658)
* perf(test): reuse one stdio MCP server per JavaScript application file

Repeated analyzeJavaScriptDirectory MCP cases started a full server for
every case. Share one file-scoped server and compile cache so later cases
skip that startup while CLI cases still launch a fresh process.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(test): close in-flight shared MCP startup with the file

File teardown waits for a stdio session that is still starting, skips a
server once close has begun, and still closes one registered during that
wait. The later-case reuse test can run without the earlier case.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(test): keep a failed MCP handshake when cleanup fails

The shared session reports the startup error and carries the cleanup failure
as its cause. A cleanup that failed stays owned and is retried on close
without running a cleanup that already succeeded.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(test): fail shared MCP cleanup when the rejection has no reason

A cleanup rejection of undefined still fails the drain and stays owned.
Close caches completion only after every retained cleanup succeeds, so a
later close can release it.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(test): keep one MCP server while failed cleanup is retained

A later call retries retained cleanup before opening another server. The
new server starts only after that cleanup succeeds.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-10-21 03:46:14 +08:00

79 lines
2.5 KiB
TypeScript

import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { expect } from "vitest";
import { z } from "zod";
import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js";
import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js";
import type { SharedMcpCall, TestCli } from "./cliFixture.js";
const execute = promisify(execFile);
export const runNodeJsonControl = async (
root: string,
filename: string,
): Promise<unknown> => {
const { stdout } = await execute(process.execPath, [filename], {
cwd: root,
timeout: 5_000,
});
return JSON.parse(stdout) as unknown;
};
/**
* Use an explicit caller when the test owns the server. Otherwise reuse the
* file-scoped session from `cliTest`, and start a fresh server only for
* drivers created outside that fixture.
*/
export const resolveJavaScriptApplicationMcpCall = async (
cli: TestCli,
mcpCall: SharedMcpCall | undefined,
): Promise<SharedMcpCall> => {
if (mcpCall !== undefined) return mcpCall;
if (cli.sharedMcpCall !== undefined) return cli.sharedMcpCall();
return (await connectLocalToolsMcp()).call;
};
/** Analyze a directory; a supplied MCP caller reuses its test-owned connection. */
export const analyzeJavaScriptDirectory = async (
surface: "CLI" | "stdio MCP",
cli: TestCli,
root: string,
{
sourcePaths = [],
mcpCall,
}: {
readonly sourcePaths?: readonly string[];
readonly mcpCall?: SharedMcpCall | undefined;
} = {},
) => {
let document: unknown;
if (surface === "CLI") {
const response = await cli.run({
arguments: [
"analyze-javascript-application",
root,
...sourcePaths.flatMap((path) => ["--source-path", path]),
"--format",
"json",
],
environment: { REA_LOG_LEVEL: "silent" },
});
expect(response.exitCode, response.stderr).toBe(0);
document = response.json;
} else {
const call = await resolveJavaScriptApplicationMcpCall(cli, mcpCall);
const response = await call("analyze_javascript_application", {
input_path: root,
format: "directory",
...(sourcePaths.length === 0 ? {} : { source_paths: sourcePaths }),
});
expect(response.isError, JSON.stringify(response)).not.toBe(true);
document = response.structuredContent;
}
return z
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
.parse(document).normalized_result;
};