Files
reverse-engineering-anything/tests/fixtures/javascriptReturnShapes.ts
T
morluto 88fd8a9df0 test: drop detail requests the budget seed made redundant (#14615)
PR #14252 added explicit complete-detail requests across tests before the
vitest REA_AGENT_RESULT_BUDGET_BYTES seed and the complete-Evidence harness
existed. Restore the pre-#14252 form where the seed or the harness already
delivers complete Evidence.

Keep the requests that are load-bearing: delivery, summary and budget tests,
tools whose omitted detail is summary, and callers that neither the seed nor
the harness reaches (unwrapped SDK clients on servers with an explicit
environment, and in-process CLIs). Those were found by running the touched
files with the shipped budget lowered to its 4 KiB minimum.
2026-11-25 21:08:12 +00:00

120 lines
4.5 KiB
TypeScript

import { withVerifierCleanup } from "../../scripts/lib/verifier-cleanup.mjs";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { writeFile } from "node:fs/promises";
import { join, resolve } from "node:path";
import { expect } from "vitest";
import { toolContract } from "../../src/contracts/toolContracts.js";
import { javascriptApplicationAnalysisResultSchema } from "../../src/domain/javascript/javascriptApplicationAnalysis.js";
import { projectedExportReturnShapesSchema } from "../../src/domain/javascript/javascriptExportShapeComparisonSchemas.js";
import { createTestTempDirectory } from "./temporaryDirectory.js";
import type { TestCli } from "../support/cli/cliFixture.js";
import { createMcpSchemaValidation } from "./mcpSchemaValidation.js";
const exportedReturnFields = (value: unknown) => {
const analysis = javascriptApplicationAnalysisResultSchema.parse(value);
const projections = analysis.graph.nodes
.flatMap(({ observations }) => observations)
.filter(
({ properties }) => properties.semantic_role === "export-return-shapes",
)
.map(({ properties }) =>
projectedExportReturnShapesSchema.parse(properties),
);
return (name: string) => {
const projection = projections.find(
({ exported_name }) => exported_name === name,
);
if (projection === undefined) throw new Error(`Missing export ${name}`);
expect(projection.return_shape_coverage).toMatchObject({
status: "complete",
projection_complete: true,
});
return projection.static_return_shapes.flatMap(({ fields }) => fields);
};
};
/** Select complete return fields from a real JavaScript application result. */
export type JavaScriptReturnFields = ReturnType<typeof exportedReturnFields>;
const analyzeReturnShapesThroughCli = async (
cli: TestCli,
source: string,
check: (fields: JavaScriptReturnFields) => void,
fileName: string,
) => {
const root = await createTestTempDirectory("rea-return-shapes-");
await writeFile(join(root, fileName), source);
const cliResponse = await cli.run({
arguments: [
"analyze-javascript-application",
root,
"--artifact-format",
"directory",
"--json",
],
});
expect(cliResponse.exitCode).toBe(0);
const contract = toolContract("analyze_javascript_application");
const cliEvidence = contract.outputSchema.parse(cliResponse.json);
check(exportedReturnFields(cliEvidence.normalized_result));
return { root, contract, cliEvidence };
};
/** Exercise return assertions through the compiled CLI and its real worker. */
export const verifyJavaScriptReturnShapesThroughCli = async (
cli: TestCli,
source: string,
check: (fields: JavaScriptReturnFields) => void,
): Promise<void> => {
await analyzeReturnShapesThroughCli(cli, source, check, "app.js");
};
/** Exercise identical return assertions through the compiled CLI and stdio MCP. */
export const verifyJavaScriptReturnShapes = async (
cli: TestCli,
source: string,
check: (fields: JavaScriptReturnFields) => void,
fileName = "app.js",
): Promise<void> => {
const { root, contract, cliEvidence } = await analyzeReturnShapesThroughCli(
cli,
source,
check,
fileName,
);
const validation = createMcpSchemaValidation();
const client = new Client(
{ name: "return-shapes-e2e", version: "1" },
{ jsonSchemaValidator: validation },
);
const transport = new StdioClientTransport({
command: process.execPath,
args: [resolve("scripts/rea.mjs"), "mcp"],
cwd: process.cwd(),
env: { PATH: process.env.PATH ?? "" },
stderr: "pipe",
});
transport.stderr?.on("data", () => undefined);
await withVerifierCleanup(async () => {
await client.connect(transport);
const response = await client.callTool({
name: "analyze_javascript_application",
arguments: { detail: "complete", input_path: root, format: "directory" },
});
expect(response.isError).not.toBe(true);
const evidence = contract.outputSchema.parse(response.structuredContent);
expect(evidence.evidence_id).toBe(cliEvidence.evidence_id);
check(exportedReturnFields(evidence.normalized_result));
const text = response.content.find((item) => item.type === "text");
if (text?.type !== "text") throw new Error("Missing MCP text result");
check(
exportedReturnFields(
contract.outputSchema.parse(JSON.parse(text.text)).normalized_result,
),
);
await client.ping();
}, [() => client.close(), () => transport.close(), () => validation.clear()]);
};