test(sourcemap): consolidate original source dependency coverage (#13064)

This commit is contained in:
morluto authored and GitHub committed 2026-11-21 05:10:07 -05:00
1 parent d69a7fdbf6
commit 35ece22417
3 files changed
+162 -177

No files matched your search

@@ -1069,94 +1069,101 @@ describe("web source-map sourcesContent validation", () => {
});
});
describe("source-map dependency coverage of hard-to-parse sources", () => {
const fetchWithSources = (sources: readonly string[]) =>
fetchWebSourceMapsOk([request], input(), undefined, {
fetch: () =>
Promise.resolve(
new Response(
JSON.stringify({
version: 3,
names: [],
sources: sources.map((_, index) => `source-${String(index)}.ts`),
sourcesContent: [...sources],
mappings: "AAAA",
}),
),
),
});
it.each([
it("keeps exact dependency coverage for supported, recovered, and unparsable sources", async () => {
const supported = [
[
"decorated classes and parameters",
"decorated-parameters.ts",
'@Component({ selector: "app" })\nexport class A { @Inject() b: C; constructor(@Optional() private s: S) {} }\nimport "./a.js";',
["./a.js"],
],
[
"legacy class decorators",
"decorated-class.ts",
'class A { @dec method() {} }\nimport "./a.js";',
["./a.js"],
],
["import-equals.ts", 'import lib = require("./lib.js");', ["./lib.js"]],
[
"TypeScript import equals",
'import lib = require("./lib.js");',
["./lib.js"],
],
[
"type-only import equals",
"type-only-equals.ts",
'import type Types = require("./types.js");\nimport lib = require("./lib.js");',
["./lib.js"],
],
["type-only-import.ts", 'import type { Value } from "./types.js";', []],
["type-only-export.ts", 'export type { Value } from "./types.js";', []],
[
"whole-statement type-only import",
'import type { Value } from "./types.js";',
[],
],
[
"whole-statement type-only export",
'export type { Value } from "./types.js";',
[],
],
[
"inline type import retains module side effects",
"inline-types.ts",
'import { type Value } from "./types.js";',
["./types.js"],
],
[
"a local import alias",
"local-alias.ts",
'import lib = localAlias;\nimport "./a.js";',
["./a.js"],
],
])("recovers dependencies from %s", async (_label, text, expected) => {
const result = await fetchWithSources([text]);
expect(result.items[0]?.status).toBe("included");
expect(
result.items[0]?.original_module_edges.map(({ specifier }) => specifier),
).toEqual(expected);
] as const;
const maps = [
supported.map(([path, text]) => ({ path, text })),
[
{
path: "recovering.ts",
text: 'with (scope) { require("./c.js"); }\nrequire("./b.js");\nimport "./a.js";',
},
],
[{ path: "unparsable.ts", text: 'import "./a.js"; const s = "oops' }],
[{ path: "clean.ts", text: "export const value = 1;" }],
].map((sources, index) => {
const url = `${origin}/assets/${index}.map`;
return {
request: {
scriptKey: `scr_${String(index + 1).padStart(64, "0")}`,
declaredUrl: url,
fetchUrl: url,
},
text: JSON.stringify({
version: 3,
names: [],
sources: sources.map(({ path }) => path),
sourcesContent: sources.map(({ text }) => text),
mappings: "AAAA",
}),
};
});
it("keeps recovered edges from a partly recovered source and reports them as partial", async () => {
const result = await fetchWithSources([
'with (scope) { require("./c.js"); }\nrequire("./b.js");\nimport "./a.js";',
]);
expect(result.status).toBe("partial");
expect(result.items[0]?.status).toBe("partial");
expect(result.items[0]?.limitation).toContain(
"could not be parsed in full",
);
expect(
result.items[0]?.original_module_edges.map(({ specifier }) => specifier),
).toEqual(["./b.js", "./a.js"]);
});
it("distinguishes an unparsable source from a source with no imports", async () => {
const broken = await fetchWithSources(['import "./a.js"; const s = "oops']);
expect(broken.items[0]?.status).toBe("partial");
expect(broken.items[0]?.original_module_edges).toEqual([]);
const clean = await fetchWithSources(["export const value = 1;"]);
expect(clean.status).toBe("included");
expect(clean.items[0]?.status).toBe("included");
expect(clean.items[0]?.original_module_edges).toEqual([]);
const replies = new Map(
maps.map(({ request, text }) => [request.fetchUrl, text]),
);
const result = await fetchWebSourceMapsOk(
maps.map(({ request }) => request),
input(),
undefined,
{ fetch: async (url) => new Response(replies.get(String(url))) },
);
expect(result).toMatchObject({
status: "partial",
requested: 4,
processed: 4,
items: [
{
status: "included",
limitation: null,
original_module_edges: supported.flatMap(
([path, , specifiers], declaration_index) =>
Array.from(specifiers, (specifier) => ({
declaration_index,
from_source: `${origin}/assets/${path}`,
specifier,
})),
),
},
{
status: "partial",
limitation: expect.stringContaining("could not be parsed in full"),
original_module_edges: [
{ specifier: "./b.js", kind: "require" },
{ specifier: "./a.js", kind: "static_import" },
],
},
{ status: "partial", original_module_edges: [] },
{ status: "included", limitation: null, original_module_edges: [] },
],
});
});
@@ -85,46 +85,3 @@ it("retains literal dependencies from HTTP source-map originals", async () => {
});
}
});
it("retains type-wrapped source-map requires with an unbound loader", async () => {
const origin = "https://fixture.example";
const url = `${origin}/dist/app.js.map`;
const source = [
'(require as typeof require)("./asserted.cjs");',
'require!("./non-null.cjs");',
"function local(require: (id: string) => unknown) {",
' (require as typeof require)("./shadowed.cjs");',
"}",
].join("\n");
const text = JSON.stringify({
version: 3,
names: [],
mappings: "AAAA",
sources: ["../src/main.cts"],
sourcesContent: [source],
});
const result = await fetchWebSourceMapsOk(
[{ scriptKey: `scr_${"1".repeat(64)}`, declaredUrl: url, fetchUrl: url }],
analyzeWebBundleInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "fixture",
allowed_origins: [origin],
fetch_source_maps: true,
}),
undefined,
{ fetch: async () => new Response(text) },
);
expect(result.items[0]).toMatchObject({
status: "included",
original_sources: [{ artifact: { text: source } }],
original_module_edges: ["./asserted.cjs", "./non-null.cjs"].map(
(specifier) => ({
declaration_index: 0,
from_source: `${origin}/src/main.cts`,
kind: "require",
specifier,
resolved_source: new URL(specifier, `${origin}/src/main.cts`).href,
}),
),
});
});
@@ -7,67 +7,76 @@ import { expect, it } from "vitest";
import { analyzeWebBundleInputSchema } from "../../../src/domain/webBundleAnalysis.js";
import { fetchWebSourceMapsOk } from "../../fixtures/fetchWebSourceMapsOk.js";
it("selects original source language from decoded URL paths while retaining URL evidence", async () => {
const typescriptContent = 'import "./dep.ts"; const value = <number>1;';
const annotatedContent = 'import "./dep.ts"; const value: number = 1;';
const javascriptContent = 'import "./dep.ts"; const value = 1;';
const declarations = [
{
path: "plain.ts?revision=2#view.tsx",
mediaType: "text/typescript",
content: typescriptContent,
},
{
path: "escaped%2Ets?revision=2#view.tsx",
mediaType: "text/typescript",
content: typescriptContent,
},
{
path: "escaped.%74s?revision=2#view.tsx",
mediaType: "text/typescript",
content: typescriptContent,
},
{
path: "escaped%2E%54S?revision=2#original.js",
mediaType: "text/typescript",
content: typescriptContent,
},
{
path: "invalid%.ts?revision=2#view.tsx",
mediaType: "text/typescript",
content: typescriptContent,
},
{
path: "view.tsx#original.ts",
mediaType: "text/typescript",
content: 'import "./dep.ts"; export const view: JSX.Element = <main />;',
},
{
path: "module.mts",
mediaType: "text/typescript",
content: annotatedContent,
},
{
path: "module.MTS",
mediaType: "text/typescript",
content: annotatedContent,
},
{
path: "common.cts",
mediaType: "text/typescript",
content: annotatedContent,
},
{
path: "main.js?filename=main.ts#original.ts",
mediaType: "text/javascript",
content: javascriptContent,
},
{
path: "extensionless?revision=2",
mediaType: "text/javascript",
content: javascriptContent,
},
];
const typescriptContent = 'import "./dep.ts"; const value = <number>1;';
const annotatedContent = 'import "./dep.ts"; const value: number = 1;';
const javascriptContent = 'import "./dep.ts"; const value = 1;';
const declarations = [
{
path: "plain.ts?revision=2#view.tsx",
mediaType: "text/typescript",
content: typescriptContent,
},
{
path: "escaped%2Ets?revision=2#view.tsx",
mediaType: "text/typescript",
content: typescriptContent,
},
{
path: "escaped.%74s?revision=2#view.tsx",
mediaType: "text/typescript",
content: typescriptContent,
},
{
path: "escaped%2E%54S?revision=2#original.js",
mediaType: "text/typescript",
content: typescriptContent,
},
{
path: "invalid%.ts?revision=2#view.tsx",
mediaType: "text/typescript",
content: typescriptContent,
},
{
path: "view.tsx#original.ts",
mediaType: "text/typescript",
content: 'import "./dep.ts"; export const view: JSX.Element = <main />;',
},
{
path: "module.mts",
mediaType: "text/typescript",
content: annotatedContent,
},
{
path: "module.MTS",
mediaType: "text/typescript",
content: annotatedContent,
},
{
path: "common.cts",
mediaType: "text/typescript",
content: [
annotatedContent,
'(require as typeof require)("./asserted.cjs");',
'require!("./non-null.cjs");',
"function local(require: (id: string) => unknown) {",
' (require as typeof require)("./shadowed.cjs");',
"}",
].join("\n"),
requires: ["./asserted.cjs", "./non-null.cjs"],
},
{
path: "main.js?filename=main.ts#original.ts",
mediaType: "text/javascript",
content: javascriptContent,
},
{
path: "extensionless?revision=2",
mediaType: "text/javascript",
content: javascriptContent,
},
];
it("selects the language of HTTP map originals and retains exact URLs and dependencies", async () => {
const text = JSON.stringify({
version: 3,
names: [],
@@ -122,12 +131,24 @@ it("selects original source language from decoded URL paths while retaining URL
generated_line: index + 1,
source,
})),
original_module_edges: sources.map(({ source }) => ({
from_source: source,
kind: "static_import",
specifier: "./dep.ts",
resolved_source: `${origin}/maps/dep.ts`,
})),
original_module_edges: sources.flatMap(
({ source, requires = [] }, declaration_index) => [
{
declaration_index,
from_source: source,
kind: "static_import",
specifier: "./dep.ts",
resolved_source: `${origin}/maps/dep.ts`,
},
...requires.map((specifier) => ({
declaration_index,
from_source: source,
kind: "require",
specifier,
resolved_source: new URL(specifier, source).href,
})),
],
),
});
} finally {
server.closeAllConnections();