Files
reverse-engineering-anything/src/browser/CdpCaptureDocuments.test.ts
T
morluto 085ef4cc3b test(browser): consolidate DOM and resource capture coverage
Preserve exact DOM and frame identity, SVG and XML URL handling, accessibility and resource completeness, and caller-visible comparison results in shared fixtures.
2026-11-18 08:43:07 -05:00

398 lines
11 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
captureDom,
captureFrames,
captureResources,
classifyCdpFrameUrl,
observeCdpFrameUrl,
} from "./CdpCaptureDocuments.js";
import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
import {
frameTreeReply,
resourceTreeReply,
} from "../../tests/fixtures/fakeCdpSend.js";
import { inspectWebPageInputSchema } from "../domain/browserObservation.js";
const origin = "https://app.example.test";
it("keeps reported origin evidence independent of malformed or missing frame URLs", () => {
const allowed = new Set([origin]);
const url = "https://user:password@app.example.test/page?token=local#old";
for (const urlFragment of [7, null, "without-hash"]) {
const observed = observeCdpFrameUrl({ url, urlFragment });
expect(observed, String(urlFragment)).toEqual({
kind: "invalid_fragment",
scopeUrl: url,
});
expect(classifyCdpFrameUrl(observed, allowed), String(urlFragment)).toEqual(
{
scope: "approved",
origin,
url: undefined,
},
);
}
expect(
classifyCdpFrameUrl(
observeCdpFrameUrl({ urlFragment: `#${origin}` }),
allowed,
),
).toEqual({
scope: "unknown",
origin: null,
url: undefined,
});
});
it("publishes exact approved frame URLs and counts malformed and foreign children", () => {
const completeness = new CdpCaptureCompleteness();
const children = [
{ id: "inline", url: `${origin}/inline?x=1#old` },
{
id: "replaced",
url: "https://user:password@app.example.test/replaced?token=local#old",
urlFragment: "#new",
},
{ id: "cleared", url: `${origin}/cleared?x=1#old`, urlFragment: "" },
{ id: "malformed-approved", url: `${origin}/invalid`, urlFragment: 7 },
{
id: "malformed-foreign-number",
url: "https://foreign.test/number",
urlFragment: 7,
},
{
id: "malformed-foreign-string",
url: "https://foreign.test/string",
urlFragment: "without-hash",
},
{
id: "foreign",
url: "https://foreign.test/page",
urlFragment: `#${origin}`,
},
];
const captured = captureFrames(
frameTreeReply({
frameTree: {
frame: { id: "root", url: `${origin}/` },
childFrames: children.map((frame) => ({
frame: { ...frame, parentId: "root" },
})),
},
}),
new Set([origin]),
undefined,
completeness,
);
expect(captured.items).toEqual([
{ frame_id: "root", parent_frame_id: null, url: `${origin}/`, origin },
{
frame_id: "inline",
parent_frame_id: "root",
url: `${origin}/inline?x=1#old`,
origin,
},
{
frame_id: "replaced",
parent_frame_id: "root",
url: `${origin}/replaced?token=local#new`,
origin,
},
{
frame_id: "cleared",
parent_frame_id: "root",
url: `${origin}/cleared?x=1`,
origin,
},
]);
expect(completeness.snapshot()).toMatchObject({
unavailable_sections: ["frames"],
policy_filtered_sections: ["frames"],
excluded: [
{ section: "frames", reason: "disallowed_origin", count: 3 },
{ section: "frames", reason: "invalid_protocol_value", count: 3 },
],
});
});
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 completeness = new CdpCaptureCompleteness();
const generator = captureDom(
{
strings: [
origin + "/app",
"#document",
"HTML",
"SPAN",
"href",
"/decoy",
"/kept",
42,
"😀a",
"A",
],
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),
},
},
],
},
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.nodes).toEqual([
{
index: 0,
node_type: 9,
node_name: "#document",
parent_index: -1,
node_value_length: 0,
attribute_names: [],
},
{
index: 1,
node_type: 1,
node_name: "HTML",
parent_index: 0,
node_value_length: 0,
attribute_names: [],
},
{
index: 18,
node_type: 1,
node_name: "SPAN",
parent_index: 2,
node_value_length: 3,
attribute_names: [],
},
{
index: 19,
node_type: 1,
node_name: "A",
parent_index: 1,
node_value_length: 0,
attribute_names: ["href"],
},
]);
expect(result.value.urls).toEqual([
{
node_index: 19,
attribute: "href",
url: `${origin}/kept`,
destination_scope: "approved",
},
]);
expect(completeness.snapshot()).toMatchObject({
unavailable_sections: ["dom", "metadata"],
excluded: [
{ section: "dom", reason: "invalid_protocol_value", count: 16 },
{ section: "metadata", reason: "invalid_protocol_value", count: 16 },
],
});
});
describe("CDP DOM string and namespace validity", () => {
it("does not infer SVG ancestry through an element with malformed namespace data", () => {
const strings: unknown[] = [
origin + "/app",
"#document",
"svg",
"a",
"xmlns:xlink",
"http://www.w3.org/1999/xlink",
"xlink:href",
"/svg-link",
];
const completeness = new CdpCaptureCompleteness();
const generator = captureDom(
{
strings,
documents: [
{
documentURL: 0,
baseURL: 0,
nodes: {
nodeType: [9, 1, 1],
nodeName: [1, 2, 3],
nodeValue: [0, 0, 0],
parentIndex: [-1, 0, 1],
attributes: [[], [4, 99], [6, 7]],
},
},
],
},
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) => node.index)).toEqual([0, 2]);
expect(captured.value.nodes[1]?.attribute_names).toEqual(["xlink:href"]);
expect(captured.value.urls).toEqual([]);
expect(
completeness
.snapshot()
.excluded.some(
({ section, reason }) =>
section === "metadata" && reason === "invalid_protocol_value",
),
).toBe(true);
});
});
describe("CDP frame capture", () => {
it("walks deeply nested frame trees and returns every frame and resource", () => {
const rootIndex = 1_999;
let frameTree: Record<string, unknown> = {
frame: { id: "frame-0", parentId: "frame-1", url: `${origin}/0` },
resources: [
{ url: `${origin}/0.js`, type: "Script", mimeType: "text/javascript" },
],
};
for (let index = 1; index <= rootIndex; index += 1) {
frameTree = {
frame: {
id: `frame-${index}`,
...(index === rootIndex ? {} : { parentId: `frame-${index + 1}` }),
url: `${origin}/${index}`,
},
resources: [
{
url: `${origin}/${String(index)}.js`,
type: "Script",
mimeType: "text/javascript",
},
],
childFrames: [frameTree],
};
}
const result = { frameTree };
const frames = captureFrames(frameTreeReply(result), new Set([origin]));
const resources = captureResources(
resourceTreeReply(result),
new Set([origin]),
);
const frameIndexes = Array.from(
{ length: rootIndex + 1 },
(_, index) => rootIndex - index,
);
expect(frames.items).toEqual(
frameIndexes.map((index) => ({
frame_id: `frame-${index}`,
parent_frame_id: index === rootIndex ? null : `frame-${index + 1}`,
url: `${origin}/${index}`,
origin,
})),
);
expect(resources.items.map(({ url }) => url)).toEqual(
frameIndexes.map((index) => `${origin}/${index}.js`),
);
});
});
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([]);
});
});