mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 14:35:45 +00:00
test(web): consolidate CDP document selection cases (#13183)
This commit is contained in:
1 parent
af0d69ee91
commit
ce61cca68a
1 file changed
+114
-138
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
|
||||
import { inspectWebPageInputSchema } from "../../../src/domain/browserObservation.js";
|
||||
import type { BrowserObservationFailureReason } from "../../../src/domain/browserObservationErrors.js";
|
||||
import { captureWebScreenshotInputSchema } from "../../../src/domain/webScreenshot.js";
|
||||
import { discoverWebMcpToolsInputSchema } from "../../../src/domain/webMcpDiscovery.js";
|
||||
import { observeWebSessionInputSchema } from "../../../src/domain/browserSession.js";
|
||||
@@ -20,7 +21,7 @@ import { frameNavigated, trackBrowser } from "./cdpBrowserProvider.support.js";
|
||||
*/
|
||||
interface Document {
|
||||
readonly id?: string;
|
||||
readonly loaderId?: string;
|
||||
readonly loaderId?: string | undefined;
|
||||
readonly path?: string;
|
||||
readonly url?: string;
|
||||
}
|
||||
@@ -72,7 +73,7 @@ const known: Document = { loaderId: "loader-main" };
|
||||
* A refusal reason, or a completed result whose document was proven
|
||||
* (`verified`) or left unproven with limited coverage (`limited`).
|
||||
*/
|
||||
type Outcome = "verified" | "limited" | string;
|
||||
type Outcome = "verified" | "limited" | BrowserObservationFailureReason;
|
||||
|
||||
const expectOutcome = (
|
||||
result:
|
||||
@@ -80,15 +81,15 @@ const expectOutcome = (
|
||||
| Awaited<ReturnType<CdpBrowserProvider["discoverWebMcpTools"]>>
|
||||
| Awaited<ReturnType<CdpBrowserProvider["captureScreenshot"]>>,
|
||||
outcome: Outcome,
|
||||
section: string,
|
||||
section: "frames" | "webmcp_tools" | "screenshot",
|
||||
) => {
|
||||
if (outcome !== "verified" && outcome !== "limited") {
|
||||
expect(result).toMatchObject({ ok: false, error: { reason: outcome } });
|
||||
return;
|
||||
}
|
||||
if (!result.ok) throw result.error;
|
||||
const limited = result.value.completeness.attach_limited_sections.includes(
|
||||
section as never,
|
||||
const limited = result.value.completeness.attach_limited_sections.some(
|
||||
(value) => value === section,
|
||||
);
|
||||
expect(limited).toBe(outcome === "limited");
|
||||
expect(
|
||||
@@ -143,12 +144,15 @@ describe("page capture", () => {
|
||||
after: { ...known, url: "https://outside.example/" },
|
||||
outcome: "target_not_allowed",
|
||||
},
|
||||
])("reports $name as $outcome", async ({ before, after, outcome }) => {
|
||||
const result = await inspect({
|
||||
commandResult: frameTrees(2, before ?? known, after),
|
||||
});
|
||||
expectOutcome(result, outcome, "frames");
|
||||
});
|
||||
] as const)(
|
||||
"reports $name as $outcome",
|
||||
async ({ before, after, outcome }) => {
|
||||
const result = await inspect({
|
||||
commandResult: frameTrees(2, before ?? known, after),
|
||||
});
|
||||
expectOutcome(result, outcome, "frames");
|
||||
},
|
||||
);
|
||||
|
||||
// Reads: 1 selects the document, 2 opens final capture, 3+ complete it.
|
||||
it.each([
|
||||
@@ -283,13 +287,16 @@ describe("WebMCP discovery", () => {
|
||||
after: { loaderId: "loader-next", url: "https://outside.example/" },
|
||||
outcome: "target_changed",
|
||||
},
|
||||
])("reports $name as $outcome", async ({ before, after, outcome }) => {
|
||||
const result = await discover({
|
||||
commandResult: frameTrees(1, before ?? known, after),
|
||||
});
|
||||
expectOutcome(result, outcome, "webmcp_tools");
|
||||
if (result.ok) expect(result.value.target.url).toMatch(/\/app$/u);
|
||||
});
|
||||
] as const)(
|
||||
"reports $name as $outcome",
|
||||
async ({ before, after, outcome }) => {
|
||||
const result = await discover({
|
||||
commandResult: frameTrees(1, before ?? known, after),
|
||||
});
|
||||
expectOutcome(result, outcome, "webmcp_tools");
|
||||
if (result.ok) expect(result.value.target.url).toMatch(/\/app$/u);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("screenshot capture", () => {
|
||||
@@ -309,7 +316,7 @@ describe("screenshot capture", () => {
|
||||
after: { ...known, path: "/next" },
|
||||
outcome: "target_changed",
|
||||
},
|
||||
])("reports $name as $outcome", async ({ after, outcome }) => {
|
||||
] as const)("reports $name as $outcome", async ({ after, outcome }) => {
|
||||
const browser = await start({
|
||||
commandResult: frameTrees(1, known, after),
|
||||
});
|
||||
@@ -370,7 +377,7 @@ type RootEvent = (
|
||||
command: Parameters<NonNullable<FakeOptions["commandEvents"]>>[0],
|
||||
origin: string,
|
||||
) => NonNullable<ReturnType<NonNullable<FakeOptions["commandEvents"]>>>[number];
|
||||
const rootEvents: Record<string, RootEvent> = {
|
||||
const rootEvents = {
|
||||
"a malformed loader on the selected frame": (command, origin) =>
|
||||
frameNavigated(command, {
|
||||
id: "frame-main",
|
||||
@@ -390,7 +397,7 @@ const rootEvents: Record<string, RootEvent> = {
|
||||
: { sessionId: command.sessionId }),
|
||||
params: { frameId: "frame-main", url: "https://outside.example/" },
|
||||
}),
|
||||
};
|
||||
} satisfies Record<string, RootEvent>;
|
||||
const runWithRootEvent = async (
|
||||
consumer: "page capture" | "screenshot",
|
||||
event: RootEvent,
|
||||
@@ -421,128 +428,97 @@ describe("root navigation events through the target session's document owner", (
|
||||
// Before the owner, page capture ignored same-document events and reported
|
||||
// a replacement root with a malformed loader as protocol_error; screenshot
|
||||
// reported a malformed loader on its own frame as target_changed.
|
||||
it.each([
|
||||
[
|
||||
"page capture",
|
||||
"a malformed loader on the selected frame",
|
||||
"protocol_error",
|
||||
],
|
||||
[
|
||||
"page capture",
|
||||
"a malformed loader on a replacement root frame",
|
||||
"target_changed",
|
||||
],
|
||||
[
|
||||
"page capture",
|
||||
"a same-document move to another origin",
|
||||
"target_not_allowed",
|
||||
],
|
||||
[
|
||||
"screenshot",
|
||||
"a malformed loader on the selected frame",
|
||||
"protocol_error",
|
||||
],
|
||||
[
|
||||
"screenshot",
|
||||
"a malformed loader on a replacement root frame",
|
||||
"target_changed",
|
||||
],
|
||||
[
|
||||
"screenshot",
|
||||
"a same-document move to another origin",
|
||||
"target_not_allowed",
|
||||
],
|
||||
] as const)("%s reports %s as %s", async (consumer, name, reason) => {
|
||||
const event = rootEvents[name];
|
||||
if (event === undefined) throw new Error(`Unknown event ${name}`);
|
||||
const result = await runWithRootEvent(consumer, event);
|
||||
expect(result).toMatchObject({ ok: false, error: { reason } });
|
||||
});
|
||||
|
||||
// The runtime session checks navigation observed between its frame-tree
|
||||
// reads first, so a reply that still shows the selected document cannot
|
||||
// hide a commit of another one.
|
||||
it.each([
|
||||
["another loader", "document-next", "target_changed"],
|
||||
["the selected loader", "document-1", null],
|
||||
] as const)(
|
||||
"runtime listener inspection checks a commit of %s observed between reads",
|
||||
async (_name, loaderId, reason) => {
|
||||
const browser = await startRuntimeBrowser({
|
||||
eventsBeforeReply: ["DOM.getDocument"],
|
||||
commandEvents: (command, origin) =>
|
||||
command.method === "DOM.getDocument"
|
||||
? [
|
||||
frameNavigated(command, {
|
||||
id: "runtime-main",
|
||||
loaderId,
|
||||
url: `${origin}/app`,
|
||||
}),
|
||||
]
|
||||
: undefined,
|
||||
});
|
||||
trackBrowser(browser);
|
||||
const result = await new CdpWebRuntimeProvider().inspectEventListeners(
|
||||
inspectWebEventListenersInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
allowed_origins: [browser.allowedOrigin],
|
||||
selector: "#selected",
|
||||
}),
|
||||
);
|
||||
if (reason === null) {
|
||||
if (!result.ok) throw result.error;
|
||||
return;
|
||||
}
|
||||
const transitions = [
|
||||
["a malformed loader on the selected frame", "protocol_error"],
|
||||
["a malformed loader on a replacement root frame", "target_changed"],
|
||||
["a same-document move to another origin", "target_not_allowed"],
|
||||
] as const;
|
||||
it.each(
|
||||
(["page capture", "screenshot"] as const).flatMap((consumer) =>
|
||||
transitions.map(([name, reason]) => ({ consumer, name, reason })),
|
||||
),
|
||||
)(
|
||||
"$consumer reports $name as $reason",
|
||||
async ({ consumer, name, reason }) => {
|
||||
const result = await runWithRootEvent(consumer, rootEvents[name]);
|
||||
expect(result).toMatchObject({ ok: false, error: { reason } });
|
||||
},
|
||||
);
|
||||
|
||||
// A selection without a loader cannot be proven continuous with a document
|
||||
// that later reports one, even when an event taught the owner that loader
|
||||
// before the next frame-tree read.
|
||||
it("runtime listener inspection reports a loader first seen after a loaderless selection", async () => {
|
||||
let committed = false;
|
||||
const browser = await startRuntimeBrowser({
|
||||
eventsBeforeReply: ["DOM.getDocument"],
|
||||
commandResult: (command, origin) =>
|
||||
command.method === "Page.getFrameTree"
|
||||
? {
|
||||
frameTree: {
|
||||
frame: {
|
||||
id: "runtime-main",
|
||||
url: `${origin}/app`,
|
||||
...(committed ? { loaderId: "document-next" } : {}),
|
||||
},
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
commandEvents: (command, origin) => {
|
||||
if (command.method !== "DOM.getDocument") return undefined;
|
||||
committed = true;
|
||||
return [
|
||||
frameNavigated(command, {
|
||||
id: "runtime-main",
|
||||
loaderId: "document-next",
|
||||
url: `${origin}/app`,
|
||||
it.each([
|
||||
{
|
||||
name: "a replacement reported only by the event",
|
||||
initial: "document-1",
|
||||
commit: "document-next",
|
||||
changed: true,
|
||||
},
|
||||
{
|
||||
name: "a duplicate commit",
|
||||
initial: "document-1",
|
||||
commit: "document-1",
|
||||
changed: false,
|
||||
},
|
||||
{
|
||||
name: "a first loader after a loaderless selection",
|
||||
initial: undefined,
|
||||
commit: "document-next",
|
||||
changed: true,
|
||||
},
|
||||
] as const)(
|
||||
"runtime listener inspection handles $name",
|
||||
async ({ initial, commit, changed }) => {
|
||||
let committed = false;
|
||||
const browser = await startRuntimeBrowser({
|
||||
eventsBeforeReply: ["DOM.getDocument"],
|
||||
commandResult: ({ method }, origin) =>
|
||||
method === "Page.getFrameTree"
|
||||
? frameTree(origin, {
|
||||
id: "runtime-main",
|
||||
loaderId: initial ?? (committed ? commit : undefined),
|
||||
})
|
||||
: undefined,
|
||||
commandEvents: (command, origin) => {
|
||||
if (command.method !== "DOM.getDocument") return undefined;
|
||||
committed = true;
|
||||
return [
|
||||
frameNavigated(command, {
|
||||
id: "runtime-main",
|
||||
loaderId: commit,
|
||||
url: `${origin}/app`,
|
||||
}),
|
||||
];
|
||||
},
|
||||
});
|
||||
trackBrowser(browser);
|
||||
const provider = new CdpWebRuntimeProvider();
|
||||
try {
|
||||
const result = await provider.inspectEventListeners(
|
||||
inspectWebEventListenersInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
allowed_origins: [browser.allowedOrigin],
|
||||
target_id: "allowed-page",
|
||||
selector: "#selected",
|
||||
}),
|
||||
];
|
||||
},
|
||||
});
|
||||
trackBrowser(browser);
|
||||
const result = await new CdpWebRuntimeProvider().inspectEventListeners(
|
||||
inspectWebEventListenersInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
allowed_origins: [browser.allowedOrigin],
|
||||
selector: "#selected",
|
||||
}),
|
||||
);
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { reason: "target_changed" },
|
||||
});
|
||||
});
|
||||
);
|
||||
expect(committed).toBe(true);
|
||||
if (changed) {
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { reason: "target_changed" },
|
||||
});
|
||||
} else {
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value).toMatchObject({
|
||||
selected_node: { selector: "#selected", match: "found" },
|
||||
listeners: [{ type: "click", execution: "unknown" }],
|
||||
cleanup: "confirmed",
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
await provider.close();
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("observation window replay", () => {
|
||||
|
||||
Reference in new issue
Block a user