mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 22:45:47 +00:00
test(web): consolidate CDP frame and session identity checks (#13174)
This commit is contained in:
1 parent
10597e10f2
commit
48a2ad5e0b
3 files changed
+115
-182
No files matched your search
@@ -1,108 +1,59 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { expect, it } from "vitest";
|
||||
import { selectCdpFrameIdentity } from "./CdpFrameIdentity.js";
|
||||
import { receiveCdpEvent, type CdpEvent } from "./CdpConnection.js";
|
||||
import { receiveCdpEvent } from "./CdpConnection.js";
|
||||
import { cdpFrameEvent } from "./protocol/CdpEvents.js";
|
||||
|
||||
/** Frame facts as the connection's registered event schema parses them. */
|
||||
const cdpFrameEventFacts = (event: CdpEvent) => {
|
||||
const frame = cdpFrameEvent(receiveCdpEvent(event));
|
||||
if (frame === undefined)
|
||||
throw new Error(`${event.method} is not a frame event`);
|
||||
return frame.facts;
|
||||
const selectFrame = (frame: unknown, selected = "selected-main") => {
|
||||
const event = cdpFrameEvent(
|
||||
receiveCdpEvent({ method: "Page.frameNavigated", params: { frame } }),
|
||||
);
|
||||
if (event === undefined) throw new Error("Expected a frame navigation");
|
||||
return selectCdpFrameIdentity(event.facts, selected);
|
||||
};
|
||||
|
||||
describe("CDP frame identity ownership", () => {
|
||||
it("keeps missing or malformed frame objects from becoming root evidence", () => {
|
||||
for (const params of [{}, { frame: null }, { frame: "frame" }]) {
|
||||
expect(
|
||||
selectCdpFrameIdentity(
|
||||
cdpFrameEventFacts({ method: "Page.frameNavigated", params }),
|
||||
"selected-main",
|
||||
),
|
||||
).toMatchObject({
|
||||
selection: "unknown",
|
||||
frameId: null,
|
||||
identityIssue: "missing_frame",
|
||||
parentRelation: "unknown",
|
||||
isRootReplacement: false,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("recognizes a valid frame with no parent property as a root", () => {
|
||||
expect(
|
||||
selectCdpFrameIdentity(
|
||||
cdpFrameEventFacts({
|
||||
method: "Page.frameNavigated",
|
||||
params: { frame: { id: "replacement-main" } },
|
||||
}),
|
||||
"selected-main",
|
||||
),
|
||||
).toMatchObject({
|
||||
applicability: "applicable",
|
||||
selection: "other",
|
||||
isRootReplacement: true,
|
||||
identityIssue: null,
|
||||
parentRelation: "root",
|
||||
parentId: null,
|
||||
parentIssue: null,
|
||||
it("keeps missing or malformed frame objects from becoming root evidence", () => {
|
||||
for (const frame of [undefined, null, "frame"]) {
|
||||
expect(selectFrame(frame)).toMatchObject({
|
||||
selection: "unknown",
|
||||
frameId: null,
|
||||
identityIssue: "missing_frame",
|
||||
parentRelation: "unknown",
|
||||
isRootReplacement: false,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("distinguishes a replacement root from a frame with contradictory parent facts", () => {
|
||||
expect(selectFrame({ id: "candidate" })).toMatchObject({
|
||||
applicability: "applicable",
|
||||
selection: "other",
|
||||
isRootReplacement: true,
|
||||
identityIssue: null,
|
||||
parentRelation: "root",
|
||||
parentId: null,
|
||||
parentIssue: null,
|
||||
});
|
||||
|
||||
it("does not interpret a malformed parent value as root evidence", () => {
|
||||
expect(
|
||||
selectCdpFrameIdentity(
|
||||
cdpFrameEventFacts({
|
||||
method: "Page.frameNavigated",
|
||||
params: { frame: { id: "replacement-main", parentId: "" } },
|
||||
}),
|
||||
"selected-main",
|
||||
),
|
||||
).toMatchObject({
|
||||
for (const [parentId, parentIssue] of [
|
||||
["", "invalid_parent_id"],
|
||||
["candidate", "self_parent"],
|
||||
]) {
|
||||
expect(selectFrame({ id: "candidate", parentId })).toMatchObject({
|
||||
selection: "other",
|
||||
isRootReplacement: false,
|
||||
frameId: "candidate",
|
||||
identityIssue: null,
|
||||
parentRelation: "unknown",
|
||||
parentId: null,
|
||||
parentIssue: "invalid_parent_id",
|
||||
parentIssue,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
it("preserves a self-parented frame ID but leaves its parent relation unknown", () => {
|
||||
expect(
|
||||
selectCdpFrameIdentity(
|
||||
cdpFrameEventFacts({
|
||||
method: "Page.frameNavigated",
|
||||
params: {
|
||||
frame: { id: "candidate-root", parentId: "candidate-root" },
|
||||
},
|
||||
}),
|
||||
"selected-main",
|
||||
),
|
||||
).toMatchObject({
|
||||
selection: "other",
|
||||
isRootReplacement: false,
|
||||
frameId: "candidate-root",
|
||||
identityIssue: null,
|
||||
parentRelation: "unknown",
|
||||
parentId: null,
|
||||
parentIssue: "self_parent",
|
||||
});
|
||||
});
|
||||
|
||||
it("retains the selected-main parent issue for a self-parented selected frame", () => {
|
||||
expect(
|
||||
selectCdpFrameIdentity(
|
||||
cdpFrameEventFacts({
|
||||
method: "Page.frameNavigated",
|
||||
params: { frame: { id: "selected-main", parentId: "selected-main" } },
|
||||
}),
|
||||
"selected-main",
|
||||
),
|
||||
).toMatchObject({
|
||||
selection: "main",
|
||||
parentRelation: "unknown",
|
||||
parentIssue: "main_frame_has_parent",
|
||||
});
|
||||
expect(
|
||||
selectFrame({ id: "candidate", parentId: "candidate" }, "candidate"),
|
||||
).toMatchObject({
|
||||
selection: "main",
|
||||
parentRelation: "unknown",
|
||||
parentIssue: "main_frame_has_parent",
|
||||
});
|
||||
});
|
||||
@@ -1,17 +1,14 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { fakeCdpEvents } from "../../tests/fixtures/fakeCdpEvents.js";
|
||||
import type { CdpEvent } from "./CdpConnection.js";
|
||||
import type { CdpEvent, CdpTransportEvent } from "./CdpConnection.js";
|
||||
import { onCdpTargetEvent } from "./CdpTargetEvents.js";
|
||||
|
||||
const route = (
|
||||
sessionId: string | undefined,
|
||||
sent: readonly CdpEvent[],
|
||||
): (string | undefined)[] => {
|
||||
const route = (sessionId: string | undefined, sent: readonly CdpEvent[]) => {
|
||||
const events = fakeCdpEvents();
|
||||
const received: (string | undefined)[] = [];
|
||||
const received: CdpTransportEvent[] = [];
|
||||
const unsubscribe = onCdpTargetEvent(events, sessionId, (event) =>
|
||||
received.push(event.registered?.method ?? event.domain),
|
||||
received.push(event),
|
||||
);
|
||||
for (const event of sent) events.emit(event);
|
||||
unsubscribe();
|
||||
@@ -40,12 +37,24 @@ describe("target event routing", () => {
|
||||
detached("other"),
|
||||
detached("mine"),
|
||||
]),
|
||||
).toEqual(["Page.frameNavigated", "Target.detachedFromTarget"]);
|
||||
).toMatchObject([
|
||||
{ sessionId: "mine", registered: { method: "Page.frameNavigated" } },
|
||||
{
|
||||
registered: {
|
||||
method: "Target.detachedFromTarget",
|
||||
payload: { sessionId: "mine" },
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("delivers a page socket's unscoped events, never another session's", () => {
|
||||
expect(
|
||||
route(undefined, [navigated(), navigated("other"), detached("other")]),
|
||||
).toEqual(["Page.frameNavigated"]);
|
||||
route(undefined, [
|
||||
navigated(),
|
||||
navigated("other"),
|
||||
detached("other"),
|
||||
]).map(({ sessionId, registered }) => [sessionId, registered?.method]),
|
||||
).toEqual([[undefined, "Page.frameNavigated"]]);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { withVerifierCleanup } from "../../../scripts/lib/verifier-cleanup.mjs";
|
||||
|
||||
import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
|
||||
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
|
||||
@@ -10,29 +11,46 @@ import { observeWebSessionInputSchema } from "../../../src/domain/browserSession
|
||||
import { analyzeWebBundleInputSchema } from "../../../src/domain/webBundleAnalysis.js";
|
||||
import { discoverWebMcpToolsInputSchema } from "../../../src/domain/webMcpDiscovery.js";
|
||||
import { captureWebScreenshotInputSchema } from "../../../src/domain/webScreenshot.js";
|
||||
import {
|
||||
startFakeCdpBrowser,
|
||||
type FakeOptions,
|
||||
} from "../../fixtures/fakeCdpBrowser.js";
|
||||
import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js";
|
||||
import { frameNavigated, trackBrowser } from "./cdpBrowserProvider.support.js";
|
||||
|
||||
const inspectWithNetworkEvents = async (
|
||||
commandEvents: NonNullable<FakeOptions["commandEvents"]>,
|
||||
const inspectWithFrameEvent = async (
|
||||
frame: Readonly<Record<string, unknown>> | undefined,
|
||||
) => {
|
||||
const browser = await startFakeCdpBrowser({
|
||||
eventsBeforeReply: ["Network.enable"],
|
||||
commandEvents,
|
||||
commandEvents: (command, origin) => {
|
||||
if (command.method !== "Network.enable") return undefined;
|
||||
const event = frameNavigated(command, {
|
||||
url: `${origin}/app?token=frame-secret`,
|
||||
...frame,
|
||||
});
|
||||
return [frame === undefined ? { ...event, params: undefined } : event];
|
||||
},
|
||||
});
|
||||
trackBrowser(browser);
|
||||
const result = await new CdpBrowserProvider().inspectPage(
|
||||
inspectWebPageInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
allowed_origins: [browser.allowedOrigin],
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 1,
|
||||
}),
|
||||
);
|
||||
return { browser, result };
|
||||
const provider = new CdpBrowserProvider();
|
||||
return withVerifierCleanup(async () => {
|
||||
const result = await provider.inspectPage(
|
||||
inspectWebPageInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
allowed_origins: [browser.allowedOrigin],
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 1,
|
||||
}),
|
||||
);
|
||||
const enabled = browser.commands.find(
|
||||
({ method }) => method === "Network.enable",
|
||||
);
|
||||
expect(enabled?.sessionId).toEqual(expect.any(String));
|
||||
expect(
|
||||
browser.commands.filter(
|
||||
({ method }) => method === "Target.detachFromTarget",
|
||||
),
|
||||
).toMatchObject([{ params: { sessionId: enabled?.sessionId } }]);
|
||||
await expect.poll(() => browser.activeConnectionCount()).toBe(0);
|
||||
return { browser, result };
|
||||
}, [() => provider.close()]);
|
||||
};
|
||||
|
||||
it("applies exact origin selection while retaining caller-visible URLs", async () => {
|
||||
@@ -233,39 +251,32 @@ it("lists only usable direct transports and accounts for exclusions before captu
|
||||
}
|
||||
});
|
||||
|
||||
it("fails with a protocol error after an unidentifiable frame event and detaches", async () => {
|
||||
const { browser, result } = await inspectWithNetworkEvents((command) =>
|
||||
command.method === "Network.enable"
|
||||
? [
|
||||
{
|
||||
method: "Page.frameNavigated",
|
||||
...(command.sessionId === undefined
|
||||
? {}
|
||||
: { sessionId: command.sessionId }),
|
||||
params: undefined,
|
||||
},
|
||||
]
|
||||
: undefined,
|
||||
);
|
||||
it.each([
|
||||
["a missing payload", undefined, "protocol_error"],
|
||||
[
|
||||
"a self-parented frame",
|
||||
{ id: "candidate-root", parentId: "candidate-root" },
|
||||
"protocol_error",
|
||||
],
|
||||
[
|
||||
"a replacement root with the same URL and loader",
|
||||
{ id: "replacement-main", loaderId: "loader-main" },
|
||||
"target_changed",
|
||||
],
|
||||
] as const)("fails and detaches after %s", async (_label, frame, reason) => {
|
||||
const { result } = await inspectWithFrameEvent(frame);
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "BrowserObservationError", reason: "protocol_error" },
|
||||
error: { _tag: "BrowserObservationError", reason },
|
||||
});
|
||||
await expect.poll(() => browser.activeConnectionCount()).toBe(0);
|
||||
});
|
||||
|
||||
it("retains malformed child-frame identity as frame coverage while completing capture", async () => {
|
||||
const { browser, result } = await inspectWithNetworkEvents((command) =>
|
||||
command.method === "Network.enable"
|
||||
? [
|
||||
frameNavigated(command, {
|
||||
id: 3,
|
||||
parentId: "frame-main",
|
||||
url: "https://outside.invalid/child",
|
||||
}),
|
||||
]
|
||||
: undefined,
|
||||
);
|
||||
const { browser, result } = await inspectWithFrameEvent({
|
||||
id: 3,
|
||||
parentId: "frame-main",
|
||||
url: "https://outside.invalid/child",
|
||||
});
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.completeness.unavailable_sections).toContain("frames");
|
||||
expect(result.value.target.url).toBe(
|
||||
@@ -273,44 +284,6 @@ it("retains malformed child-frame identity as frame coverage while completing ca
|
||||
);
|
||||
});
|
||||
|
||||
it("fails capture on a contradictory self-parented frame instead of accepting it as a child", async () => {
|
||||
const { browser, result } = await inspectWithNetworkEvents(
|
||||
(command, origin) =>
|
||||
command.method === "Network.enable"
|
||||
? [
|
||||
frameNavigated(command, {
|
||||
id: "candidate-root",
|
||||
parentId: "candidate-root",
|
||||
url: `${origin}/app?token=frame-secret`,
|
||||
}),
|
||||
]
|
||||
: undefined,
|
||||
);
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { reason: "protocol_error" },
|
||||
});
|
||||
await expect.poll(() => browser.activeConnectionCount()).toBe(0);
|
||||
});
|
||||
|
||||
it("treats a same-URL and same-loader root frame ID change as a capture change", async () => {
|
||||
const { result } = await inspectWithNetworkEvents((command, origin) =>
|
||||
command.method === "Network.enable"
|
||||
? [
|
||||
frameNavigated(command, {
|
||||
id: "replacement-main",
|
||||
loaderId: "loader-main",
|
||||
url: `${origin}/app?token=frame-secret`,
|
||||
}),
|
||||
]
|
||||
: undefined,
|
||||
);
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "BrowserObservationError", reason: "target_changed" },
|
||||
});
|
||||
});
|
||||
|
||||
describe("CdpBrowserProvider: discovery authorization 2", () => {
|
||||
it("rejects empty browser attachment session identifiers", async () => {
|
||||
const browser = await startFakeCdpBrowser({ invalidAttachedSession: true });
|
||||
|
||||
Reference in new issue
Block a user