Files
reverse-engineering-anything/src/application/javascript/JavaScriptApplicationTemplateLiterals.test.ts
T
morluto ffe0574e17 fix(artifacts)!: retain cleanup owners through client shutdown
Keep failed reader, root descriptor, and output-tree cleanup with the caller. Drain admitted work before close, retry the same retained owners, and block new resource work while cleanup is incomplete. Preserve primary failures and complete inventory observations.

Thread ownership through JavaScript reconstruction, the CLI's one-shot scope, MCP server shutdown, verifier scripts, and test fixtures. Remove redundant ownership flags and the unused validated test wrapper.

Validate with 850 passing focused tests across 90 suites (two skipped), targeted classification and cleanup retries, static and unused-code checks, and real macOS CLI/MCP directory, ZIP, and DMG workflows. Native Windows and deep-analysis providers were not verified.

BREAKING CHANGE: Direct artifact inventory/extraction and JavaScript analysis APIs require an explicit caller-owned ArtifactResourceScope. CLI and MCP input contracts are unchanged.
2026-10-11 04:43:02 +08:00

74 lines
2.8 KiB
TypeScript

import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js";
import { javascriptApplicationAnalysisResultSchema } from "../../domain/javascript/javascriptApplicationAnalysis.js";
import { analyzeJavaScriptApplication } from "../../../tests/support/javascriptApplicationScope.js";
const graphIdentities = async (source: string) => {
const inputPath = await createTestTempDirectory("rea-js-template-literal-");
await writeFile(join(inputPath, "app.js"), `${source}\n`);
await writeFile(join(inputPath, "dep.js"), "module.exports = 1;\n");
await writeFile(join(inputPath, "worker.js"), "self.onmessage = null;\n");
const result = await analyzeJavaScriptApplication({
input_path: inputPath,
format: "directory",
});
if (!result.ok)
throw new Error(`Expected analysis success: ${result.error.message}`);
const { graph } = javascriptApplicationAnalysisResultSchema.parse(
result.value.normalized_result,
);
return (
graph.nodes
.filter(({ kind }) => kind !== "javascript-asset" && kind !== "artifact")
// Artifact digests differ between spellings; keep the derived keys.
.map(({ kind, identity }) => ({
kind,
strategy: identity.strategy,
...(identity.strategy === "artifact-local-key"
? {
namespace: identity.namespace,
// Module-export identity is a JSON tuple whose first entry
// binds the whole container; only that digest changes here.
key:
identity.namespace === "module-export"
? JSON.stringify(
(JSON.parse(identity.key) as unknown[]).slice(1),
)
: identity.namespace === "source-module"
? identity.key.replace(/^[0-9a-f]{64}:/u, "")
: identity.key,
}
: {}),
}))
.sort((left, right) =>
JSON.stringify(left).localeCompare(JSON.stringify(right)),
)
);
};
// Each quoted literal has the same length as its template spelling, so
// source locations and every derived identity stay comparable.
it.each([
"fetch('https://api.example.com/v1/users');",
"localStorage.setItem('token', 1);",
"new Worker('./worker.js');",
"const dep = require('./dep.js'); dep();",
"import('./dep.js');",
])(
"reads a template without substitutions as its exact string: %s",
async (quoted) => {
const template = quoted.replaceAll("'", "`");
const expected = await graphIdentities(quoted);
expect(
expected.some(({ kind }) =>
["endpoint", "storage", "worker", "javascript-module"].includes(kind),
),
).toBe(true);
await expect(graphIdentities(template)).resolves.toEqual(expected);
},
);