Files
reverse-engineering-anything/tests/support/cli/javascriptApplication.ts
T

146 lines
4.6 KiB
TypeScript

import { createHash } from "node:crypto";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { expect } from "vitest";
import { z } from "zod";
import { toolContract } from "../../../src/contracts/toolContracts.js";
import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js";
import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js";
import { INLINE_EVIDENCE_MCP_ENVIRONMENT } from "../../../scripts/lib/mcp-verification-environment.mjs";
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(INLINE_EVIDENCE_MCP_ENVIRONMENT)).call;
};
/**
* Analyze a directory. A supplied MCP caller reuses its test-owned connection.
* CLI uses the driver's file-scoped process when present, and a fresh process
* otherwise.
*/
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 run = cli.run.bind(cli);
const response = await 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;
};
/** Compare the CLI and default stdio MCP Evidence before querying the same server. */
export const analyzeJavaScriptDirectoryWithMcpEvidence = async (
cli: TestCli,
root: string,
) => {
const output = toolContract("analyze_javascript_application").outputSchema;
const cliResponse = await cli.run({
arguments: [
"analyze-javascript-application",
root,
"--artifact-format",
"directory",
"--json",
],
});
expect(cliResponse.exitCode).toBe(0);
const cliEvidence = output.parse(cliResponse.json);
const { client } = await connectLocalToolsMcp();
const response = await client.callTool({
name: "analyze_javascript_application",
arguments: { input_path: root, format: "directory" },
});
expect(response.isError).not.toBe(true);
const evidence = output.parse(response.structuredContent);
expect(evidence.evidence_id).toBe(cliEvidence.evidence_id);
const text = response.content.find((item) => item.type === "text");
if (text?.type !== "text") throw new Error("Missing MCP text result");
expect(output.parse(JSON.parse(text.text)).evidence_id).toBe(
evidence.evidence_id,
);
return { client, evidence };
};
export const sourceEvidence = (
sourcePath: string,
source: string,
limitation?: string,
): unknown =>
expect.objectContaining({
artifact: expect.objectContaining({
available: true,
sha256: createHash("sha256").update(source).digest("hex"),
}),
location: expect.objectContaining({
available: true,
value: expect.objectContaining({
kind: "source-range",
source: sourcePath,
}),
}),
...(limitation === undefined
? {}
: {
limitations: expect.arrayContaining([
expect.stringContaining(limitation),
]),
}),
});