test(javascript): consolidate source map acceptance fixtures (#13056)

This commit is contained in:
morluto authored and GitHub committed 2026-11-21 04:31:30 -05:00
1 parent dc16719b04
commit 66d1bb13dc
3 files changed
+62 -119

No files matched your search

@@ -471,76 +471,7 @@ describe("Electron unavailable identity through stdio MCP", () => {
);
});
it("preserves complete line-comment source-map URLs through CLI and MCP", async () => {
const root = await createTestTempDirectory("rea-source-map-url-cli-");
const directives = [
"//# sourceMappingURL=app.js.map?variant=a*modern",
"//@ sourceMappingURL=app.js.map?variant=a*legacy",
"//# sourceMappingURL=app.js.map#first*section",
];
await writeFile(
join(root, "app.js"),
["export const value = 1;", ...directives, ""].join("\n"),
);
await writeFile(
join(root, "app.js.map"),
JSON.stringify({ version: 3, sources: [], names: [], mappings: "" }),
);
const { graph } = z
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
.parse(
await runCli(["analyze-javascript-application", root, "--json"]),
).normalized_result;
const map = graph.nodes.find(
({ kind, observations }) =>
kind === "source-map" &&
observations.some(({ properties }) => properties.path === "app.js.map"),
);
const references = map?.observations.filter(
({ label }) => label === "source map reference",
);
expect(references).toHaveLength(directives.length);
for (const [index, directive] of directives.entries()) {
const declaredUrl = directive.slice(directive.indexOf("=") + 1);
const digest = createHash("sha256").update(declaredUrl).digest("hex");
expect(references).toContainEqual(
expect.objectContaining({
properties: {
declared_url: declaredUrl,
declared_url_sha256: digest,
resolved_path: "app.js.map",
resolution_status: "resolved",
available: true,
},
evidence: expect.objectContaining({
location: {
available: true,
value: {
kind: "source-range",
source: "app.js",
start: { line: index + 2, column: 0 },
end: { line: index + 2, column: directive.length },
},
},
}),
}),
);
expect(graph.edges).toContainEqual(
expect.objectContaining({
relation: "maps_to",
target_node_id: map?.node_id,
properties: {
declared_url_sha256: digest,
resolved_path: "app.js.map",
resolution_status: "resolved",
},
}),
);
}
expect((await analyzeThroughStdioMcp(root)).graph).toEqual(graph);
});
it("resolves source-map URL paths once without changing their declarations", async () => {
it("preserves source-map declarations while decoding each path once through CLI and MCP", async () => {
const root = await createTestTempDirectory("rea-source-map-encoding-cli-");
const cases = [
["space%20part.js.map", "space part.js.map"],
@@ -549,6 +480,9 @@ it("resolves source-map URL paths once without changing their declarations", asy
["space%20part.js.map?v=1#section", "space part.js.map"],
["caf%C3%A9.js.map", "café.js.map"],
["literal%ZZ.js.map", "literal%ZZ.js.map"],
["app.js.map?variant=a*modern", "app.js.map"],
["app.js.map?variant=a*legacy", "app.js.map", "//@"],
["app.js.map#first*section", "app.js.map"],
] as const;
for (const path of new Set(cases.map(([, path]) => path))) {
await writeFile(
@@ -562,12 +496,17 @@ it("resolves source-map URL paths once without changing their declarations", asy
}),
);
}
await writeFile(
join(root, "app.js"),
cases.map(([url]) => `//# sourceMappingURL=${url}\n`).join("") +
'new Worker(new URL("./worker%20file.js", import.meta.url));\n' +
'navigator.serviceWorker.register("./service%20worker.js");\n',
const directives = cases.map(
([url, , marker = "//#"]) => `${marker} sourceMappingURL=${url}`,
);
const lines = [
...directives.slice(0, 6),
'new Worker(new URL("./worker%20file.js", import.meta.url));',
'navigator.serviceWorker.register("./service%20worker.js");',
...directives.slice(6),
"",
];
await writeFile(join(root, "app.js"), lines.join("\n"));
await writeFile(join(root, "worker file.js"), "export const worker = 1;\n");
await writeFile(
join(root, "service worker.js"),
@@ -578,11 +517,17 @@ it("resolves source-map URL paths once without changing their declarations", asy
.parse(
await runCli(["analyze-javascript-application", root, "--json"]),
).normalized_result;
const maps = graph.nodes.filter(({ kind }) => kind === "source-map");
expect(
maps.flatMap(({ observations }) =>
observations.filter(({ label }) => label === "source map reference"),
),
).toHaveLength(cases.length);
for (const [index, [declaredUrl, path]] of cases.entries()) {
const map = graph.nodes.find(
({ kind, observations }) =>
kind === "source-map" &&
observations.some(({ properties }) => properties.path === path),
const directive = directives[index]!;
const line = lines.indexOf(directive) + 1;
const map = maps.find(({ observations }) =>
observations.some(({ properties }) => properties.path === path),
);
const digest = createHash("sha256").update(declaredUrl).digest("hex");
expect(map?.observations).toContainEqual(
@@ -600,11 +545,8 @@ it("resolves source-map URL paths once without changing their declarations", asy
value: {
kind: "source-range",
source: "app.js",
start: { line: index + 1, column: 0 },
end: {
line: index + 1,
column: "//# sourceMappingURL=".length + declaredUrl.length,
},
start: { line, column: 0 },
end: { line, column: directive.length },
},
},
}),
@@ -11,9 +11,9 @@ import { asarHeaderBytes } from "../../fixtures/asarHeader.js";
import { resolveJavaScriptApplicationMcpCall } from "../../support/cli/javascriptApplication.js";
import { cliTest } from "../../support/cli/cliFixture.js";
cliTest.for(["CLI", "stdio MCP"] as const)(
"links a block source-map URL containing an asterisk through %s",
async (surface, { cli }) => {
cliTest(
"links a block source-map URL containing an asterisk through CLI and stdio MCP",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-block-map-");
const url = "app*part.js.map";
const directive = "/*# sourceMappingURL=" + url;
@@ -32,29 +32,23 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
}),
);
await writeFile(archive, Buffer.concat([header, script, mapText]));
let document: unknown;
if (surface === "CLI") {
const result = await cli.run({
arguments: ["analyze-javascript-application", archive, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
document = result.json;
} else {
const call = await resolveJavaScriptApplicationMcpCall(cli, undefined);
const result = await call("analyze_javascript_application", {
input_path: archive,
format: "asar",
});
expect(result.isError).not.toBe(true);
document = result.structuredContent;
}
const {
normalized_result: { graph },
} = z
.object({
normalized_result: javascriptApplicationAnalysisResultSchema,
})
.parse(document);
const cliResult = await cli.run({
arguments: ["analyze-javascript-application", archive, "--json"],
});
expect(cliResult.exitCode, cliResult.stderr).toBe(0);
const call = await resolveJavaScriptApplicationMcpCall(cli, undefined);
const mcpResult = await call("analyze_javascript_application", {
input_path: archive,
format: "asar",
});
expect(mcpResult.isError).not.toBe(true);
const document = z.object({
normalized_result: javascriptApplicationAnalysisResultSchema,
});
const { graph } = document.parse(cliResult.json).normalized_result;
expect(
document.parse(mcpResult.structuredContent).normalized_result.graph,
).toEqual(graph);
const digest = createHash("sha256").update(url).digest("hex");
const map = graph.nodes.find(({ observations }) =>
observations.some(({ properties }) => properties.declared_url === url),
@@ -4,10 +4,17 @@ import { join } from "node:path";
import { expect } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
import { cliTest, type TestCli } from "../../support/cli/cliFixture.js";
import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js";
import { writeFixtureFiles } from "../../support/javascriptApplicationFixture.js";
const graphThroughBothSurfaces = async (cli: TestCli, root: string) => {
const { graph } = await analyzeJavaScriptDirectory("CLI", cli, root);
const mcp = await analyzeJavaScriptDirectory("stdio MCP", cli, root);
expect(mcp.graph).toEqual(graph);
return graph;
};
cliTest.for(["CLI", "stdio MCP"] as const)(
"recovers retained prefixed source-map originals and byte identity through %s",
async (surface, { cli }) => {
@@ -243,9 +250,9 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
},
);
cliTest.for(["CLI", "stdio MCP"] as const)(
"keeps source declarations whose paths cannot be projected into the artifact through %s",
async (surface, { cli }) => {
cliTest(
"keeps source declarations whose paths cannot be projected into the artifact through CLI and MCP",
async ({ cli }) => {
const root = await createTestTempDirectory(
"rea-source-map-path-projection-",
);
@@ -272,7 +279,7 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
mappings: "",
});
await writeFixtureFiles(root, { [mapPath]: mapText });
const { graph } = await analyzeJavaScriptDirectory(surface, cli, root);
const graph = await graphThroughBothSurfaces(cli, root);
const originals = graph.nodes.filter(
({ identity }) => identity.strategy === "source-map-original",
);
@@ -302,9 +309,9 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
},
);
cliTest.for(["CLI", "stdio MCP"] as const)(
"keeps local originals but reports incomplete source maps with malformed mappings or offsets through %s",
async (surface, { cli }) => {
cliTest(
"keeps local originals but reports incomplete source maps with malformed mappings or offsets through CLI and MCP",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-malformed-source-map-");
const leaf = {
version: 3,
@@ -328,7 +335,7 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
),
);
const { graph } = await analyzeJavaScriptDirectory(surface, cli, root);
const graph = await graphThroughBothSurfaces(cli, root);
expect(graph.coverage.status).toBe("partial");
for (const path of Object.keys(maps))
expect(graph.nodes).toContainEqual(