test(sourcemap): consolidate indexed section admission checks (#13066)

This commit is contained in:
morluto authored and GitHub committed 2026-11-21 10:20:13 +00:00
1 parent 5bbd238f2f
commit 47d86582c8
4 files changed
+153 -271

No files matched your search

+33 -180
View File
@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest";
import { expect, it } from "vitest";
import {
inspectLocalSourceMapDeclarations,
@@ -45,191 +45,44 @@ it("applies observed-content policy to complete and partially invalid maps", ()
).toMatchObject({ embedded: true, complete: false });
});
describe("source-map envelope walk", () => {
it("retains ordered declarations under nested section offsets", () => {
const root = {
it("rejects non-version-3 roots and nested maps", () => {
expect(inspect({ version: 3 })).toMatchObject({
declarations: [],
limitations: ["Source map has no sources array."],
});
expect(inspect(null)).toMatchObject({
declarations: [],
limitations: ["Source map is not a version 3 map."],
});
expect(inspect([{ version: 3 }])).toMatchObject({
declarations: [],
limitations: ["Source map is not a version 3 map."],
});
expect(inspect({ version: 2, mappings: "AAAA" })).toMatchObject({
declarations: [],
limitations: ["Source map is not a version 3 map."],
});
expect(inspect({ mappings: "AAAA" })).toMatchObject({
declarations: [],
limitations: ["Source map is not a version 3 map."],
});
expect(
inspect({
version: 3,
sections: [
{
offset: { line: 0, column: 0 },
map: {
version: 3,
sections: [
{
offset: { line: 0, column: 0 },
map: { ...leaf, sources: ["b.ts"] },
},
{
offset: { line: 1, column: 0 },
map: { ...leaf, sources: ["c.ts"] },
},
],
},
},
{
offset: { line: 10, column: 0 },
map: { ...leaf, sources: ["d.ts"] },
map: { version: "3", sections: [] },
},
],
};
const result = inspect(root);
expect(result.limitations).toEqual([]);
expect(result.invalid_sections).toEqual([]);
expect(result.declarations.map(({ source }) => source)).toEqual([
"b.ts",
"c.ts",
"d.ts",
]);
});
it("retains external sections in canonical path order and embedded leaves", () => {
const url = "https://app.test/maps/part.map";
const result = inspect({
version: 3,
sections: [
{
offset: { line: 0, column: 0 },
map: {
version: 3,
sections: [
{ offset: { line: 0, column: 0 }, url: `${url}/nested` },
{ offset: { line: 1, column: 0 }, map: leaf },
],
},
},
{ offset: { line: 1, column: 0 }, url },
{
offset: { line: 2, column: 0 },
map: { ...leaf, sources: ["after.ts"] },
},
],
});
expect(result).toMatchObject({
limitations: [],
declarations: [
{ sectionPath: [0, 1], source: "app.ts" },
{ sectionPath: [2], source: "after.ts" },
],
external_sections: [
{ section_path: [0, 0], url: `${url}/nested` },
{ section_path: [1], url },
],
invalid_sections: [],
});
});
it("retains valid leaves and URLs when a sibling section is malformed", () => {
const url = "https://app.test/maps/part.map";
const result = inspect({
version: 3,
sections: [
{
offset: { line: 0, column: 0 },
map: { ...leaf, sources: ["before.ts"] },
},
{ offset: { line: 1, column: 0 }, url },
{ offset: { line: 2, column: 0 }, url: 12, map: leaf },
{
offset: { line: 3, column: 0 },
map: { ...leaf, sources: ["after.ts"] },
},
],
});
expect(result).toMatchObject({
limitations: [],
declarations: [
{ sectionPath: [0], source: "before.ts" },
{ sectionPath: [3], source: "after.ts" },
],
external_sections: [{ section_path: [1], url }],
invalid_sections: [
{
section_path: [2],
reason: expect.stringMatching(/section URL.*must be a string/iu),
},
],
});
});
it("rejects sections without embedded maps or an external URL", () => {
const missing = {
version: 3,
sections: [{ offset: { line: 0, column: 0 } }],
};
expect(inspect(missing)).toMatchObject({
invalid_sections: [
{
section_path: [0],
reason: expect.stringMatching(
/source-map section requires.*embedded map or external URL/i,
),
},
],
});
});
});
describe("malformed source-map envelope facts", () => {
it("marks a present non-array sections member malformed at the map path", () => {
expect(
inspect({
version: 3,
sections: null,
sources: ["must-not-be-read.ts"],
mappings: "AAAA",
}),
).toEqual({
declarations: [],
limitations: [],
external_sections: [],
invalid_sections: [
{
section_path: [],
reason: expect.stringMatching(/sections.*must be an array/iu),
},
],
});
});
it("rejects non-version-3 roots and nested maps", () => {
expect(inspect({ version: 3 })).toMatchObject({
declarations: [],
limitations: ["Source map has no sources array."],
});
expect(inspect(null)).toMatchObject({
declarations: [],
limitations: ["Source map is not a version 3 map."],
});
expect(inspect([{ version: 3 }])).toMatchObject({
declarations: [],
limitations: ["Source map is not a version 3 map."],
});
expect(inspect({ version: 2, mappings: "AAAA" })).toMatchObject({
declarations: [],
limitations: ["Source map is not a version 3 map."],
});
expect(inspect({ mappings: "AAAA" })).toMatchObject({
declarations: [],
limitations: ["Source map is not a version 3 map."],
});
expect(
inspect({
version: 3,
sections: [
{
offset: { line: 0, column: 0 },
map: { version: "3", sections: [] },
},
],
}),
).toMatchObject({
invalid_sections: [
{
section_path: [0],
reason: "Indexed source-map section map is not a version 3 map.",
},
],
});
}),
).toMatchObject({
invalid_sections: [
{
section_path: [0],
reason: "Indexed source-map section map is not a version 3 map.",
},
],
});
});
@@ -1,6 +1,5 @@
import { describe, expect, it } from "vitest";
import { traceSourceMap } from "./TraceSourceMap.js";
import { inspectSourceMap } from "../../domain/sourceMapFormat.js";
const url = "https://app.test/maps/app.js.map?v=2#map";
const point = { line: 1, column: 0 };
@@ -14,39 +13,6 @@ const leaf = {
const trace = (map: unknown, position = point) =>
traceSourceMap(JSON.stringify(map), url, position);
it("uses the same conservative section classification for URL-plus-map and malformed URLs", () => {
const externalUrl = "https://app.test/maps/part.map";
const external = () =>
inspectSourceMap(
JSON.stringify({
version: 3,
sections: [
{ offset: { line: 0, column: 0 }, url: externalUrl, map: leaf },
],
}),
);
expect(external).toThrow(
expect.objectContaining({
failure: expect.objectContaining({ kind: "unsupported" }),
message: `External source-map section URL is not fetched: ${externalUrl}`,
}),
);
const malformed = () =>
inspectSourceMap(
JSON.stringify({
version: 3,
sections: [{ offset: { line: 0, column: 0 }, url: 12, map: leaf }],
}),
);
expect(malformed).toThrow(
expect.objectContaining({
failure: expect.objectContaining({ kind: "malformed" }),
message: expect.stringMatching(/section URL.*must be a string.*\[0\]/iu),
}),
);
});
it("keeps ignore-list precedence and section-local indexes for duplicate sources", () => {
const map = {
version: 3,
@@ -26,50 +26,84 @@ const analyzeMaps = async (maps: Readonly<Record<string, string>>) => {
return analysis.graph;
};
it("keeps external indexed sections unsupported while retaining local siblings", async () => {
it("keeps nested declarations and exact section diagnostics across external and malformed siblings", async () => {
const url = "https://app.test/maps/part.map";
const mapText = JSON.stringify({
const original = (source: string, content: string) => ({
version: 3,
sections: [
{
offset: { line: 0, column: 0 },
map: {
version: 3,
sources: ["before.ts"],
sourcesContent: ["export const before = 1;"],
names: [],
mappings: "AAAA",
},
},
{ offset: { line: 1, column: 0 }, url },
{
offset: { line: 2, column: 0 },
url: 42,
map: {
version: 3,
sources: ["ignored.ts"],
sourcesContent: ["export const ignored = 0;"],
names: [],
mappings: "AAAA",
},
},
{
offset: { line: 3, column: 0 },
map: {
version: 3,
sources: ["after.ts"],
sourcesContent: ["export const after = 2;"],
names: [],
mappings: "AAAA",
},
},
],
sources: [source] as const,
sourcesContent: [content] as const,
names: [],
mappings: "AAAA",
});
const before = original("before.ts", "export const before = 1;");
const middle = original("middle.ts", "export const middle = 2;");
const after = original("after.ts", "export const after = 3;");
const retained = [
[before, [0, 0]],
[middle, [0, 3]],
[after, [3]],
] as const;
const indexed = [
{
offset: { line: 0, column: 0 },
map: {
version: 3,
sections: [
{
offset: { line: 0, column: 0 },
map: before,
},
{
offset: { line: 1, column: 0 },
url: `${url}/nested`,
map: original("ignored.ts", "0"),
},
{ offset: { line: 2, column: 0 } },
{
offset: { line: 3, column: 0 },
map: middle,
},
],
},
},
{ offset: { line: 4, column: 0 }, url },
{
offset: { line: 5, column: 0 },
url: 42,
map: original("ignored.ts", "0"),
},
{
offset: { line: 6, column: 0 },
map: after,
},
];
const mapText = JSON.stringify({ version: 3, sections: indexed });
const graph = await analyzeMaps({ "indexed.js.map": mapText });
const digest = createHash("sha256").update(mapText).digest("hex");
const limitation =
`Indexed source-map section at [2]: External source-map section URL must be a string.; ` +
`External source-map section URL is not fetched: ${url}`;
const invalid_sections = [
{
section_path: [0, 2],
reason:
"Indexed source-map section requires an embedded map or external URL.",
},
{
section_path: [2],
reason: "External source-map section URL must be a string.",
},
];
const external_sections = [
{ section_path: [0, 1], url: `${url}/nested` },
{ section_path: [1], url },
];
const limitation = [
...invalid_sections.map(
({ section_path, reason }) =>
`Indexed source-map section at [${section_path.join(",")}]: ${reason}`,
),
...external_sections.map(
({ url }) => `External source-map section URL is not fetched: ${url}`,
),
].join("; ");
expect(graph.coverage.status).toBe("partial");
expect(graph.nodes).toContainEqual(
expect.objectContaining({
@@ -79,15 +113,8 @@ it("keeps external indexed sections unsupported while retaining local siblings",
properties: expect.objectContaining({
path: "indexed.js.map",
operation: "parse-local-source-map",
external_sections: [{ section_path: [1], url }],
invalid_sections: [
{
section_path: [2],
reason: expect.stringMatching(
/section URL.*must be a string/iu,
),
},
],
external_sections,
invalid_sections,
}),
evidence: expect.objectContaining({
artifact: expect.objectContaining({ sha256: digest }),
@@ -97,21 +124,32 @@ it("keeps external indexed sections unsupported while retaining local siblings",
]),
}),
);
for (const [source, content] of [
["before.ts", "export const before = 1;"],
["after.ts", "export const after = 2;"],
] as const) {
expect(graph.nodes).toContainEqual(
const originals = graph.nodes.filter(
({ identity }) => identity.strategy === "source-map-original",
);
expect(originals).toHaveLength(retained.length);
for (const [declaration_index, [map, section_path]] of retained.entries())
expect(originals).toContainEqual(
expect.objectContaining({
kind: "source-module",
identity: expect.objectContaining({
source_map_sha256: digest,
original_source: source,
source_sha256: createHash("sha256").update(content).digest("hex"),
original_source: map.sources[0],
source_sha256: createHash("sha256")
.update(map.sourcesContent[0])
.digest("hex"),
}),
observations: expect.arrayContaining([
expect.objectContaining({
properties: expect.objectContaining({
declaration_index,
section_path,
source_index: 0,
}),
}),
]),
}),
);
}
});
it("marks non-array sections malformed even when the object has regular-map fields", async () => {
@@ -394,13 +394,14 @@ it.each([false, true])(
it.each([
{
failure: "external section",
failure: "external section with an embedded fallback",
text: JSON.stringify({
version: 3,
sections: [
{
offset: { line: 0, column: 0 },
url: "https://app.test/external.map",
map: JSON.parse(fixture.sourceMap.text) as unknown,
},
],
}),
@@ -410,6 +411,30 @@ it.each([
userMessage: expect.stringContaining("https://app.test/external.map"),
},
},
{
failure: "malformed section URL with an embedded fallback",
text: JSON.stringify({
version: 3,
sections: [
{
offset: { line: 0, column: 0 },
url: 12,
map: JSON.parse(fixture.sourceMap.text) as unknown,
},
],
}),
error: {
_tag: "AnalysisInputError",
issues: [
{
reason: "invalid_format",
message: expect.stringMatching(
/section URL.*must be a string.*\[0\]/iu,
),
},
],
},
},
{
failure: "malformed embedded map before row admission",
text: JSON.stringify({