Files
rea/src/browser/CdpCaptureDocuments.baseUrl.test.ts
T
b4db35f092 fix: bind identity and coverage to the producing rule (#1513)
* fix: bind identity and coverage to the producing rule

Logical paths, container families, examined bytes, path-segment conventions, destination collisions, file-id leases, Mach-O CPU selection, and firmware cleanup now follow the observation that owns each rule.

* fix: fail unmatched Mach-O slices and keep firmware roots independent

A fat target whose architecture lipo did not list now stops before slice tools run. Chained fixups still count unread LC_DYLD_INFO rebase bytes, and a firmware cleanup failure no longer blocks the next workspace.

* test: cover path identity and runtime families at the projection boundary

NFC joins and segment conventions are now asserted by the path resolver, the Apple projection, and a real APK inventory instead of helper unit tests.

* test: expect unread dyld rebase bytes in the Apple dispatch fixture

The legacy LC_DYLD_INFO fixture keeps a rebase range this reader does not decode, so the public CLI and MCP check now requires that partial coverage beside the bind opcodes.

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-10-10 17:14:47 +08:00

226 lines
6.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { inspectWebPageInputSchema } from "../domain/browserObservation.js";
import { captureDom } from "./CdpCaptureDocuments.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",
});
const snapshot = (
baseUrl?: string,
baseHref = "assets/",
pageUrl = documentUrl,
linkHref = "guide",
) => ({
strings: [
pageUrl,
baseUrl ?? "",
"#document",
"BASE",
"A",
"LINK",
"FORM",
"",
"href",
baseHref,
linkHref,
"rel",
"mcp",
"agent",
"action",
"submit",
],
documents: [
{
documentURL: 0,
...(baseUrl === undefined ? {} : { baseURL: 1 }),
nodes: {
nodeType: [9, 1, 1, 1, 1],
nodeName: [2, 3, 4, 5, 6],
nodeValue: [7, 7, 7, 7, 7],
parentIndex: [-1, 0, 0, 0, 0],
attributes: [[], [8, 9], [8, 10], [8, 13, 11, 12], [14, 15]],
},
},
],
});
const capture = (value: ReturnType<typeof snapshot>) =>
captureDom(value, new Set([origin]), input);
describe("DOM metadata document base URLs", () => {
it("resolves links, actions and agent hints against the captured document base", () => {
const result = capture(snapshot(`${origin}/screens/assets/`));
expect(result.urls.map(({ url }) => url)).toEqual([
`${origin}/screens/assets/`,
`${origin}/screens/assets/guide`,
`${origin}/screens/assets/agent`,
`${origin}/screens/assets/submit`,
]);
expect(result.agentHints[0]?.url).toBe(`${origin}/screens/assets/agent`);
});
it("uses the document URL as fallback when no baseURL was supplied", () => {
const result = capture(snapshot());
expect(result.urls.map(({ url }) => url)).toEqual([
`${origin}/screens/assets/`,
`${origin}/screens/guide`,
`${origin}/screens/agent`,
`${origin}/screens/submit`,
]);
});
it("applies destination policy after resolving an ordinary foreign base", () => {
const foreign = "https://cdn.example.test/assets/";
const result = capture(snapshot(foreign, foreign));
expect(result.nodes).toHaveLength(5);
expect(result.urls).toHaveLength(4);
expect(
result.urls.every(
({ url, destination_scope }) =>
url === null && destination_scope === "outside_policy",
),
).toBe(true);
expect(result.agentHints[0]?.url).toBeNull();
});
it("keeps an absolute approved link independent of a foreign base", () => {
const foreign = "https://cdn.example.test/assets/";
const result = capture(
snapshot(foreign, foreign, documentUrl, `${origin}/guide`),
);
expect(result.urls[1]).toMatchObject({
url: `${origin}/guide`,
destination_scope: "approved",
});
});
it.each([
"javascript:void(0)",
"mailto:someone@example.test",
"data:text/plain,hello",
"ftp://app.example.test/file",
])("classifies the non-http(s) destination %s as unsupported", (href) => {
const result = capture(snapshot(undefined, "assets/", documentUrl, href));
expect(result.urls[1]).toMatchObject({
url: null,
destination_scope: "unsupported",
});
});
it("classifies an unparseable destination as unsupported", () => {
const result = capture(
snapshot(undefined, "assets/", documentUrl, "https://[invalid"),
);
expect(result.urls[1]).toMatchObject({
url: null,
destination_scope: "unsupported",
});
});
it("classifies a same-origin destination with credentials as approved", () => {
const result = capture(
snapshot(
undefined,
"assets/",
documentUrl,
"https://user:secret@app.example.test/guide",
),
);
expect(result.urls[1]).toMatchObject({
url: `${origin}/guide`,
destination_scope: "approved",
});
});
it("does not authorize a foreign document using its approved base URL", () => {
const result = capture(
snapshot(
`${origin}/assets/`,
"assets/",
"https://outside.example.test/app",
),
);
expect(result.nodes).toEqual([]);
expect(result.urls).toEqual([]);
expect(result.agentHints).toEqual([]);
});
});
describe("empty form destinations with a document base URL", () => {
it("keeps an empty form action approved when the document base is foreign", () => {
const foreign = "https://cdn.example.test/assets/";
const value = snapshot(foreign, foreign);
value.strings[15] = "";
expect(capture(value).urls[3]).toMatchObject({
url: documentUrl,
destination_scope: "approved",
});
});
it.each([" ", "\t\r\n\f"])(
"resolves an HTML-whitespace-only form action to the document: %j",
(action) => {
const baseUrl = `${origin}/assets/`;
const value = snapshot(baseUrl);
value.strings[15] = action;
expect(capture(value).urls[3]?.url).toBe(documentUrl);
},
);
it.each(["BUTTON", "INPUT"])(
"retains document-base resolution for a whitespace-only %s formaction",
(nodeName) => {
const baseUrl = `${origin}/assets/`;
const value = snapshot(baseUrl);
value.strings[6] = nodeName;
value.strings[14] = "formaction";
value.strings[15] = " ";
expect(capture(value).urls[3]?.url).toBe(baseUrl);
},
);
it.each([
{ value: "\u000b", suffix: "" },
{ value: "\u00a0", suffix: "%C2%A0" },
])(
"does not treat non-HTML whitespace as an empty form action: $value",
({ value: action, suffix }) => {
const baseUrl = `${origin}/assets/`;
const value = snapshot(baseUrl);
value.strings[15] = action;
expect(capture(value).urls[3]?.url).toBe(`${baseUrl}${suffix}`);
},
);
it("keeps the first value when an attribute name repeats", () => {
const value = snapshot();
const later = `${origin}/later`;
value.strings.push(later);
const href = value.strings.indexOf("href");
const guide = value.strings.indexOf("guide");
const nodes = value.documents[0]?.nodes;
if (nodes === undefined) throw new TypeError("Expected a DOM snapshot");
nodes.attributes[2] = [href, guide, href, value.strings.length - 1];
const result = capture(value);
expect(result.nodes[2]?.attribute_names).toEqual(["href", "href"]);
expect(result.urls.map(({ url }) => url)).toContain(
`${origin}/screens/guide`,
);
expect(result.urls.map(({ url }) => url)).not.toContain(later);
});
it("does not apply form semantics to an action attribute on another element", () => {
const baseUrl = `${origin}/assets/`;
const value = snapshot(baseUrl);
value.strings[6] = "DIV";
value.strings[15] = "";
expect(capture(value).urls[3]?.url).toBe(baseUrl);
});
});