test(web): consolidate DOM snapshot position and SVG proofs (#13104)

This commit is contained in:
morluto authored and GitHub committed 2026-11-21 08:53:31 -05:00
1 parent ce5258043c
commit 4ad0e3ea78
5 files changed
+109 -154

No files matched your search

+27 -43
View File
@@ -1,17 +1,11 @@
import { describe, expect, it } from "vitest";
import { inspectWebPageInputSchema } from "../domain/browserObservation.js";
import { captureDom } from "./CdpCaptureDocuments.js";
import { capturedSvgNodeKind } from "./CdpCaptureDomNamespaces.js";
import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
const origin = "https://app.example.test";
const documentUrl = `${origin}/screens/app`;
const input = inspectWebPageInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: [origin],
target_id: "page-1",
});
it("splits link relations only on HTML ASCII whitespace", () => {
const result = captureSnapshot(
@@ -325,12 +319,17 @@ const domSnapshot = (
baseUrl: string | null = `${origin}/screens/assets/`,
pageUrl = documentUrl,
) => {
const strings = [pageUrl, baseUrl ?? ""];
const base = baseUrl ?? "";
const strings = [pageUrl, base];
const stringIndices = new Map([[pageUrl, 0]]);
if (base !== pageUrl) stringIndices.set(base, 1);
const intern = (value: string): number => {
const existing = strings.indexOf(value);
if (existing >= 0) return existing;
const existing = stringIndices.get(value);
if (existing !== undefined) return existing;
const index = strings.length;
strings.push(value);
return strings.length - 1;
stringIndices.set(value, index);
return index;
};
const nodeType = [9];
const nodeName = [intern("#document")];
@@ -377,7 +376,7 @@ const captureSnapshot = (value: ReturnType<typeof domSnapshot>) => {
const kinds = new Map(
value.documents.flatMap((document) => [...document.svgNodeKinds]),
);
const capture = captureDom(value, new Set([origin]), input);
const capture = captureDom(value, new Set([origin]));
let step = capture.next();
while (!step.done) {
step = capture.next(
@@ -451,33 +450,6 @@ describe("browser SVG identity", () => {
`${origin}/orphan`,
]);
});
it("records unresolved identity without inventing a destination or hiding attributes", () => {
const value = domSnapshot([
{ name: "a", parent: 0, attributes: [["xlink:href", "/unknown"]] },
]);
const completeness = new CdpCaptureCompleteness();
const capture = captureDom(value, new Set([origin]), input, completeness);
expect(capture.next()).toEqual({
done: false,
value: [{ backendNodeId: 2, nodeName: "a" }],
});
const step = capture.next([undefined]);
expect(step.done).toBe(true);
if (!step.done) throw new Error("Unexpected second query");
expect(step.value.unresolvedSvgNodes).toBe(1);
expect(step.value.urls).toEqual([]);
expect(step.value.nodes[1]?.attribute_names).toEqual(["xlink:href"]);
expect(completeness.snapshot().excluded).toEqual(
expect.arrayContaining([
expect.objectContaining({
section: "metadata",
reason: "provider_unavailable",
count: 1,
}),
]),
);
});
});
it("keeps batched SVG identities aligned around missing IDs and ordinary links", () => {
@@ -485,7 +457,9 @@ it("keeps batched SVG identities aligned around missing IDs and ordinary links",
{ name: "a", parent: 0, attributes: [["xlink:href", "/svg"]] },
{ name: "A", parent: 0, attributes: [["href", "/ordinary"]] },
{ name: "a", parent: 0, attributes: [["xlink:href", "/missing-id"]] },
{ name: "a", parent: 0, attributes: [["xlink:href", "/unknown-reply"]] },
{ name: "a", parent: 0, attributes: [["xlink:href", "/non-svg"]] },
{ name: "a", parent: 0, attributes: [["xlink:href", "/confirmed-later"]] },
]);
const completeness = new CdpCaptureCompleteness();
const capture = captureDom(
@@ -493,11 +467,10 @@ it("keeps batched SVG identities aligned around missing IDs and ordinary links",
...value,
documents: value.documents.map((document) => ({
...document,
nodes: { ...document.nodes, backendNodeId: [1, 2, 3, null, 5] },
nodes: { ...document.nodes, backendNodeId: [1, 2, 3, null, 5, 6, 7] },
})),
},
new Set([origin]),
input,
completeness,
);
expect(capture.next()).toEqual({
@@ -505,21 +478,32 @@ it("keeps batched SVG identities aligned around missing IDs and ordinary links",
value: [
{ backendNodeId: 2, nodeName: "a" },
{ backendNodeId: 5, nodeName: "a" },
{ backendNodeId: 6, nodeName: "a" },
{ backendNodeId: 7, nodeName: "a" },
],
});
const step = capture.next([true, false]);
const step = capture.next([true, undefined, false, true]);
if (!step.done) throw new Error("Unexpected second batch");
expect(
step.value.urls.map(({ node_index, url }) => [node_index, url]),
).toEqual([
[1, `${origin}/svg`],
[2, `${origin}/ordinary`],
[6, `${origin}/confirmed-later`],
]);
expect(step.value.unresolvedSvgNodes).toBe(1);
expect(
step.value.nodes
.filter(({ index }) => index === 3 || index === 4)
.map(({ index, attribute_names }) => [index, attribute_names]),
).toEqual([
[3, ["xlink:href"]],
[4, ["xlink:href"]],
]);
expect(step.value.unresolvedSvgNodes).toBe(2);
expect(completeness.snapshot().excluded).toContainEqual({
section: "metadata",
reason: "provider_unavailable",
count: 1,
count: 2,
});
});
+79 -101
View File
@@ -12,7 +12,6 @@ import {
frameTreeReply,
resourceTreeReply,
} from "../../tests/fixtures/fakeCdpSend.js";
import { inspectWebPageInputSchema } from "../domain/browserObservation.js";
const origin = "https://app.example.test";
@@ -117,31 +116,32 @@ it("publishes exact approved frame URLs and counts malformed and foreign childre
});
});
it("keeps DOM node and URL source positions when neighboring rows are malformed", () => {
const columns = [
[9, 1, -1, -1, []],
[1, 2, -1, 0, []],
// Wrong wire types in name, parent, value, and attribute references.
[1, "3", -1, 1, []],
[1, 3, -1, "1", []],
[1, 3, "0", 1, []],
[1, 3, -1, 1, [4, "bad", 4, 5]],
// Malformed and missing string references, including the value-only -1 sentinel.
[1, -1, -1, 1, []],
[1, 20, -1, 1, []],
[1, 7, -1, 1, []],
[1, 3, -2, 1, []],
[1, 3, 20, 1, []],
[1, 3, 7, 1, []],
[1, 3, -1, 1, [-1, 5]],
[1, 3, -1, 1, [20, 5]],
[1, 3, -1, 1, [4, -2]],
[1, 3, -1, 1, [4, 20]],
[1, 3, -1, 1, [4]],
[1, 3, -1, 1, "invalid"],
[1, 3, 8, 2, []],
[1, 9, -1, 1, [4, 6]],
] as const;
const mixedDomColumns = [
[9, 1, -1, -1, []],
[1, 2, -1, 0, []],
// Wrong wire types in name, parent, value, and attribute references.
[1, "3", -1, 1, []],
[1, 3, -1, "1", []],
[1, 3, "0", 1, []],
[1, 3, -1, 1, [4, "bad", 4, 5]],
// Malformed and missing string references, including the value-only -1 sentinel.
[1, -1, -1, 1, []],
[1, 20, -1, 1, []],
[1, 7, -1, 1, []],
[1, 3, -2, 1, []],
[1, 3, 20, 1, []],
[1, 3, 7, 1, []],
[1, 3, -1, 1, [-1, 5]],
[1, 3, -1, 1, [20, 5]],
[1, 3, -1, 1, [4, -2]],
[1, 3, -1, 1, [4, 20]],
[1, 3, -1, 1, [4]],
[1, 3, -1, 1, "invalid"],
[1, 3, 8, 2, []],
[1, 9, -1, 1, [4, 6]],
] as const;
it("keeps DOM source positions across malformed rows and later documents", () => {
const completeness = new CdpCaptureCompleteness();
const generator = captureDom(
{
@@ -156,31 +156,43 @@ it("keeps DOM node and URL source positions when neighboring rows are malformed"
42,
"😀a",
"A",
"hidden",
"#text",
"go",
],
documents: [
{
documentURL: 0,
nodes: {
nodeType: columns.map(([type]) => type),
nodeName: columns.map(([, name]) => name),
nodeValue: columns.map(([, , value]) => value),
parentIndex: columns.map(([, , , parent]) => parent),
attributes: columns.map(([, , , , attributes]) => attributes),
nodeType: mixedDomColumns.map(([type]) => type),
nodeName: mixedDomColumns.map(([, name]) => name),
nodeValue: mixedDomColumns.map(([, , value]) => value),
parentIndex: mixedDomColumns.map(([, , , parent]) => parent),
attributes: mixedDomColumns.map(
([, , , , attributes]) => attributes,
),
},
},
{
documentURL: 0,
baseURL: -1,
nodes: {
nodeType: [9, 1, 3],
nodeName: [1, 9, 11],
nodeValue: [-1, -1, 12],
parentIndex: [-1, 0, 1],
attributes: [[], [4, -1, 10, -1], []],
},
},
],
},
new Set([origin]),
inspectWebPageInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: [origin],
target_id: "page-1",
}),
completeness,
);
const result = generator.next();
if (!result.done) throw new Error("Unexpected SVG identity query");
expect(result.value.total).toBe(20);
expect(result.value.total).toBe(23);
expect(result.value.nodeValueLengthUnit).toBe("utf16-code-units");
expect(result.value.nodes).toEqual([
{
index: 0,
@@ -214,6 +226,30 @@ it("keeps DOM node and URL source positions when neighboring rows are malformed"
node_value_length: 0,
attribute_names: ["href"],
},
{
index: 20,
node_type: 9,
node_name: "#document",
parent_index: -1,
node_value_length: 0,
attribute_names: [],
},
{
index: 21,
node_type: 1,
node_name: "A",
parent_index: 20,
node_value_length: 0,
attribute_names: ["href", "hidden"],
},
{
index: 22,
node_type: 3,
node_name: "#text",
parent_index: 21,
node_value_length: 2,
attribute_names: [],
},
]);
expect(result.value.urls).toEqual([
{
@@ -222,6 +258,12 @@ it("keeps DOM node and URL source positions when neighboring rows are malformed"
url: `${origin}/kept`,
destination_scope: "approved",
},
{
node_index: 21,
attribute: "href",
url: `${origin}/app`,
destination_scope: "approved",
},
]);
expect(completeness.snapshot()).toMatchObject({
unavailable_sections: ["dom", "metadata"],
@@ -263,11 +305,6 @@ describe("CDP DOM string and namespace validity", () => {
],
},
new Set([origin]),
inspectWebPageInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: [origin],
target_id: "page-1",
}),
completeness,
);
let captured = generator.next([]);
@@ -336,62 +373,3 @@ describe("CDP frame capture", () => {
);
});
});
describe("CDP DOM snapshot empty strings", () => {
it("reads Chrome's -1 string index as an empty node or attribute value", () => {
const completeness = new CdpCaptureCompleteness();
const generator = captureDom(
{
strings: [
origin + "/app",
"#document",
"A",
"href",
"hidden",
"#text",
"go",
],
documents: [
{
documentURL: 0,
baseURL: -1,
nodes: {
nodeType: [9, 1, 3],
nodeName: [1, 2, 5],
nodeValue: [-1, -1, 6],
parentIndex: [-1, 0, 1],
attributes: [[], [3, -1, 4, -1], []],
},
},
],
},
new Set([origin]),
inspectWebPageInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: [origin],
target_id: "page-1",
}),
completeness,
);
let captured = generator.next([]);
while (!captured.done) captured = generator.next([]);
expect(
captured.value.nodes.map(
({ node_name, node_value_length, attribute_names }) => ({
node_name,
node_value_length,
attribute_names,
}),
),
).toEqual([
{ node_name: "#document", node_value_length: 0, attribute_names: [] },
{
node_name: "A",
node_value_length: 0,
attribute_names: ["href", "hidden"],
},
{ node_name: "#text", node_value_length: 2, attribute_names: [] },
]);
expect(completeness.snapshot().excluded).toEqual([]);
});
});
-2
View File
@@ -1,4 +1,3 @@
import type { InspectWebPageInput } from "../domain/browserObservation.js";
import { sanitizeBrowserUrl } from "../domain/browserObservation.js";
import type { WebPageInspection } from "../domain/browserObservationSchemas.js";
import type { CdpFrameTree, CdpResourceTree } from "./protocol/CdpFrameTree.js";
@@ -226,7 +225,6 @@ interface PendingDomMetadata {
export function* captureDom(
root: CdpCommandResult<"DOMSnapshot.captureSnapshot">,
allowedOrigins: ReadonlySet<string>,
input: InspectWebPageInput,
completeness?: CdpCaptureCompleteness,
): Generator<
readonly DomSvgNodeQuery[],
+1 -6
View File
@@ -401,12 +401,7 @@ const capturePageDom = async (state: CaptureState): Promise<DomCapture> => {
);
return emptyDomCapture;
}
const capture = captureDom(
result,
allowedOrigins,
context.input,
state.events.completeness,
);
const capture = captureDom(result, allowedOrigins, state.events.completeness);
let step = capture.next();
let descriptionUnavailable = false;
const pendingController = new AbortController();
@@ -125,8 +125,8 @@ describe("CDP SVG identity at the snapshot boundary", () => {
reason: "provider_unavailable",
count: 3,
});
expect(result.value.limitations).toContain(
"SVG identity was unavailable for 3 snapshotted XLink candidate nodes; their URL destinations remain unknown.",
expect(result.value.limitations).toContainEqual(
expect.stringContaining("SVG identity"),
);
});