From 41bdf03a98b477be669a43731bb94984a5a420aa Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 25 Nov 2026 23:52:17 +0800 Subject: [PATCH] test(browser): share validated inspection comparisons (#14567) --- src/domain/webCaptureDiff.network.test.ts | 10 +-- src/domain/webCaptureDiff.resources.test.ts | 14 +-- ...owserProvider.storage_completeness.test.ts | 11 +-- .../cdpDomComparisonCompleteness.test.ts | 11 +-- .../browser/cdpJsonShapeComparison.test.ts | 11 +-- tests/boundary/browser/webCaptureDiff.test.ts | 89 ++++++------------- tests/support/browser/webCaptureComparison.ts | 24 +++++ 7 files changed, 61 insertions(+), 109 deletions(-) create mode 100644 tests/support/browser/webCaptureComparison.ts diff --git a/src/domain/webCaptureDiff.network.test.ts b/src/domain/webCaptureDiff.network.test.ts index 66c31451926..b4d45798699 100644 --- a/src/domain/webCaptureDiff.network.test.ts +++ b/src/domain/webCaptureDiff.network.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from "vitest"; +import { compareInspectionDimension } from "../../tests/support/browser/webCaptureComparison.js"; import { unselectBodyShapes, @@ -6,16 +7,9 @@ import { withPassiveDroppedEvents, } from "../../tests/fixtures/passiveNetworkCapture.js"; import type { WebPageInspection } from "./browserObservationSchemas.js"; -import { compareWebCaptures } from "./webCaptureDiff.js"; import { compareWebCapturesInputSchema } from "./webCaptureDiffSchemas.js"; -const compare = (before: WebPageInspection, after: WebPageInspection) => - compareWebCaptures( - compareWebCapturesInputSchema.parse({ - before: { inspection: before }, - after: { inspection: after }, - }), - ).dimensions.network; +const compare = compareInspectionDimension("network"); const redirectCapture = (resourceType: string | null): WebPageInspection => { const capture = unselectBodyShapes(passiveNetworkCapture()); diff --git a/src/domain/webCaptureDiff.resources.test.ts b/src/domain/webCaptureDiff.resources.test.ts index 63fe5cb593a..4a02376bd07 100644 --- a/src/domain/webCaptureDiff.resources.test.ts +++ b/src/domain/webCaptureDiff.resources.test.ts @@ -1,9 +1,8 @@ import { describe, expect, it } from "vitest"; +import { compareInspectionDimension } from "../../tests/support/browser/webCaptureComparison.js"; import { passiveNetworkCapture } from "../../tests/fixtures/passiveNetworkCapture.js"; import type { WebPageInspection } from "./browserObservationSchemas.js"; -import { compareWebCaptures } from "./webCaptureDiff.js"; -import { compareWebCapturesInputSchema } from "./webCaptureDiffSchemas.js"; import { stableWebResources } from "./webInventory.js"; const resource = ( @@ -23,16 +22,7 @@ const capture = (resources: readonly ReturnType[]) => { return inspection; }; -const compareResources = ( - before: WebPageInspection, - after: WebPageInspection, -) => - compareWebCaptures( - compareWebCapturesInputSchema.parse({ - before: { inspection: before }, - after: { inspection: after }, - }), - ).dimensions.resources; +const compareResources = compareInspectionDimension("resources"); describe("web resource comparison identity and optional size", () => { it.each([ diff --git a/tests/boundary/browser/cdpBrowserProvider.storage_completeness.test.ts b/tests/boundary/browser/cdpBrowserProvider.storage_completeness.test.ts index 260f84c0894..9e469d557f5 100644 --- a/tests/boundary/browser/cdpBrowserProvider.storage_completeness.test.ts +++ b/tests/boundary/browser/cdpBrowserProvider.storage_completeness.test.ts @@ -1,10 +1,9 @@ import { expect, it } from "vitest"; +import { compareInspectionDimension } from "../../support/browser/webCaptureComparison.js"; import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js"; import { inspectWebPageInputSchema } from "../../../src/domain/browserObservation.js"; import type { WebPageInspection } from "../../../src/domain/browserObservationSchemas.js"; -import { compareWebCaptures } from "../../../src/domain/webCaptureDiff.js"; -import { compareWebCapturesInputSchema } from "../../../src/domain/webCaptureDiffSchemas.js"; import { startFakeCdpBrowser, type FakeOptions, @@ -30,13 +29,7 @@ const inspect = async (options: FakeOptions, includeFingerprints = true) => { return { browser, capture }; }; -const compareStorage = (before: WebPageInspection, after: WebPageInspection) => - compareWebCaptures( - compareWebCapturesInputSchema.parse({ - before: { inspection: before }, - after: { inspection: after }, - }), - ).dimensions.storage; +const compareStorage = compareInspectionDimension("storage"); it.each([ ["CacheStorage.requestCacheNames", {}, []], diff --git a/tests/boundary/browser/cdpDomComparisonCompleteness.test.ts b/tests/boundary/browser/cdpDomComparisonCompleteness.test.ts index 22831ac258c..762b90db05c 100644 --- a/tests/boundary/browser/cdpDomComparisonCompleteness.test.ts +++ b/tests/boundary/browser/cdpDomComparisonCompleteness.test.ts @@ -1,12 +1,11 @@ import { describe, expect, it } from "vitest"; +import { compareInspections as compare } from "../../support/browser/webCaptureComparison.js"; import { writeFile } from "node:fs/promises"; import { join } from "node:path"; import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js"; import { inspectWebPageInputSchema } from "../../../src/domain/browserObservation.js"; import type { WebPageInspection } from "../../../src/domain/browserObservationSchemas.js"; -import { compareWebCaptures } from "../../../src/domain/webCaptureDiff.js"; -import { compareWebCapturesInputSchema } from "../../../src/domain/webCaptureDiffSchemas.js"; import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js"; import { trackBrowser } from "./cdpBrowserProvider.support.js"; import { cliTest } from "../../support/cli/cliFixture.js"; @@ -123,14 +122,6 @@ const startBrowser = async () => { return { state, inspect, origin: browser.allowedOrigin }; }; -const compare = (before: WebPageInspection, after: WebPageInspection) => - compareWebCaptures( - compareWebCapturesInputSchema.parse({ - before: { inspection: before }, - after: { inspection: after }, - }), - ); - const unknownDimension = { status: "unknown", total_changes: 0, changes: [] }; const documentExclusions = (capture: WebPageInspection) => diff --git a/tests/boundary/browser/cdpJsonShapeComparison.test.ts b/tests/boundary/browser/cdpJsonShapeComparison.test.ts index b3b67cb5457..2e41474cec7 100644 --- a/tests/boundary/browser/cdpJsonShapeComparison.test.ts +++ b/tests/boundary/browser/cdpJsonShapeComparison.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from "vitest"; +import { compareInspectionDimension } from "../../support/browser/webCaptureComparison.js"; import { receiveCdpEvent } from "../../../src/browser/CdpConnection.js"; import { CdpCaptureEvents } from "../../../src/browser/CdpCaptureEvents.js"; @@ -8,8 +9,6 @@ import { webPageInspectionSchema, type WebPageInspection, } from "../../../src/domain/browserObservationSchemas.js"; -import { compareWebCaptures } from "../../../src/domain/webCaptureDiff.js"; -import { compareWebCapturesInputSchema } from "../../../src/domain/webCaptureDiffSchemas.js"; import { passiveNetworkCapture } from "../../fixtures/passiveNetworkCapture.js"; const ORIGIN = "https://example.test"; @@ -71,13 +70,7 @@ it("captures all repeated array rows and empty arrays without retaining payload expect(JSON.stringify(shape)).not.toContain("payload-value-excluded"); }); -const compareNetwork = (before: WebPageInspection, after: WebPageInspection) => - compareWebCaptures( - compareWebCapturesInputSchema.parse({ - before: { inspection: before }, - after: { inspection: after }, - }), - ).dimensions.network; +const compareNetwork = compareInspectionDimension("network"); /** Give retained request facts a complete, independently authored inventory. */ const completeInventory = (source: WebPageInspection): WebPageInspection => ({ diff --git a/tests/boundary/browser/webCaptureDiff.test.ts b/tests/boundary/browser/webCaptureDiff.test.ts index 25991ad80ab..2bd8b98a076 100644 --- a/tests/boundary/browser/webCaptureDiff.test.ts +++ b/tests/boundary/browser/webCaptureDiff.test.ts @@ -1,4 +1,8 @@ import { afterEach, describe, expect, it } from "vitest"; +import { + compareInspectionDimension, + compareInspections, +} from "../../support/browser/webCaptureComparison.js"; import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js"; import { inspectWebPageInputSchema } from "../../../src/domain/browserObservation.js"; @@ -37,13 +41,7 @@ const inspectStorage = async ( return captured.value; }; -const compareStorage = (left: WebPageInspection, right: WebPageInspection) => - compareWebCaptures( - compareWebCapturesInputSchema.parse({ - before: { inspection: left }, - after: { inspection: right }, - }), - ).dimensions.storage; +const compareStorage = compareInspectionDimension("storage"); it.each([ ["local", "DOMStorage.getDOMStorageItems", { entries: [null] }], @@ -296,13 +294,7 @@ describe("web capture dimension uncertainty", () => { ]; } - const compareAfter = () => - compareWebCaptures( - compareWebCapturesInputSchema.parse({ - before: { inspection: captured.value }, - after: { inspection: after }, - }), - ); + const compareAfter = () => compareInspections(captured.value, after); const node = after.dom.nodes[0]; if (node === undefined) throw new Error("Expected a retained DOM node"); node.node_name = "MAIN"; @@ -603,12 +595,7 @@ it("compares HTTP and script evidence independently of local request IDs and app after.network.requests[0]!.request_id = "changed-cdp-id"; response.request_id = "changed-cdp-id"; script.source.reason = "different approval explanation"; - const dimensions = compareWebCaptures( - compareWebCapturesInputSchema.parse({ - before: { inspection: before }, - after: { inspection: after }, - }), - ).dimensions; + const dimensions = compareInspections(before, after).dimensions; for (const dimension of [ dimensions.network, dimensions.metadata, @@ -621,13 +608,7 @@ it("compares HTTP and script evidence independently of local request IDs and app }); }); -const compareScripts = (before: WebPageInspection, after: WebPageInspection) => - compareWebCaptures( - compareWebCapturesInputSchema.parse({ - before: { inspection: before }, - after: { inspection: after }, - }), - ).dimensions.scripts; +const compareScripts = compareInspectionDimension("scripts"); const expectScriptMembershipCoverage = (complete: WebPageInspection): void => { const absent = structuredClone(complete); @@ -850,13 +831,6 @@ it("compares partial source-map declarations against compatible script occurrenc inspection.completeness.policy_filtered_sections.push("source_maps"); return inspection; }; - const compare = (left: WebPageInspection, right: WebPageInspection) => - compareWebCaptures( - compareWebCapturesInputSchema.parse({ - before: { inspection: left }, - after: { inspection: right }, - }), - ).dimensions.scripts; const unknown = { status: "unknown", total_changes: 0, changes: [] }; const modified = { status: "changed", @@ -884,8 +858,8 @@ it("compares partial source-map declarations against compatible script occurrenc ], true, ); - expect(compare(partial, partial)).toMatchObject(unknown); - expect(compare(partial, changedMap)).toMatchObject(modified); + expect(compareScripts(partial, partial)).toMatchObject(unknown); + expect(compareScripts(partial, changedMap)).toMatchObject(modified); const omitted = capture( [script(hashA, ""), script(hashB, null), script(hashA, null)], @@ -896,14 +870,14 @@ it("compares partial source-map declarations against compatible script occurrenc script(hashA, ""), script(hashB, map("b")), ]); - expect(compare(omitted, explicit)).toMatchObject(unknown); - expect(compare(explicit, omitted)).toMatchObject(unknown); + expect(compareScripts(omitted, explicit)).toMatchObject(unknown); + expect(compareScripts(explicit, omitted)).toMatchObject(unknown); const absent = capture([ script(hashA, null), script(hashA, null), script(hashB, map("b")), ]); - expect(compare(omitted, absent)).toMatchObject(modified); + expect(compareScripts(omitted, absent)).toMatchObject(modified); // The flexible declaration can use either slot. The second declaration must use the matching hash. const needs = capture([ @@ -911,33 +885,37 @@ it("compares partial source-map declarations against compatible script occurrenc script(hashA, map("constrained")), ]); const slots = [script(hashA, null), script(hashB, null)]; - expect(compare(needs, capture(slots, true))).toMatchObject(unknown); - expect(compare(needs, capture(slots.toReversed(), true))).toMatchObject( - unknown, - ); + expect(compareScripts(needs, capture(slots, true))).toMatchObject(unknown); + expect( + compareScripts(needs, capture(slots.toReversed(), true)), + ).toMatchObject(unknown); const incompatible = capture([ script(null, map("flexible")), script(hashC, map("constrained")), ]); - expect(compare(incompatible, capture(slots, true))).toMatchObject(modified); + expect(compareScripts(incompatible, capture(slots, true))).toMatchObject( + modified, + ); const copies = (count: number, declaration: string) => Array.from({ length: count }, () => script(hashA, declaration)); const nineX = capture([...copies(9, map("x")), script(hashA, null)], true); - expect(compare(nineX, nineX)).toMatchObject(unknown); - expect(compare(nineX, capture(copies(10, map("y"))))).toMatchObject(modified); + expect(compareScripts(nineX, nineX)).toMatchObject(unknown); + expect(compareScripts(nineX, capture(copies(10, map("y"))))).toMatchObject( + modified, + ); const sevenA = capture( [...copies(7, map("a")), script(hashA, map("b")), script(hashA, null)], true, ); - expect(compare(sevenA, capture(copies(12, map("a"))))).toMatchObject( + expect(compareScripts(sevenA, capture(copies(12, map("a"))))).toMatchObject( modified, ); const repeated = capture( [...copies(2_000, map("a")), script(hashA, null)], true, ); - expect(compare(repeated, repeated)).toMatchObject(unknown); + expect(compareScripts(repeated, repeated)).toMatchObject(unknown); }); it("compares retained accessibility and storage facts across incomplete captures", async () => { @@ -978,12 +956,7 @@ it("compares retained accessibility and storage facts across incomplete captures ); const compare = (after: WebPageInspection) => - compareWebCaptures( - compareWebCapturesInputSchema.parse({ - before: { inspection: observed }, - after: { inspection: after }, - }), - ).dimensions; + compareInspections(observed, after).dimensions; const identical = compare(observed); for (const dimension of [identical.accessibility, identical.storage]) expect(dimension).toMatchObject({ @@ -1142,10 +1115,4 @@ const markSectionsComplete = ( ); }; -const compareNetwork = (before: WebPageInspection, after: WebPageInspection) => - compareWebCaptures( - compareWebCapturesInputSchema.parse({ - before: { inspection: before }, - after: { inspection: after }, - }), - ).dimensions.network; +const compareNetwork = compareInspectionDimension("network"); diff --git a/tests/support/browser/webCaptureComparison.ts b/tests/support/browser/webCaptureComparison.ts new file mode 100644 index 00000000000..2ac2aa4f76d --- /dev/null +++ b/tests/support/browser/webCaptureComparison.ts @@ -0,0 +1,24 @@ +import type { WebPageInspection } from "../../../src/domain/browserObservationSchemas.js"; +import { compareWebCaptures } from "../../../src/domain/webCaptureDiff.js"; +import { + compareWebCapturesInputSchema, + type WebCaptureDiff, +} from "../../../src/domain/webCaptureDiffSchemas.js"; + +export const compareInspections = ( + before: WebPageInspection, + after: WebPageInspection, +) => + compareWebCaptures( + compareWebCapturesInputSchema.parse({ + before: { inspection: before }, + after: { inspection: after }, + }), + ); + +export const compareInspectionDimension = + ( + dimension: Dimension, + ) => + (before: WebPageInspection, after: WebPageInspection) => + compareInspections(before, after).dimensions[dimension];