perf(tests): reuse stateless workflows and immutable fixtures
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { copyFile, mkdir, rm, writeFile } from "node:fs/promises";
|
||||
import { copyFile, mkdir, writeFile } from "node:fs/promises";
|
||||
import { createRequire } from "node:module";
|
||||
import { join, relative, resolve } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { Client } from "@modelcontextprotocol/client";
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { afterAll, describe, expect, it } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { writeElectronApiAliasApplication } from "../../fixtures/electronApiAliasApplication.js";
|
||||
@@ -28,7 +28,33 @@ import { ELECTRON_IDENTITY_LIMITATION } from "../../../src/domain/javascript/jav
|
||||
|
||||
const execute = promisify(execFile);
|
||||
const requireFixture = createRequire(import.meta.url);
|
||||
const temporary: string[] = [];
|
||||
// Analysis requests are stateless; retain one compiled server and its SDK
|
||||
// validators while every scenario still owns a separate application directory.
|
||||
const mcpTransport = new StdioClientTransport({
|
||||
command: process.execPath,
|
||||
args: [resolve("scripts/rea.mjs"), "mcp"],
|
||||
cwd: process.cwd(),
|
||||
env: { PATH: process.env.PATH ?? "", REA_LOG_LEVEL: "silent" },
|
||||
stderr: "pipe",
|
||||
});
|
||||
mcpTransport.stderr?.on("data", () => undefined);
|
||||
const mcpClient = new Client({
|
||||
name: "application-workflow-parity",
|
||||
version: "1",
|
||||
});
|
||||
let mcpConnection: Promise<void> | undefined;
|
||||
const connectedMcpClient = async (): Promise<Client> => {
|
||||
await (mcpConnection ??= mcpClient.connect(mcpTransport));
|
||||
return mcpClient;
|
||||
};
|
||||
|
||||
afterAll(async () => {
|
||||
try {
|
||||
await mcpClient.close();
|
||||
} finally {
|
||||
await mcpTransport.close();
|
||||
}
|
||||
});
|
||||
const WEBPACK_FACTORY_BODY = `
|
||||
const key = "dynamic";
|
||||
const __webpack_require__ = {
|
||||
@@ -57,14 +83,6 @@ const WEBPACK_FACTORY_BODY = `
|
||||
exports = function ignored() { return 8; };
|
||||
`;
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(
|
||||
temporary
|
||||
.splice(0)
|
||||
.map(async (path) => rm(path, { recursive: true, force: true })),
|
||||
);
|
||||
});
|
||||
|
||||
const writeSelfImportFixture = async (root: string): Promise<void> => {
|
||||
await Promise.all([
|
||||
writeFile(
|
||||
@@ -162,51 +180,42 @@ const expectSelfImportEvidence = (
|
||||
const analyzeThroughStdioMcp = async (
|
||||
inputPath: string,
|
||||
): Promise<JavaScriptApplicationAnalysisResult> => {
|
||||
const transport = new StdioClientTransport({
|
||||
command: process.execPath,
|
||||
args: [resolve("scripts/rea.mjs"), "mcp"],
|
||||
cwd: process.cwd(),
|
||||
env: {
|
||||
PATH: process.env.PATH ?? "",
|
||||
REA_LOG_LEVEL: "silent",
|
||||
},
|
||||
stderr: "pipe",
|
||||
const client = await connectedMcpClient();
|
||||
const response = await client.callTool({
|
||||
name: "analyze_javascript_application",
|
||||
arguments: { input_path: inputPath },
|
||||
});
|
||||
const client = new Client({
|
||||
name: "javascript-self-import-parity",
|
||||
version: "1",
|
||||
expect(response.isError).not.toBe(true);
|
||||
expect(response.content).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ type: "text" })]),
|
||||
);
|
||||
const result = z
|
||||
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
|
||||
.parse(response.structuredContent).normalized_result;
|
||||
expect(result).toMatchObject({
|
||||
input_path: inputPath,
|
||||
format: "directory",
|
||||
});
|
||||
try {
|
||||
await client.connect(transport);
|
||||
const response = await client.callTool({
|
||||
name: "analyze_javascript_application",
|
||||
arguments: { input_path: inputPath },
|
||||
});
|
||||
expect(response.isError).not.toBe(true);
|
||||
expect(response.content).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ type: "text" })]),
|
||||
);
|
||||
const result = z
|
||||
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
|
||||
.parse(response.structuredContent).normalized_result;
|
||||
expect(result).toMatchObject({
|
||||
input_path: inputPath,
|
||||
format: "directory",
|
||||
});
|
||||
return result;
|
||||
} finally {
|
||||
try {
|
||||
await client.close();
|
||||
} finally {
|
||||
await transport.close();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
describe("Electron alias identity CLI and MCP", () => {
|
||||
const expectCliAnalysisParity = async (
|
||||
root: string,
|
||||
mcp: JavaScriptApplicationAnalysisResult,
|
||||
): Promise<void> => {
|
||||
const cli = z
|
||||
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
|
||||
.parse(
|
||||
await runCli(["analyze-javascript-application", root, "--json"]),
|
||||
).normalized_result;
|
||||
expect(cli).toEqual(mcp);
|
||||
};
|
||||
|
||||
describe("Electron alias identity through stdio MCP", () => {
|
||||
it.each([
|
||||
{
|
||||
name: "CommonJS binding chains",
|
||||
cliParity: true,
|
||||
esm: false,
|
||||
main: 'const { BrowserWindow, ipcMain } = require("electron"); const First = BrowserWindow; const Win = First; const ipc = ipcMain;',
|
||||
preload:
|
||||
@@ -235,16 +244,16 @@ describe("Electron alias identity CLI and MCP", () => {
|
||||
},
|
||||
{
|
||||
name: "ESM binding aliases",
|
||||
cliParity: true,
|
||||
esm: true,
|
||||
main: 'import { BrowserWindow, ipcMain } from "electron"; const Win = BrowserWindow; const ipc = ipcMain;',
|
||||
preload:
|
||||
'import { contextBridge, ipcRenderer } from "electron"; const bridge = contextBridge; const ipc = ipcRenderer;',
|
||||
},
|
||||
])(
|
||||
"recovers Electron boundaries through $name in CLI and MCP (#1656)",
|
||||
async ({ main, preload, esm }) => {
|
||||
"recovers Electron boundaries through $name (#1656)",
|
||||
async ({ cliParity, main, preload, esm }) => {
|
||||
const root = await createTestTempDirectory("rea-electron-alias-cli-");
|
||||
temporary.push(root);
|
||||
await Promise.all([
|
||||
writeFile(
|
||||
join(root, "package.json"),
|
||||
@@ -264,15 +273,11 @@ describe("Electron alias identity CLI and MCP", () => {
|
||||
`${preload}\nbridge.exposeInMainWorld("api", { ping: () => ipc.invoke("ch:ping") });\nfunction shadow(bridge, ipc) { bridge.exposeInMainWorld("decoy", {}); ipc.invoke("ch:decoy"); }\n`,
|
||||
),
|
||||
]);
|
||||
const cli = z
|
||||
.object({
|
||||
normalized_result: javascriptApplicationAnalysisResultSchema,
|
||||
})
|
||||
.parse(
|
||||
await runCli(["analyze-javascript-application", root, "--json"]),
|
||||
).normalized_result;
|
||||
const mcp = await analyzeThroughStdioMcp(root);
|
||||
expect(cli.summary).toMatchObject({
|
||||
const result = await analyzeThroughStdioMcp(root);
|
||||
// Keep CLI parity for both module formats; the other rows vary only AST
|
||||
// semantics already exercised by the same public analysis workflow.
|
||||
if (cliParity) await expectCliAnalysisParity(root, result);
|
||||
expect(result.summary).toMatchObject({
|
||||
browser_windows: 1,
|
||||
explicit_web_preferences: 1,
|
||||
preload_entrypoints: 1,
|
||||
@@ -286,67 +291,65 @@ describe("Electron alias identity CLI and MCP", () => {
|
||||
paired_renderer_transmissions: 1,
|
||||
},
|
||||
});
|
||||
expect(mcp.summary).toEqual(cli.summary);
|
||||
for (const result of [cli, mcp]) {
|
||||
expect(result.limitations).toContain(ELECTRON_IDENTITY_LIMITATION);
|
||||
expect(result.graph.nodes).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
kind: "ipc-channel",
|
||||
observations: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({
|
||||
channel: "ch:ping",
|
||||
operation: "handle",
|
||||
}),
|
||||
evidence: expect.objectContaining({
|
||||
authority: "ast-static-analysis",
|
||||
location: expect.objectContaining({
|
||||
available: true,
|
||||
value: expect.objectContaining({
|
||||
kind: "source-range",
|
||||
source: "main.js",
|
||||
}),
|
||||
expect(result.limitations).toContain(ELECTRON_IDENTITY_LIMITATION);
|
||||
expect(result.graph.nodes).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
kind: "ipc-channel",
|
||||
observations: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({
|
||||
channel: "ch:ping",
|
||||
operation: "handle",
|
||||
}),
|
||||
evidence: expect.objectContaining({
|
||||
authority: "ast-static-analysis",
|
||||
location: expect.objectContaining({
|
||||
available: true,
|
||||
value: expect.objectContaining({
|
||||
kind: "source-range",
|
||||
source: "main.js",
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({
|
||||
channel: "ch:ping",
|
||||
operation: "invoke",
|
||||
}),
|
||||
evidence: expect.objectContaining({
|
||||
authority: "ast-static-analysis",
|
||||
location: expect.objectContaining({
|
||||
available: true,
|
||||
value: expect.objectContaining({
|
||||
kind: "source-range",
|
||||
source: "preload.js",
|
||||
}),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({
|
||||
channel: "ch:ping",
|
||||
operation: "invoke",
|
||||
}),
|
||||
evidence: expect.objectContaining({
|
||||
authority: "ast-static-analysis",
|
||||
location: expect.objectContaining({
|
||||
available: true,
|
||||
value: expect.objectContaining({
|
||||
kind: "source-range",
|
||||
source: "preload.js",
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
]),
|
||||
);
|
||||
expect(result.graph.edges).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ relation: "handles" }),
|
||||
expect.objectContaining({ relation: "invokes" }),
|
||||
expect.objectContaining({ relation: "loads" }),
|
||||
expect.objectContaining({ relation: "exposes" }),
|
||||
]),
|
||||
);
|
||||
}
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
]),
|
||||
);
|
||||
expect(result.graph.edges).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ relation: "handles" }),
|
||||
expect.objectContaining({ relation: "invokes" }),
|
||||
expect.objectContaining({ relation: "loads" }),
|
||||
expect.objectContaining({ relation: "exposes" }),
|
||||
]),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("Electron unavailable identity CLI and MCP", () => {
|
||||
describe("Electron unavailable identity through stdio MCP", () => {
|
||||
it.each([
|
||||
{
|
||||
name: "reassigned aliases",
|
||||
cliParity: true,
|
||||
main: 'const { BrowserWindow, ipcMain } = require("electron"); let Win = BrowserWindow; let ipc = ipcMain; Win = class {}; ipc = { handle() {} };',
|
||||
preload:
|
||||
'const { contextBridge, ipcRenderer } = require("electron"); let bridge = contextBridge; let ipc = ipcRenderer; bridge = { exposeInMainWorld() {} }; ipc = { invoke() {} };',
|
||||
@@ -406,10 +409,9 @@ describe("Electron unavailable identity CLI and MCP", () => {
|
||||
'const electron = require("electron"); const bridge = electron["fake.contextBridge"]; const ipc = electron["fake.ipcRenderer"];',
|
||||
},
|
||||
])(
|
||||
"keeps Electron identity unresolved for $name in CLI and MCP (#1656)",
|
||||
async ({ main, preload }) => {
|
||||
"keeps Electron identity unresolved for $name (#1656)",
|
||||
async ({ cliParity, main, preload }) => {
|
||||
const root = await createTestTempDirectory("rea-electron-alias-unknown-");
|
||||
temporary.push(root);
|
||||
await Promise.all([
|
||||
writeFile(
|
||||
join(root, "package.json"),
|
||||
@@ -428,25 +430,17 @@ describe("Electron unavailable identity CLI and MCP", () => {
|
||||
`${preload}\nbridge.exposeInMainWorld("api", { ping: () => ipc.invoke("ch:ping") });\n`,
|
||||
),
|
||||
]);
|
||||
const cli = z
|
||||
.object({
|
||||
normalized_result: javascriptApplicationAnalysisResultSchema,
|
||||
})
|
||||
.parse(
|
||||
await runCli(["analyze-javascript-application", root, "--json"]),
|
||||
).normalized_result;
|
||||
const mcp = await analyzeThroughStdioMcp(root);
|
||||
for (const result of [cli, mcp]) {
|
||||
expect(result.summary).toMatchObject({
|
||||
browser_windows: 0,
|
||||
context_bridge_apis: 0,
|
||||
ipc: { operations: 0 },
|
||||
});
|
||||
expect(
|
||||
result.graph.nodes.some(({ kind }) => kind === "ipc-channel"),
|
||||
).toBe(false);
|
||||
expect(result.limitations).toContain(ELECTRON_IDENTITY_LIMITATION);
|
||||
}
|
||||
const result = await analyzeThroughStdioMcp(root);
|
||||
if (cliParity) await expectCliAnalysisParity(root, result);
|
||||
expect(result.summary).toMatchObject({
|
||||
browser_windows: 0,
|
||||
context_bridge_apis: 0,
|
||||
ipc: { operations: 0 },
|
||||
});
|
||||
expect(
|
||||
result.graph.nodes.some(({ kind }) => kind === "ipc-channel"),
|
||||
).toBe(false);
|
||||
expect(result.limitations).toContain(ELECTRON_IDENTITY_LIMITATION);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -454,7 +448,6 @@ describe("Electron unavailable identity CLI and MCP", () => {
|
||||
describe("JavaScript application path CLI", () => {
|
||||
it("accepts a relative local application path and preserves canonical Evidence identity", async () => {
|
||||
const root = await createTestTempDirectory("rea-relative-application-cli-");
|
||||
temporary.push(root);
|
||||
await writeFile(join(root, "app.js"), "export const value = 1;\n");
|
||||
const absolute = await analyzeJavaScriptApplication({ input_path: root });
|
||||
if (!absolute.ok) throw absolute.error;
|
||||
@@ -482,7 +475,6 @@ describe("JavaScript application path CLI", () => {
|
||||
const root = await createTestTempDirectory(
|
||||
"rea-self-import-application-cli-",
|
||||
);
|
||||
temporary.push(root);
|
||||
await writeSelfImportFixture(root);
|
||||
|
||||
const analyzed = await runCli([
|
||||
@@ -504,7 +496,6 @@ describe("JavaScript application path CLI", () => {
|
||||
|
||||
it("preserves canonical Electron API aliases through CLI and stdio MCP", async () => {
|
||||
const root = await createTestTempDirectory("rea-electron-api-alias-");
|
||||
temporary.push(root);
|
||||
await writeElectronApiAliasApplication(root);
|
||||
|
||||
const cliResult = z
|
||||
@@ -588,7 +579,6 @@ describe("application workflow CLI parity", () => {
|
||||
});
|
||||
|
||||
const root = await createTestTempDirectory("rea-application-cli-");
|
||||
temporary.push(root);
|
||||
const comparisonPath = join(root, "comparison.json");
|
||||
await writeFile(
|
||||
comparisonPath,
|
||||
@@ -654,7 +644,6 @@ describe("application workflow CLI parity", () => {
|
||||
|
||||
it("traces the same authenticated semantic graph through the CLI", async () => {
|
||||
const root = await createTestTempDirectory("rea-semantic-cli-");
|
||||
temporary.push(root);
|
||||
await writeFile(
|
||||
join(root, "app.js"),
|
||||
"function add(value) { return value + 1; } add(2);",
|
||||
@@ -777,7 +766,6 @@ describe("empty property key application CLI", () => {
|
||||
describe("Webpack factory CommonJS export keys", () => {
|
||||
it("keeps exact static keys and omits computed names through CLI and MCP", async () => {
|
||||
const root = await createTestTempDirectory("rea-webpack-export-keys-");
|
||||
temporary.push(root);
|
||||
const bundle = `globalThis.webpackChunkStaticExports.push([["main"], {
|
||||
1: function(module, exports, __webpack_require__) {${WEBPACK_FACTORY_BODY}},
|
||||
2: function(module) { module.exports = function realDefault() {}; },
|
||||
@@ -798,7 +786,6 @@ describe("Webpack factory CommonJS export keys", () => {
|
||||
const oracleRoot = await createTestTempDirectory(
|
||||
"rea-webpack-export-oracle-",
|
||||
);
|
||||
temporary.push(oracleRoot);
|
||||
const oraclePath = join(oracleRoot, "oracle.cjs");
|
||||
await writeFile(oraclePath, WEBPACK_FACTORY_BODY);
|
||||
const actual = requireFixture(oraclePath) as Record<string, unknown>;
|
||||
@@ -864,32 +851,16 @@ describe("Webpack factory CommonJS export keys", () => {
|
||||
"--json",
|
||||
]),
|
||||
);
|
||||
const transport = new StdioClientTransport({
|
||||
command: process.execPath,
|
||||
args: [resolve("scripts/rea.mjs"), "mcp"],
|
||||
cwd: process.cwd(),
|
||||
env: { PATH: process.env.PATH ?? "", REA_LOG_LEVEL: "silent" },
|
||||
stderr: "pipe",
|
||||
const client = await connectedMcpClient();
|
||||
const response = await client.callTool({
|
||||
name: "analyze_javascript_application",
|
||||
arguments: { input_path: root, format: "directory" },
|
||||
});
|
||||
transport.stderr?.on("data", () => undefined);
|
||||
const client = new Client({ name: "webpack-export-keys", version: "1" });
|
||||
let mcpEvidence: z.infer<typeof applicationEvidenceSchema>;
|
||||
try {
|
||||
await client.connect(transport);
|
||||
const response = await client.callTool({
|
||||
name: "analyze_javascript_application",
|
||||
arguments: { input_path: root, format: "directory" },
|
||||
});
|
||||
expect(response.isError).not.toBe(true);
|
||||
mcpEvidence = applicationEvidenceSchema.parse(response.structuredContent);
|
||||
expect(await client.ping()).toEqual({});
|
||||
} finally {
|
||||
try {
|
||||
await client.close();
|
||||
} finally {
|
||||
await transport.close();
|
||||
}
|
||||
}
|
||||
expect(response.isError).not.toBe(true);
|
||||
const mcpEvidence = applicationEvidenceSchema.parse(
|
||||
response.structuredContent,
|
||||
);
|
||||
expect(await client.ping()).toEqual({});
|
||||
expect(mcpEvidence.evidence_id).toBe(cliEvidence.evidence_id);
|
||||
|
||||
const expectBundleExports = (
|
||||
@@ -947,7 +918,6 @@ describe("application workflow CLI input", () => {
|
||||
describe("application workflow CLI copy boundaries", () => {
|
||||
it("compares source values excluded from escaped rest and spread copies", async () => {
|
||||
const root = await createTestTempDirectory("rea-copy-boundary-cli-");
|
||||
temporary.push(root);
|
||||
const sources = [
|
||||
`export default function make() {
|
||||
function mutate(value) { value.changed = true; }
|
||||
@@ -1020,7 +990,6 @@ describe("application workflow CLI copy boundaries", () => {
|
||||
describe("application workflow CLI and MCP capture lifetimes", () => {
|
||||
it("compares captured primitives and source values after aliases are replaced", async () => {
|
||||
const root = await createTestTempDirectory("rea-capture-lifetime-cli-");
|
||||
temporary.push(root);
|
||||
const fields = [
|
||||
"rebound",
|
||||
"rest",
|
||||
@@ -1122,7 +1091,6 @@ describe("application workflow CLI and MCP capture lifetimes", () => {
|
||||
describe("application workflow CLI export Evidence", () => {
|
||||
it("preserves uncertainty after a helper can mutate an awaited return object", async () => {
|
||||
const root = await createTestTempDirectory("rea-awaited-export-shape-cli-");
|
||||
temporary.push(root);
|
||||
const sources = [
|
||||
`export default async function make() {
|
||||
const result = { kind: "record" };
|
||||
@@ -1159,7 +1127,6 @@ describe("application workflow CLI export Evidence", () => {
|
||||
|
||||
it("compares exact export shapes from file-backed Evidence", async () => {
|
||||
const root = await createTestTempDirectory("rea-export-shape-cli-");
|
||||
temporary.push(root);
|
||||
const leftRoot = join(root, "left");
|
||||
const rightRoot = join(root, "right");
|
||||
await Promise.all([mkdir(leftRoot), mkdir(rightRoot)]);
|
||||
@@ -1223,7 +1190,6 @@ describe("application workflow CLI export property presence", () => {
|
||||
const root = await createTestTempDirectory(
|
||||
"rea-export-shape-presence-cli-",
|
||||
);
|
||||
temporary.push(root);
|
||||
const leftRoot = join(root, "left");
|
||||
const rightRoot = join(root, "right");
|
||||
await Promise.all([mkdir(leftRoot), mkdir(rightRoot)]);
|
||||
@@ -1349,32 +1315,13 @@ describe("application workflow CLI validation", () => {
|
||||
const compareThroughStdioMcp = async (
|
||||
input: Record<string, unknown>,
|
||||
): Promise<unknown> => {
|
||||
const transport = new StdioClientTransport({
|
||||
command: process.execPath,
|
||||
args: [resolve("scripts/rea.mjs"), "mcp"],
|
||||
cwd: process.cwd(),
|
||||
env: { PATH: process.env.PATH ?? "", REA_LOG_LEVEL: "silent" },
|
||||
stderr: "pipe",
|
||||
const client = await connectedMcpClient();
|
||||
const response = await client.callTool({
|
||||
name: "compare_javascript_export_shapes",
|
||||
arguments: input,
|
||||
});
|
||||
const client = new Client({
|
||||
name: "javascript-capture-parity",
|
||||
version: "1",
|
||||
});
|
||||
try {
|
||||
await client.connect(transport);
|
||||
const response = await client.callTool({
|
||||
name: "compare_javascript_export_shapes",
|
||||
arguments: input,
|
||||
});
|
||||
expect(response.isError).not.toBe(true);
|
||||
return response.structuredContent;
|
||||
} finally {
|
||||
try {
|
||||
await client.close();
|
||||
} finally {
|
||||
await transport.close();
|
||||
}
|
||||
}
|
||||
expect(response.isError).not.toBe(true);
|
||||
return response.structuredContent;
|
||||
};
|
||||
|
||||
const analyzeCliSources = (
|
||||
|
||||
Reference in New Issue
Block a user