mirror of
https://github.com/morluto/rea
synced 2026-10-26 06:16:17 -04:00
04672f2b91
Track module writes and escapes at their owner, preserve capture and reassignment ordering, and share CommonJS/default namespace identity without erasing parent-slot changes. Carry write sites into ambient alias resolution and use parsed write targets across consumers. Deduplicate returned-reference work with cycle context; retain unchanged temporal selection identity so provenance memoization stays effective. Reviewed fixes include #2041, #2465, #2830, #2881, and #2948. Remove superseded mutation representations and the ineffective per-markValue dedup layer. Validation: 380 focused JavaScript tests passed under a 384 MiB heap cap, including the previously aborting 30-level/128-call alias regression. Six public CLI/MCP builtin tests passed. Exact pdf.js completed-workflow performance remains unmeasured.
209 lines
5.9 KiB
TypeScript
209 lines
5.9 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
|
|
|
describe("JavaScript semantic analysis: native bindings 1", () => {
|
|
it("recovers child-process argv, env, stdio, listeners, and signals", () => {
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
import { spawn as launch } from "node:child_process";
|
|
function run() {
|
|
const child = launch(
|
|
"/bin/tool",
|
|
["--mode", "fast"],
|
|
{ env: { MODE: "test" }, stdio: ["ignore", "pipe", "pipe"] },
|
|
);
|
|
child.on("exit", onExit);
|
|
child.once("error", onError);
|
|
child.kill("SIGINT");
|
|
}
|
|
`);
|
|
|
|
expect(ir.childProcessSpawns).toEqual([
|
|
expect.objectContaining({
|
|
method: "spawn",
|
|
command: "/bin/tool",
|
|
argvCount: 2,
|
|
environmentSupplied: true,
|
|
stdioMode: "array",
|
|
resolution: "complete",
|
|
}),
|
|
]);
|
|
expect(
|
|
ir.childProcessInteractions.map(
|
|
({ kind, eventName, signalName, resolution }) => ({
|
|
kind,
|
|
eventName,
|
|
signalName,
|
|
resolution,
|
|
}),
|
|
),
|
|
).toEqual([
|
|
{
|
|
kind: "listener",
|
|
eventName: "exit",
|
|
signalName: null,
|
|
resolution: "complete",
|
|
},
|
|
{
|
|
kind: "listener",
|
|
eventName: "error",
|
|
signalName: null,
|
|
resolution: "complete",
|
|
},
|
|
{
|
|
kind: "signal",
|
|
eventName: null,
|
|
signalName: "SIGINT",
|
|
resolution: "complete",
|
|
},
|
|
]);
|
|
expect(ir.functionFingerprints[0]?.components.effects).toContain(
|
|
"child-process",
|
|
);
|
|
});
|
|
|
|
it("recovers configuration, requests, response consumers, and boundaries", () => {
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
import { readFileSync } from "node:fs";
|
|
import * as http from "node:http";
|
|
async function run(schema) {
|
|
const endpoint = process.env.API_URL ?? "https://fallback.test";
|
|
const mode = process.argv[2];
|
|
const raw = readFileSync("./config.json", "utf8");
|
|
const parsed = JSON.parse(raw);
|
|
const port = Number(process.env.PORT);
|
|
const validated = schema.parse(parsed);
|
|
const response = await fetch(endpoint, {
|
|
method: "POST",
|
|
body: validated,
|
|
});
|
|
const body = await response.json();
|
|
http.request("https://audit.test", { body });
|
|
return { mode, port, body };
|
|
}
|
|
`);
|
|
|
|
expect(
|
|
ir.configurationOperations.map(({ kind, key }) => ({ kind, key })),
|
|
).toEqual(
|
|
expect.arrayContaining([
|
|
{ kind: "environment", key: "API_URL" },
|
|
{ kind: "default", key: "API_URL" },
|
|
{ kind: "argv", key: "2" },
|
|
{ kind: "file", key: "./config.json" },
|
|
{ kind: "environment", key: "PORT" },
|
|
]),
|
|
);
|
|
expect(
|
|
ir.requestOperations.map(({ kind, method, endpoint, resolution }) => ({
|
|
kind,
|
|
method,
|
|
endpoint,
|
|
resolution,
|
|
})),
|
|
).toEqual(
|
|
expect.arrayContaining([
|
|
{
|
|
kind: "request",
|
|
method: "fetch",
|
|
endpoint: null,
|
|
resolution: "partial",
|
|
},
|
|
{
|
|
kind: "response-consumer",
|
|
method: "json",
|
|
endpoint: null,
|
|
resolution: "complete",
|
|
},
|
|
{
|
|
kind: "request",
|
|
method: "request",
|
|
endpoint: "https://audit.test",
|
|
resolution: "complete",
|
|
},
|
|
]),
|
|
);
|
|
expect(
|
|
ir.boundaryOperations.map(({ kind, method, resolution }) => ({
|
|
kind,
|
|
method,
|
|
resolution,
|
|
})),
|
|
).toEqual(
|
|
expect.arrayContaining([
|
|
{ kind: "parse", method: "JSON.parse", resolution: "complete" },
|
|
{ kind: "coerce", method: "Number", resolution: "complete" },
|
|
{ kind: "parse", method: "parse", resolution: "partial" },
|
|
]),
|
|
);
|
|
expect(ir.functionFingerprints[0]?.components.effects).toContain("network");
|
|
});
|
|
});
|
|
|
|
describe("JavaScript semantic analysis: native bindings 2", () => {
|
|
it("recovers built-in resource acquisition and exact local release", () => {
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
import { open } from "node:fs/promises";
|
|
import { connect as dial } from "node:net";
|
|
async function run(path) {
|
|
const file = await open(path);
|
|
const socket = dial({ port: 9000 });
|
|
await file.close();
|
|
socket.destroy();
|
|
}
|
|
`);
|
|
|
|
expect(
|
|
ir.resourceOperations.map(({ kind, method, resolution }) => ({
|
|
kind,
|
|
method,
|
|
resolution,
|
|
})),
|
|
).toEqual([
|
|
{ kind: "acquire", method: "open", resolution: "complete" },
|
|
{ kind: "acquire", method: "connect", resolution: "complete" },
|
|
{ kind: "release", method: "close", resolution: "complete" },
|
|
{ kind: "release", method: "destroy", resolution: "complete" },
|
|
]);
|
|
expect(ir.functionFingerprints[0]?.components.effects).toContain(
|
|
"resource",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("JavaScript semantic analysis: child-process signals", () => {
|
|
it("keeps unresolved kill signals distinct from default and literal signals", () => {
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
import { spawn } from "node:child_process";
|
|
function run(signal) {
|
|
const child = spawn("/bin/tool");
|
|
child.kill(signal);
|
|
child.kill();
|
|
child.kill("SIGINT");
|
|
child.kill(undefined);
|
|
child.kill(undefined as NodeJS.Signals | undefined);
|
|
child.kill(void 0);
|
|
child.kill(void signal);
|
|
}
|
|
function shadowed(undefined) {
|
|
const child = spawn("/bin/tool");
|
|
child.kill(undefined);
|
|
}
|
|
`);
|
|
|
|
expect(ir.childProcessSpawns).toHaveLength(2);
|
|
expect(
|
|
ir.childProcessInteractions.map(({ signalName }) => signalName),
|
|
).toEqual([
|
|
null,
|
|
"SIGTERM",
|
|
"SIGINT",
|
|
"SIGTERM",
|
|
"SIGTERM",
|
|
"SIGTERM",
|
|
"SIGTERM",
|
|
null,
|
|
]);
|
|
});
|
|
});
|