From e415ba6673ca368d5ce2b9e02a8a9b5838ca7f93 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:02:58 -0500 Subject: [PATCH] test(source-maps): consolidate decoder and evidence coverage --- ...javascriptStaticAnalysisSourceMaps.test.ts | 126 +-- src/domain/sourceMapEnvelope.test.ts | 7 +- .../sourceMaps/SourceMapAdmission.test.ts | 56 - .../sourceMaps/SourceMapCommand.test.ts | 192 +--- .../sourceMaps/SourceMapDecoder.test.ts | 18 +- .../sourceMaps/TraceSourceMap.test.ts | 168 +-- .../applications/blockSourceMapCli.test.ts | 48 +- .../applications/sourceMapPrefixCli.test.ts | 241 ----- .../browserSourceMapCancellationCli.test.ts | 1 - .../sourceMapCaptureExclusions.test.ts | 2 +- .../browser/webSourceMapFetcher.test.ts | 961 ++++++++---------- .../browser/webSourceMapHttpStatus.test.ts | 106 +- .../webSourceMapNestedCoordinates.test.ts | 592 +++-------- ... webSourceMapOriginalDependencies.test.ts} | 36 +- .../webSourceMapResponseIdentity.test.ts | 110 +- .../webSourceMapSourceLanguage.test.ts | 82 +- .../artifactSourceMapTargets.test.ts | 16 +- ...scriptApplicationSourceMapMappings.test.ts | 293 +----- .../javascriptSourceMapComments.test.ts | 52 - .../javascriptSourceMapLocations.test.ts | 152 ++- .../javascript/sourceMapDecoder.test.ts | 431 +++++--- .../javascript/sourceMapPreparation.test.ts | 88 -- .../sourceMapRuntimeAvailability.test.ts | 46 - .../browserSourceMapCancellationMcp.test.ts | 89 +- .../process/sourceMapUncHosts.test.ts | 39 +- 25 files changed, 1288 insertions(+), 2664 deletions(-) rename tests/boundary/browser/{optionalSourceMapRequire.test.ts => webSourceMapOriginalDependencies.test.ts} (61%) delete mode 100644 tests/boundary/filesystem/javascriptSourceMapComments.test.ts delete mode 100644 tests/boundary/javascript/sourceMapPreparation.test.ts delete mode 100644 tests/boundary/javascript/sourceMapRuntimeAvailability.test.ts diff --git a/src/domain/javascript/javascriptStaticAnalysisSourceMaps.test.ts b/src/domain/javascript/javascriptStaticAnalysisSourceMaps.test.ts index b86b08fde0..6a99ddcf62 100644 --- a/src/domain/javascript/javascriptStaticAnalysisSourceMaps.test.ts +++ b/src/domain/javascript/javascriptStaticAnalysisSourceMaps.test.ts @@ -8,26 +8,35 @@ const sourceMapUrls = (source: string): readonly string[] => ); describe("source map directives", () => { - it.each([ - ["line comment", "//# sourceMappingURL=app.js.map", "app.js.map"], - ["legacy line comment", "//@ sourceMappingURL=app.js.map", "app.js.map"], - // Minifiers commonly emit the block form; it previously matched nothing. - ["block comment", "/*# sourceMappingURL=app.js.map */", "app.js.map"], - [ - "block comment after code", - "code();\n/*# sourceMappingURL=vendor.abc.map */", - "vendor.abc.map", - ], - ])("reads the %s form", (_label, source, expected) => { - expect(sourceMapUrls(source)).toEqual([expected]); - }); - - it("retains every directive in a source", () => { - expect( - sourceMapUrls( - "//# sourceMappingURL=first.map\n/*# sourceMappingURL=second.map */", - ), - ).toEqual(["first.map", "second.map"]); + it("retains every line and block directive with its exact URL and range", () => { + const cases = [ + { marker: "//#", url: "app*part.js.map", suffix: "" }, + { marker: "//@", url: "legacy.map", suffix: "" }, + { marker: "/*#", url: "vendor*part.js.map", suffix: " */" }, + { marker: "/*@", url: "*leading.map", suffix: "*/", prefix: "code(); " }, + { marker: "/*#", url: "trailing.map*", suffix: "*/" }, + { marker: "/*#", url: "**double.map", suffix: " */" }, + { marker: "/*#", url: "plain.map", suffix: "*/" }, + ]; + const directives = cases.map(({ marker, url, suffix, prefix = "" }) => ({ + url, + prefix, + comment: `${marker} sourceMappingURL=${url}`, + suffix, + })); + const source = directives + .map(({ prefix, comment, suffix }) => prefix + comment + suffix) + .join("\n"); + const result = analyzeJavaScriptStaticSource(source); + expect(result.source_map_urls).toEqual( + directives.map(({ url, prefix, comment }, index) => ({ + declared_url: url, + location: { + start: { line: index + 1, column: prefix.length }, + end: { line: index + 1, column: prefix.length + comment.length }, + }, + })), + ); }); it("reads a large inline map from a two-byte source without overflowing", () => { @@ -56,11 +65,8 @@ describe("source map directives", () => { }); it.each([ - "//#", "//# \t", - "//# sourceMappingURL", "//# sourceMappingURL \t", - "//# sourceMappingURL=", "//# sourceMappingURL= \t", "/*# sourceMappingURL= */", "//# sourceMappingURLx=app.js.map", @@ -83,71 +89,33 @@ describe("source map directives", () => { }, ]); }); - - it.each([ - ["/*#", "app*part.js.map", " */"], - ["/*@", "*part.js.map", "*/"], - ["/*#", "app.js.map*", "*/"], - ["/*#", "**part.js.map", " */"], - ["/*#", "app.js.map", "*/"], - ["//#", "app*part.js.map", ""], - ])("preserves the URL and range in %s %s%s", (marker, url, suffix) => { - const directive = marker + " sourceMappingURL=" + url; - const result = analyzeJavaScriptStaticSource( - "export const value = 1;\n" + directive + suffix, - ); - expect(result.source_map_urls).toEqual([ - { - declared_url: url, - location: { - start: { line: 2, column: 0 }, - end: { line: 2, column: directive.length }, - }, - }, - ]); - }); - - it("reports nothing when no directive is present", () => { - expect(sourceMapUrls("const value = 1;")).toEqual([]); - }); }); -const quotedDirectiveSources = [ - 'const documentation = "//# sourceMappingURL=ghost.map ";', - 'const documentation = "/*# sourceMappingURL=ghost.map */";', - "const documentation = `//# sourceMappingURL=ghost.map `;", - "const pattern = /[//# sourceMappingURL=ghost.map ]/;", - "const view =
//# sourceMappingURL=ghost.map
;", - 'const documentation: string = "//# sourceMappingURL=ghost.map ";', - "/* Documentation example: //# sourceMappingURL=ghost.map */", -]; - describe("source map comment boundaries", () => { - it.each(quotedDirectiveSources)( - "does not read a directive from %s", - (source) => { - const result = analyzeJavaScriptStaticSource(source); - expect(result.parse_status).toBe("complete"); - expect(result.source_map_urls).toEqual([]); - }, - ); + it("ignores comment markers in JavaScript literals and ordinary comments", () => { + const source = [ + 'const documentation = "/*# sourceMappingURL=ghost.map */";', + "const template = `//# sourceMappingURL=ghost.map `;", + "const pattern = /[//# sourceMappingURL=ghost.map ]/;", + "/* Documentation example: //# sourceMappingURL=ghost.map */", + ].join("\n"); + const result = analyzeJavaScriptStaticSource(source); + expect(result.parse_status).toBe("complete"); + expect(result.source_map_urls).toEqual([]); + }); - it("retains only the real trailing directive after quoted examples", () => { - expect( - sourceMapUrls( - [ - quotedDirectiveSources[0], - "const template = `/*# sourceMappingURL=template.map */`;", - "//# sourceMappingURL=real.map", - ].join("\n"), - ), - ).toEqual(["real.map"]); + it.each([ + "const view =
//# sourceMappingURL=ghost.map
;", + 'const documentation: string = "//# sourceMappingURL=ghost.map ";', + ])("ignores comment markers inside supported syntax: %s", (source) => { + const result = analyzeJavaScriptStaticSource(source); + expect(result.parse_status).toBe("complete"); + expect(result.source_map_urls).toEqual([]); }); it.each([ "const value: number = 1;\n//# sourceMappingURL=typed.map", "const view =
;\n//# sourceMappingURL=typed.map", - "const value = 1; /*@ sourceMappingURL=typed.map */", "const text = `${(() => { /*# sourceMappingURL=typed.map */ return 1; })()}`;", ])("keeps authentic comments in supported syntax: %s", (source) => { expect(sourceMapUrls(source)).toEqual(["typed.map"]); diff --git a/src/domain/sourceMapEnvelope.test.ts b/src/domain/sourceMapEnvelope.test.ts index 253e57a413..cc15b00377 100644 --- a/src/domain/sourceMapEnvelope.test.ts +++ b/src/domain/sourceMapEnvelope.test.ts @@ -176,7 +176,12 @@ describe("malformed source-map envelope facts", () => { expect( inspectSourceMapEnvelope({ version: 3, - sections: [{ offset: { line: 0, column: 0 }, map: { version: 2 } }], + sections: [ + { + offset: { line: 0, column: 0 }, + map: { version: "3", sections: [] }, + }, + ], }), ).toMatchObject({ status: "malformed", diff --git a/src/javascript/sourceMaps/SourceMapAdmission.test.ts b/src/javascript/sourceMaps/SourceMapAdmission.test.ts index 53424aea3e..8b4e4fbc30 100644 --- a/src/javascript/sourceMaps/SourceMapAdmission.test.ts +++ b/src/javascript/sourceMaps/SourceMapAdmission.test.ts @@ -4,64 +4,8 @@ import { webSourceLocationArgs } from "../../../tests/fixtures/webSourceLocation import { selectedSourceMapLimits } from "../../domain/sourceMapLimits.js"; import { expect, it } from "vitest"; -import { inspectSourceMapEnvelope } from "../../domain/sourceMapEnvelope.js"; -import { inspectSourceMapValue } from "../../domain/sourceMapFormat.js"; import { traceSourceMap } from "./TraceSourceMap.js"; -const leaf = { - version: 3, - sources: ["original.ts"], - sourcesContent: ["hello"], - mappings: "AAAA", -}; -const indexed = (map: unknown) => ({ - version: 3, - sections: [{ offset: { line: 0, column: 0 }, map }], -}); - -it.each([2, null, undefined, "3"])( - "rejects nested indexed version %s consistently", - (version) => { - const map = indexed({ ...indexed(leaf), version }); - expect(inspectSourceMapEnvelope(map)).toMatchObject({ - status: "malformed", - invalid_sections: [{ section_path: [0] }], - }); - for (const profile of [undefined, "browser-collection"] as const) - expect(() => - inspectSourceMapValue(map, profile === undefined ? {} : { profile }), - ).toThrow( - expect.objectContaining({ - failure: expect.objectContaining({ kind: "malformed" }), - }), - ); - expect(() => - traceSourceMap(JSON.stringify(map), "https://example.test/main.js.map", { - line: 1, - column: 0, - }), - ).toThrow( - expect.objectContaining({ - failure: expect.objectContaining({ kind: "malformed" }), - }), - ); - }, -); - -it("admits numeric version 3 through the same consumers", () => { - const map = indexed(indexed(leaf)); - expect(inspectSourceMapEnvelope(map)).toMatchObject({ status: "included" }); - expect( - inspectSourceMapValue(map, { profile: "browser-collection" }).leaves, - ).toHaveLength(1); - expect( - traceSourceMap(JSON.stringify(map), "https://example.test/main.js.map", { - line: 1, - column: 0, - }).matches, - ).toMatchObject([{ section_path: [0, 0], reported_source: "original.ts" }]); -}); - it("recovers the same finite map by raising the exact decoder record admission", () => { const text = JSON.stringify({ version: 3, diff --git a/src/javascript/sourceMaps/SourceMapCommand.test.ts b/src/javascript/sourceMaps/SourceMapCommand.test.ts index 1b93547480..be42fd5ea2 100644 --- a/src/javascript/sourceMaps/SourceMapCommand.test.ts +++ b/src/javascript/sourceMaps/SourceMapCommand.test.ts @@ -4,9 +4,7 @@ import { afterEach, expect, it, onTestFinished, vi } from "vitest"; import * as processOutput from "../../process/ExecFileOutput.js"; import { AnalysisCancelledError, - AnalysisOutputError, AnalysisResourceConstraintError, - AnalysisTimeoutError, } from "../../domain/analysisErrorCore.js"; import { PrivateRuntimeRoot } from "../../process/PrivateRuntimeRoot.js"; import { @@ -60,7 +58,6 @@ const runChild = ( signal: AbortSignal | undefined, script: string, onRuntime?: (path: string) => void, - onLaunch?: () => void, ) => runSourceMapCommand( "{}", @@ -80,45 +77,22 @@ const runChild = ( ...options, arguments: ["-e", script], }); - onLaunch?.(); return child; }, }, unexpectedCleanup, ); -const exitsAfterHoldingOutput = - 'const {spawn}=require("node:child_process"); spawn(process.execPath,["-e","setTimeout(()=>{},400)"],{stdio:["ignore","inherit","inherit"]}); process.stderr.write("physical exit 7"); process.exit(7)'; - -it("preserves Linux process inspection failure and recovery before launching the codec", async () => { - const cause = Object.assign(new Error("ps permission denied"), { - code: "EACCES", +const lateDiagnostic = + 'setTimeout(() => process.stderr.write("late descendant diagnostic"), 400)'; +const exitsAfterHoldingOutput = ` + const { spawn } = require("node:child_process"); + spawn(process.execPath, ["-e", ${JSON.stringify(lateDiagnostic)}], { + stdio: ["ignore", "inherit", "inherit"], }); - const command = vi - .spyOn(processOutput, "execFileOutput") - .mockRejectedValue(cause); - onTestFinished(() => command.mockRestore()); - const host = createSystemProcessOwnershipHost("linux", {}); - const result = await runSourceMapCommand( - "{}", - "/probe/source.map", - undefined, - { - prepareOwnership: () => host.prepare!(), - launcher: async () => { - throw new Error("must not launch"); - }, - }, - unexpectedCleanup, - ); - expect(result.ok).toBe(false); - if (result.ok) throw new Error("expected prerequisite failure"); - expect(result.error).toBeInstanceOf(LinuxOwnedProcessPrerequisiteError); - expect(result.error).toMatchObject({ - cause, - remediationAction: expect.stringContaining("EACCES"), - }); -}); + process.stderr.write("physical exit 7"); + process.exit(7); +`; it("retains a physical process inspection failure when the caller cancels concurrently", async () => { const controller = new AbortController(); @@ -149,6 +123,9 @@ it("retains a physical process inspection failure when the caller cancels concur if (result.ok) throw new Error("expected inspection failure"); expect(result.error).toBeInstanceOf(LinuxOwnedProcessPrerequisiteError); expect(result.error.cause).toBe(physical); + expect(result.error).toMatchObject({ + remediationAction: expect.stringContaining("EACCES"), + }); expect(result.error.secondaryFailures).toHaveLength(1); expect(result.error.secondaryFailures[0]).toMatchObject({ _tag: "AnalysisCancelledError", @@ -156,53 +133,9 @@ it("retains a physical process inspection failure when the caller cancels concur }); }); -it("does not adopt an opaque typed cancellation reason as an ownership failure", async () => { - const controller = new AbortController(); - const reason = new AnalysisOutputError("unrelated", "opaque caller reason"); - const result = await runSourceMapCommand( - "{}", - "/probe/source.map", - { signal: controller.signal }, - { - prepareOwnership: async () => { - controller.abort(reason); - throw reason; - }, - launcher: async () => { - throw new Error("must not launch"); - }, - }, - unexpectedCleanup, - ); - if (result.ok) throw new Error("expected cancellation"); - expect(result.error).toBeInstanceOf(AnalysisCancelledError); - expect(result.error.cause).toBe(reason); -}); - const exitsThenDescendantExceedsStderrBudget = 'const {spawn}=require("node:child_process"); spawn(process.execPath,["-e",`setTimeout(()=>{process.stderr.write("x".repeat(1024*1024+64)); setInterval(()=>{},1000)},25)`],{stdio:["ignore","inherit","inherit"]}); process.stderr.write("physical exit 7"); process.exit(7)'; -it("preserves the exact object reason for an already-aborted caller", async () => { - const reason = { caller: "opaque source-map request" }; - const controller = new AbortController(); - controller.abort(reason); - const result = await runSourceMapCommand( - "{}", - "/probe/source.map", - { signal: controller.signal }, - { - prepareOwnership: async () => {}, - launcher: async () => { - throw new Error("must not launch"); - }, - }, - unexpectedCleanup, - ); - if (result.ok) throw new Error("expected cancellation"); - expect(result.error).toBeInstanceOf(AnalysisCancelledError); - expect(result.error.cause).toBe(reason); -}); - it.each([null, { caller: "late source-map cancellation" }])( "retains a late caller reason %j when runtime cleanup first fails", async (reason) => { @@ -259,30 +192,6 @@ it.each([null, { caller: "late source-map cancellation" }])( }, ); -it("returns completed codec output after successful cleanup", async () => { - const result = await runChild(undefined, completedCodec); - expect(result).toEqual({ ok: true, value: completedReport }); -}); - -it("preserves exact caller reason when cancellation precedes process exit", async () => { - const reason = { caller: "active source-map cancellation" }; - const controller = new AbortController(); - let runtimePath: string | undefined; - const result = await runChild( - controller.signal, - 'process.stdout.write("ready\\n"); setInterval(()=>{},1000)', - (path) => { - runtimePath = path; - }, - () => setTimeout(() => controller.abort(reason), 0), - ); - if (result.ok) throw new Error("expected cancellation"); - expect(result.error).toBeInstanceOf(AnalysisCancelledError); - expect(result.error.cause).toBe(reason); - if (runtimePath === undefined) throw new Error("runtime was not acquired"); - await expect(access(runtimePath)).rejects.toMatchObject({ code: "ENOENT" }); -}); - it("preserves an observed nonzero exit before caller cancellation during output drain", async () => { const reason = { caller: "later than owned exit" }; const controller = new AbortController(); @@ -342,7 +251,7 @@ it("classifies the same nonzero exit without cancellation", async () => { _tag: "ProviderAdapterError", diagnostics: { exit_code: 7, - stderr: "physical exit 7", + stderr: "physical exit 7late descendant diagnostic", output_complete: true, }, }); @@ -435,81 +344,6 @@ it("preserves an observed process-memory limit over the codec exit status", asyn expect(result.error.capturedOutput?.truncated).toBe(true); }); -it("keeps a timeout distinct from caller cancellation", async () => { - vi.useFakeTimers({ - toFake: ["setTimeout", "clearTimeout", "performance"], - }); - let runtimePath: string | undefined; - try { - const result = await runSourceMapCommand( - "{}", - "/probe/source.map", - undefined, - { - prepareOwnership: async () => {}, - createRuntime: async () => { - const runtime = await PrivateRuntimeRoot.create({ - prefix: "rea-source-map-timeout-test-", - }); - runtimePath = runtime.path; - return runtime; - }, - launcher: async () => { - await vi.advanceTimersByTimeAsync( - WEB_SOURCE_MAP_LIMITS.decodeTimeoutMs + 1, - ); - throw new Error("launcher settled after deadline"); - }, - }, - unexpectedCleanup, - ); - if (result.ok) throw new Error("expected timeout"); - expect(result.error).toBeInstanceOf(AnalysisTimeoutError); - if (runtimePath === undefined) throw new Error("runtime was not acquired"); - await expect(access(runtimePath)).rejects.toMatchObject({ code: "ENOENT" }); - } finally { - vi.useRealTimers(); - } -}); - -it("applies a non-default decode deadline and releases its runtime", async () => { - vi.useFakeTimers({ - toFake: ["setTimeout", "clearTimeout", "performance"], - }); - let runtimePath: string | undefined; - try { - const result = await runSourceMapCommand( - "{}", - "/probe/source.map", - undefined, - { - prepareOwnership: async () => {}, - createRuntime: async () => { - const runtime = await PrivateRuntimeRoot.create({ - prefix: "rea-source-map-selected-deadline-", - }); - runtimePath = runtime.path; - return runtime; - }, - launcher: async () => { - await vi.advanceTimersByTimeAsync(251); - throw new Error("launcher settled after deadline"); - }, - }, - unexpectedCleanup, - 250, - ); - if (result.ok) throw new Error("expected timeout"); - expect(result.error).toBeInstanceOf(AnalysisTimeoutError); - expect(result.error.userMessage).toContain("250"); - expect(result.error.userMessage).toContain("decode_timeout_ms"); - if (runtimePath === undefined) throw new Error("runtime was not acquired"); - await expect(access(runtimePath)).rejects.toMatchObject({ code: "ENOENT" }); - } finally { - vi.useRealTimers(); - } -}); - it("retains a physical process admission error alongside cancellation", async () => { const controller = new AbortController(); const reason = { caller: "during admission" }; diff --git a/src/javascript/sourceMaps/SourceMapDecoder.test.ts b/src/javascript/sourceMaps/SourceMapDecoder.test.ts index cf2468c22c..b5b7a40157 100644 --- a/src/javascript/sourceMaps/SourceMapDecoder.test.ts +++ b/src/javascript/sourceMaps/SourceMapDecoder.test.ts @@ -42,7 +42,7 @@ it("retains physical failure and late cancellation through cleanup failure and r const root = await PrivateRuntimeRoot.create({ prefix: "rea-source-map-cleanup-retry-test-", }); - let cleanupAttempts = 0; + let denyNextClose = true; const controller = new AbortController(); const reason = { caller: "after physical exit during final cleanup" }; const dependencies: SourceMapDecoderDependencies = { @@ -50,8 +50,8 @@ it("retains physical failure and late cancellation through cleanup failure and r createRuntime: async () => ({ path: root.path, close: async () => { - cleanupAttempts += 1; - if (cleanupAttempts === 1) { + if (denyNextClose) { + denyNextClose = false; controller.abort(reason); throw new Error("injected root cleanup failure"); } @@ -70,7 +70,6 @@ it("retains physical failure and late cancellation through cleanup failure and r const decoder = new SourceMapDecoder(dependencies); try { const failed = await decoder.trace(input, { signal: controller.signal }); - expect(failed.ok).toBe(false); if (failed.ok) throw new Error("expected physical failure with cleanup failure"); const primary = failed.error.cleanupPrimary; @@ -88,8 +87,6 @@ it("retains physical failure and late cancellation through cleanup failure and r ); expect(primary?.secondaryFailures[0]?.cause).toBe(reason); expect(failed.error.capturedOutput).toEqual(primary?.capturedOutput); - expect(failed.error.cause).toBeInstanceOf(AggregateError); - expect((failed.error.cause as AggregateError).errors[0]).toBe(primary); expect(failed.error.cleanupResources).toEqual([root.path]); expect(failed.error.diagnostics).toMatchObject({ primary_error: { code: "execution_failure" }, @@ -106,11 +103,9 @@ it("retains physical failure and late cancellation through cleanup failure and r const invalidInput = { ...input, text: null } as unknown as typeof input; const retried = await decoder.trace(invalidInput); - expect(retried.ok).toBe(false); if (retried.ok) throw new Error("expected invalid input after cleanup retry"); expect(retried.error._tag).toBe("AnalysisInputError"); - expect(cleanupAttempts).toBe(2); await expect(access(root.path)).rejects.toMatchObject({ code: "ENOENT" }); expect(failed.error.cleanupIncomplete).toBe(false); expect(failed.error.cleanupResources).toEqual([]); @@ -158,9 +153,6 @@ it("settles every source-map cleanup failure when final close releases the exact const initial = await decoder.trace(input); if (initial.ok) throw new Error("expected initial cleanup failure"); expect(initial.error.cleanupPrimary?.cause).toBe(launchCause); - expect((initial.error.cause as AggregateError).errors[0]).toBe( - initial.error.cleanupPrimary, - ); const retry = await decoder.trace(input); if (retry.ok) throw new Error("expected retry cleanup failure"); for (const failure of [initial.error, retry.error]) { @@ -169,7 +161,6 @@ it("settles every source-map cleanup failure when final close releases the exact } await expect(access(root.path)).resolves.toBeUndefined(); await decoder.close(); - expect(attempts).toBe(3); await expect(access(root.path)).rejects.toMatchObject({ code: "ENOENT" }); for (const failure of [initial.error, retry.error]) { expect(failure.cleanupIncomplete).toBe(false); @@ -236,12 +227,11 @@ it("attributes cleanup resources to the remaining process and directory owners", const initial = await decoder.trace(input); if (initial.ok) throw new Error("expected process cleanup failure"); expect(initial.error.cleanupResources).toEqual([runId, root.path]); - expect(rootAttempts).toBe(0); + await expect(access(root.path)).resolves.toBeUndefined(); denyProcessRelease = false; await expect(decoder.close()).rejects.toMatchObject({ cleanupIncomplete: true, }); - expect(rootAttempts).toBe(1); expect(initial.error.cleanupIncomplete).toBe(true); expect(initial.error.cleanupResources).toEqual([root.path]); await expect(access(root.path)).resolves.toBeUndefined(); diff --git a/src/javascript/sourceMaps/TraceSourceMap.test.ts b/src/javascript/sourceMaps/TraceSourceMap.test.ts index c9faadf905..724c8f0ad3 100644 --- a/src/javascript/sourceMaps/TraceSourceMap.test.ts +++ b/src/javascript/sourceMaps/TraceSourceMap.test.ts @@ -1,12 +1,6 @@ -import { createHash } from "node:crypto"; - import { describe, expect, it } from "vitest"; import { traceSourceMap } from "./TraceSourceMap.js"; -import { - inspectSourceMap, - isSourceMapSectionOverlap, -} from "../../domain/sourceMapFormat.js"; -import { ParseFailureError } from "../../domain/parseFailure.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 }; @@ -20,47 +14,6 @@ const leaf = { const trace = (map: unknown, position = point) => traceSourceMap(JSON.stringify(map), url, position); -it("classifies external indexed sections as unsupported with their exact URL", () => { - const externalUrl = "https://app.test/maps/part.map?revision=7"; - try { - inspectSourceMap( - JSON.stringify({ - version: 3, - sections: [{ offset: { line: 0, column: 0 }, url: externalUrl }], - }), - ); - throw new Error("Expected external section rejection"); - } catch (error: unknown) { - expect(error).toBeInstanceOf(ParseFailureError); - expect(error).toMatchObject({ - failure: { - kind: "unsupported", - format: "source-map", - detail: `External source-map section URL is not fetched: ${externalUrl}`, - }, - }); - } -}); - -it.each([ - ["root field", { ...leaf, version: 2 }], - ["leaf field", { ...leaf, mappings: "!" }], - [ - "section field", - { version: 3, sections: [{ offset: { line: "0" }, map: leaf }] }, - ], -])("reports a %s schema rejection as a format failure", (_, map) => { - try { - inspectSourceMap(JSON.stringify(map)); - throw new Error("Expected schema rejection"); - } catch (error: unknown) { - expect(error).toBeInstanceOf(ParseFailureError); - expect(error).toMatchObject({ - failure: { kind: "malformed", format: "source-map" }, - }); - } -}); - it("uses the same conservative section classification for URL-plus-map and malformed URLs", () => { const externalUrl = "https://app.test/maps/part.map"; const external = () => @@ -94,38 +47,6 @@ it("uses the same conservative section classification for URL-plus-map and malfo ); }); -it("rejects a present non-array sections member through the shared section-list rule", () => { - const malformed = () => - inspectSourceMap( - JSON.stringify({ - version: 3, - sections: null, - sources: ["ignored.ts"], - mappings: "AAAA", - }), - ); - expect(malformed).toThrow( - expect.objectContaining({ - failure: expect.objectContaining({ kind: "malformed" }), - message: expect.stringMatching(/sections.*must be an array/iu), - }), - ); -}); - -it("traces BOM-prefixed maps with a source-map transport prefix ending in CR", () => { - const text = `\uFEFF)]}'transport prefix\r${JSON.stringify(leaf)}`; - const report = traceSourceMap(text, url, point); - expect(report).toMatchObject({ - source_map_sha256: createHash("sha256").update(text).digest("hex"), - matches: [ - { - reported_source: "app.ts", - content: { text: "original" }, - }, - ], - }); -}); - it("keeps ignore-list precedence and section-local indexes for duplicate sources", () => { const map = { version: 3, @@ -168,44 +89,32 @@ it("keeps ignore-list precedence and section-local indexes for duplicate sources }); describe("upstream source-map point evidence", () => { - it("keeps repeated separators in distinct resolved source identities", () => { + it("preserves duplicate-column candidates, their URL spelling and their own content", () => { const report = trace({ ...leaf, - sources: ["dir/original.ts", "dir//original.ts"], - sourcesContent: ["single", "repeated"], - mappings: "AAAA,ACAA", + sources: ["dir/same.ts", "dir/same.ts", "dir//same.ts"], + sourcesContent: ["first", "second", "repeated separator"], + mappings: "AAAA,ACAA,ACAA", }); - expect( - report.matches.map((match) => - match.state === "mapped" ? match.resolved_url : null, - ), - ).toEqual([ - "https://app.test/maps/dir/original.ts", - "https://app.test/maps/dir//original.ts", - ]); - }); - - it("preserves every duplicate-column/source-URL candidate and its own embedded bytes", () => { - const report = trace({ - ...leaf, - sources: ["same.ts", "same.ts"], - sourcesContent: ["first", "second"], - mappings: "AAAA,ACAA", - }); - expect(report.matches).toHaveLength(2); expect(report.matches).toMatchObject([ { flattened_source_index: 0, source_index: 0, - resolved_url: "https://app.test/maps/same.ts", + resolved_url: "https://app.test/maps/dir/same.ts", content: { text: "first" }, }, { flattened_source_index: 1, source_index: 1, - resolved_url: "https://app.test/maps/same.ts", + resolved_url: "https://app.test/maps/dir/same.ts", content: { text: "second" }, }, + { + flattened_source_index: 2, + source_index: 2, + resolved_url: "https://app.test/maps/dir//same.ts", + content: { text: "repeated separator" }, + }, ]); }); it("keeps sourceRoot, query/fragment identity and decoded-text UTF-8 digest", () => { @@ -231,20 +140,16 @@ describe("upstream source-map point evidence", () => { content: { utf8_bytes: 8 }, }); }); - it("retains generated-only mappings and no-match positions explicitly", () => { - expect( - trace({ ...leaf, mappings: "AAAA,K" }, { line: 1, column: 5 }).matches, - ).toEqual([{ state: "generated-only" }]); - expect(trace({ ...leaf, mappings: "KAAA" }).matches).toEqual([]); - expect( - trace({ ...leaf, mappings: "" }).lookup.matched_generated_position, - ).toBeNull(); - }); - it("uses cross-line greatest-position lookup without inventing a mapping", () => { - expect( - trace({ ...leaf, mappings: "AAAA;;" }, { line: 3, column: 2 }).lookup - .matched_generated_position, - ).toEqual({ line: 1, column: 0 }); + it("distinguishes no match from a generated-only match carried across empty lines", () => { + const map = { ...leaf, mappings: "CAAA,I;;" }; + expect(trace(map)).toMatchObject({ + matches: [], + lookup: { matched_generated_position: null }, + }); + expect(trace(map, { line: 3, column: 2 })).toMatchObject({ + matches: [{ state: "generated-only" }], + lookup: { matched_generated_position: { line: 1, column: 5 } }, + }); }); it("preserves unknown source identity while retaining embedded content", () => { expect(trace({ ...leaf, sources: [null] }).matches[0]).toMatchObject({ @@ -325,14 +230,6 @@ describe("source-map layout admission", () => { it("validates regular-map source indexes after reusing its decoded rows", () => { expect(() => trace({ ...leaf, mappings: "ACAA" })).toThrow(/index/u); }); - it("rejects huge indexed offsets before upstream row allocation", () => { - expect(() => - trace({ - version: 3, - sections: [{ offset: { line: 1000000000, column: 0 }, map: leaf }], - }), - ).toThrow(/decoder budget/u); - }); it("reports the actual malformed section offset constraint", () => { expect(() => trace({ @@ -341,25 +238,6 @@ describe("source-map layout admission", () => { }), ).toThrow(/offset/u); }); - it("rejects overlapping section mappings rather than silently clipping evidence", () => { - let overlap: unknown; - try { - trace({ - version: 3, - sections: [ - { - offset: { line: 0, column: 0 }, - map: { ...leaf, mappings: "AAAA,UAAA" }, - }, - { offset: { line: 0, column: 5 }, map: leaf }, - ], - }); - } catch (cause) { - overlap = cause; - } - expect(isSourceMapSectionOverlap(overlap)).toBe(true); - expect(overlap).toMatchObject({ failure: { kind: "malformed" } }); - }); it("accepts absent optional names and null contents without changing reported identity", () => { expect( trace({ diff --git a/tests/acceptance/applications/blockSourceMapCli.test.ts b/tests/acceptance/applications/blockSourceMapCli.test.ts index 4e7cb7e115..231f27c95c 100644 --- a/tests/acceptance/applications/blockSourceMapCli.test.ts +++ b/tests/acceptance/applications/blockSourceMapCli.test.ts @@ -1,41 +1,37 @@ import { createHash } from "node:crypto"; -import { mkdir, readFile, writeFile } from "node:fs/promises"; -import { join, sep } from "node:path"; +import { writeFile } from "node:fs/promises"; +import { join } from "node:path"; -import { createPackageWithOptions, listPackage } from "@electron/asar"; import { expect } from "vitest"; import { z } from "zod"; import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; +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 }) => { const root = await createTestTempDirectory("rea-block-map-"); - const directory = join(root, "input"); - await mkdir(directory); const url = "app*part.js.map"; const directive = "/*# sourceMappingURL=" + url; - await writeFile(join(directory, "app.js"), directive + "*/"); - await writeFile( - join(directory, "app_part.js.map"), + const script = Buffer.from(directive + "*/"); + const mapText = Buffer.from( JSON.stringify({ version: 3, sources: [], names: [], mappings: "" }), ); const archive = join(root, "app.asar"); - await createPackageWithOptions(directory, archive, {}); - // ASAR logical names can contain '*' even on Windows. Rename this header - // key without changing entry bytes, sizes or payload offsets. - const bytes = await readFile(archive); - const key = Buffer.from('"app_part.js.map"'); - const offset = bytes.indexOf(key); - expect(offset).toBeGreaterThan(0); - expect(bytes.indexOf(key, offset + key.length)).toBe(-1); - Buffer.from('"' + url + '"').copy(bytes, offset); - await writeFile(archive, bytes); - expect(listPackage(archive, { isPack: false })).toContain(sep + url); + // ASAR logical names can contain '*' even when the host disallows it. + const header = asarHeaderBytes( + JSON.stringify({ + files: { + "app.js": { size: script.length, offset: "0" }, + [url]: { size: mapText.length, offset: String(script.length) }, + }, + }), + ); + await writeFile(archive, Buffer.concat([header, script, mapText])); let document: unknown; if (surface === "CLI") { const result = await cli.run({ @@ -44,7 +40,7 @@ cliTest.for(["CLI", "stdio MCP"] as const)( expect(result.exitCode, result.stderr).toBe(0); document = result.json; } else { - const { call } = await connectLocalToolsMcp(); + const call = await resolveJavaScriptApplicationMcpCall(cli, undefined); const result = await call("analyze_javascript_application", { input_path: archive, format: "asar", @@ -63,11 +59,9 @@ cliTest.for(["CLI", "stdio MCP"] as const)( const map = graph.nodes.find(({ observations }) => observations.some(({ properties }) => properties.declared_url === url), ); - expect( - map, - "the complete declared URL must identify the map", - ).toBeDefined(); - expect(map?.observations).toContainEqual( + if (map === undefined) + throw new Error("Expected the complete declared URL to identify the map"); + expect(map.observations).toContainEqual( expect.objectContaining({ properties: { declared_url: url, @@ -92,7 +86,7 @@ cliTest.for(["CLI", "stdio MCP"] as const)( expect(graph.edges).toContainEqual( expect.objectContaining({ relation: "maps_to", - target_node_id: map?.node_id, + target_node_id: map.node_id, properties: { declared_url_sha256: digest, resolved_path: url, diff --git a/tests/acceptance/applications/sourceMapPrefixCli.test.ts b/tests/acceptance/applications/sourceMapPrefixCli.test.ts index de87f16265..4d7adfe1a3 100644 --- a/tests/acceptance/applications/sourceMapPrefixCli.test.ts +++ b/tests/acceptance/applications/sourceMapPrefixCli.test.ts @@ -2,105 +2,12 @@ import { createHash } from "node:crypto"; import { readFile, writeFile } from "node:fs/promises"; import { join } from "node:path"; -import { TraceMap, originalPositionFor } from "@jridgewell/trace-mapping"; import { expect } from "vitest"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js"; import { writeFixtureFiles } from "../../support/javascriptApplicationFixture.js"; -cliTest.for(["CLI", "stdio MCP"] as const)( - "keeps missing source-map mappings partial while retaining explicit declarations through %s", - async (surface, { cli }) => { - const root = await createTestTempDirectory("rea-source-map-mappings-"); - const original = "export const answer: number = 42;"; - const cases = [ - { - directory: "missing", - map: { - version: 3, - sources: ["original.ts"], - sourcesContent: [original], - names: [], - }, - expectedCoverage: "partial", - limitation: "Source map has no mappings string.", - }, - { - directory: "empty", - map: { - version: 3, - sources: ["original.ts"], - sourcesContent: [original], - names: [], - mappings: "", - }, - expectedCoverage: "complete", - limitation: null, - }, - { - directory: "mapped", - map: { - version: 3, - sources: ["original.ts"], - sourcesContent: [original], - names: [], - mappings: "AAAA", - }, - expectedCoverage: "complete", - limitation: null, - }, - ] as const; - for (const { directory, map, expectedCoverage, limitation } of cases) { - const app = join(root, directory); - const mapText = JSON.stringify(map); - await writeFixtureFiles(app, { "app.js.map": mapText }); - const { graph } = await analyzeJavaScriptDirectory(surface, cli, app); - const mapDigest = createHash("sha256").update(mapText).digest("hex"); - const source = graph.nodes.find( - (node) => - node.kind === "source-module" && - node.identity.strategy === "source-map-original" && - node.identity.source_map_sha256 === mapDigest, - ); - - expect(graph.coverage.status).toBe(expectedCoverage); - expect(source).toMatchObject({ - identity: { - strategy: "source-map-original", - original_source: "original.ts", - source_sha256: createHash("sha256").update(original).digest("hex"), - }, - observations: [ - expect.objectContaining({ - evidence: expect.objectContaining({ - artifact: expect.objectContaining({ sha256: mapDigest }), - limitations: limitation === null ? [] : [limitation], - }), - }), - ], - }); - if (limitation !== null) - expect(graph.nodes).toContainEqual( - expect.objectContaining({ - kind: "unknown", - observations: expect.arrayContaining([ - expect.objectContaining({ - properties: expect.objectContaining({ - operation: "parse-local-source-map", - }), - evidence: expect.objectContaining({ - artifact: expect.objectContaining({ sha256: mapDigest }), - limitations: [limitation], - }), - }), - ]), - }), - ); - } - }, -); - cliTest.for(["CLI", "stdio MCP"] as const)( "recovers retained prefixed source-map originals and byte identity through %s", async (surface, { cli }) => { @@ -195,17 +102,6 @@ cliTest.for(["CLI", "stdio MCP"] as const)( "unnamed-empty.js": "export const answer = 42;\n//# sourceMappingURL=unnamed-empty.js.map\n", }); - for (const [mapPath, expectedSource] of [ - ["absent.js.map", "unembedded.ts"], - ["mixed.js.map", "original.ts"], - ["unnamed-empty.js.map", "original.ts"], - ] as const) - expect( - originalPositionFor(new TraceMap(maps[mapPath], `file:///${mapPath}`), { - line: 1, - column: 0, - }).source, - ).toBe(`file:///${expectedSource}`); const { graph } = await analyzeJavaScriptDirectory(surface, cli, root); const digest = (text: string) => createHash("sha256").update(text).digest("hex"); @@ -281,143 +177,6 @@ cliTest.for(["CLI", "stdio MCP"] as const)( }, ); -cliTest.for(["CLI", "stdio MCP"] as const)( - "retains valid declarations beside non-string source-map names through %s", - async (surface, { cli }) => { - const root = await createTestTempDirectory("rea-invalid-map-source-"); - const mapText = JSON.stringify({ - version: 3, - sources: [42, "discarded.ts"], - sourcesContent: [null, "export const answer: number = 42;"], - names: [], - mappings: "ACAA", - }); - await writeFixtureFiles(root, { "invalid.js.map": mapText }); - const { graph } = await analyzeJavaScriptDirectory(surface, cli, root); - expect(graph.coverage.status).toBe("partial"); - const mapDigest = createHash("sha256").update(mapText).digest("hex"); - expect(graph.nodes).toContainEqual( - expect.objectContaining({ - kind: "source-module", - identity: expect.objectContaining({ - strategy: "source-map-original", - source_map_sha256: mapDigest, - original_source: "discarded.ts", - source_sha256: createHash("sha256") - .update("export const answer: number = 42;") - .digest("hex"), - }), - observations: expect.arrayContaining([ - expect.objectContaining({ - properties: expect.objectContaining({ - section_path: [], - source_index: 1, - declaration_index: 0, - }), - }), - ]), - }), - ); - expect(graph.nodes).toContainEqual( - expect.objectContaining({ - kind: "unknown", - observations: expect.arrayContaining([ - expect.objectContaining({ - properties: expect.objectContaining({ - operation: "parse-local-source-map", - }), - evidence: expect.objectContaining({ - artifact: expect.objectContaining({ - sha256: mapDigest, - }), - limitations: expect.arrayContaining([ - "Source map contains a non-string source name at index 0.", - ]), - }), - }), - ]), - }), - ); - expect(await readFile(join(root, "invalid.js.map"), "utf8")).toBe(mapText); - }, -); - -cliTest.for(["CLI", "stdio MCP"] as const)( - "keeps external indexed sections unknown while retaining embedded siblings through %s", - async (surface, { cli }) => { - const root = await createTestTempDirectory("rea-external-indexed-map-"); - const url = "https://app.test/maps/part.map?revision=7"; - const mapText = JSON.stringify({ - 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 }, - map: { - version: 3, - sources: ["after.ts"], - sourcesContent: ["export const after = 2;"], - names: [], - mappings: "AAAA", - }, - }, - ], - }); - await writeFixtureFiles(root, { "indexed.js.map": mapText }); - const { graph } = await analyzeJavaScriptDirectory(surface, cli, root); - const mapDigest = createHash("sha256").update(mapText).digest("hex"); - - expect(graph.coverage.status).toBe("partial"); - expect(graph.nodes).toContainEqual( - expect.objectContaining({ - kind: "unknown", - observations: expect.arrayContaining([ - expect.objectContaining({ - properties: expect.objectContaining({ - path: "indexed.js.map", - operation: "parse-local-source-map", - limitation: `External source-map section URL is not fetched: ${url}`, - external_sections: [{ section_path: [1], url }], - }), - evidence: expect.objectContaining({ - artifact: expect.objectContaining({ sha256: mapDigest }), - limitations: [ - `External source-map section URL is not fetched: ${url}`, - ], - }), - }), - ]), - }), - ); - for (const [source, content] of [ - ["before.ts", "export const before = 1;"], - ["after.ts", "export const after = 2;"], - ] as const) - expect(graph.nodes).toContainEqual( - expect.objectContaining({ - kind: "source-module", - identity: expect.objectContaining({ - strategy: "source-map-original", - source_map_sha256: mapDigest, - original_source: source, - source_sha256: createHash("sha256").update(content).digest("hex"), - }), - }), - ); - expect(await readFile(join(root, "indexed.js.map"), "utf8")).toBe(mapText); - }, -); - cliTest.for(["CLI", "stdio MCP"] as const)( "preserves source-root declarations and URL path identity through %s", async (surface, { cli }) => { diff --git a/tests/acceptance/browser/browserSourceMapCancellationCli.test.ts b/tests/acceptance/browser/browserSourceMapCancellationCli.test.ts index 3055f77ee6..a147dc897c 100644 --- a/tests/acceptance/browser/browserSourceMapCancellationCli.test.ts +++ b/tests/acceptance/browser/browserSourceMapCancellationCli.test.ts @@ -90,7 +90,6 @@ processTest.skipIf(process.platform === "win32")( }, ], }); - expect(acquisition.source_maps.items).toHaveLength(1); expect(acquisition.inspection.completeness.excluded).toContainEqual({ section: "source_maps", reason: "disallowed_origin", diff --git a/tests/boundary/browser/sourceMapCaptureExclusions.test.ts b/tests/boundary/browser/sourceMapCaptureExclusions.test.ts index 22a74a7301..bd0e82e951 100644 --- a/tests/boundary/browser/sourceMapCaptureExclusions.test.ts +++ b/tests/boundary/browser/sourceMapCaptureExclusions.test.ts @@ -46,7 +46,7 @@ it.each(["none", "excluded", "mixed", "redirect", "not-selected"] as const)( "main", mode === "none" ? undefined - : mode === "redirect" + : mode === "redirect" || mode === "not-selected" ? "/app.js.map" : "https://outside.example.test/main.js.map", ), diff --git a/tests/boundary/browser/webSourceMapFetcher.test.ts b/tests/boundary/browser/webSourceMapFetcher.test.ts index b87a5dc388..709b1ef246 100644 --- a/tests/boundary/browser/webSourceMapFetcher.test.ts +++ b/tests/boundary/browser/webSourceMapFetcher.test.ts @@ -267,15 +267,13 @@ it("parses ECMA-426 HTTP source-map prefixes while retaining exact response evid mappings: "AAAA", }); const prefix = ")]}'transport prefix"; - const texts = [ - map, + const included = [ `\uFEFF${prefix}\n${map}`, - `${prefix}\n${map}`, `${prefix}\r\n${map}`, `${prefix}\r${map}`, - `${prefix}${map}`, - `${prefix}\n{`, ]; + const invalid = [`${prefix}${map}`, `\uFEFF${prefix}\n{`]; + const texts = [...included, ...invalid]; const server = createServer((incoming, response) => { const index = Number(incoming.url?.slice(1)); response.setHeader("content-type", "application/source-map+json"); @@ -299,15 +297,13 @@ it("parses ECMA-426 HTTP source-map prefixes while retaining exact response evid input({ allowed_origins: [localOrigin] }), ); expect(result.items.map(({ status }) => status)).toEqual([ - "included", - "included", "included", "included", "included", "invalid", "invalid", ]); - for (const [index, text] of texts.slice(0, 5).entries()) + for (const [index, text] of included.entries()) expect(result.items[index]).toMatchObject({ artifact: { text, @@ -332,11 +328,18 @@ it("parses ECMA-426 HTTP source-map prefixes while retaining exact response evid }, ], }); - expect(result.items.slice(5)).toMatchObject([ - { artifact: null, mappings: [], original_sources: [] }, - { artifact: null, mappings: [], original_sources: [] }, - ]); - expect(webSourceMapsSchema.parse(result)).toEqual(result); + expect(result.items.slice(included.length)).toMatchObject( + invalid.map((text) => ({ + artifact: null, + mappings: [], + original_sources: [], + response_identity: { + bytes: Buffer.byteLength(text), + sha256: createHash("sha256").update(text).digest("hex"), + representation: "fetch-response-body-bytes", + }, + })), + ); const without = (value: object, omit: string) => Object.fromEntries(Object.entries(value).filter(([key]) => key !== omit)); for (const omit of ["source_index", "declaration_index"]) @@ -443,41 +446,6 @@ describe("source-map redirect URL resolution", () => { }, ); - it.each([ - [404, "unavailable", "fetch_failed"], - [200, "not-json", "invalid"], - ])( - "retains the delivered URL when its HTTP %s response cannot supply a map", - async (responseStatus, body, itemStatus) => { - const effectiveUrl = `${origin}${deliveredPath}`; - const calls: string[] = []; - const result = await fetchWebSourceMapsOk([request], input(), undefined, { - fetch: (url) => { - calls.push(String(url)); - return Promise.resolve( - calls.length === 1 - ? new Response(null, { - status: 302, - headers: { location: effectiveUrl }, - }) - : new Response(body, { status: responseStatus }), - ); - }, - }); - expect(calls).toEqual([request.fetchUrl, effectiveUrl]); - expect(result.items[0]).toMatchObject({ - status: itemStatus, - declared_url: request.declaredUrl, - effective_url: effectiveUrl, - artifact: null, - limitation: - responseStatus === 200 - ? "Source-map JSON is not a version 3 map. Response text was not retained; completed response body identity is available." - : "Source-map server returned HTTP 404.", - }); - }, - ); - it("retains the last observed response URL when the next redirect fetch rejects", async () => { const nextUrl = `${origin}${deliveredPath}`; const calls: string[] = []; @@ -503,16 +471,20 @@ describe("source-map redirect URL resolution", () => { }); }); - it("reports an absent effective URL when the first fetch rejects", async () => { + it("represents the effective URL as null when no response was observed", async () => { const result = await fetchWebSourceMapsOk([request], input(), undefined, { fetch: () => Promise.reject(new TypeError("fixture connection failed")), }); - expect(result.items[0]).toMatchObject({ + const item = result.items[0]; + if (item === undefined) throw new Error("Expected a fetch failure"); + expect(item).toMatchObject({ status: "fetch_failed", declared_url: request.declaredUrl, effective_url: null, artifact: null, }); + const { effective_url: _effective, ...withoutEffectiveUrl } = item; + expectInvalidSourceMaps({ ...result, items: [withoutEffectiveUrl] }); }); }); @@ -1099,44 +1071,35 @@ it("shares the caller's fetch byte budget across completed map responses", async expect(result.items[1]?.limitation).toContain("retained budget"); }); -describe("indexed source-map offsets", () => { - it.each([ - { line: 0, column: -1 }, - { line: -1, column: 0 }, - { line: 0, column: "1" }, - { line: "0", column: 0 }, - { line: 0, column: 0.5 }, - { line: 0.5, column: 0 }, - { line: 0 }, - { line: 0, column: Number.MAX_SAFE_INTEGER + 1 }, - ])("retains an invalid-map item for malformed offset %j", async (offset) => { - const result = await fetchWebSourceMapsOk([request], input(), undefined, { - fetch: () => - Promise.resolve( - new Response( - JSON.stringify({ - version: 3, - sections: [ - { - offset, - map: { - version: 3, - names: [], - sources: ["original.js"], - sourcesContent: ["export const stable = 1;"], - mappings: "AAAA", - }, - }, - ], - }), - { status: 200 }, - ), - ), - }); - expect(result).toMatchObject({ - status: "unavailable", - items: [{ status: "invalid", artifact: null, mappings: [] }], - }); +it.each([ + { line: 0, column: -1 }, + { line: -1, column: 0 }, + { line: "0", column: 0 }, + { line: 0, column: 0.5 }, + { line: 0 }, + { line: 0, column: Number.MAX_SAFE_INTEGER + 1 }, +])("retains an invalid-map item for malformed offset %j", async (offset) => { + const text = JSON.stringify({ + version: 3, + sections: [ + { + offset, + map: { + version: 3, + sources: ["original.js"], + names: [], + mappings: "AAAA", + }, + }, + ], + }); + const result = await fetchWebSourceMapsOk([request], input(), undefined, { + fetch: async () => new Response(text), + }); + + expect(result).toMatchObject({ + status: "unavailable", + items: [{ status: "invalid", artifact: null, mappings: [] }], }); }); @@ -1295,71 +1258,6 @@ describe("web source-map sourcesContent validation", () => { }); }); -describe("source-map original dependency syntax", () => { - it("does not invent edges from comments or string contents", async () => { - const text = [ - '// import "./comment.js";', - String.raw`const quoted = "require(\"./string.js\")";`, - `export { value } from "./it's-real.js";`, - "const template = `import './template-comment.js'`;", - 'import("./dynamic.js");', - 'require("./common.cjs");', - ].join("\n"); - const result = await fetchWebSourceMapsOk([request], input(), undefined, { - fetch: () => - Promise.resolve( - new Response( - JSON.stringify({ - version: 3, - names: [], - sources: ["main.ts"], - sourcesContent: [text], - mappings: "AAAA", - }), - ), - ), - }); - expect( - result.items[0]?.original_module_edges.map(({ specifier }) => specifier), - ).toEqual(["./it's-real.js", "./dynamic.js", "./common.cjs"]); - }); - - it("reads expression-free template module specifiers as literals", async () => { - const text = [ - "import(`./dep.js`);", - "import(`./dep.${name}.js`);", - "require(`./common.cjs`);", - "require(`./${name}.cjs`);", - ].join("\n"); - const result = await fetchWebSourceMapsOk([request], input(), undefined, { - fetch: () => - Promise.resolve( - new Response( - JSON.stringify({ - version: 3, - names: [], - sources: ["main.ts"], - sourcesContent: [text], - mappings: "AAAA", - }), - ), - ), - }); - const item = result.items[0]; - if (item?.status !== "included") throw new Error("map not included"); - expect(item.original_module_edges).toEqual([ - expect.objectContaining({ - kind: "dynamic_import", - specifier: "./dep.js", - resolved_source: `${origin}/assets/dep.js`, - }), - expect.objectContaining({ kind: "require", specifier: "./common.cjs" }), - ]); - expect(item.original_sources[0]?.artifact?.text).toBe(text); - expect(item.mappings).toHaveLength(1); - }); -}); - describe("source-map dependency coverage of hard-to-parse sources", () => { const fetchWithSources = (sources: readonly string[]) => fetchWebSourceMapsOk([request], input(), undefined, { @@ -1426,34 +1324,6 @@ describe("source-map dependency coverage of hard-to-parse sources", () => { ).toEqual(expected); }); - it("selects parser mode from the original source URL pathname", async () => { - const result = await fetchWebSourceMapsOk([request], input(), undefined, { - fetch: () => - Promise.resolve( - new Response( - JSON.stringify({ - version: 3, - names: [], - sources: ["main.ts?revision=2#view.tsx"], - sourcesContent: [`import "./dep.ts"; const value = 1;`], - mappings: "AAAA", - }), - ), - ), - }); - expect(result.items[0]?.status).toBe("included"); - expect(result.items[0]?.limitation).toBeNull(); - expect(result.items[0]?.original_sources[0]?.source).toBe( - `${origin}/assets/main.ts?revision=2#view.tsx`, - ); - expect(result.items[0]?.original_module_edges).toEqual([ - expect.objectContaining({ - specifier: "./dep.ts", - resolved_source: `${origin}/assets/dep.ts`, - }), - ]); - }); - 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";', @@ -1480,49 +1350,25 @@ describe("source-map dependency coverage of hard-to-parse sources", () => { }); describe("web source-map collection", () => { - it("rejects mapping expansion before the trace-mapping decoder allocates it", async () => { + it("rejects a map above the default decoded segment limit", async () => { const mapText = JSON.stringify({ version: 3, names: [], sources: [], mappings: Array.from({ length: 262_145 }, () => "A").join(","), }); - const response = new Response(mapText, { status: 200 }); const result = await fetchWebSourceMapsOk([request], input(), undefined, { - fetch: () => Promise.resolve(response), + fetch: async () => new Response(mapText), }); expect(result.status).toBe("unavailable"); expect(result.items[0]).toMatchObject({ status: "fetch_failed", artifact: null, + limitation: expect.stringContaining( + "source_map_limits.decoded_segments is 262144", + ), }); - expect(result.items[0]?.limitation).toContain("262144"); - }); - - it("accounts for decoded records across the complete fetch operation", async () => { - const mapText = JSON.stringify({ - version: 3, - names: [], - sources: [], - mappings: Array.from({ length: 150_000 }, () => "A").join(","), - }); - const requests = [ - request, - { ...request, scriptKey: `scr_${"2".repeat(64)}` }, - ]; - const result = await fetchWebSourceMapsOk(requests, input(), undefined, { - fetch: () => Promise.resolve(new Response(mapText, { status: 200 })), - }); - - expect(result.status).toBe("partial"); - expect(result.items.map(({ status }) => status)).toEqual([ - "included", - "fetch_failed", - ]); - expect(result.items[1]?.limitation).toContain( - "source_map_limits.decoded_records is 262144", - ); }); it("rejects deeply nested indexed maps before recursive flattening", async () => { @@ -1626,127 +1472,180 @@ describe("web source-map collection", () => { }); }); -describe("source-map output admission", () => { - it.each([ - { selectionMiB: 8, responseStatus: "invalid" }, - { selectionMiB: 16, responseStatus: "invalid" }, - { selectionMiB: 16, responseStatus: "included" }, - ])( - "accounts for JSON escaping in a $selectionMiB MiB effective URL for $responseStatus responses", - async ({ selectionMiB, responseStatus }) => { - const effectiveUrl = `${origin}/delivered.map?selection=${"\\".repeat(selectionMiB * 1_024 * 1_024)}`; - const secondRequest = { ...request, scriptKey: `scr_${"2".repeat(64)}` }; - let calls = 0; - const result = await fetchWebSourceMapsOk( - [request, secondRequest], - input(), - undefined, - { - fetch: () => { - calls += 1; - return Promise.resolve( - calls === 1 - ? validMapResponse() - : calls === 2 - ? new Response(null, { - status: 302, - headers: { location: effectiveUrl }, - }) - : responseStatus === "included" - ? validMapResponse() - : new Response("not-json", { status: 200 }), - ); - }, +it("shares the selected decoded record limit across maps", async () => { + // Each map consumes one structural record, two rows and two segments. + const mapText = JSON.stringify({ + version: 3, + names: [], + sources: [], + mappings: "A;A", + }); + const second = { ...request, scriptKey: `scr_${"2".repeat(64)}` }; + const fetchMaps = (decodedRecords: number) => + fetchWebSourceMapsOk( + [request, second], + input({ source_map_limits: { decoded_records: decodedRecords } }), + undefined, + { fetch: async () => new Response(mapText) }, + ); + + expect(await fetchMaps(10)).toMatchObject({ + status: "included", + requested: 2, + processed: 2, + items: [ + { script_key: request.scriptKey, status: "included" }, + { script_key: second.scriptKey, status: "included" }, + ], + }); + expect(await fetchMaps(9)).toMatchObject({ + status: "partial", + requested: 2, + processed: 2, + items: [ + { script_key: request.scriptKey, status: "included" }, + { + script_key: second.scriptKey, + status: "fetch_failed", + limitation: expect.stringContaining( + "source_map_limits.decoded_records is 9", + ), + }, + ], + }); +}); + +it("charges the escaped effective URL before reading the redirected response", async () => { + const effectiveUrl = `${origin}/delivered.map?selection=${"\\".repeat(6_000)}`; + const second = { + scriptKey: `scr_${"2".repeat(64)}`, + declaredUrl: `${origin}/redirect.map`, + fetchUrl: `${origin}/redirect.map`, + }; + const fetchMaps = async (outputBytes: number) => { + let read = false; + let cancelled = false; + const result = await fetchWebSourceMapsOk( + [request, second], + input({ source_map_limits: { output_bytes: outputBytes } }), + undefined, + { + fetch: async (url) => { + if (String(url) === request.fetchUrl) return validMapResponse(); + if (String(url) === second.fetchUrl) + return new Response(null, { + status: 302, + headers: { location: effectiveUrl }, + }); + expect(String(url)).toBe(effectiveUrl); + const body = new ReadableStream( + { + pull(controller) { + read = true; + controller.enqueue(Buffer.from("not-json")); + controller.close(); + }, + cancel() { + cancelled = true; + }, + }, + { highWaterMark: 0 }, + ); + return new Response(body); }, - ); - expect(calls).toBe(3); - expect(result).toMatchObject({ - status: "partial", - requested: 2, - processed: selectionMiB === 8 ? 2 : 1, - }); - if (selectionMiB === 8) { - expect(result.items[1]).toMatchObject({ - status: "invalid", - declared_url: secondRequest.declaredUrl, - }); - expect(result.items[1]?.effective_url === effectiveUrl).toBe(true); - } else { - expect(result.items).toHaveLength(1); - expect(result.limitation).toContain("1 requested map"); - } - expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual( - 32 * 1_024 * 1_024, - ); - }, + }, + ); + return { result, read, cancelled }; + }; + + const admitted = await fetchMaps(16_000); + expect(admitted.result).toMatchObject({ + status: "partial", + requested: 2, + processed: 2, + items: [ + { script_key: request.scriptKey, status: "included" }, + { + script_key: second.scriptKey, + status: "invalid", + declared_url: second.declaredUrl, + effective_url: effectiveUrl, + }, + ], + }); + expect(admitted.read).toBe(true); + + const budget = 10_000; + expect(Buffer.byteLength(effectiveUrl)).toBeLessThan(budget); + expect(Buffer.byteLength(JSON.stringify(effectiveUrl))).toBeGreaterThan( + budget, ); + const excluded = await fetchMaps(budget); + expect(excluded.result).toMatchObject({ + status: "partial", + requested: 2, + processed: 1, + items: [{ script_key: request.scriptKey, status: "included" }], + limitation: expect.stringContaining("1 requested map"), + }); + expect(excluded.cancelled).toBe(true); + expect(excluded.read).toBe(false); }); describe("source-map set output admission", () => { - it("admits a maximum-size map set according to its serialized output size", async () => { - const localOrigin = "http://127.0.0.1:9222"; - const mapUrl = `${localOrigin}/m`; - const requests = Array.from({ length: 50_000 }, (_, index) => ({ - ...request, - scriptKey: `scr_${index.toString(16).padStart(64, "0")}`, - declaredUrl: mapUrl, - fetchUrl: mapUrl, + it("retains only the maps that fit the selected aggregate output budget", async () => { + const requests = ["first", "second", "third"].map((name, index) => ({ + scriptKey: `scr_${String(index + 1).repeat(64)}`, + declaredUrl: `${origin}/${name}.map${ + name === "third" ? `?revision=${"r".repeat(3_000)}` : "" + }`, + fetchUrl: `${origin}/${name}.map`, })); const map = JSON.stringify({ version: 3, names: [], - sources: ["x"], + sources: ["source.ts"], + sourcesContent: [`// ${"x".repeat(4096)}`], mappings: "AAAA", }); - const result = await fetchWebSourceMapsOk( - requests, - input({ - allowed_origins: [localOrigin], - source_map_fetch_timeout_ms: 120_000, - }), - undefined, - { - fetch: () => Promise.resolve(new Response(map, { status: 200 })), - }, - ); - - const outputLimitBytes = 32 * 1_024 * 1_024; - const itemSizes = new Set( - result.items.map((item) => Buffer.byteLength(JSON.stringify(item))), - ); - expect(itemSizes.size).toBe(1); - const firstItem = result.items[0]; - if (firstItem === undefined) throw new Error("no source map was admitted"); - const itemBytes = Buffer.byteLength(JSON.stringify(firstItem)); - const completeEnvelope = JSON.stringify({ - ...result, - status: "included", - processed: requests.length, - items: [], - limitation: undefined, - }); - const projectedCompleteBytes = - Buffer.byteLength(completeEnvelope) + - itemBytes * requests.length + - requests.length - - 1; - const completeOutputFits = projectedCompleteBytes <= outputLimitBytes; - - expect(result.status).toBe(completeOutputFits ? "included" : "partial"); - if (completeOutputFits) { - expect(result.limitation).toBeUndefined(); - expect(result.items).toHaveLength(requests.length); - } else { - expect(result.items.length).toBeLessThan(requests.length); - expect(result.limitation).toContain( - `${String(requests.length - result.items.length)} requested maps`, + const fetchMaps = (outputBytes: number) => + fetchWebSourceMapsOk( + requests, + input({ source_map_limits: { output_bytes: outputBytes } }), + undefined, + { fetch: async () => new Response(map) }, ); - expect(projectedCompleteBytes).toBeGreaterThan(outputLimitBytes); - } - expect(result.processed).toBe(result.items.length); - expect(result.requested).toBe(requests.length); - expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual( - outputLimitBytes, + + const complete = await fetchMaps(32_000); + const includedItems = requests.map(({ scriptKey, declaredUrl }) => ({ + script_key: scriptKey, + declared_url: declaredUrl, + status: "included", + })); + expect(complete).toMatchObject({ + status: "included", + requested: 3, + processed: 3, + items: includedItems, + }); + + const outputBytes = 21_000; + const limited = await fetchMaps(outputBytes); + expect(limited).toMatchObject({ + status: "partial", + requested: 3, + processed: 2, + items: includedItems.slice(0, 2), + limitation: expect.stringContaining("1 requested map"), + }); + expect(limited.limitation).toContain( + "source_map_limits.output_bytes is 21000", + ); + expect(Buffer.byteLength(JSON.stringify(limited))).toBeLessThanOrEqual( + outputBytes, + ); + expect(Buffer.byteLength(JSON.stringify(complete))).toBeGreaterThan( + outputBytes, ); }); @@ -1796,174 +1695,209 @@ describe("source-map set output admission", () => { }); describe("expanded source-map evidence budget", () => { - it("retains a large declared source whose exact public representation fits the budget", async () => { - const source = "x".repeat(12 * 1024 * 1024); + it("includes a declared source whose serialized evidence fits", async () => { + const source = "x".repeat(4_000); const map = JSON.stringify({ version: 3, names: [], sources: [source], mappings: "", }); - const result = await fetchWebSourceMapsOk([request], input(), undefined, { - fetch: () => Promise.resolve(new Response(map, { status: 200 })), - }); - expect(result.items[0]?.status).toBe("included"); - expect(result.items[0]?.original_sources[0]?.source).toBe( - `${origin}/assets/${source}`, - ); - }); - - it("releases discarded malformed maps before reserving a later valid map", async () => { - const malformed = JSON.stringify({ - version: 3, - names: [], - sourceRoot: "a/../".repeat(3_300_000), - sources: ["main.ts"], - mappings: "ACAA", - }); - const valid = JSON.stringify({ - version: 3, - names: [], - sources: ["main.js"], - sourcesContent: [`export default "${"x".repeat(2 * 1024 * 1024)}";`], - mappings: "AAAA", - }); - const responses = [malformed, malformed, valid]; + const budget = 11_500; const result = await fetchWebSourceMapsOk( - responses.map((_, index) => ({ - ...request, - scriptKey: `scr_${String(index + 1).padStart(64, "0")}`, - })), - input(), + [request], + input({ source_map_limits: { output_bytes: budget } }), undefined, - { - fetch: () => - Promise.resolve(new Response(responses.shift(), { status: 200 })), - }, + { fetch: async () => new Response(map) }, + ); + expect(result).toMatchObject({ + status: "included", + items: [ + { + artifact: { text: map }, + original_sources: [{ source: `${origin}/assets/${source}` }], + }, + ], + }); + expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual( + budget, ); - expect(result.items.map(({ status }) => status)).toEqual([ - "invalid", - "invalid", - "included", - ]); - expect(result.items[2]?.artifact?.text).toBe(valid); }); - it("bounds expanded evidence across individually valid maps", async () => { - const expanded = JSON.stringify({ - version: 3, - names: [], - sources: ["x".repeat(60_000)], - mappings: Array.from({ length: 280 }, () => "AAAA").join(","), - }); - const responses = [expanded, expanded]; - const requests = Array.from({ length: 2 }, (_, index) => ({ - ...request, - scriptKey: `scr_${String(index + 1).padStart(64, "0")}`, - })); - const result = await fetchWebSourceMapsOk(requests, input(), undefined, { - fetch: () => - Promise.resolve(new Response(responses.shift() ?? "", { status: 200 })), - }); - - expect(result.status).toBe("partial"); - expect(result.items.map(({ status }) => status)).toEqual([ - "included", - "fetch_failed", - ]); - expect(result.items[0]?.mappings).toHaveLength(280); - expect(result.items[1]?.limitation).toContain( - "Expanded source-map evidence", - ); - expect(result.items[1]?.mappings).toEqual([]); - }); - - it("accepts a large raw source root that the pinned resolver normalizes away", async () => { + it("bounds expanded mapping evidence across individually valid maps", async () => { + const source = `https://sources.example.test/${"x".repeat(1_000)}.ts`; const map = JSON.stringify({ version: 3, names: [], - sourceRoot: "a/../".repeat(2_300_000), - sources: ["main.ts"], - mappings: "", + sources: [source], + mappings: "AAAA,CAAA,CAAA,CAAA", }); - const result = await fetchWebSourceMapsOk([request], input(), undefined, { - fetch: () => Promise.resolve(new Response(map, { status: 200 })), - }); - - expect(result.items[0]?.status).toBe("included"); - expect(result.items[0]?.original_sources[0]?.source).toBe( - `${origin}/assets/main.ts`, + const second = { ...request, scriptKey: `scr_${"2".repeat(64)}` }; + const result = await fetchWebSourceMapsOk( + [request, second], + input({ source_map_limits: { output_bytes: 11_000 } }), + undefined, + { fetch: async () => new Response(map) }, ); + + expect(result).toMatchObject({ + status: "partial", + requested: 2, + processed: 2, + items: [ + { + script_key: request.scriptKey, + status: "included", + mappings: [0, 1, 2, 3].map((generated_column) => ({ + generated_column, + source, + })), + }, + { + script_key: second.scriptKey, + status: "fetch_failed", + artifact: null, + mappings: [], + limitation: expect.stringContaining("Expanded source-map evidence"), + }, + ], + }); }); - it("rejects a large resolved source product before allocating mapping rows", async () => { - const map = JSON.stringify({ - version: 3, - names: [], - sourceRoot: "x".repeat(8 * 1_024 * 1_024), - sources: ["main.ts"], - mappings: "AAAA,AAAA,AAAA", - }); - const result = await fetchWebSourceMapsOk([request], input(), undefined, { - fetch: () => Promise.resolve(new Response(map, { status: 200 })), - }); + it("applies the output budget to resolved sources after path normalization", async () => { + const fetchMap = (sourceRoot: string) => + fetchWebSourceMapsOk( + [request], + input({ source_map_limits: { output_bytes: 16_000 } }), + undefined, + { + fetch: async () => + new Response( + JSON.stringify({ + version: 3, + names: [], + sourceRoot, + sources: ["main.ts"], + mappings: "AAAA,CAAA,CAAA", + }), + ), + }, + ); - expect(result.items[0]).toMatchObject({ - status: "fetch_failed", - artifact: null, - mappings: [], + const source = `${origin}/assets/main.ts`; + expect(await fetchMap("a/../".repeat(1_900))).toMatchObject({ + status: "included", + items: [ + { + original_sources: [{ source }], + mappings: [0, 1, 2].map((generated_column) => ({ + generated_column, + source, + })), + }, + ], }); - expect(result.items[0]?.limitation).toContain( - "Expanded source-map evidence", - ); - }); - - it("bounds raw resolved identities even when URL sanitization removes credentials", async () => { - const userinfo = "u".repeat(1 * 1_024 * 1_024); - const map = JSON.stringify({ - version: 3, - names: [], - sourceRoot: `https://${userinfo}@sources.example.test/`, - sources: Array.from({ length: 65 }, (_, index) => `source-${index}.ts`), - mappings: "", + expect(await fetchMap("x".repeat(9_500))).toMatchObject({ + status: "unavailable", + items: [ + { + status: "fetch_failed", + artifact: null, + original_sources: [], + mappings: [], + limitation: expect.stringContaining("Expanded source-map evidence"), + }, + ], }); - const result = await fetchWebSourceMapsOk([request], input(), undefined, { - fetch: () => Promise.resolve(new Response(map, { status: 200 })), - }); - - expect(result.items[0]).toMatchObject({ - status: "fetch_failed", - artifact: null, - original_sources: [], - }); - expect(result.items[0]?.limitation).toContain( - "source_map_limits.fetch_bytes is 67108864", - ); }); }); -describe("raw source-map structure preflight", () => { - it("budgets source inventory before schema parsing its entries", async () => { - const mapText = JSON.stringify({ - version: 3, - names: [], - sources: Array.from({ length: 262_145 }, (_, index) => - index === 0 ? 42 : "source.js", - ), - mappings: "", - }); - const response = new Response(mapText, { status: 200 }); - const result = await fetchWebSourceMapsOk([request], input(), undefined, { - fetch: () => Promise.resolve(response), - }); - - expect(result.items[0]).toMatchObject({ - status: "fetch_failed", - artifact: null, - }); - expect(result.items[0]?.limitation).toContain( - "source_map_limits.decoded_records is 262144", +it("bounds resolved identities even when public URLs omit credentials", async () => { + const userinfo = "u".repeat(1_500); + const map = JSON.stringify({ + version: 3, + names: [], + sourceRoot: `https://${userinfo}@sources.example.test/`, + sources: ["a.ts", "b.ts", "c.ts", "d.ts"], + mappings: "", + }); + const fetchMaps = (fetchBytes: number) => + fetchWebSourceMapsOk( + [request], + input({ source_map_limits: { fetch_bytes: fetchBytes } }), + undefined, + { fetch: async () => new Response(map) }, ); + + expect(await fetchMaps(7_000)).toMatchObject({ + status: "included", + items: [ + { + original_sources: [ + { source: "https://sources.example.test/a.ts" }, + { source: "https://sources.example.test/b.ts" }, + { source: "https://sources.example.test/c.ts" }, + { source: "https://sources.example.test/d.ts" }, + ], + }, + ], + }); + + expect(await fetchMaps(5_000)).toMatchObject({ + status: "unavailable", + items: [ + { + status: "fetch_failed", + artifact: null, + original_sources: [], + limitation: expect.stringContaining( + "source_map_limits.fetch_bytes is 5000", + ), + response_identity: { + bytes: Buffer.byteLength(map), + sha256: createHash("sha256").update(map).digest("hex"), + }, + }, + ], + }); +}); + +it("reports the source inventory limit before malformed entries", async () => { + const map = JSON.stringify({ + version: 3, + names: [], + sources: [42, "source.js", "other.js"], + mappings: "", + }); + const fetchMap = (decodedRecords: number) => + fetchWebSourceMapsOk( + [request], + input({ source_map_limits: { decoded_records: decodedRecords } }), + undefined, + { fetch: async () => new Response(map) }, + ); + + expect(await fetchMap(3)).toMatchObject({ + status: "unavailable", + items: [ + { + status: "fetch_failed", + artifact: null, + limitation: expect.stringContaining( + "source_map_limits.decoded_records is 3", + ), + }, + ], + }); + expect(await fetchMap(10)).toMatchObject({ + status: "unavailable", + items: [ + { + status: "invalid", + artifact: null, + limitation: expect.stringContaining("Source-map JSON"), + }, + ], }); }); @@ -1989,109 +1923,6 @@ const input = (overrides: Record = {}) => { }); }; -it("keeps bare original module specifiers unresolved", async () => { - const result = await fetchWebSourceMapsOk([request], input(), undefined, { - fetch: () => - Promise.resolve( - new Response( - JSON.stringify({ - version: 3, - names: [], - sources: ["main.ts"], - sourcesContent: ['import "package-name"; import "./actual.js";'], - mappings: "AAAA", - }), - ), - ), - }); - const edges = result.items[0]?.original_module_edges; - expect( - edges?.find(({ specifier }) => specifier === "package-name") - ?.resolved_source, - ).toBeNull(); - expect( - edges?.find(({ specifier }) => specifier === "./actual.js") - ?.resolved_source, - ).toContain("/assets/actual.js"); -}); - -it("uses source URL paths for language metadata while retaining the mapped identities", async () => { - const sources = [ - { name: "main.ts?revision=2#view.js", mediaType: "text/typescript" }, - { name: "view.tsx#original", mediaType: "text/typescript" }, - { name: "module.mts", mediaType: "text/typescript" }, - { name: "common.cts", mediaType: "text/typescript" }, - { - name: "main.js?filename=main.ts#original.ts", - mediaType: "text/javascript", - }, - { name: "extensionless?revision=2", mediaType: "text/javascript" }, - ].map((source) => ({ - ...source, - text: - source.mediaType === "text/typescript" - ? "export const value: number = 1;" - : "export const value = 1;", - })); - const server = createServer((_incoming, response) => { - response.setHeader("content-type", "application/json"); - response.end( - JSON.stringify({ - version: 3, - sources: sources.map(({ name }) => name), - sourcesContent: sources.map(({ text }) => text), - names: [], - // One generated line per source; the source index advances after the first. - mappings: "AAAA;ACAA;ACAA;ACAA;ACAA;ACAA", - }), - ); - }); - try { - await new Promise((resolve, reject) => { - server.once("error", reject); - server.listen(0, "127.0.0.1", resolve); - }); - const address = server.address(); - if (address === null || typeof address === "string") - throw new TypeError("Expected a TCP listener address"); - const localOrigin = `http://127.0.0.1:${String(address.port)}`; - const url = `${localOrigin}/main.js.map`; - const result = await fetchWebSourceMapsOk( - [{ ...request, fetchUrl: url, declaredUrl: url }], - input({ allowed_origins: [localOrigin] }), - AbortSignal.timeout(2_000), - ); - expect(result).toMatchObject({ - status: "included", - items: [ - { - original_sources: sources.map(({ name, mediaType, text }) => ({ - source: `${localOrigin}/${name}`, - artifact: { - media_type: mediaType, - text, - bytes: Buffer.byteLength(text), - sha256: createHash("sha256").update(text).digest("hex"), - }, - })), - mappings: sources.map(({ name }, index) => ({ - generated_line: index + 1, - source: `${localOrigin}/${name}`, - })), - }, - ], - }); - } finally { - server.closeAllConnections(); - if (server.listening) - await new Promise((resolve, reject) => { - server.close((error) => - error === undefined ? resolve() : reject(error), - ); - }); - } -}); - describe("deep source-map original syntax", () => { it("retains valid source maps and native/CommonJS evidence after a parser-admitted deep property chain", async () => { const source = `const value = root${".next".repeat(12_000)};\nimport "./last.js";\nrequire("./common.js");\nfunction local(require) { require("./shadowed.js"); }`; diff --git a/tests/boundary/browser/webSourceMapHttpStatus.test.ts b/tests/boundary/browser/webSourceMapHttpStatus.test.ts index 268aa04199..69f9f7a0e8 100644 --- a/tests/boundary/browser/webSourceMapHttpStatus.test.ts +++ b/tests/boundary/browser/webSourceMapHttpStatus.test.ts @@ -6,10 +6,7 @@ import { expect, it } from "vitest"; import { fetchWebSourceMaps } from "../../../src/browser/WebSourceMapFetcher.js"; import { fetchWebSourceMapsOk } from "../../fixtures/fetchWebSourceMapsOk.js"; -import { - analyzeWebBundleInputSchema, - webSourceMapsSchema, -} from "../../../src/domain/webBundleAnalysis.js"; +import { analyzeWebBundleInputSchema } from "../../../src/domain/webBundleAnalysis.js"; import { WEB_SOURCE_MAP_LIMITS } from "../../../src/domain/sourceMapLimits.js"; it.each([ @@ -141,11 +138,17 @@ const mapWithSourceBytes = (bytes: Uint8Array) => Buffer.concat([mapStart, bytes, mapEnd]); it.each([ - { name: "invalid JSON", body: Buffer.from("not-json"), status: "invalid" }, + { + name: "invalid JSON", + body: Buffer.from("not-json"), + status: "invalid", + diagnostic: "Source-map JSON is not a version 3 map", + }, { name: "invalid UTF-8", body: mapWithSourceBytes(Uint8Array.of(0xff)), status: "invalid", + diagnostic: "Source-map response is not valid UTF-8", }, { name: "a valid UTF-8 replacement character", @@ -157,6 +160,7 @@ it.each([ body: Buffer.from("service unavailable"), status: "fetch_failed", responseStatus: 503, + diagnostic: "Source-map server returned HTTP 503", }, { name: "response limit", @@ -211,18 +215,10 @@ it.each([ representation: "fetch-response-body-bytes", }, }); - if (scenario.name === "invalid UTF-8") - expect(item.limitation).toContain( - "Source-map response is not valid UTF-8", - ); + expect(item.limitation).toContain("Response text was not retained"); } - const { effective_url: _effective, ...withoutEffectiveUrl } = item; - expect( - webSourceMapsSchema.safeParse({ - ...result, - items: [withoutEffectiveUrl], - }).success, - ).toBe(false); + if (scenario.diagnostic !== undefined) + expect(item.limitation).toContain(scenario.diagnostic); }); it.each([ @@ -318,53 +314,49 @@ it.each([ }, ); -it("charges complete escaped redirect errors and cancels every discarded response", async () => { +it("fits complete malformed redirect diagnostics within the output budget", async () => { const origin = "https://app.example.test"; - const location = `http://[${'\\"'.repeat(3_000)}`; - const request = { - scriptKey: "script", - declaredUrl: `${origin}/map`, - fetchUrl: `${origin}/map`, - }; + const location = `http://[${'\\"'.repeat(400)}`; + const requests = ["first", "second", "third", "fourth"].map((name) => ({ + scriptKey: name, + declaredUrl: `${origin}/${name}.map`, + fetchUrl: `${origin}/${name}.map`, + })); + const budget = 9_000; const input = analyzeWebBundleInputSchema.parse({ cdp_endpoint: "http://127.0.0.1:9222", target_id: "page", allowed_origins: [origin], fetch_source_maps: true, + source_map_limits: { output_bytes: budget }, }); - let calls = 0; - let cancelled = 0; - const host = { - fetch: () => { - calls += 1; - return Promise.resolve( - new Response( - new ReadableStream({ - cancel() { - cancelled += 1; - }, - }), - { status: 302, headers: { location } }, - ), - ); - }, - }; - const control = await fetchWebSourceMapsOk([request], input, undefined, host); - expect(control.items[0]?.limitation).toContain(JSON.stringify(location)); - expect(control.items[0]?.limitation?.length).toBeGreaterThan(512); - const result = await fetchWebSourceMapsOk( - Array.from({ length: 1_500 }, () => request), - input, - undefined, - host, + const cancelled: string[] = []; + const result = await fetchWebSourceMapsOk(requests, input, undefined, { + fetch: async (url) => + new Response( + new ReadableStream({ + cancel() { + cancelled.push(String(url)); + }, + }), + { status: 302, headers: { location } }, + ), + }); + + expect(result).toMatchObject({ + status: "unavailable", + requested: 4, + processed: 2, + items: requests.slice(0, 2).map(({ scriptKey, declaredUrl }) => ({ + script_key: scriptKey, + declared_url: declaredUrl, + status: "fetch_failed", + limitation: expect.stringContaining(JSON.stringify(location)), + })), + limitation: expect.stringContaining("2 requested maps"), + }); + expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual(budget); + expect(cancelled).toEqual( + requests.slice(0, 3).map(({ fetchUrl }) => fetchUrl), ); - expect(result).toMatchObject({ status: "unavailable", requested: 1_500 }); - expect(result.processed).toBe(result.items.length); - expect(result.processed).toBeLessThan(result.requested); - expect(result.limitation).toContain("unknown"); - expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual( - WEB_SOURCE_MAP_LIMITS.outputBytes, - ); - expect(calls).toBeLessThan(1_501); - expect(cancelled).toBe(calls); }); diff --git a/tests/boundary/browser/webSourceMapNestedCoordinates.test.ts b/tests/boundary/browser/webSourceMapNestedCoordinates.test.ts index a3fa4c163a..23eb03ae15 100644 --- a/tests/boundary/browser/webSourceMapNestedCoordinates.test.ts +++ b/tests/boundary/browser/webSourceMapNestedCoordinates.test.ts @@ -1,449 +1,171 @@ import { createHash } from "node:crypto"; import { expect, it } from "vitest"; -import { fetchWebSourceMapsOk } from "../../fixtures/fetchWebSourceMapsOk.js"; import { analyzeWebBundleInputSchema } from "../../../src/domain/webBundleAnalysis.js"; -import { traceSourceMap } from "../../../src/javascript/sourceMaps/TraceSourceMap.js"; +import { fetchWebSourceMapsOk } from "../../fixtures/fetchWebSourceMapsOk.js"; -const cases = [ - { - name: "later nested line resets outer column", - map: { - version: 3, - sections: [ - { - offset: { - line: 5, - column: 10, - }, - map: { - version: 3, - sections: [ - { - offset: { - line: 1, - column: 0, - }, - map: { - version: 3, - sources: ["a.ts"], - sourcesContent: ["export const value=1;"], - names: [], - mappings: "AAAA", - }, - }, - ], - }, - }, - ], - }, - expected: [[7, 0, "a.ts"]], - dedicatedControl: true, - }, - { - name: "same line retains summed columns", - map: { - version: 3, - sections: [ - { - offset: { - line: 5, - column: 10, - }, - map: { - version: 3, - sections: [ - { - offset: { - line: 0, - column: 3, - }, - map: { - version: 3, - sources: ["a.ts"], - sourcesContent: ["export const value=1;"], - names: [], - mappings: "AAAA", - }, - }, - ], - }, - }, - ], - }, - expected: [[6, 13, "a.ts"]], - dedicatedControl: true, - }, - { - name: "ordinary leaf unchanged", - map: { - version: 3, - sources: ["a.ts"], - sourcesContent: ["export const value=1;"], - names: [], - mappings: "AAAA;AACA", - }, - expected: [ - [1, 0, "a.ts"], - [2, 0, "a.ts"], - ], - dedicatedControl: true, - }, - { - name: "same-line sibling clipping", - map: { - version: 3, - sections: [ - { - offset: { - line: 0, - column: 0, - }, - map: { - version: 3, - sources: ["a.ts"], - sourcesContent: ["export const value=1;"], - names: [], - mappings: "AAAA,QAAQ,IAAI", - }, - }, - { - offset: { - line: 0, - column: 10, - }, - map: { - version: 3, - sources: ["b.ts"], - sourcesContent: ["export const value=1;"], - names: [], - mappings: "AAAA", - }, - }, - ], - }, - expected: [ - [1, 0, "a.ts"], - [1, 8, "a.ts"], - [1, 10, "b.ts"], - ], - dedicatedControl: false, - expectedStatus: "invalid", - }, - { - name: "nested gap clips previous parent leaf", - map: { - version: 3, - sections: [ - { - offset: { - line: 0, - column: 0, - }, - map: { - version: 3, - sources: ["a.ts"], - sourcesContent: ["export const value=1;"], - names: [], - mappings: "AAAA,QAAQ,IAAI", - }, - }, - { - offset: { - line: 0, - column: 5, - }, - map: { - version: 3, - sections: [ - { - offset: { - line: 0, - column: 3, - }, - map: { - version: 3, - sources: ["b.ts"], - sourcesContent: ["export const value=1;"], - names: [], - mappings: "AAAA", - }, - }, - ], - }, - }, - ], - }, - expected: [ - [1, 0, "a.ts"], - [1, 8, "b.ts"], - ], - dedicatedControl: false, - expectedStatus: "invalid", - }, - { - name: "empty indexed sibling clips previous leaf", - map: { - version: 3, - sections: [ - { - offset: { - line: 0, - column: 0, - }, - map: { - version: 3, - sources: ["a.ts"], - sourcesContent: ["export const value=1;"], - names: [], - mappings: "AAAA,QAAQ,IAAI", - }, - }, - { - offset: { - line: 0, - column: 5, - }, - map: { - version: 3, - sections: [], - }, - }, - ], - }, - expected: [[1, 0, "a.ts"]], - dedicatedControl: false, - expectedStatus: "invalid", - }, - { - name: "depth-three same-line composition", - map: { - version: 3, - sections: [ - { - offset: { - line: 0, - column: 2, - }, - map: { - version: 3, - sections: [ - { - offset: { - line: 0, - column: 3, - }, - map: { - version: 3, - sections: [ - { - offset: { - line: 0, - column: 4, - }, - map: { - version: 3, - sources: ["a.ts"], - sourcesContent: ["export const value=1;"], - names: [], - mappings: "AAAA", - }, - }, - ], - }, - }, - ], - }, - }, - ], - }, - expected: [[1, 9, "a.ts"]], - dedicatedControl: true, - }, - { - name: "multiline inherited sibling stop", - map: { - version: 3, - sections: [ - { - offset: { - line: 0, - column: 0, - }, - map: { - version: 3, - sections: [ - { - offset: { - line: 1, - column: 3, - }, - map: { - version: 3, - sources: ["a.ts"], - sourcesContent: ["export const value=1;"], - names: [], - mappings: "AAAA;AACA;AACA;AACA", - }, - }, - ], - }, - }, - { - offset: { - line: 3, - column: 1, - }, - map: { - version: 3, - sources: ["b.ts"], - sourcesContent: ["export const value=1;"], - names: [], - mappings: "AAAA", - }, - }, - ], - }, - expected: [ - [2, 3, "a.ts"], - [3, 0, "a.ts"], - [4, 0, "a.ts"], - [4, 1, "b.ts"], - ], - dedicatedControl: false, - expectedStatus: "invalid", - }, -] as const; +const origin = "https://example.test"; +const url = `${origin}/app.js.map`; +const request = { + scriptKey: `scr_${"1".repeat(64)}`, + declaredUrl: url, + fetchUrl: url, +}; +const input = analyzeWebBundleInputSchema.parse({ + cdp_endpoint: "http://127.0.0.1:9222", + allowed_origins: [origin], + target_id: "page-1", + fetch_source_maps: true, +}); -it.each(cases)( - "preserves indexed coordinate contract: $name", - async (testCase) => { - const { map, expected, dedicatedControl } = testCase; - const expectedStatus = - "expectedStatus" in testCase ? testCase.expectedStatus : "included"; - const origin = "https://example.test"; - const url = origin + "/app.js.map"; - const text = JSON.stringify(map); - // The dedicated decoder already normalizes absolute leaves. Its stricter - // overlap profile is compared only for the valid non-overlapping maps. - if (dedicatedControl) { - for (const [line, column] of expected) { - const point = traceSourceMap(text, url, { line, column }); - expect(point.lookup.matched_generated_position).toEqual({ - line, - column, - }); - expect(point.matches).toHaveLength(1); - } - } - const result = await fetchWebSourceMapsOk( - [{ scriptKey: "scr_" + "1".repeat(64), declaredUrl: url, fetchUrl: url }], - analyzeWebBundleInputSchema.parse({ - cdp_endpoint: "http://127.0.0.1:9222", - allowed_origins: [origin], - target_id: "page-1", - fetch_source_maps: true, - }), - undefined, - { fetch: () => Promise.resolve(new Response(text, { status: 200 })) }, - ); - const item = result.items[0]; - if (expectedStatus === "invalid") { - expect(item).toMatchObject({ +const leaf = (source: string, mappings = "AAAA") => ({ + version: 3, + sources: [source], + sourcesContent: ["export const value = 1;"], + names: [], + mappings, +}); + +interface Section { + offset: { line: number; column: number }; + map: ReturnType | { version: number; sections: Section[] }; +} + +const section = ( + line: number, + column: number, + map: Section["map"], +): Section => ({ + offset: { line, column }, + map, +}); + +const indexed = (...sections: Section[]) => ({ version: 3, sections }); + +const fetchMap = (text: string) => + fetchWebSourceMapsOk([request], input, undefined, { + fetch: async () => new Response(text), + }); + +it.each([ + { + name: "a later nested line discards the outer column", + map: indexed(section(5, 10, indexed(section(1, 3, leaf("a.ts"))))), + line: 7, + column: 3, + }, + { + name: "three offsets on the same line add their columns", + map: indexed( + section( + 0, + 2, + indexed(section(0, 3, indexed(section(0, 4, leaf("a.ts"))))), + ), + ), + line: 1, + column: 9, + }, +])("publishes nested coordinates when $name", async ({ map, line, column }) => { + const text = JSON.stringify(map); + const result = await fetchMap(text); + expect(result).toMatchObject({ + status: "included", + items: [ + { + artifact: { + sha256: createHash("sha256").update(text).digest("hex"), + bytes: Buffer.byteLength(text), + }, + original_sources: [{ source: `${origin}/a.ts` }], + mappings: [ + { + generated_line: line, + generated_column: column, + source: `${origin}/a.ts`, + }, + ], + }, + ], + }); +}); + +it.each([ + { + name: "a following indexed section starts before its first child", + map: indexed( + section(0, 0, leaf("a.ts", "AAAA,QAAQ")), + section(0, 5, indexed(section(0, 6, leaf("b.ts")))), + ), + }, + { + name: "a following indexed section is empty", + map: indexed( + section(0, 0, leaf("a.ts", "AAAA,QAAQ")), + section(0, 5, indexed()), + ), + }, + { + name: "a descendant mapping passes its parent's multiline stop", + map: indexed( + section( + 0, + 0, + indexed(section(1, 3, leaf("a.ts", "AAAA;AACA;AACA;AACA"))), + ), + section(3, 1, leaf("b.ts")), + ), + }, +])("rejects an overlap when $name", async ({ map }) => { + const text = JSON.stringify(map); + expect(await fetchMap(text)).toMatchObject({ + status: "unavailable", + items: [ + { status: "invalid", artifact: null, mappings: [], limitation: expect.stringContaining("overlap"), - }); - return; - } - if (item?.status !== "included") throw new Error(JSON.stringify(result)); - expect( - item.mappings.map((entry) => [ - entry.generated_line, - entry.generated_column, - entry.source?.replace(origin + "/", "") ?? null, - ]), - ).toEqual(expected); - expect(item.artifact.sha256).toBe( - createHash("sha256").update(text).digest("hex"), - ); - expect(item.artifact.bytes).toBe(Buffer.byteLength(text)); - // Boundary-only markers must not introduce empty original-source identities. - expect(item.original_sources.map(({ source }) => source)).toEqual([ - ...new Set(expected.map(([, , source]) => origin + "/" + source)), - ]); - }, -); - -it("rejects a source mapping that crosses an indexed section boundary", async () => { - const origin = "https://example.test"; - const url = `${origin}/app.js.map`; - const makeMap = (firstMappings: string) => ({ - version: 3, - sections: [ - { - offset: { line: 0, column: 0 }, - map: { - version: 3, - sources: ["a.js"], - sourcesContent: ["x();"], - names: [], - mappings: firstMappings, + response_identity: { + sha256: createHash("sha256").update(text).digest("hex"), + bytes: Buffer.byteLength(text), }, }, - { - offset: { line: 0, column: 1 }, - map: { - version: 3, - sources: ["b.js"], - sourcesContent: ["y();"], - names: [], - mappings: "AAAA", - }, - }, - ], - }); - const input = analyzeWebBundleInputSchema.parse({ - cdp_endpoint: "http://127.0.0.1:9222", - allowed_origins: [origin], - target_id: "page-1", - fetch_source_maps: true, - }); - const request = { - scriptKey: "scr_" + "1".repeat(64), - declaredUrl: url, - fetchUrl: url, - }; - const fetchMap = (map: ReturnType) => - fetchWebSourceMapsOk([request], input, undefined, { - fetch: () => - Promise.resolve(new Response(JSON.stringify(map), { status: 200 })), - }); - const overlapping = makeMap("AAAA,EAAA"); - const overlapText = JSON.stringify(overlapping); - expect(() => - traceSourceMap(overlapText, url, { line: 1, column: 2 }), - ).toThrow(/overlap/u); - const overlappingResult = await fetchMap(overlapping); - expect(overlappingResult).toMatchObject({ - status: "unavailable", - requested: 1, - processed: 1, - }); - expect(overlappingResult.items[0]).toMatchObject({ - status: "invalid", - artifact: null, - mappings: [], - limitation: expect.stringContaining("overlap"), - }); - - const controlResult = await fetchMap(makeMap("AAAA")); - expect(controlResult.items[0]).toMatchObject({ - status: "included", - limitation: null, - mappings: [ - { generated_line: 1, generated_column: 0, source: `${origin}/a.js` }, - { generated_line: 1, generated_column: 1, source: `${origin}/b.js` }, + ], + }); +}); + +it("treats the next section start as an exclusive end for the previous section", async () => { + const fetchSiblings = (firstMappings: string) => + fetchMap( + JSON.stringify( + indexed( + section(0, 0, leaf("a.ts", firstMappings)), + section(0, 10, leaf("b.ts")), + ), + ), + ); + + expect(await fetchSiblings("AAAA,QAAQ")).toMatchObject({ + status: "included", + items: [ + { + mappings: [ + { generated_line: 1, generated_column: 0, source: `${origin}/a.ts` }, + { generated_line: 1, generated_column: 8, source: `${origin}/a.ts` }, + { generated_line: 1, generated_column: 10, source: `${origin}/b.ts` }, + ], + }, + ], + }); + expect(await fetchSiblings("AAAA,UAAA")).toMatchObject({ + status: "unavailable", + items: [ + { + status: "invalid", + artifact: null, + mappings: [], + limitation: expect.stringContaining("overlap"), + }, ], }); }); diff --git a/tests/boundary/browser/optionalSourceMapRequire.test.ts b/tests/boundary/browser/webSourceMapOriginalDependencies.test.ts similarity index 61% rename from tests/boundary/browser/optionalSourceMapRequire.test.ts rename to tests/boundary/browser/webSourceMapOriginalDependencies.test.ts index 4d4c7dcf11..5623e99d1a 100644 --- a/tests/boundary/browser/optionalSourceMapRequire.test.ts +++ b/tests/boundary/browser/webSourceMapOriginalDependencies.test.ts @@ -6,9 +6,19 @@ import { expect, it } from "vitest"; import { fetchWebSourceMapsOk } from "../../fixtures/fetchWebSourceMapsOk.js"; import { analyzeWebBundleInputSchema } from "../../../src/domain/webBundleAnalysis.js"; -it("retains unshadowed optional require dependencies from HTTP source-map originals", async () => { +it("retains literal dependencies from HTTP source-map originals", async () => { const source = [ + '// import "./comment.js";', + String.raw`const quoted = "require(\"./string.js\")";`, + "const template = `import './template-comment.js'`;", + `export { value } from "./it's-real.js";`, + 'import "package-name";', + 'import("./dynamic.js");', + "import(`./dep.js`);", + "import(`./dep.${name}.js`);", 'require("./normal.cjs");', + "require(`./templated.cjs`);", + "require(`./${name}.cjs`);", 'require?.("./optional.cjs");', 'function local(require) { require?.("./shadowed.cjs"); }', "require?.(selectedModule);", @@ -48,14 +58,22 @@ it("retains unshadowed optional require dependencies from HTTP source-map origin sha256: createHash("sha256").update(text).digest("hex"), }, original_sources: [{ artifact: { text: source } }], - original_module_edges: ["./normal.cjs", "./optional.cjs"].map( - (specifier) => ({ - from_source: `${origin}/main.cjs`, - kind: "require", - specifier, - resolved_source: new URL(specifier, origin).href, - }), - ), + mappings: [{ generated_line: 1, source: `${origin}/main.cjs` }], + original_module_edges: [ + { specifier: "./it's-real.js", kind: "static_import" }, + { specifier: "package-name", kind: "static_import" }, + { specifier: "./dynamic.js", kind: "dynamic_import" }, + { specifier: "./dep.js", kind: "dynamic_import" }, + { specifier: "./normal.cjs", kind: "require" }, + { specifier: "./templated.cjs", kind: "require" }, + { specifier: "./optional.cjs", kind: "require" }, + ].map(({ kind, specifier }) => ({ + from_source: `${origin}/main.cjs`, + kind, + specifier, + resolved_source: + specifier === "package-name" ? null : new URL(specifier, origin).href, + })), }); } finally { server.closeAllConnections(); diff --git a/tests/boundary/browser/webSourceMapResponseIdentity.test.ts b/tests/boundary/browser/webSourceMapResponseIdentity.test.ts index f2f2ac2541..299b147098 100644 --- a/tests/boundary/browser/webSourceMapResponseIdentity.test.ts +++ b/tests/boundary/browser/webSourceMapResponseIdentity.test.ts @@ -1,11 +1,8 @@ import { createHash } from "node:crypto"; -import { expect, it, onTestFinished, vi } from "vitest"; +import { expect, it, vi } from "vitest"; import { fetchWebSourceMaps } from "../../../src/browser/WebSourceMapFetcher.js"; -import { - analyzeWebBundleInputSchema, - webSourceMapsSchema, -} from "../../../src/domain/webBundleAnalysis.js"; +import { analyzeWebBundleInputSchema } from "../../../src/domain/webBundleAnalysis.js"; const origin = "https://maps.example.test"; const request = (index: number) => ({ @@ -23,9 +20,7 @@ const digest = (bytes: Uint8Array | string): string => createHash("sha256").update(bytes).digest("hex"); it.each([ - { label: "an empty body", body: "" }, - { label: "JSON", body: "not-json" }, - { label: "BOM and XSSI framing", body: "\uFEFF)]}'\nnot-json" }, + { label: "an absent body", body: null }, { label: "mapping indices", body: JSON.stringify({ @@ -51,8 +46,8 @@ it.each([ status: "invalid", artifact: null, response_identity: { - sha256: digest(body), - bytes: Buffer.byteLength(body), + sha256: digest(body ?? ""), + bytes: Buffer.byteLength(body ?? ""), representation: "fetch-response-body-bytes", }, original_sources: [], @@ -62,64 +57,40 @@ it.each([ }, ], }); - expect(webSourceMapsSchema.parse(result.value)).toEqual(result.value); }, ); -it("retains only completed response bytes when UTF-8 decoding fails", async () => { - const body = Uint8Array.of(0x7b, 0x22, 0xc3, 0x28, 0x22, 0x7d); - const result = await fetchWebSourceMaps([request(1)], input, undefined, { - fetch: () => Promise.resolve(new Response(body, { status: 200 })), - }); - if (!result.ok) throw result.error; - expect(result.value).toMatchObject({ - status: "unavailable", - items: [ - { - status: "invalid", - artifact: null, - response_identity: { - sha256: digest(body), - bytes: body.byteLength, - representation: "fetch-response-body-bytes", - }, - original_sources: [], - original_module_edges: [], - mappings: [], - limitation: expect.stringContaining("Response text was not retained"), - }, - ], - }); -}); - -it("retains completed body identity when map interpretation reaches its deadline", async () => { +it("retains completed body identity when the deadline lands as the response stream ends", async () => { const body = JSON.stringify({ version: 3, sources: ["a.js"], names: [], mappings: "AAAA", }); - const response = new Response(body, { status: 200 }); - const realDecode = TextDecoder.prototype.decode; let clock = 0; const clockSpy = vi.spyOn(performance, "now").mockImplementation(() => clock); - const decodeSpy = vi - .spyOn(TextDecoder.prototype, "decode") - .mockImplementation(function ( - this: TextDecoder, - input?: Parameters[0], - options?: Parameters[1], - ) { - const text = realDecode.call(this, input, options); - clock = 1_001; - return text; - }); + let sentBody = false; + const stream = new ReadableStream( + { + pull(controller) { + if (sentBody) { + clock = 1_001; + controller.close(); + return; + } + sentBody = true; + controller.enqueue(Buffer.from(body)); + }, + }, + { highWaterMark: 0 }, + ); + try { const result = await fetchWebSourceMaps( [request(1)], { ...input, source_map_fetch_timeout_ms: 1_000 }, undefined, - { fetch: () => Promise.resolve(response) }, + { fetch: async () => new Response(stream) }, ); if (!result.ok) throw result.error; expect(result.value).toMatchObject({ @@ -145,34 +116,30 @@ it("retains completed body identity when map interpretation reaches its deadline ], }); } finally { - decodeSpy.mockRestore(); clockSpy.mockRestore(); } }); -it("refuses oversized text before JSON interpretation while retaining body identity and later maps", async () => { - // JSON escaping expands these decoded control characters beyond the default - // 32 MiB source-map output allowance while the response stays below 64 MiB. - const oversized = Buffer.alloc(6 * 1024 * 1024, 1); - const oversizedText = oversized.toString("utf8"); +it("retains body identity and later maps when escaping exceeds the output budget", async () => { + const budget = 8_000; + const oversized = Buffer.alloc(2_000, 1); + expect(oversized.byteLength).toBeLessThan(budget); + expect( + Buffer.byteLength(JSON.stringify(oversized.toString("utf8"))), + ).toBeGreaterThan(budget); const valid = JSON.stringify({ version: 3, sources: ["a.js"], names: [], mappings: "AAAA", }); - const parseSpy = vi.spyOn(JSON, "parse"); - onTestFinished(() => parseSpy.mockRestore()); - let calls = 0; const result = await fetchWebSourceMaps( [request(1), request(2)], - input, + { ...input, source_map_limits: { output_bytes: budget } }, undefined, { - fetch: () => - Promise.resolve( - new Response(calls++ === 0 ? oversized : valid, { status: 200 }), - ), + fetch: async (url) => + new Response(String(url) === request(1).fetchUrl ? oversized : valid), }, ); if (!result.ok) throw result.error; @@ -197,16 +164,9 @@ it("refuses oversized text before JSON interpretation while retaining body ident }); const limitation = result.value.items[0]?.limitation; expect(limitation).toMatch(/JSON[- ]escap/i); - expect(limitation).toMatch(/metadata/i); - expect(limitation).toContain( - "source_map_limits.output_bytes is 33554432 bytes", - ); + expect(limitation).toContain("source_map_limits.output_bytes is 8000 bytes"); expect(limitation).toContain("Increase that per-call field"); expect(Buffer.byteLength(JSON.stringify(result.value))).toBeLessThanOrEqual( - 32 * 1024 * 1024, + budget, ); - expect(parseSpy.mock.calls.some(([text]) => text === oversizedText)).toBe( - false, - ); - expect(calls).toBe(2); }); diff --git a/tests/boundary/browser/webSourceMapSourceLanguage.test.ts b/tests/boundary/browser/webSourceMapSourceLanguage.test.ts index 266d8e850e..dc54e436dc 100644 --- a/tests/boundary/browser/webSourceMapSourceLanguage.test.ts +++ b/tests/boundary/browser/webSourceMapSourceLanguage.test.ts @@ -1,3 +1,4 @@ +import { createHash } from "node:crypto"; import { once } from "node:events"; import { createServer } from "node:http"; @@ -7,19 +8,63 @@ import { analyzeWebBundleInputSchema } from "../../../src/domain/webBundleAnalys 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 = 1;'; + const annotatedContent = 'import "./dep.ts"; const value: number = 1;'; + const javascriptContent = 'import "./dep.ts"; const value = 1;'; const declarations = [ - "plain.ts?revision=2#view.tsx", - "escaped%2Ets?revision=2#view.tsx", - "escaped.%74s?revision=2#view.tsx", - "invalid%.ts?revision=2#view.tsx", + { + 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: "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 =
;', + }, + { + 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 content = 'import "./dep.ts"; const value = 1;'; const text = JSON.stringify({ version: 3, names: [], - sources: declarations, - sourcesContent: declarations.map(() => content), - mappings: "AAAA,ACAA,ACAA,ACAA", + sources: declarations.map(({ path }) => path), + sourcesContent: declarations.map(({ content }) => content), + // One generated line per source, with the source index advancing each time. + mappings: `AAAA${";ACAA".repeat(declarations.length - 1)}`, }); const requests: string[] = []; const server = createServer((request, response) => { @@ -44,19 +89,30 @@ it("selects original source language from decoded URL paths while retaining URL fetch_source_maps: true, }), ); - const sources = declarations.map((source) => `${origin}/maps/${source}`); + const sources = declarations.map(({ path, ...artifact }) => ({ + source: `${origin}/maps/${path}`, + ...artifact, + })); expect(requests).toEqual(["/maps/app.js.map"]); expect(result.status).toBe("included"); expect(result.items[0]).toMatchObject({ status: "included", limitation: null, artifact: { text }, - original_sources: sources.map((source) => ({ + original_sources: sources.map(({ source, content, mediaType }) => ({ source, - artifact: { media_type: "text/typescript", text: content }, + artifact: { + media_type: mediaType, + text: content, + bytes: Buffer.byteLength(content), + sha256: createHash("sha256").update(content).digest("hex"), + }, })), - mappings: sources.map((source) => ({ source })), - original_module_edges: sources.map((source) => ({ + mappings: sources.map(({ source }, index) => ({ + generated_line: index + 1, + source, + })), + original_module_edges: sources.map(({ source }) => ({ from_source: source, kind: "static_import", specifier: "./dep.ts", diff --git a/tests/boundary/filesystem/artifactSourceMapTargets.test.ts b/tests/boundary/filesystem/artifactSourceMapTargets.test.ts index 6327ce9529..60be3e3b06 100644 --- a/tests/boundary/filesystem/artifactSourceMapTargets.test.ts +++ b/tests/boundary/filesystem/artifactSourceMapTargets.test.ts @@ -5,22 +5,16 @@ import { expect, it } from "vitest"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { inventoryArtifact } from "../../fixtures/artifactInventory.js"; -it.each([ - "directory", - "directory-with-child", - "file", - "directory-map", - "missing", -])( +it.each(["directory-with-child", "file", "directory-map", "missing"] as const)( "admits only a regular mapped file while retaining ZIP containment (%s)", async (target) => { const root = await createTestTempDirectory("rea-map-target-kind-"); const path = join(root, "sample.zip"); const writer = new ZipWriter(new Uint8ArrayWriter()); - if (target.startsWith("directory") && target !== "directory-map") + if (target === "directory-with-child") { await writer.add("main.js/", undefined, { directory: true }); - if (target === "directory-with-child") await writer.add("main.js/child.txt", new TextReader("child")); + } if (target === "file" || target === "directory-map") await writer.add("main.js", new TextReader("export const answer = 42;")); if (target === "directory-map") @@ -64,7 +58,7 @@ it.each([ target === "file" ? main?.artifact_id : carrier.artifact_id, }); expect(map.limitations).toEqual([]); - if (target.startsWith("directory") && target !== "directory-map") { + if (target === "directory-with-child") { expect(main).toMatchObject({ entry_kind: "directory", artifact_kind: "container", @@ -77,8 +71,6 @@ it.each([ relation: "contains", parent_artifact_id: carrier.artifact_id, }); - } - if (target === "directory-with-child") { const child = inventory.occurrences.find( ({ logical_path }) => logical_path === "main.js/child.txt", ); diff --git a/tests/boundary/filesystem/javascript-applications/javascriptApplicationSourceMapMappings.test.ts b/tests/boundary/filesystem/javascript-applications/javascriptApplicationSourceMapMappings.test.ts index fadc5882aa..a9445b8031 100644 --- a/tests/boundary/filesystem/javascript-applications/javascriptApplicationSourceMapMappings.test.ts +++ b/tests/boundary/filesystem/javascript-applications/javascriptApplicationSourceMapMappings.test.ts @@ -26,183 +26,6 @@ const analyzeMaps = async (maps: Readonly>) => { return analysis.graph; }; -it("marks missing regular-map mappings unavailable and accepts empty or populated strings", async () => { - const original = "export const answer: number = 42;"; - const maps = { - "missing.js.map": JSON.stringify({ - version: 3, - sources: ["original.ts"], - sourcesContent: [original], - names: [], - }), - "empty.js.map": JSON.stringify({ - version: 3, - sources: ["original.ts"], - sourcesContent: [original], - names: [], - mappings: "", - }), - "mapped.js.map": JSON.stringify({ - version: 3, - sources: ["original.ts"], - sourcesContent: [original], - names: [], - mappings: "AAAA", - }), - }; - const graph = await analyzeMaps(maps); - const missingDigest = createHash("sha256") - .update(maps["missing.js.map"]) - .digest("hex"); - expect(graph.coverage.status).toBe("partial"); - expect(graph.nodes).toContainEqual( - expect.objectContaining({ - kind: "unknown", - observations: expect.arrayContaining([ - expect.objectContaining({ - properties: expect.objectContaining({ - operation: "parse-local-source-map", - }), - evidence: expect.objectContaining({ - artifact: expect.objectContaining({ sha256: missingDigest }), - limitations: ["Source map has no mappings string."], - }), - }), - ]), - }), - ); - const originals = graph.nodes.filter( - (node) => node.identity.strategy === "source-map-original", - ); - expect(originals).toHaveLength(3); - expect(originals).toContainEqual( - expect.objectContaining({ - kind: "source-module", - identity: expect.objectContaining({ - source_map_sha256: missingDigest, - original_source: "original.ts", - source_sha256: createHash("sha256").update(original).digest("hex"), - }), - observations: expect.arrayContaining([ - expect.objectContaining({ - evidence: expect.objectContaining({ - artifact: expect.objectContaining({ sha256: missingDigest }), - limitations: ["Source map has no mappings string."], - }), - }), - ]), - }), - ); - for (const mapPath of ["empty.js.map", "mapped.js.map"] as const) { - const digest = createHash("sha256").update(maps[mapPath]).digest("hex"); - expect(originals).toContainEqual( - expect.objectContaining({ - kind: "source-module", - identity: expect.objectContaining({ source_map_sha256: digest }), - observations: expect.arrayContaining([ - expect.objectContaining({ - evidence: expect.objectContaining({ limitations: [] }), - }), - ]), - }), - ); - } -}); - -it("retains declarations before and after an invalid indexed-map leaf", async () => { - const mapText = JSON.stringify({ - 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 }, - map: { - version: 3, - sources: ["middle.ts"], - sourcesContent: ["export const middle = 2;"], - names: [], - }, - }, - { - offset: { line: 2, column: 0 }, - map: { - version: 3, - sources: ["after.ts"], - sourcesContent: ["export const after = 3;"], - names: [], - mappings: "AAAA", - }, - }, - ], - }); - const graph = await analyzeMaps({ "indexed.js.map": mapText }); - const digest = createHash("sha256").update(mapText).digest("hex"); - expect(graph.coverage.status).toBe("partial"); - expect(graph.nodes).toContainEqual( - expect.objectContaining({ - kind: "unknown", - observations: expect.arrayContaining([ - expect.objectContaining({ - properties: expect.objectContaining({ - path: "indexed.js.map", - operation: "parse-local-source-map", - }), - evidence: expect.objectContaining({ - artifact: expect.objectContaining({ sha256: digest }), - limitations: ["Source map has no mappings string in section [1]."], - }), - }), - ]), - }), - ); - const originals = graph.nodes.filter( - (node) => - node.identity.strategy === "source-map-original" && - node.identity.source_map_sha256 === digest, - ); - expect(originals).toHaveLength(3); - for (const [index, sourceName, content] of [ - [0, "before.ts", "export const before = 1;"], - [1, "middle.ts", "export const middle = 2;"], - [2, "after.ts", "export const after = 3;"], - ] as const) { - expect(originals).toContainEqual( - expect.objectContaining({ - kind: "source-module", - identity: expect.objectContaining({ - source_map_sha256: digest, - original_source: sourceName, - source_sha256: createHash("sha256").update(content).digest("hex"), - }), - observations: expect.arrayContaining([ - expect.objectContaining({ - properties: expect.objectContaining({ - declaration_index: index, - source_name_available: true, - content_available: true, - }), - evidence: expect.objectContaining({ - artifact: expect.objectContaining({ sha256: digest }), - limitations: [ - "Source map has no mappings string in section [1].", - ], - }), - }), - ]), - }), - ); - } -}); - it("keeps external indexed sections unsupported while retaining local siblings", async () => { const url = "https://app.test/maps/part.map"; const mapText = JSON.stringify({ @@ -340,7 +163,7 @@ it("marks non-array sections malformed even when the object has regular-map fiel ).toBe(false); }); -it("retains declarations around a semantically invalid indexed leaf", async () => { +it("retains safe declarations and section diagnostics across malformed leaves", async () => { const mapText = JSON.stringify({ version: 3, sections: [ @@ -351,81 +174,7 @@ it("retains declarations around a semantically invalid indexed leaf", async () = sources: ["before.ts"], sourcesContent: ["export const before = 1;"], names: [], - mappings: "AAAA", - }, - }, - { - offset: { line: 1, column: 0 }, - map: { version: 3, sourcesContent: [], names: [], mappings: "AAAA" }, - }, - { - offset: { line: 2, column: 0 }, - map: { - version: 3, - sources: ["after.ts"], - sourcesContent: ["export const after = 2;"], - names: [], - mappings: "AAAA", - }, - }, - ], - }); - const graph = await analyzeMaps({ "indexed-invalid-leaf.js.map": mapText }); - const digest = createHash("sha256").update(mapText).digest("hex"); - const limitation = "Source map has no sources array in section [1]."; - - expect(graph.coverage.status).toBe("partial"); - expect(graph.nodes).toContainEqual( - expect.objectContaining({ - kind: "unknown", - observations: expect.arrayContaining([ - expect.objectContaining({ - properties: expect.objectContaining({ - path: "indexed-invalid-leaf.js.map", - operation: "parse-local-source-map", - }), - evidence: expect.objectContaining({ - artifact: expect.objectContaining({ sha256: digest }), - limitations: [limitation], - }), - }), - ]), - }), - ); - for (const [source, section_path, content] of [ - ["before.ts", [0], "export const before = 1;"], - ["after.ts", [2], "export const after = 2;"], - ] as const) - expect(graph.nodes).toContainEqual( - expect.objectContaining({ - kind: "source-module", - identity: expect.objectContaining({ - source_map_sha256: digest, - original_source: source, - source_sha256: createHash("sha256").update(content).digest("hex"), - }), - observations: expect.arrayContaining([ - expect.objectContaining({ - properties: expect.objectContaining({ section_path }), - evidence: expect.objectContaining({ limitations: [limitation] }), - }), - ]), - }), - ); -}); - -it("keeps safe declarations across malformed roots, contents, and source entries", async () => { - const mapText = JSON.stringify({ - version: 3, - sections: [ - { - offset: { line: 0, column: 0 }, - map: { - version: 3, - sources: ["before.ts"], - sourcesContent: ["export const before = 1;"], - names: [], - mappings: "AAAA", + mappings: "", }, }, { @@ -461,6 +210,19 @@ it("keeps safe declarations across malformed roots, contents, and source entries }, { offset: { line: 4, column: 0 }, + map: { version: 3, sourcesContent: [], names: [], mappings: "AAAA" }, + }, + { + offset: { line: 5, column: 0 }, + map: { + version: 3, + sources: ["no-mappings.ts"], + sourcesContent: ["export const noMappings = 4;"], + names: [], + }, + }, + { + offset: { line: 6, column: 0 }, map: { version: 3, sources: ["after.ts"], @@ -479,6 +241,8 @@ it("keeps safe declarations across malformed roots, contents, and source entries "Source map sourceRoot must be a string or null in section [1].", "Source map sourcesContent must be an array of strings or null entries in section [2].", "Source map contains a non-string source name at index 0 in section [3].", + "Source map has no sources array in section [4].", + "Source map has no mappings string in section [5].", ].join("; "); expect(graph.coverage.status).toBe("partial"); @@ -495,13 +259,22 @@ it("keeps safe declarations across malformed roots, contents, and source entries ]), }), ); - for (const [source, section_path, content, source_index] of [ + const originals = graph.nodes.filter( + ({ identity }) => identity.strategy === "source-map-original", + ); + expect(originals).toHaveLength(5); + const retained = [ ["before.ts", [0], "export const before = 1;", 0], ["content.ts", [2], null, 0], ["valid-entry.ts", [3], "export const validEntry = 3;", 1], - ["after.ts", [4], "export const after = 4;", 0], - ] as const) - expect(graph.nodes).toContainEqual( + ["no-mappings.ts", [5], "export const noMappings = 4;", 0], + ["after.ts", [6], "export const after = 4;", 0], + ] as const; + for (const [ + declaration_index, + [source, section_path, content, source_index], + ] of retained.entries()) + expect(originals).toContainEqual( expect.objectContaining({ kind: "source-module", identity: expect.objectContaining({ @@ -514,7 +287,11 @@ it("keeps safe declarations across malformed roots, contents, and source entries }), observations: expect.arrayContaining([ expect.objectContaining({ - properties: expect.objectContaining({ section_path, source_index }), + properties: expect.objectContaining({ + section_path, + source_index, + declaration_index, + }), evidence: expect.objectContaining({ limitations: [limitation] }), }), ]), diff --git a/tests/boundary/filesystem/javascriptSourceMapComments.test.ts b/tests/boundary/filesystem/javascriptSourceMapComments.test.ts deleted file mode 100644 index aa20dd9b75..0000000000 --- a/tests/boundary/filesystem/javascriptSourceMapComments.test.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { 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("creates source-map graph relationships only for real comments", async () => { - const root = await createTestTempDirectory("rea-source-map-comments-"); - const path = join(root, "documentation.js"); - const quoted = 'const documentation = "//# sourceMappingURL=ghost.map ";'; - await writeFile(path, quoted); - const withoutDirective = await reconstructJavaScriptArtifact({ - input_path: root, - }); - expect(withoutDirective.statistics.parse_failures).toBe(0); - expect( - withoutDirective.graph.nodes.filter(({ kind }) => kind === "source-map"), - ).toEqual([]); - expect( - withoutDirective.graph.edges.filter( - ({ relation, target_node_id }) => - relation === "maps_to" && - withoutDirective.graph.nodes.some( - ({ kind, node_id }) => - kind === "source-map" && node_id === target_node_id, - ), - ), - ).toEqual([]); - - await writeFile(path, `${quoted}\n//# sourceMappingURL=real.map\n`); - const withDirective = await reconstructJavaScriptArtifact({ - input_path: root, - }); - expect( - withDirective.graph.nodes.filter(({ kind }) => kind === "source-map"), - ).toHaveLength(1); - const nodes = new Map( - withDirective.graph.nodes.map((node) => [node.node_id, node]), - ); - const sourceMapEdge = withDirective.graph.edges.find( - ({ relation, target_node_id }) => - relation === "maps_to" && - nodes.get(target_node_id)?.kind === "source-map", - ); - expect( - nodes - .get(sourceMapEdge?.target_node_id ?? "") - ?.observations.map(({ properties }) => properties.declared_url), - ).toContain("real.map"); -}); diff --git a/tests/boundary/filesystem/javascriptSourceMapLocations.test.ts b/tests/boundary/filesystem/javascriptSourceMapLocations.test.ts index 21d5719e06..0725cd2161 100644 --- a/tests/boundary/filesystem/javascriptSourceMapLocations.test.ts +++ b/tests/boundary/filesystem/javascriptSourceMapLocations.test.ts @@ -11,55 +11,65 @@ import { serializeJavaScriptApplicationGraph, } from "../../../src/domain/javascript/javascriptApplicationGraph.js"; -it.each(["\n", "\r\n", "\r", "\u2028", "\u2029"])( - "preserves source-map graph evidence coordinates after %j", - async (separator) => { - const root = await createTestTempDirectory("rea-source-map-locations-"); - const directive = "//# sourceMappingURL=app.js.map"; - const source = `const marker = "😀";${separator} ${directive}`; - await writeFile(join(root, "app.js"), source); - const result = await reconstructJavaScriptArtifact({ input_path: root }); - expect(result.statistics.parse_failures).toBe(0); - const nodes = new Map( - result.graph.nodes.map((node) => [node.node_id, node]), - ); - const edge = result.graph.edges.find( - ({ relation, target_node_id }) => - relation === "maps_to" && - nodes.get(target_node_id)?.kind === "source-map", - ); - expect(edge).toMatchObject({ - properties: { - declared_url_sha256: createHash("sha256") - .update("app.js.map") - .digest("hex"), - resolved_path: null, - }, - evidence: { - state: "inferred", - artifact: { - sha256: createHash("sha256").update(source).digest("hex"), - }, - location: { - available: true, - value: { - kind: "source-range", - source: "app.js", - start: { line: 2, column: 2 }, - end: { line: 2, column: directive.length + 2 }, +it("publishes the location of real source-map comments across JavaScript line breaks", async () => { + const root = await createTestTempDirectory("rea-source-map-locations-"); + const directive = "//# sourceMappingURL=app.js.map"; + const quoted = [ + 'const marker = "😀 //# sourceMappingURL=ghost.map";', + "const template = `/*# sourceMappingURL=template.map */`;", + ].join(" "); + const files = ["\n", "\r\n", "\r", "\u2028", "\u2029"].map( + (separator, index) => ({ + name: `app-${index}.js`, + text: `${quoted}${separator} ${directive}`, + }), + ); + await Promise.all( + files.map(({ name, text }) => writeFile(join(root, name), text)), + ); + + const result = await reconstructJavaScriptArtifact({ input_path: root }); + expect(result.statistics.parse_failures).toBe(0); + const nodes = new Map(result.graph.nodes.map((node) => [node.node_id, node])); + const edges = result.graph.edges.filter( + ({ relation, target_node_id }) => + relation === "maps_to" && + nodes.get(target_node_id)?.kind === "source-map", + ); + expect(edges).toHaveLength(files.length); + for (const { name, text } of files) + expect(edges).toContainEqual( + expect.objectContaining({ + properties: expect.objectContaining({ + declared_url_sha256: createHash("sha256") + .update("app.js.map") + .digest("hex"), + resolved_path: null, + }), + evidence: expect.objectContaining({ + state: "inferred", + artifact: expect.objectContaining({ + sha256: createHash("sha256").update(text).digest("hex"), + }), + location: { + available: true, + value: { + kind: "source-range", + source: name, + start: { line: 2, column: 2 }, + end: { line: 2, column: directive.length + 2 }, + }, }, - }, - }, - }); - expect( - nodes - .get(edge?.target_node_id ?? "") - ?.observations.some( - ({ properties }) => properties.declared_url === "app.js.map", - ), - ).toBe(true); - }, -); + }), + }), + ); + const declaredUrls = result.graph.nodes + .filter(({ kind }) => kind === "source-map") + .flatMap(({ observations }) => + observations.map(({ properties }) => properties.declared_url), + ); + expect(new Set(declaredUrls)).toEqual(new Set(["app.js.map"])); +}); it("owns an inline source-map URL once and recovers it from the target after JSON roundtrip", async () => { const root = await createTestTempDirectory("rea-inline-source-map-url-"); @@ -134,6 +144,8 @@ it("associates each source-map URL with its edge when aliases share one physical ); expect(mapEdges).toHaveLength(2); expect(mapEdges[0]?.target_node_id).toBe(mapEdges[1]?.target_node_id); + const mapNode = nodes.get(mapEdges[0]?.target_node_id ?? ""); + expect(mapNode?.identity.strategy).toBe("content-digest"); // Edge order follows content-derived edge IDs, so pair each edge with the // script whose comment declared it. const declaredBySource = new Map( @@ -152,6 +164,8 @@ it("associates each source-map URL with its edge when aliases share one physical { declared_url: observation?.properties.declared_url, resolved_path: edge.properties.resolved_path, + resolution_status: edge.properties.resolution_status, + available: observation?.properties.available, }, ]; }), @@ -160,15 +174,19 @@ it("associates each source-map URL with its edge when aliases share one physical "dist/one.js": { declared_url: "app.js.map", resolved_path: "dist/app.js.map", + resolution_status: "resolved", + available: true, }, "dist/two.js": { declared_url: "./app.js.map", resolved_path: "dist/app.js.map", + resolution_status: "resolved", + available: true, }, }); }); -it("resolves a bare source-map file name and module worker URL next to the script", async () => { +it("resolves a module worker relative to its script while keeping the document base unknown", async () => { const root = await createTestTempDirectory("rea-source-map-relative-"); await mkdir(join(root, "dist")); await writeFile( @@ -176,7 +194,6 @@ it("resolves a bare source-map file name and module worker URL next to the scrip [ 'new Worker(new URL("worker.js", import.meta.url));', 'navigator.serviceWorker.register("sw.js");', - "//# sourceMappingURL=app.js.map", ].join("\n"), ); await writeFile( @@ -184,43 +201,8 @@ it("resolves a bare source-map file name and module worker URL next to the scrip "self.onmessage = () => {};", ); await writeFile(join(root, "dist", "sw.js"), "self.onfetch = () => {};"); - await writeFile( - join(root, "dist", "app.js.map"), - JSON.stringify({ - version: 3, - sources: ["../src/app.js"], - sourcesContent: ['new Worker("worker.js");'], - names: [], - mappings: "AAAA", - }), - ); const result = await reconstructJavaScriptArtifact({ input_path: root }); const nodes = new Map(result.graph.nodes.map((node) => [node.node_id, node])); - const mapEdge = result.graph.edges.find( - ({ relation, target_node_id }) => - relation === "maps_to" && - nodes.get(target_node_id)?.kind === "source-map", - ); - expect(mapEdge?.properties).toEqual({ - declared_url_sha256: createHash("sha256") - .update("app.js.map") - .digest("hex"), - resolved_path: "dist/app.js.map", - resolution_status: "resolved", - }); - expect( - nodes - .get(mapEdge?.target_node_id ?? "") - ?.observations.some( - ({ properties }) => - properties.declared_url === "app.js.map" && - properties.resolved_path === "dist/app.js.map" && - properties.available === true, - ), - ).toBe(true); - expect(nodes.get(mapEdge?.target_node_id ?? "")?.identity.strategy).toBe( - "content-digest", - ); const workerEdge = result.graph.edges.find( ({ relation, source_node_id }) => relation === "maps_to" && nodes.get(source_node_id)?.kind === "worker", diff --git a/tests/boundary/javascript/sourceMapDecoder.test.ts b/tests/boundary/javascript/sourceMapDecoder.test.ts index 88d052691f..a9e1357066 100644 --- a/tests/boundary/javascript/sourceMapDecoder.test.ts +++ b/tests/boundary/javascript/sourceMapDecoder.test.ts @@ -1,9 +1,10 @@ +import { createHash } from "node:crypto"; import { fileURLToPath } from "node:url"; import { access } from "node:fs/promises"; import { expect, it, vi } from "vitest"; -import { AnalysisInputError } from "../../../src/domain/analysisErrorCore.js"; +import { PrivateRuntimeRootUnavailableError } from "../../../src/domain/privateRuntimeRootUnavailableError.js"; +import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js"; import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js"; -import { webSourceMapReportSchema } from "../../../src/domain/webSourceLocation.js"; import { SourceMapDecoder } from "../../../src/javascript/sourceMaps/SourceMapDecoder.js"; import type { SourceMapDecoderDependencies } from "../../../src/javascript/sourceMaps/SourceMapCommand.js"; import { PrivateRuntimeRoot } from "../../../src/process/PrivateRuntimeRoot.js"; @@ -49,32 +50,151 @@ const input = { position: webSourceLocationArgs.generated_position, }; -it("starts the codec deadline after cold ownership preparation", async () => { - vi.useFakeTimers(); - try { - let prepared = false; - const responsePromise = realDecoder({ - prepareOwnership: async (signal) => { - await vi.advanceTimersByTimeAsync(20_001); - expect(signal?.aborted).not.toBe(true); - prepared = true; - }, - launcher: (options) => { - expect(prepared).toBe(true); - expect(options.signal?.aborted).not.toBe(true); - return Promise.reject(new Error("observed launch after preparation")); - }, - }).trace(input); - const response = await responsePromise; - if (response.ok) throw new Error("expected launch failure"); - expect(response.error).toMatchObject({ - diagnostics: { error_message: "observed launch after preparation" }, +it.each([ + { name: "null", reason: null }, + { + name: "an opaque cleanup error", + reason: new ProviderCleanupError("caller", ["caller-owned-resource"], {}), + }, +])( + "preserves cancellation during ownership preparation with $name", + async ({ reason }) => { + const controller = new AbortController(); + const createRuntime = vi.fn(async () => { + throw new Error("Cancelled preparation must not acquire a runtime"); }); - } finally { - vi.useRealTimers(); - } + const launcher = vi.fn(async () => { + throw new Error("Cancelled preparation must not launch a codec"); + }); + const decoder = realDecoder({ + prepareOwnership: async (signal) => { + controller.abort(reason); + signal?.throwIfAborted(); + }, + createRuntime, + launcher, + }); + const result = await decoder.trace(input, { signal: controller.signal }); + if (result.ok) throw new Error("Expected preparation cancellation"); + expect(result.error).toMatchObject({ + _tag: "AnalysisCancelledError", + cleanupIncomplete: false, + cleanupResources: [], + }); + expect(result.error.cause).toBe(reason); + await decoder.close(); + if (reason instanceof ProviderCleanupError) { + expect(reason.cleanupIncomplete).toBe(true); + expect(reason.cleanupResources).toEqual(["caller-owned-resource"]); + } + expect(createRuntime).not.toHaveBeenCalled(); + expect(launcher).not.toHaveBeenCalled(); + }, +); + +it("retains diagnostics for an unknown ownership preparation failure", async () => { + const cause = new Error("unexpected ownership failure"); + const createRuntime = vi.fn(async () => { + throw new Error("Failed preparation must not acquire a runtime"); + }); + const decoder = realDecoder({ + prepareOwnership: async () => { + throw cause; + }, + createRuntime, + }); + const result = await decoder.trace(input); + if (result.ok) throw new Error("Expected preparation failure"); + expect(result.error).toMatchObject({ + _tag: "ProviderAdapterError", + cleanupIncomplete: false, + diagnostics: { + map_path: input.path, + error_message: cause.message, + phase: "process_ownership_preparation", + }, + }); + expect(result.error.cause).toBe(cause); + expect(createRuntime).not.toHaveBeenCalled(); }); +it.each([ + { retryable: true, category: "unavailable" }, + { retryable: false, category: "unsupported_provider" }, +])( + "preserves private-runtime rejection as $category", + async ({ retryable, category }) => { + const reason = "Native runtime self-test failed with a sharing violation"; + const launcher = vi.fn(async () => { + throw new Error("A refused runtime must not launch a codec"); + }); + const decoder = realDecoder({ + prepareOwnership: async () => {}, + createRuntime: async () => { + throw new PrivateRuntimeRootUnavailableError(reason, { retryable }); + }, + launcher, + }); + const result = await decoder.trace(input); + if (result.ok) throw new Error("Expected runtime refusal"); + expect(projectAnalysisError(result.error)).toMatchObject({ + code: "capability_unavailable", + category, + retryable, + details: { + provider_id: "source-map-decoder", + operation: "trace_web_source_location", + reason, + }, + }); + expect(result.error.cleanupIncomplete).toBe(false); + expect(launcher).not.toHaveBeenCalled(); + }, +); + +it.each([ + { name: "default", selected: undefined, expectedMs: 20_000 }, + { name: "selected", selected: 250, expectedMs: 250 }, +])( + "starts the $name codec deadline after ownership preparation", + async ({ selected, expectedMs }) => { + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance"] }); + let path: string | undefined; + try { + const response = await realDecoder({ + prepareOwnership: async () => { + await vi.advanceTimersByTimeAsync(20_001); + }, + createRuntime: async () => { + const root = await PrivateRuntimeRoot.create(); + path = root.path; + return root; + }, + launcher: async ({ signal }) => { + await vi.advanceTimersByTimeAsync(expectedMs - 1); + expect(signal?.aborted).toBe(false); + await vi.advanceTimersByTimeAsync(1); + expect(signal?.aborted).toBe(true); + throw new Error("launcher settled after deadline"); + }, + }).trace({ + ...input, + ...(selected === undefined ? {} : { decodeTimeoutMs: selected }), + }); + if (response.ok) throw new Error("expected decode deadline"); + expect(response.error).toMatchObject({ + _tag: "AnalysisTimeoutError", + userMessage: expect.stringContaining(String(expectedMs)), + }); + expect(response.error.userMessage).toContain("decode_timeout_ms"); + if (path === undefined) throw new Error("runtime not acquired"); + await expect(access(path)).rejects.toMatchObject({ code: "ENOENT" }); + } finally { + vi.useRealTimers(); + } + }, +); + it.each([ { heap: undefined, effectiveHeap: 192 }, { heap: 384, effectiveHeap: 384 }, @@ -101,7 +221,10 @@ it.each([ if (!response.ok) throw response.error; expect(response.value).toMatchObject({ engine: { id: "jridgewell-trace-mapping", version: "0.3.31" }, - matches: [{ content: { text: "original" } }], + source_map_sha256: fixture.sourceMap.file.sha256, + url_context: input.url, + lookup: { requested: input.position }, + matches: [{ state: "mapped", content: { text: "original" } }], }); if (process.versions.bun === undefined) { expect(response.value.runtime).toMatchObject({ @@ -135,7 +258,6 @@ it.each([ ); it.each([ - { cancel: false, denyCleanup: false }, { cancel: true, denyCleanup: false }, { cancel: false, denyCleanup: true }, { cancel: true, denyCleanup: true }, @@ -145,7 +267,7 @@ it.each([ const controller = new AbortController(); const reason = { phase: "source-map root finalization" }; let path: string | undefined; - let cleanupAttempts = 0; + let denyNextClose = denyCleanup; const decoder = realDecoder({ createRuntime: async () => { const root = await PrivateRuntimeRoot.create(); @@ -153,10 +275,11 @@ it.each([ return { path: root.path, close: async () => { - cleanupAttempts += 1; if (cancel) controller.abort(reason); - if (denyCleanup && cleanupAttempts === 1) + if (denyNextClose) { + denyNextClose = false; throw new Error("controlled source-map cleanup denial"); + } await root.close(); }, }; @@ -166,39 +289,33 @@ it.each([ const response = await decoder.trace(input, { signal: controller.signal, }); - expect(response.ok).toBe(!cancel && !denyCleanup); - const report = webSourceMapReportSchema.parse( - response.ok ? response.value : response.error.partialObservation, - ); + if (response.ok) throw new Error("Expected interrupted finalization"); + const { error } = response; + const report = error.partialObservation; expect(report).toMatchObject({ source_map_sha256: fixture.sourceMap.file.sha256, url_context: input.url, lookup: { requested: input.position }, matches: [{ state: "mapped", content: { text: "original" } }], }); + expect(error).toMatchObject({ operation: "trace_web_source_location" }); + expect(projectAnalysisError(error)).toMatchObject({ + details: { partial_observation: report }, + }); + if (cancel) { + const primary = error.cleanupPrimary ?? error; + expect(primary._tag).toBe("AnalysisCancelledError"); + expect(primary.cause).toBe(reason); + } if (path === undefined) throw new Error("runtime not acquired"); - if (!response.ok) { - expect(response.error).toMatchObject({ - operation: "trace_web_source_location", - }); - expect(projectAnalysisError(response.error)).toMatchObject({ - details: { partial_observation: report }, - }); - if (cancel) { - const primary = response.error.cleanupPrimary ?? response.error; - expect(primary._tag).toBe("AnalysisCancelledError"); - expect(primary.cause).toBe(reason); - } - expect(response.error.cleanupIncomplete).toBe(denyCleanup); - if (denyCleanup) { - expect(response.error.cleanupResources).toEqual([path]); - await expect(access(path)).resolves.toBeUndefined(); - await decoder.close(); - expect(cleanupAttempts).toBe(2); - expect(response.error.cleanupIncomplete).toBe(false); - expect(response.error.cleanupResources).toEqual([]); - expect(response.error.partialObservation).toEqual(report); - } + expect(error.cleanupIncomplete).toBe(denyCleanup); + if (denyCleanup) { + expect(error.cleanupResources).toEqual([path]); + await expect(access(path)).resolves.toBeUndefined(); + await decoder.close(); + expect(error.cleanupIncomplete).toBe(false); + expect(error.cleanupResources).toEqual([]); + expect(error.partialObservation).toEqual(report); } await expect(access(path)).rejects.toMatchObject({ code: "ENOENT" }); } finally { @@ -212,17 +329,20 @@ it.each([false, true])( async (denyCleanup) => { const controller = new AbortController(); const reason = new Error("cancel after codec refusal"); - let cleanupAttempts = 0; + let path: string | undefined; + let denyNextClose = denyCleanup; const decoder = realDecoder({ createRuntime: async () => { const root = await PrivateRuntimeRoot.create(); + path = root.path; return { path: root.path, close: async () => { - cleanupAttempts += 1; controller.abort(reason); - if (denyCleanup && cleanupAttempts === 1) + if (denyNextClose) { + denyNextClose = false; throw new Error("controlled refusal cleanup denial"); + } await root.close(); }, }; @@ -234,15 +354,28 @@ it.each([false, true])( { signal: controller.signal }, ); if (response.ok) throw new Error("Expected malformed-map refusal"); - expect(controller.signal.reason).toBe(reason); - expect(response.error.cleanupPrimary ?? response.error).toBeInstanceOf( - AnalysisInputError, + const { error } = response; + const primary = error.cleanupPrimary ?? error; + expect(primary).toMatchObject({ + _tag: "AnalysisInputError", + operation: "trace_web_source_location", + issues: [{ reason: "invalid_format" }], + }); + const cancellation = primary.secondaryFailures.find( + ({ _tag }) => _tag === "AnalysisCancelledError", ); - expect(response.error.partialObservation).toBeUndefined(); - expect(response.error.cleanupIncomplete).toBe(denyCleanup); - await decoder.close(); - expect(response.error.cleanupIncomplete).toBe(false); - expect(cleanupAttempts).toBe(denyCleanup ? 2 : 1); + expect(cancellation?.cause).toBe(reason); + expect(error.partialObservation).toBeUndefined(); + if (path === undefined) throw new Error("runtime not acquired"); + expect(error.cleanupIncomplete).toBe(denyCleanup); + if (denyCleanup) { + expect(error.cleanupResources).toEqual([path]); + await expect(access(path)).resolves.toBeUndefined(); + await decoder.close(); + expect(error.cleanupIncomplete).toBe(false); + expect(error.cleanupResources).toEqual([]); + } + await expect(access(path)).rejects.toMatchObject({ code: "ENOENT" }); } finally { await decoder.close(); } @@ -250,14 +383,6 @@ it.each([false, true])( ); it.each([ - { - failure: "malformed JSON", - text: "{", - error: { - _tag: "AnalysisInputError", - issues: [{ reason: "invalid_format" }], - }, - }, { failure: "external section", text: JSON.stringify({ @@ -279,7 +404,12 @@ it.each([ failure: "malformed embedded map before row admission", text: JSON.stringify({ version: 3, - sections: [{ offset: { line: 1000000000, column: 0 }, map: {} }], + sections: [ + { + offset: { line: 1000000000, column: 0 }, + map: { version: "3", sections: [] }, + }, + ], }), error: { _tag: "AnalysisInputError", @@ -308,6 +438,49 @@ it.each([ detail: expect.stringContaining("generated-row decoder budget"), }, }, + { + failure: "invalid section list with regular-map fields", + text: JSON.stringify({ + version: 3, + sections: null, + sources: ["ignored.ts"], + mappings: "AAAA", + }), + error: { + _tag: "AnalysisInputError", + issues: [ + { + reason: "invalid_format", + message: expect.stringContaining("sections"), + }, + ], + }, + }, + { + failure: "overlapping indexed mappings", + text: JSON.stringify({ + version: 3, + sections: [ + { + offset: { line: 0, column: 0 }, + map: { version: 3, sources: ["a.js"], mappings: "AAAA,EAAA" }, + }, + { + offset: { line: 0, column: 1 }, + map: { version: 3, sources: ["b.js"], mappings: "AAAA" }, + }, + ], + }), + error: { + _tag: "AnalysisInputError", + issues: [ + { + reason: "invalid_format", + message: expect.stringContaining("overlap"), + }, + ], + }, + }, ])("preserves actual producer failures: $failure", async ({ text, error }) => { const response = await realDecoder().trace({ ...input, text }); if (response.ok) throw new Error("expected failed map"); @@ -317,47 +490,8 @@ it.each([ }); }); -it("keeps indexed section overlap typed as a malformed source map", async () => { - const text = JSON.stringify({ - version: 3, - sections: [ - { - offset: { line: 0, column: 0 }, - map: { - version: 3, - sources: ["a.js"], - sourcesContent: ["x();"], - names: [], - mappings: "AAAA,EAAA", - }, - }, - { - offset: { line: 0, column: 1 }, - map: { - version: 3, - sources: ["b.js"], - sourcesContent: ["y();"], - names: [], - mappings: "AAAA", - }, - }, - ], - }); - const response = await realDecoder().trace({ ...input, text }); - if (response.ok) throw new Error("expected overlapping map rejection"); - expect(response.error).toBeInstanceOf(AnalysisInputError); - if (!(response.error instanceof AnalysisInputError)) - throw new Error("expected malformed source-map input error"); - expect(response.error.issues).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - reason: "invalid_format", - message: expect.stringContaining("overlap"), - }), - ]), - ); -}); it("cancels a real owned process and removes the runtime request", async () => { + const reason = { caller: "cancel active codec" }; const controller = new AbortController(); let path: string | undefined; const response = await realDecoder({ @@ -375,46 +509,57 @@ it("cancels a real owned process and removes the runtime request", async () => { ], }); await waitForProviderProcessReady(child.process); - setImmediate(() => controller.abort()); + setImmediate(() => controller.abort(reason)); return child; }, }).trace(input, { signal: controller.signal }); if (response.ok) throw new Error("expected cancellation"); expect(response.error._tag).toBe("AnalysisCancelledError"); + expect(response.error.cause).toBe(reason); if (path === undefined) throw new Error("runtime not acquired"); await expect(access(path)).rejects.toMatchObject({ code: "ENOENT" }); }); -it("retains final diagnostic bytes for an actual failed codec process", async () => { - const response = await realDecoder({ - launcher: (options) => - spawnOwnedProviderProcess({ - ...options, - arguments: [ - "-e", - 'process.stderr.write("final diagnostic"); process.exitCode = 7', - ], - }), - }).trace(input); - if (response.ok) throw new Error("expected exit failure"); - expect(response.error).toMatchObject({ - diagnostics: { exit_code: 7, stderr: "final diagnostic" }, - }); -}); -it("reports complete evidence amplification as a resource failure before serializing an oversized reply", async () => { - const text = JSON.stringify({ +it("returns all point matches when they fit and reports output exhaustion otherwise", async () => { + const embedded = "a".repeat(1_800); + const text = `\uFEFF)]}'transport prefix\r${JSON.stringify({ version: 3, - sources: ["large.ts"], - sourcesContent: ["a".repeat(600000)], + sources: ["original.ts"], + sourcesContent: [embedded], names: [], - mappings: Array.from({ length: 60 }, () => "AAAA").join(","), - }); - const response = await realDecoder().trace({ ...input, text }); - if (response.ok) throw new Error("expected complete-evidence budget failure"); - expect(response.error).toMatchObject({ + mappings: "AAAA,AAAC,AAAC,AAAC", + })}`; + const trace = (outputBytes: number) => + realDecoder().trace({ + ...input, + text, + limits: { output_bytes: outputBytes }, + }); + + const complete = await trace(14_000); + if (!complete.ok) throw complete.error; + expect(complete.value.source_map_sha256).toBe( + createHash("sha256").update(text).digest("hex"), + ); + expect(complete.value.matches).toMatchObject( + [0, 1, 2, 3].map((column) => ({ + reported_source: "original.ts", + original_position: { line: 1, column }, + content: { state: "embedded", text: embedded }, + })), + ); + + const budget = 5_500; + expect(Buffer.byteLength(JSON.stringify(complete.value))).toBeGreaterThan( + budget, + ); + const limited = await trace(budget); + if (limited.ok) throw new Error("expected complete-evidence budget failure"); + expect(limited.error).toMatchObject({ _tag: "ArtifactOperationError", reason: "limit", - detail: expect.stringContaining("no partial"), + detail: expect.stringContaining("source_map_limits.output_bytes is 5500"), }); + expect(limited.error.partialObservation).toBeUndefined(); }); it("rejects a malformed actual subprocess reply", async () => { const response = await realDecoder({ diff --git a/tests/boundary/javascript/sourceMapPreparation.test.ts b/tests/boundary/javascript/sourceMapPreparation.test.ts deleted file mode 100644 index 904462079f..0000000000 --- a/tests/boundary/javascript/sourceMapPreparation.test.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { expect, it, vi } from "vitest"; -import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js"; -import { ProviderAdapterError } from "../../../src/domain/providerAdapterError.js"; -import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js"; -import { - runSourceMapCommand, - type SourceMapCleanupRetention, -} from "../../../src/javascript/sourceMaps/SourceMapCommand.js"; - -it.each([ - null, - new ProviderCleanupError("caller", ["caller-owned-resource"], {}), -])( - "preserves opaque cancellation during ownership preparation: %s", - async (reason) => { - const controller = new AbortController(); - const retainedCleanup: SourceMapCleanupRetention = {}; - const createRuntime = vi.fn(async () => { - throw new Error("Cancelled preparation must not acquire a runtime"); - }); - const launcher = vi.fn(async () => { - throw new Error("Cancelled preparation must not launch a codec"); - }); - const result = await runSourceMapCommand( - "{}", - "/selected.map", - { signal: controller.signal }, - { - prepareOwnership: async (signal) => { - controller.abort(reason); - signal?.throwIfAborted(); - }, - createRuntime, - launcher, - }, - retainedCleanup, - ); - expect(retainedCleanup.owner).toBeUndefined(); - if (result.ok) throw new Error("Preparation must return cancellation"); - expect(result.error).toBeInstanceOf(AnalysisCancelledError); - expect(result.error.cause).toBe(reason); - expect(result.error.cleanupIncomplete).toBe(false); - expect(result.error.cleanupResources).toEqual([]); - if (reason instanceof ProviderCleanupError) { - expect(reason.cleanupIncomplete).toBe(true); - expect(reason.cleanupResources).toEqual(["caller-owned-resource"]); - } - expect(createRuntime).not.toHaveBeenCalled(); - expect(launcher).not.toHaveBeenCalled(); - }, -); - -it("retains diagnostics for an unknown ownership preparation failure", async () => { - const cause = new Error("unexpected ownership failure"); - const retainedCleanup: SourceMapCleanupRetention = {}; - const createRuntime = vi.fn(async () => { - throw new Error("Failed preparation must not acquire a runtime"); - }); - const launcher = vi.fn(async () => { - throw new Error("Failed preparation must not launch a codec"); - }); - const result = await runSourceMapCommand( - "{}", - "/selected.map", - undefined, - { - prepareOwnership: async () => { - throw cause; - }, - createRuntime, - launcher, - }, - retainedCleanup, - ); - expect(retainedCleanup.owner).toBeUndefined(); - if (result.ok) throw new Error("Preparation must return its failure"); - expect(result.error).toBeInstanceOf(ProviderAdapterError); - expect(result.error.cause).toBe(cause); - expect(result.error).toMatchObject({ - diagnostics: { - map_path: "/selected.map", - error_message: cause.message, - phase: "process_ownership_preparation", - }, - }); - expect(createRuntime).not.toHaveBeenCalled(); - expect(launcher).not.toHaveBeenCalled(); -}); diff --git a/tests/boundary/javascript/sourceMapRuntimeAvailability.test.ts b/tests/boundary/javascript/sourceMapRuntimeAvailability.test.ts deleted file mode 100644 index c81a92cd7e..0000000000 --- a/tests/boundary/javascript/sourceMapRuntimeAvailability.test.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { expect, it } from "vitest"; -import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js"; -import { - runSourceMapCommand, - type SourceMapCleanupRetention, -} from "../../../src/javascript/sourceMaps/SourceMapCommand.js"; -import { PrivateRuntimeRootUnavailableError } from "../../../src/domain/privateRuntimeRootUnavailableError.js"; - -it.each([false, true])( - "preserves private-runtime retryable=%s through the source-map operation", - async (retryable) => { - const reason = "Native runtime self-test failed with a sharing violation"; - let launches = 0; - const retainedCleanup: SourceMapCleanupRetention = {}; - const result = await runSourceMapCommand( - "{}", - "/selected.map", - undefined, - { - prepareOwnership: async () => {}, - createRuntime: async () => { - throw new PrivateRuntimeRootUnavailableError(reason, { retryable }); - }, - launcher: async () => { - launches += 1; - throw new Error("A refused runtime must not launch a codec"); - }, - }, - retainedCleanup, - ); - expect(retainedCleanup.owner).toBeUndefined(); - expect(launches).toBe(0); - if (result.ok) throw new Error("Unavailable runtime must refuse decoding"); - expect(projectAnalysisError(result.error)).toMatchObject({ - code: "capability_unavailable", - category: retryable ? "unavailable" : "unsupported_provider", - retryable, - details: { - provider_id: "source-map-decoder", - operation: "trace_web_source_location", - reason, - }, - }); - expect(result.error.cleanupIncomplete).toBe(false); - }, -); diff --git a/tests/boundary/mcp/browserSourceMapCancellationMcp.test.ts b/tests/boundary/mcp/browserSourceMapCancellationMcp.test.ts index 813c480b36..57c06732c7 100644 --- a/tests/boundary/mcp/browserSourceMapCancellationMcp.test.ts +++ b/tests/boundary/mcp/browserSourceMapCancellationMcp.test.ts @@ -1,21 +1,14 @@ -import { createHash } from "node:crypto"; - -import type { CallToolResult } from "@modelcontextprotocol/server"; -import { expect, it, onTestFinished, vi } from "vitest"; -import { z } from "zod"; +import { expect, it, onTestFinished } from "vitest"; import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js"; import { toolContract } from "../../../src/contracts/toolContracts.js"; -import { parseEvidence } from "../../../src/domain/evidence.js"; import { webBundleAcquisitionPartialObservationSchema } from "../../../src/domain/webBundleAnalysis.js"; -import { ToolResultDelivery } from "../../../src/server/ToolResultDelivery.js"; import { startSourceMapCancellationBrowser } from "../../fixtures/browserSourceMapCancellation.js"; -import { createDeferred } from "../../fixtures/binarySession.js"; import { createApplicationMcpHarness, type ApplicationMcpHarness, } from "../../fixtures/applicationMcpHarness.js"; -import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; +import { waitFor } from "../../support/testWait.js"; it("retains completed source maps through MCP cancellation for later Evidence retrieval", async () => { const { @@ -30,7 +23,6 @@ it("retains completed source maps through MCP cancellation for later Evidence re let harness: ApplicationMcpHarness | undefined; onTestFinished(async () => { controller.abort(); - vi.restoreAllMocks(); releasePendingResponse(); try { await harness?.close(); @@ -43,16 +35,6 @@ it("retains completed source maps through MCP cancellation for later Evidence re }); harness = connected; - const delivered = createDeferred(); - const original = ToolResultDelivery.prototype.toPartialErrorToolResult; - vi.spyOn( - ToolResultDelivery.prototype, - "toPartialErrorToolResult", - ).mockImplementation(function (this: ToolResultDelivery, ...args) { - const result = original.apply(this, args); - delivered.resolve(result); - return result; - }); const call = connected.client.callTool( { name: "analyze_web_bundle", @@ -66,7 +48,7 @@ it("retains completed source maps through MCP cancellation for later Evidence re }, { signal: controller.signal }, ); - const pendingMapPath = await Promise.race([ + await Promise.race([ secondRequest, call.then( () => { @@ -78,39 +60,29 @@ it("retains completed source maps through MCP cancellation for later Evidence re ), ]); const retainedMapPath = await completedMapPath; - expect([retainedMapPath, pendingMapPath].sort()).toEqual([ - "/first.js.map", - "/second.js.map", - ]); controller.abort(new Error("Caller cancelled after the first map")); await expect(call).rejects.toThrow(); - // SDK cancellation suppresses the reply. This is the server's recording boundary. - const { error } = parseMcpToolError(await delivered.promise); - expect(error.code).toBe("cancelled"); - const reference = z - .object({ - partial_observation: z.object({ - kind: z.literal("retained-evidence"), - evidence_id: z.string(), - }), - }) - .parse(error.details).partial_observation; - const reply = await connected.client.callTool({ - name: "get_evidence_bundle", - arguments: { detail: "complete" }, + // The SDK suppresses the cancelled reply. Wait for the public Evidence bundle + // to publish the acquisition after cleanup. + const evidence = await waitFor(async () => { + const reply = await connected.client.callTool({ + name: "get_evidence_bundle", + arguments: { detail: "complete" }, + }); + expect(reply.isError, JSON.stringify(reply.content)).not.toBe(true); + const bundle = toolContract("get_evidence_bundle").outputSchema.parse( + reply.structuredContent, + ).result; + if ("kind" in bundle) throw new Error("Expected complete Evidence bundle"); + const records = bundle.records.filter( + ({ operation }) => operation === "analyze_web_bundle", + ); + expect(records).toHaveLength(1); + const record = records[0]; + if (record === undefined) throw new Error("Expected retained acquisition"); + return record; }); - expect(reply.isError, JSON.stringify(reply.content)).not.toBe(true); - const bundle = toolContract("get_evidence_bundle").outputSchema.parse( - reply.structuredContent, - ).result; - if ("kind" in bundle) throw new Error("Expected complete Evidence bundle"); - const evidence = parseEvidence( - bundle.records.find( - (record) => record.evidence_id === reference.evidence_id, - ), - ); - expect(evidence.operation).toBe("analyze_web_bundle"); const acquisition = webBundleAcquisitionPartialObservationSchema.parse( evidence.normalized_result, ); @@ -119,32 +91,15 @@ it("retains completed source maps through MCP cancellation for later Evidence re status: "partial", requested: 2, processed: 1, - excluded: [ - { section: "source_maps", reason: "disallowed_origin", count: 1 }, - ], items: [ { status: "included", declared_url: `${browser.allowedOrigin}${retainedMapPath}`, artifact: { text: mapText, - sha256: createHash("sha256").update(mapText).digest("hex"), }, }, ], }); - expect(acquisition.source_maps.items).toHaveLength(1); - expect(acquisition.inspection.completeness.excluded).toContainEqual({ - section: "source_maps", - reason: "disallowed_origin", - count: 1, - }); - expect(JSON.stringify(evidence)).not.toContain("outside.example.test"); - await expect(connected.client.ping()).resolves.toEqual({}); expect(browser.activeConnectionCount()).toBe(0); - expect( - browser.httpRequests - .filter(({ url }) => url.endsWith(".map")) - .map(({ url }) => url), - ).toEqual([retainedMapPath, pendingMapPath]); }); diff --git a/tests/boundary/process/sourceMapUncHosts.test.ts b/tests/boundary/process/sourceMapUncHosts.test.ts index e09bfec02a..6fb0b88ec9 100644 --- a/tests/boundary/process/sourceMapUncHosts.test.ts +++ b/tests/boundary/process/sourceMapUncHosts.test.ts @@ -62,27 +62,36 @@ const cases = [ }, ] as const; -processTest.for(cases)( - "resolves $name in an owned subprocess without a native abort", - async (scenario, { processes }) => { - const sourceRoot = "root" in scenario ? scenario.root : null; - // Import the built production helper in a child. A native assertion must - // become a failed signal assertion here, never terminate the Vitest worker. +processTest( + "preserves UNC host resolution and source spelling in an owned subprocess", + async ({ processes }) => { + const inputs = cases.map((scenario) => ({ + name: scenario.name, + source: scenario.source, + root: "root" in scenario ? scenario.root : null, + })); const result = await processes.run(process.execPath, [ "--input-type=module", "--eval", `import { resolveJavaScriptSourceMapReferences } from "./dist/domain/javascript/javascriptSourceMapPaths.js"; - const [source, root] = JSON.parse(process.argv[1]); - console.log(JSON.stringify(resolveJavaScriptSourceMapReferences([source], root, "maps/main.js.map")[0]));`, - JSON.stringify([scenario.source, sourceRoot]), + const inputs = JSON.parse(process.argv[1]); + const references = inputs.map(({ name, source, root }) => { + console.error("Resolving", name); + return resolveJavaScriptSourceMapReferences([source], root, "maps/main.js.map")[0]; + }); + console.log(JSON.stringify(references));`, + JSON.stringify(inputs), ]); + expect(result.signal, result.stderr).toBeNull(); expect(result.exitCode, result.stderr).toBe(0); - expect(JSON.parse(result.stdout)).toMatchObject({ - source_name: scenario.source, - source_root: sourceRoot, - map_path: "maps/main.js.map", - resolution: scenario.expected, - }); + expect(JSON.parse(result.stdout)).toMatchObject( + cases.map((scenario) => ({ + source_name: scenario.source, + source_root: "root" in scenario ? scenario.root : null, + map_path: "maps/main.js.map", + resolution: scenario.expected, + })), + ); }, );