Files
reverse-engineering-anything/src/domain/browserScenarioDiff.test.ts
T

1464 lines
44 KiB
TypeScript

import { createHash } from "node:crypto";
import { describe, expect, it } from "vitest";
import { compareBrowserScenarios } from "./browserScenarioDiff.js";
import {
networkContentCoverageReason,
storageFingerprintCoverageReason,
} from "./browserScenarioDiffEvidence.js";
import {
SCENARIO_NETWORK_COVERAGE_GUIDANCE,
scenarioNetworkCoverageReason,
} from "./browserScenarioNetworkComparison.js";
import {
compareBrowserScenariosInputSchema,
browserScenarioDiffSchema,
type BrowserScenarioNormalizationRule,
} from "./browserScenarioDiffValues.js";
import {
browserScenarioCaptureSchema,
retainedBrowserScenarioCaptureSchema,
type BrowserScenarioCapture,
} from "./browserScenarioCapture.js";
import { DEFAULT_BROWSER_SCENARIO_CAPTURE_LIMITS } from "./browserScenarioCaptureLimits.js";
import { BROWSER_STORAGE_FINGERPRINT_VERSION } from "./browserScenarioCaptureValues.js";
const noEventOmissions = {
by_kind: [],
network_transactions: [],
network_transaction_range: null,
} as const;
it("admits legacy event-family omission only for retained capture consumers", () => {
const current = scenarioCapture({});
const { event_families: _selection, ...scenario } = current.scenario;
const legacy = { ...current, scenario };
expect(browserScenarioCaptureSchema.safeParse(legacy).success).toBe(false);
const admitted = retainedBrowserScenarioCaptureSchema.parse(legacy);
expect(admitted.scenario.event_families).toBeUndefined();
for (const [before_scenario, after_scenario] of [
[current, legacy],
[legacy, current],
[legacy, legacy],
]) {
const result = compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario,
after_scenario,
}),
);
expect(result.overall_status).toBe("unknown");
expect(
result.steps
.flatMap((step) => step.artifact_diffs)
.filter(({ artifact }) => artifact === "events"),
).toContainEqual(
expect.objectContaining({
status: "unknown",
reason: expect.stringContaining("not recorded"),
}),
);
}
});
const networkCapture = (sourceSequence: number, receiptStep: number) => {
const base = scenarioCapture({});
const events = [
{
sequence: sourceSequence,
step_index: 0,
kind: "request",
method: "POST",
url: {
url: "https://app.example.test/api",
origin: "https://app.example.test",
query_parameter_names: [],
redacted: false,
},
resource_type: "fetch",
transaction_id: "request-1",
redirected_from_transaction_id: null,
redirected_from_transaction_state: "root",
header_names: [],
status: null,
failure: null,
},
{
sequence: sourceSequence + 1,
step_index: receiptStep,
kind: "network-content",
transaction_id: "request-1",
phase: "request",
source_event_sequence: sourceSequence,
headers: { state: "not_requested" },
body: {
state: "captured",
representation: "browser-exposed-request-bytes",
encoding: "base64",
content: "eA==",
bytes: 1,
sha256: createHash("sha256").update("x").digest("hex"),
media_type: null,
redacted: false,
},
},
];
return browserScenarioCaptureSchema.parse({
...base,
scenario: {
...base.scenario,
network_content: {
request_body: true,
response_body: false,
header_values: false,
},
event_families: ["network"],
},
events: {
items: events,
retained: 2,
dropped: 0,
omissions: noEventOmissions,
},
});
};
it("normalizes network content anchors and attributes late content to its source step", () => {
const before = networkCapture(3, 0);
const after = networkCapture(90, 1);
expect(
compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: before,
after_scenario: after,
}),
).overall_status,
).toBe("unchanged");
const content = after.events.items[1];
if (content?.kind !== "network-content")
throw new Error("Expected content fixture");
for (const mutation of [
{ transaction_id: "another-request" },
{ source_event_sequence: 91 },
{ phase: "response" },
])
expect(
browserScenarioCaptureSchema.safeParse({
...after,
events: {
...after.events,
items: [after.events.items[0], { ...content, ...mutation }],
},
}).success,
).toBe(false);
});
it("reports content-selection mismatch as unknown instead of a website change", () => {
const withBody = networkCapture(1, 0);
const metadataOnly = browserScenarioCaptureSchema.parse({
...withBody,
scenario: {
...withBody.scenario,
network_content: {
request_body: false,
response_body: false,
header_values: false,
},
},
events: {
...withBody.events,
items: withBody.events.items.filter(
(event) => event.kind !== "network-content",
),
retained: 1,
},
});
const compared = compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: metadataOnly,
after_scenario: withBody,
}),
);
expect(compared.overall_status).toBe("unknown");
expect(compared.steps.flatMap((step) => step.artifact_diffs)).toContainEqual(
expect.objectContaining({
artifact: "events",
status: "unknown",
reason: expect.stringContaining("content selections differ"),
}),
);
for (const networkContent of [undefined, {}, { request_body: false }])
expect(
compareBrowserScenariosInputSchema.safeParse({
before_scenario: {
...metadataOnly,
scenario: {
...metadataOnly.scenario,
network_content: networkContent,
},
},
after_scenario: withBody,
}).success,
).toBe(false);
});
it("reports event-family selection mismatch as unknown", () => {
const consoleOnly = scenarioCapture({});
const withFamilies = (
capture: BrowserScenarioCapture,
families: BrowserScenarioCapture["scenario"]["event_families"],
) =>
browserScenarioCaptureSchema.parse({
...capture,
scenario: { ...capture.scenario, event_families: families },
});
const compare = (
before: BrowserScenarioCapture,
after: BrowserScenarioCapture,
) =>
compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: before,
after_scenario: after,
}),
);
const eventDiffs = (compared: ReturnType<typeof compare>) =>
compared.steps
.flatMap((step) => step.artifact_diffs)
.filter(({ artifact }) => artifact === "events");
const widened = withFamilies(consoleOnly, ["page-errors", "console"]);
const mismatched = compare(consoleOnly, widened);
expect(mismatched.overall_status).toBe("unknown");
expect(eventDiffs(mismatched)).toContainEqual(
expect.objectContaining({
status: "unknown",
before_state: "captured",
after_state: "captured",
reason: expect.stringContaining(
"Event family selections differ (console before, console, page-errors after)",
),
}),
);
expect(
compare(
withFamilies(consoleOnly, ["console", "page-errors"]),
withFamilies(consoleOnly, ["page-errors", "console"]),
).overall_status,
).toBe("unchanged");
expect(
compareBrowserScenariosInputSchema.safeParse({
before_scenario: {
...consoleOnly,
scenario: { ...consoleOnly.scenario, event_families: ["dialogs"] },
},
after_scenario: consoleOnly,
}).success,
).toBe(false);
const quiet = browserScenarioCaptureSchema.parse({
...withFamilies(consoleOnly, []),
events: { retained: 0, dropped: 0, items: [], omissions: noEventOmissions },
});
const differentEvents = compare(quiet, widened);
expect(differentEvents.overall_status).toBe("unknown");
expect(eventDiffs(differentEvents).map(({ status }) => status)).toEqual([
"unknown",
"unknown",
]);
const both = eventDiffs(compare(networkCapture(1, 0), consoleOnly));
expect(both).toContainEqual(
expect.objectContaining({
reason: expect.stringMatching(
/Event family selections differ.*content selections differ/u,
),
}),
);
for (const diff of both) {
expect(diff.reason?.match(/Event family selections differ/gu)).toHaveLength(
1,
);
expect(
diff.reason?.match(/Network content selections differ/gu),
).toHaveLength(1);
}
});
it("retains network coverage guidance alongside independent console changes", () => {
const network = networkCapture(1, 0);
const consoleEvent = scenarioCapture({}).events.items[0];
if (consoleEvent?.kind !== "console")
throw new Error("Expected console fixture");
const capture = ({
unknownPredecessor = false,
responseBody = false,
text = consoleEvent.text,
} = {}) =>
browserScenarioCaptureSchema.parse({
...network,
scenario: {
...network.scenario,
event_families: ["network", "console"],
network_content: {
...network.scenario.network_content,
response_body: responseBody,
},
},
events: {
...network.events,
retained: 3,
items: [
...network.events.items.map((event) =>
unknownPredecessor && event.kind === "request"
? { ...event, redirected_from_transaction_state: "unknown" }
: event,
),
{ ...consoleEvent, sequence: 3, text },
],
},
});
const compare = (
before_scenario: BrowserScenarioCapture,
after_scenario: BrowserScenarioCapture,
) =>
compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario,
after_scenario,
}),
);
const original = capture();
const matching = compare(original, original);
expect(matching.overall_status).toBe("unchanged");
expect(matching.artifact_diffs.total).toBe(0);
const missing = capture({ unknownPredecessor: true });
const responseSelected = capture({ responseBody: true });
for (const { before, quietAfter, changedAfter, reason } of [
{
before: original,
quietAfter: responseSelected,
changedAfter: capture({ responseBody: true, text: "settings-failed" }),
reason: networkContentCoverageReason(original, responseSelected),
},
{
before: missing,
quietAfter: missing,
changedAfter: capture({
unknownPredecessor: true,
text: "settings-failed",
}),
reason: scenarioNetworkCoverageReason(missing),
},
]) {
if (reason === undefined)
throw new Error("Expected incomplete network coverage");
const quiet = compare(before, quietAfter);
expect(quiet.overall_status).toBe("unknown");
const changed = compare(before, changedAfter);
expect(changed.overall_status).toBe("changed");
for (const [result, consoleStatus] of [
[quiet, "unknown"],
[changed, "changed"],
] as const) {
const diffs = result.steps.flatMap(
({ artifact_diffs }) => artifact_diffs,
);
expect(diffs.map(({ artifact, status }) => [artifact, status])).toEqual([
["events", "unknown"],
["events", consoleStatus],
]);
for (const diff of diffs) {
expect(diff.reason).toBe(reason);
expect(diff.before_sha256).not.toBeNull();
expect(diff.after_sha256).not.toBeNull();
if (diff.status === "changed")
expect(diff.before_sha256).not.toBe(diff.after_sha256);
else expect(diff.before_sha256).toBe(diff.after_sha256);
}
}
}
});
const concurrentCapture = (
requestOrder: readonly ("a" | "b")[],
statuses: Readonly<Record<"a" | "b", number>>,
responseStep = 0,
responseOrder: readonly ("a" | "b")[] = requestOrder,
redirect = false,
) => {
const base = scenarioCapture({});
const network = (path: "a" | "b") => ({
step_index: 0,
transaction_id: `request-${String(requestOrder.indexOf(path) + 1)}`,
redirected_from_transaction_id:
redirect && path === "b"
? `request-${String(requestOrder.indexOf("a") + 1)}`
: null,
...(!redirect || path !== "b"
? { redirected_from_transaction_state: "root" as const }
: {}),
method: "GET",
url: {
url: `https://app.example.test/${path}.js`,
origin: "https://app.example.test",
query_parameter_names: [],
redacted: false,
},
resource_type: "script",
header_names: [],
failure: null,
});
const request = (path: "a" | "b") => ({
...network(path),
kind: "request",
status: null,
});
const response = (path: "a" | "b") => ({
...network(path),
step_index: responseStep,
kind: "response",
status: statuses[path],
});
const events = (
redirect
? requestOrder.flatMap((path) => [request(path), response(path)])
: [...requestOrder.map(request), ...responseOrder.map(response)]
).map((event, index) => ({ ...event, sequence: index + 1 }));
return browserScenarioCaptureSchema.parse({
...base,
scenario: { ...base.scenario, event_families: ["network"] },
events: {
items: events,
retained: events.length,
dropped: 0,
omissions: noEventOmissions,
},
});
};
const redirectChainCapture = (swapLeaves = false, reverseReceipt = false) => {
const base = scenarioCapture({});
const nodes = [
{ id: "root-a", parent: null, path: "first-a", status: 301 },
{ id: "root-b", parent: null, path: "first-b", status: 302 },
{ id: "middle-a", parent: "root-a", path: "same-middle", status: 307 },
{ id: "middle-b", parent: "root-b", path: "same-middle", status: 307 },
{
id: "leaf-a",
parent: swapLeaves ? "middle-b" : "middle-a",
path: "last-a",
status: 200,
},
{
id: "leaf-b",
parent: swapLeaves ? "middle-a" : "middle-b",
path: "last-b",
status: 200,
},
];
const events = (reverseReceipt ? [...nodes].reverse() : nodes)
.flatMap((node) => {
const network = {
step_index: 0,
transaction_id: (reverseReceipt ? "renamed-" : "") + node.id,
redirected_from_transaction_id:
node.parent === null
? null
: (reverseReceipt ? "renamed-" : "") + node.parent,
...(node.parent === null
? { redirected_from_transaction_state: "root" as const }
: {}),
method: "GET",
url: {
url: `https://app.example.test/${node.path}`,
origin: "https://app.example.test",
query_parameter_names: [],
redacted: false,
},
resource_type: "document",
header_names: [],
failure: null,
};
return [
{ ...network, kind: "request", status: null },
{ ...network, kind: "response", status: node.status },
];
})
.map((event, index) => ({ ...event, sequence: index + 1 }));
return browserScenarioCaptureSchema.parse({
...base,
scenario: { ...base.scenario, event_families: ["network"] },
events: {
items: events,
retained: events.length,
dropped: 0,
omissions: noEventOmissions,
},
});
};
describe("browser scenario network event order", () => {
it("retains multi-hop redirect ancestry while ignoring IDs and concurrent receipt order", () => {
const compare = (after: BrowserScenarioCapture) =>
compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: redirectChainCapture(),
after_scenario: after,
}),
);
expect(compare(redirectChainCapture(false, true)).overall_status).toBe(
"unchanged",
);
expect(compare(redirectChainCapture(true)).overall_status).toBe("changed");
});
it("retains ambiguous request multiplicity in the step evidence", () => {
const capture = concurrentCapture(["a", "b"], { a: 200, b: 404 });
const request = capture.events.items.find(
(event) => event.kind === "request",
);
if (request === undefined) throw new Error("Expected request fixture");
const ambiguous = browserScenarioCaptureSchema.parse({
...capture,
events: {
...capture.events,
retained: capture.events.retained + 1,
items: [
...capture.events.items,
{ ...request, sequence: capture.events.retained + 1, step_index: 1 },
],
},
});
const reason = scenarioNetworkCoverageReason(ambiguous);
if (reason === undefined)
throw new Error("Expected ambiguous request coverage");
for (const [before_scenario, after_scenario, equalDigests] of [
[ambiguous, ambiguous, true],
[ambiguous, capture, false],
[capture, ambiguous, false],
] as const) {
const input = compareBrowserScenariosInputSchema.parse({
before_scenario,
after_scenario,
});
const compared = compareBrowserScenarios(input);
expect(compared.overall_status).toBe("unknown");
const diff = compared.steps[0]?.artifact_diffs.find(
({ artifact }) => artifact === "events",
);
if (diff === undefined) throw new Error("Expected request-step evidence");
expect(diff).toMatchObject({
status: "unknown",
before_sha256: expect.any(String),
after_sha256: expect.any(String),
reason,
});
// An additional copy changes the retained fingerprint even when identity is ambiguous.
expect(diff.before_sha256 === diff.after_sha256).toBe(equalDigests);
expect(input.before_scenario.events.items).toEqual(
before_scenario.events.items,
);
expect(input.after_scenario.events.items).toEqual(
after_scenario.events.items,
);
}
});
it("distinguishes cycles from unresolved or contradictory redirect predecessors", () => {
const capture = redirectChainCapture();
const { cycle, missingPredecessor } = SCENARIO_NETWORK_COVERAGE_GUIDANCE;
for (const [id, parent, state, phase, reason] of [
["root-a", "missing", undefined, undefined, missingPredecessor],
["root-a", null, "unknown", undefined, missingPredecessor],
["root-a", "middle-a", undefined, undefined, cycle],
["root-b", "root-a", undefined, "response", missingPredecessor],
["middle-b", null, "root", "response", missingPredecessor],
] as const) {
const items = capture.events.items.map((event) =>
"transaction_id" in event &&
event.transaction_id === id &&
(phase === undefined || event.kind === phase)
? {
...event,
redirected_from_transaction_id: parent,
redirected_from_transaction_state: state,
}
: event,
);
const compared = compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: capture,
after_scenario: { ...capture, events: { ...capture.events, items } },
}),
);
expect(compared.overall_status).toBe("unknown");
const diff = compared.steps[0]?.artifact_diffs.find(
({ artifact }) => artifact === "events",
);
if (diff === undefined)
throw new Error("Expected request-step comparison");
expect(diff).toMatchObject({
status: "unknown",
before_state: "captured",
after_state: "captured",
before_sha256: expect.any(String),
after_sha256: expect.any(String),
reason,
});
expect(diff.before_sha256).not.toBe(diff.after_sha256);
}
});
});
describe("browser scenario network comparison stability", () => {
it("keeps normalization away from generated network fingerprints", () => {
const rules = [..."0123456789abcdef"].map((match, index) => ({
rule_id: `remove-fingerprint-hex-${String(index)}`,
artifacts: ["events"] as const,
match,
replacement: "",
}));
const compared = compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: concurrentCapture(["a", "b"], {
a: 200,
b: 404,
}),
after_scenario: concurrentCapture(["a", "b"], {
a: 201,
b: 404,
}),
normalization: { rules },
}),
);
expect(compared.overall_status).toBe("changed");
});
it("compares concurrent transactions across receipt order, step, and capture-local IDs", () => {
const statuses = { a: 200, b: 404 };
const before = concurrentCapture(["a", "b"], statuses);
const compare = (after: BrowserScenarioCapture) =>
compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: before,
after_scenario: after,
}),
).overall_status;
expect(
compare(concurrentCapture(["b", "a"], statuses, 1, ["a", "b"])),
).toBe("unchanged");
expect(
compare(concurrentCapture(["a", "b"], statuses, 0, ["b", "a"])),
).toBe("unchanged");
expect(compare(concurrentCapture(["b", "a"], { a: 404, b: 200 }))).toBe(
"changed",
);
expect(
compare(concurrentCapture(["a", "b"], statuses, 0, ["a", "b"], true)),
).toBe("changed");
});
});
describe("browser scenario network request times", () => {
/** A two-request capture whose `a` request finished and whose `b` request failed with `failure`. */
const ended = (
capture: BrowserScenarioCapture,
receiptStep: number,
failure = "net::ERR_ABORTED",
): BrowserScenarioCapture => {
let sequence = Math.max(
...capture.events.items.map(({ sequence }) => sequence),
);
const terminal = capture.events.items.flatMap((event) => {
if (event.kind !== "request") return [];
const receipt = {
...event,
sequence: ++sequence,
step_index: receiptStep,
};
return [
event.url.url.endsWith("/a.js")
? { ...receipt, kind: "request-finished" as const }
: { ...receipt, kind: "request-failed" as const, failure },
];
});
return browserScenarioCaptureSchema.parse({
...capture,
events: {
...capture.events,
retained: capture.events.items.length + terminal.length,
items: [...capture.events.items, ...terminal],
},
});
};
const timed = (
capture: BrowserScenarioCapture,
offset: number,
): BrowserScenarioCapture => {
const start = Date.parse("2026-10-24T04:59:59.990Z") + offset;
const started_at = new Date(start).toISOString();
const observed_at = new Date(start + 10).toISOString();
return browserScenarioCaptureSchema.parse({
...capture,
events: {
...capture.events,
items: capture.events.items.map((event) => {
if (event.kind === "request") return { ...event, observed_at };
if (
event.kind === "request-finished" ||
event.kind === "request-failed"
)
return { ...event, started_at, elapsed_ms: 3 + offset };
return event;
}),
},
});
};
const compare = (
before: BrowserScenarioCapture,
after: BrowserScenarioCapture,
) =>
compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: before,
after_scenario: after,
}),
);
it("ignores volatile timing and attributes a changed late failure to the request step", () => {
const statuses = { a: 200, b: 404 };
const untimed = ended(concurrentCapture(["a", "b"], statuses), 0);
const reordered = concurrentCapture(["b", "a"], statuses, 1, ["a", "b"]);
const early = timed(untimed, 0);
const late = timed(ended(reordered, 1), 7_000);
const compared = compare(early, late);
expect(compared.overall_status).toBe("unchanged");
expect(compared.normalization.built_in_rules).toContain(
"ignore_network_request_timing",
);
expect(compare(untimed, late).overall_status).toBe("unchanged");
const changed = compare(
early,
timed(ended(reordered, 1, "net::ERR_CONNECTION_RESET"), 7_000),
);
expect(changed.overall_status).toBe("changed");
expect(changed.steps).toMatchObject([
{
step_id: "scenario_start",
status: "changed",
artifact_diffs: [
{
artifact: "events",
status: "changed",
before_state: "captured",
after_state: "captured",
},
],
},
{ step_id: "open-settings", status: "unchanged", artifact_diffs: [] },
]);
expect(changed.artifact_diffs.total).toBe(1);
});
});
describe("browser scenario comparison", () => {
it("applies committed cascading rules in canonical order and ignores volatile fields", () => {
const before = scenarioCapture({
domText: "<main data-build='build-123'>Settings</main>",
elapsedMs: 11,
eventSequence: 3,
});
const after = scenarioCapture({
domText: "<main data-build='build-456'>Settings</main>",
elapsedMs: 900,
eventSequence: 88,
});
const rules = [
{
rule_id: "new-build",
artifacts: ["url", "dom"] as const,
match: "build-456",
replacement: "build-123",
},
{
rule_id: "old-build",
artifacts: ["dom"] as const,
match: "build-123",
replacement: "[BUILD]",
},
];
const compared = compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: before,
after_scenario: after,
normalization: { rules },
}),
);
const reordered = compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: before,
after_scenario: after,
normalization: {
rules: [...rules].reverse().map((rule) => ({
...rule,
artifacts: [...rule.artifacts].reverse(),
})),
},
}),
);
expect(compared.overall_status).toBe("unchanged");
expect(compared.alignment.status).toBe("aligned");
expect(compared.artifact_diffs.total).toBe(0);
expect(compared.normalization).toMatchObject({
rules: [
{ rule_id: "new-build", artifacts: ["dom", "url"] },
{ rule_id: "old-build", artifacts: ["dom"] },
],
});
expect(reordered).toEqual(compared);
});
it("exposes action mismatches and normalized artifact-level changes", () => {
const before = scenarioCapture({
domText: "<main>Settings</main>",
});
const after = scenarioCapture({
action: "dblclick",
domText: "<main>Advanced settings</main>",
});
const compared = compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: before,
after_scenario: after,
}),
);
const step = compared.steps.find(
({ step_id: stepId }) => stepId === "open-settings",
);
expect(compared.overall_status).toBe("changed");
expect(compared.alignment).toMatchObject({
status: "partial",
failures: [
expect.objectContaining({
code: "action_mismatch",
step_id: "open-settings",
}),
],
});
expect(step).toMatchObject({
status: "changed",
artifact_diffs: [
expect.objectContaining({
artifact: "action_state",
status: "changed",
}),
expect.objectContaining({ artifact: "dom", status: "changed" }),
],
});
expect(compared.artifact_diffs).toEqual({ total: 2 });
});
});
describe("browser scenario comparison text values", () => {
it("accepts long normalization values and comparison diagnostics", () => {
const longText = "x".repeat(5_000);
const ruleId = "r".repeat(100);
const capture = scenarioCapture({});
const compared = compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: capture,
after_scenario: capture,
normalization: {
rules: [
{
rule_id: ruleId,
artifacts: ["dom"],
match: longText,
replacement: longText,
},
],
},
}),
);
const stepId = "s".repeat(100);
const diagnostic = browserScenarioDiffSchema.parse({
...compared,
alignment: {
...compared.alignment,
before_only: [stepId],
after_only: [stepId],
failures: [
{
code: "missing_before_step",
step_id: stepId,
reason: longText,
},
],
},
steps: compared.steps.map((step) => ({
...step,
step_id: stepId,
artifact_diffs: [
{
artifact: "dom",
status: "unknown",
before_state: "missing",
after_state: "missing",
before_sha256: null,
after_sha256: null,
reason: longText,
},
],
})),
limitations: [longText],
});
expect(diagnostic.normalization.rules[0]).toMatchObject({
rule_id: ruleId,
match: longText,
replacement: longText,
});
expect(diagnostic.alignment.failures[0]?.reason).toBe(longText);
expect(diagnostic.steps[0]?.artifact_diffs[0]?.reason).toBe(longText);
expect(diagnostic.limitations).toEqual([longText]);
});
});
describe("browser scenario comparison validation", () => {
it("does not accept caller limits that would omit difference records", () => {
const capture = scenarioCapture({});
expect(
compareBrowserScenariosInputSchema.safeParse({
before_scenario: capture,
after_scenario: capture,
max_changes: 1,
}).success,
).toBe(false);
});
it("returns every aligned step of long scenarios", () => {
const before = multiStepScenarioCapture(130, "before");
const after = multiStepScenarioCapture(130, "after");
const result = compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: before,
after_scenario: after,
}),
);
expect(result.alignment).toMatchObject({
status: "aligned",
aligned_steps: 131,
});
expect(result.steps).toHaveLength(131);
expect(result.steps.at(-1)).toMatchObject({
before_step_index: 130,
after_step_index: 130,
status: "changed",
});
expect(result.artifact_diffs.total).toBe(130);
});
it("aligns reordered unique steps while distinguishing missing and ambiguous IDs", () => {
const capture = scenarioCapture({});
const action = capture.steps[1];
if (action === undefined) throw new Error("Expected scenario action step");
const withSteps = (ids: readonly string[]) => ({
...capture,
scenario: { ...capture.scenario, action_count: ids.length },
steps: [
capture.steps[0],
...ids.map((step_id, index) => ({
...action,
step_id,
step_index: index + 1,
event_sequence_start: 1,
event_sequence_end: 0,
})),
],
events: { ...capture.events, retained: 0, items: [] },
});
const compared = compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: withSteps([
"keep-z",
"ambiguous-before",
"keep-a",
"before-only",
"ambiguous-before",
"ambiguous-after",
]),
after_scenario: withSteps([
"keep-a",
"ambiguous-after",
"keep-z",
"after-only",
"ambiguous-after",
"ambiguous-before",
]),
}),
);
expect(compared.overall_status).toBe("unknown");
expect(compared.alignment).toMatchObject({
status: "partial",
aligned_steps: 3,
before_only: ["before-only"],
after_only: ["after-only"],
});
expect(
compared.alignment.failures.map(({ code, step_id }) => [code, step_id]),
).toEqual(
expect.arrayContaining([
["duplicate_before_step_id", "ambiguous-before"],
["duplicate_after_step_id", "ambiguous-after"],
["missing_before_step", "after-only"],
["missing_after_step", "before-only"],
]),
);
expect(compared.alignment.failures).toHaveLength(4);
expect(compared.steps).toMatchObject([
{
step_id: "scenario_start",
before_step_index: 0,
after_step_index: 0,
status: "unchanged",
},
{
step_id: "keep-z",
before_step_index: 1,
after_step_index: 3,
status: "unchanged",
},
{
step_id: "keep-a",
before_step_index: 3,
after_step_index: 1,
status: "unchanged",
},
]);
});
it("rejects duplicate normalization rule IDs", () => {
const capture = scenarioCapture({});
expect(
compareBrowserScenariosInputSchema.safeParse({
before_scenario: capture,
after_scenario: capture,
normalization: {
rules: [
{
rule_id: "volatile",
artifacts: ["dom"],
match: "one",
replacement: "[VALUE]",
},
{
rule_id: "volatile",
artifacts: ["url"],
match: "two",
replacement: "[VALUE]",
},
],
},
}).success,
).toBe(false);
});
});
const multiStepScenarioCapture = (
actionCount: number,
content: string,
): BrowserScenarioCapture => {
const capture = scenarioCapture({});
const template = capture.steps[1];
if (template === undefined) throw new Error("Expected scenario action step");
const steps = [
capture.steps[0],
...Array.from({ length: actionCount }, (_, index) => ({
...template,
step_index: index + 1,
step_id: `step-${String(index + 1)}`,
event_sequence_start: index + 1,
event_sequence_end: index + 1,
artifacts: {
...template.artifacts,
dom: {
state: "captured" as const,
value: textArtifact(`${content}-${String(index + 1)}`),
},
},
})),
];
return browserScenarioCaptureSchema.parse({
...capture,
scenario: { ...capture.scenario, action_count: actionCount },
steps,
});
};
const withStorage = (value: unknown) => {
const base = scenarioCapture({});
return browserScenarioCaptureSchema.parse({
...base,
steps: base.steps.map((step) => ({
...step,
artifacts: {
...step.artifacts,
storage: { state: "captured", value },
},
})),
});
};
const emptyStorage = { cookies: [], local_storage: [], session_storage: [] };
describe("storage fingerprint provenance", () => {
const compare = (
before_scenario: BrowserScenarioCapture,
after_scenario: BrowserScenarioCapture,
rules: BrowserScenarioNormalizationRule[] = [],
) =>
compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario,
after_scenario,
normalization: { rules },
}),
);
const current = (value: Readonly<Record<string, unknown>>) =>
withStorage({
...value,
fingerprint_version: BROWSER_STORAGE_FINGERPRINT_VERSION,
});
const hashed = (kind: keyof typeof emptyStorage, hex = "a") => {
const entry = {
name: "value",
value_state: "hashed",
value_sha256: hex.repeat(64),
};
return {
...emptyStorage,
[kind]: [
kind === "cookies"
? {
...entry,
domain: "app.example.test",
path: "/",
secure: false,
http_only: false,
same_site: "Lax",
}
: entry,
],
};
};
const expectLegacyDiff = (
before: BrowserScenarioCapture,
after: BrowserScenarioCapture,
equalDigests: boolean,
rules: BrowserScenarioNormalizationRule[] = [],
) => {
const beforeStorage = before.steps[0]?.artifacts.storage;
const afterStorage = after.steps[0]?.artifacts.storage;
if (beforeStorage === undefined || afterStorage === undefined)
throw new Error("Expected storage fixtures");
const reason = storageFingerprintCoverageReason(
beforeStorage,
afterStorage,
);
if (reason === undefined)
throw new Error("Expected legacy fingerprint guidance");
const result = compare(before, after, rules);
expect(result.overall_status).toBe("unknown");
const diffs = result.steps.flatMap(({ artifact_diffs }) => artifact_diffs);
expect(diffs).toHaveLength(2);
for (const diff of diffs) {
expect(diff).toMatchObject({
artifact: "storage",
status: "unknown",
reason,
before_sha256: expect.any(String),
after_sha256: expect.any(String),
});
expect(diff.before_sha256 === diff.after_sha256).toBe(equalDigests);
}
};
it.each(["cookies", "local_storage", "session_storage"] as const)(
"withholds %s equality on either side while retaining the same captured digest",
(kind) => {
const value = hashed(kind);
const legacy = withStorage(value);
const versioned = current(value);
expectLegacyDiff(legacy, legacy, true);
expectLegacyDiff(legacy, versioned, true);
expectLegacyDiff(versioned, legacy, true);
expect(compare(versioned, versioned).overall_status).toBe("unchanged");
},
);
it("withholds legacy identity despite different digests or normalized value states", () => {
const value = hashed("local_storage");
const changed = hashed("local_storage", "b");
const legacy = withStorage(value);
const versioned = current(value);
const differentLegacy = withStorage(changed);
expectLegacyDiff(legacy, differentLegacy, false);
expectLegacyDiff(legacy, current(changed), false);
expectLegacyDiff(versioned, differentLegacy, false);
expectLegacyDiff(legacy, versioned, true, [
{
rule_id: "rewrite-value-state",
artifacts: ["storage"],
match: "hashed",
replacement: "redacted-secret",
},
]);
});
it("ignores encoding metadata for empty and fully redacted inventories", () => {
for (const local_storage of [
[],
[{ name: "secret", value_state: "redacted-secret", value_sha256: null }],
]) {
const value = { ...emptyStorage, local_storage };
const legacy = withStorage(value);
const versioned = current(value);
expect(compare(legacy, versioned).overall_status).toBe("unchanged");
expect(compare(versioned, legacy).overall_status).toBe("unchanged");
}
});
});
interface ScenarioCaptureOptions {
readonly stepId?: string;
readonly action?: string;
readonly domText?: string;
readonly elapsedMs?: number;
readonly eventSequence?: number;
}
const scenarioCapture = (
options: ScenarioCaptureOptions,
): BrowserScenarioCapture => {
const url = {
url: "https://app.example.test/settings",
origin: "https://app.example.test",
query_parameter_names: [],
redacted: false,
};
const completeness = {
status: "complete",
equality_eligible: true,
missing_sections: [],
truncated_sections: [],
};
const artifacts = (text: string) => ({
screenshot: { state: "not_requested" },
dom: { state: "captured", value: textArtifact(text) },
accessibility: { state: "not_requested" },
url: { state: "captured", value: url },
history: { state: "not_requested" },
storage: { state: "not_requested" },
});
const eventSequence = options.eventSequence ?? 1;
return browserScenarioCaptureSchema.parse({
browser: {
mode: "launch",
process_ownership: "provider-owned",
cleanup: "terminated-owned-process",
product: "Chromium",
version: "149.0",
},
scenario: {
start_origin: "https://app.example.test",
action_count: 1,
secret_references: [],
network_content: {
request_body: false,
response_body: false,
header_values: false,
},
event_families: ["console"],
limits: DEFAULT_BROWSER_SCENARIO_CAPTURE_LIMITS,
},
duration_ms: options.elapsedMs ?? 10,
steps: [
{
step_index: 0,
step_id: "scenario_start",
action: "scenario_start",
status: "completed",
elapsed_ms: 0,
before_url: url,
after_url: url,
error: null,
event_sequence_start: 1,
event_sequence_end: 0,
artifacts: artifacts("<main>Ready</main>"),
completeness,
},
{
step_index: 1,
step_id: options.stepId ?? "open-settings",
action: options.action ?? "click",
status: "completed",
elapsed_ms: options.elapsedMs ?? 10,
before_url: url,
after_url: url,
error: null,
event_sequence_start: eventSequence,
event_sequence_end: eventSequence,
artifacts: artifacts(options.domText ?? "<main>Settings</main>"),
completeness,
},
],
events: {
retained: 1,
dropped: 0,
items: [
{
sequence: eventSequence,
step_index: 1,
kind: "console",
level: "info",
text: "settings-ready",
url: null,
},
],
omissions: noEventOmissions,
},
completeness,
limitations: [],
});
};
const textArtifact = (text: string) => {
const bytes = Buffer.from(text);
return {
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.byteLength,
text,
};
};
const storageScenarioCapture = (
family: "cookies" | "local_storage" | "session_storage",
entries: readonly { name: string; value_sha256: string }[],
): BrowserScenarioCapture => {
const values = entries.map((entry) => ({
...(family === "cookies"
? {
domain: "app.example.test",
path: "/",
secure: true,
http_only: false,
same_site: "Lax",
}
: {}),
...entry,
value_state: "hashed",
}));
return withStorage({
...emptyStorage,
fingerprint_version: BROWSER_STORAGE_FINGERPRINT_VERSION,
[family]: values,
});
};
it.each(["cookies", "local_storage", "session_storage"] as const)(
"compares %s contents independently of enumeration order without losing edits or multiplicity",
(family) => {
const entries = [
{ name: "theme", value_sha256: "a".repeat(64) },
{ name: "locale", value_sha256: "b".repeat(64) },
] as const;
const before = storageScenarioCapture(family, entries);
const after = storageScenarioCapture(family, entries.toReversed());
const retained = structuredClone(after);
const compare = (
right: BrowserScenarioCapture,
rules: BrowserScenarioNormalizationRule[] = [],
) =>
compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: before,
after_scenario: right,
normalization: { rules },
}),
);
const ordered = compare(after);
expect(ordered.overall_status).toBe("unchanged");
expect(ordered.normalization.built_in_rules).toContain(
"compare_storage_entries_as_unordered_collection",
);
expect(
compare(after, [
{
rule_id: "names",
artifacts: ["storage"],
match: "theme",
replacement: "locale",
},
]).overall_status,
).toBe("unchanged");
expect(after).toEqual(retained);
for (const changed of [
[{ ...entries[0], value_sha256: "c".repeat(64) }, entries[1]],
[entries[0]],
[...entries, entries[0]],
]) {
const result = compare(storageScenarioCapture(family, changed));
expect(result.overall_status).toBe("changed");
expect(result.steps[0]?.artifact_diffs).toContainEqual(
expect.objectContaining({ artifact: "storage", status: "changed" }),
);
}
},
);
it("retains cookie scope and flags when canonicalizing storage inventories", () => {
const before = storageScenarioCapture("cookies", [
{ name: "session", value_sha256: "a".repeat(64) },
]);
for (const change of [
{ path: "/profile" },
{ domain: ".example.test" },
{ secure: false },
{ http_only: true },
{ same_site: "Strict" },
]) {
const after = structuredClone(before);
const storage = after.steps[0]?.artifacts.storage;
if (storage?.state !== "captured" || storage.value.cookies[0] === undefined)
throw new Error("Expected captured cookie fixture");
Object.assign(storage.value.cookies[0], change);
expect(
compareBrowserScenarios(
compareBrowserScenariosInputSchema.parse({
before_scenario: before,
after_scenario: after,
}),
).overall_status,
).toBe("changed");
}
});