test(browser): share validated inspection comparisons (#14567)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-11-25 15:52:17 +00:00
1 parent b89c7ecc05
commit 41bdf03a98
7 files changed
+61 -109

No files matched your search

+2 -8
View File
@@ -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());
+2 -12
View File
@@ -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<typeof resource>[]) => {
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([
@@ -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", {}, []],
@@ -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) =>
@@ -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 => ({
+28 -61
View File
@@ -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");
@@ -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 extends keyof WebCaptureDiff["dimensions"]>(
dimension: Dimension,
) =>
(before: WebPageInspection, after: WebPageInspection) =>
compareInspections(before, after).dimensions[dimension];