import { createHash } from "node:crypto"; import { mkdir, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { expect, it } from "vitest"; import { reconstructJavaScriptArtifact } from "../../support/javascriptApplicationScope.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; it("preserves source digest, opening-tag range and decoded local base resolution", async () => { const root = await createTestTempDirectory("rea-html-entrypoint-evidence-"); const opening = '\n'; const html = `${prefix}${opening}`; await mkdir(join(root, "assets")); await Promise.all([ writeFile(join(root, "index.html"), html), writeFile( join(root, "assets", "actual.js"), "globalThis.ownedActual = true;", ), writeFile(join(root, "assets", "decoy.js"), "globalThis.decoy = true;"), ]); const result = await reconstructJavaScriptArtifact({ input_path: root }); const loads = result.graph.edges.filter( ({ relation, properties }) => relation === "loads" && properties.script_path !== undefined, ); expect(loads).toHaveLength(1); expect(loads[0]).toMatchObject({ properties: { script_path: "actual.js", base_href: "assets/", resolved_path: "assets/actual.js", }, evidence: { authority: "static-relationship-inference", artifact: { available: true, sha256: createHash("sha256").update(html).digest("hex"), }, location: { available: true, value: { kind: "source-range", source: "index.html", start: { line: 2, column: 0 }, end: { line: 2, column: opening.length }, }, }, }, }); }); it("preserves external and inline HTML script ranges across line endings", async () => { const root = await createTestTempDirectory("rea-html-coordinate-evidence-"); const opening = '"; const inlineOpening = "', '', "", "", '', "", ].join("\n"); await mkdir(join(root, "www")); await Promise.all([ writeFile( join(root, "package.json"), JSON.stringify({ name: "game", version: "1.0.0" }), ), writeFile(join(root, "www", "index.html"), html), writeFile(join(root, "www", "cordova.js"), "globalThis.cordova = {};"), ]); const result = await reconstructJavaScriptArtifact({ input_path: root }); const gaps = result.graph.nodes.filter( ({ kind, observations }) => kind === "unknown" && observations.some( ({ properties }) => properties.operation === "parse-html-inline-scripts", ), ); expect(result.graph.coverage.status).toBe("partial"); expect(gaps).toHaveLength(1); expect(gaps[0]?.observations[0]).toMatchObject({ properties: { path: "www/index.html", inline_script_count: 2, inline_scripts: [ { script_type: "classic", location: { start: { line: 5, column: 8 }, end: { line: 7, column: 0 }, }, }, { script_type: "module", location: { start: { line: 8, column: 22 }, end: { line: 8, column: 67 }, }, }, ], }, evidence: { state: "unavailable", artifact: { sha256: createHash("sha256").update(html).digest("hex"), }, coverage: { status: "partial" }, }, }); expect(result.graph.limitations).toContain( "www/index.html has 2 inline JavaScript bodies starting at lines 5, 8 that were not analyzed; endpoints, storage, imports and other facts in them are absent from the evidence.", ); // The external script is still projected as before. expect( result.graph.edges.some( ({ relation, properties }) => relation === "loads" && properties.resolved_path === "www/cordova.js", ), ).toBe(true); }); it("keeps complete coverage when HTML has only external scripts and data blocks", async () => { const root = await createTestTempDirectory("rea-html-no-inline-scripts-"); await Promise.all([ writeFile( join(root, "index.html"), '', ), writeFile(join(root, "actual.js"), "globalThis.ownedActual = true;"), ]); const result = await reconstructJavaScriptArtifact({ input_path: root }); expect(result.graph.coverage.status).toBe("complete"); expect( result.graph.limitations.some((limitation) => limitation.includes("inline JavaScript"), ), ).toBe(false); }); it("retains the first 100 HTML and SVG inline script locations in document order", async () => { const root = await createTestTempDirectory("rea-html-many-inline-scripts-"); const scriptPair = [ "", "", "", ].join("\n"); await writeFile(join(root, "index.html"), scriptPair.repeat(2_500)); const result = await reconstructJavaScriptArtifact({ input_path: root }); const gap = result.graph.nodes .flatMap(({ observations }) => observations) .find( ({ properties }) => properties.operation === "parse-html-inline-scripts", ); const limit = { name: "html_inline_script_locations_per_file", value: 100, unit: "items", }; expect(gap?.properties.inline_script_count).toBe(5_000); expect(gap?.properties.inline_scripts).toEqual( Array.from({ length: 100 }, (_, index) => { const line = index + 1; const svg = line % 2 === 0; return { script_type: "classic", location: { start: { line, column: svg ? 13 : 8 }, end: { line, column: svg ? 22 : 14 }, }, }; }), ); // The gap record knows exactly which locations the bound dropped. expect(gap?.evidence.coverage).toEqual({ status: "partial", truncated: true, omitted_count: 4_900, limits: [limit], }); // The graph is truncated too, but unanalyzed bodies hide an unknown total. expect(result.graph.coverage).toMatchObject({ status: "partial", truncated: true, omitted_count: null, limits: expect.arrayContaining([limit]), }); expect(result.graph.limitations).toContain( "index.html has 5000 inline JavaScript bodies starting at lines 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and 4990 more that were not analyzed; endpoints, storage, imports and other facts in them are absent from the evidence.", ); });