mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 03:33:25 +00:00
* refactor(browser): send only CDP commands with registered result schemas * refactor(browser): open Playwright CDP sessions only through the registered-command owner * fix(browser): keep the restore obligation for an applied override with a malformed reply A schema-rejected success reply means the browser applied the Emulation override, so the attached environment owner records the reset obligation before reporting the protocol error. Only a rejected or unanswered command leaves no obligation. * refactor(browser): open verifier-script CDP sessions through the registered-command owner (#10710) * fix(browser): register the CDP commands JavaScript verifiers send The verifier scripts send Browser.close, Page.createIsolatedWorld, Page.navigate, Page.reload, Target.activateTarget, Target.closeTarget and Target.createTarget through CdpConnection, which now admits every reply through its command's result schema. Register those schemas, refuse an unregistered run-time name as a typed malformed reply instead of a TypeError, and check script send literals against the registry. * fix(browser): keep Target.closeTarget's success member for the verifiers * test(browser): characterize target_changed rules per consumer * refactor(browser): decide target_changed from one document identity owner * fix(browser): compare the selected document on every authorization read Final-capture retries and the pre-capture frame-tree read now observe each reply through the selected document tracker before scope, and a screenshot command failure after a proven document change reports target_changed with the failure as its cause.
134 lines
4.9 KiB
TypeScript
134 lines
4.9 KiB
TypeScript
import { expect, it } from "vitest";
|
|
import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
|
|
import { inspectWebPageInputSchema } from "../../../src/domain/browserObservation.js";
|
|
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
|
|
import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js";
|
|
import { frameNavigated, trackBrowser } from "./cdpBrowserProvider.support.js";
|
|
|
|
// A proven document change is reported before scope, carrying the failure it
|
|
// caused; an unproven navigation (no loader) is judged by its current scope.
|
|
it.each(
|
|
[
|
|
{ method: "Network.enable", sourceFails: false },
|
|
{ method: "DOMSnapshot.captureSnapshot", sourceFails: false },
|
|
{ method: "DOMSnapshot.captureSnapshot", sourceFails: true },
|
|
].flatMap((stage) => [
|
|
{ ...stage, loader: true },
|
|
{ ...stage, loader: false },
|
|
]),
|
|
)(
|
|
"reports navigation to another origin during $method (loader $loader, source failure $sourceFails)",
|
|
async ({ method: stage, sourceFails, loader }) => {
|
|
const browser = await startFakeCdpBrowser({
|
|
eventsBeforeReply: [stage],
|
|
commandEvents: (command) =>
|
|
command.method === stage
|
|
? [
|
|
frameNavigated(command, {
|
|
id: "frame-main",
|
|
...(loader ? { loaderId: "outside-document" } : {}),
|
|
url: "https://outside.example/page",
|
|
}),
|
|
]
|
|
: undefined,
|
|
commandError: ({ method }) =>
|
|
sourceFails && method === "Debugger.getScriptSource"
|
|
? { code: -32000, message: "No script for id" }
|
|
: undefined,
|
|
});
|
|
trackBrowser(browser);
|
|
const result = await new CdpBrowserProvider().inspectPage(
|
|
inspectWebPageInputSchema.parse({
|
|
cdp_endpoint: browser.endpoint,
|
|
target_id: "allowed-page",
|
|
observation_ms: 0,
|
|
include_script_sources: true,
|
|
}),
|
|
);
|
|
if (result.ok) throw new Error("Expected observed navigation refusal");
|
|
expect(projectAnalysisError(result.error)).toMatchObject(
|
|
loader
|
|
? {
|
|
code: "target_unavailable",
|
|
message: expect.stringContaining("target changed"),
|
|
details: {
|
|
operation: "inspect_web_page",
|
|
reason: "target_changed",
|
|
},
|
|
}
|
|
: {
|
|
code: "target_unavailable",
|
|
message: expect.stringContaining("permitted HTTP(S) origin scope"),
|
|
remediation: { action: expect.stringContaining("allowed_origins") },
|
|
details: {
|
|
operation: "inspect_web_page",
|
|
reason: "target_not_allowed",
|
|
},
|
|
},
|
|
);
|
|
if (sourceFails)
|
|
expect(result.error.cause).toMatchObject({ reason: "protocol_error" });
|
|
expect(browser.commands.map(({ method }) => method)).toContain(
|
|
"Target.detachFromTarget",
|
|
);
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
{ changed: true, sourceFails: true, reason: "target_changed" },
|
|
{ changed: false, sourceFails: true, reason: "protocol_error" },
|
|
{ changed: false, sourceFails: false, reason: null },
|
|
])(
|
|
"preserves observed navigation before a stale source-read failure: $changed/$sourceFails",
|
|
async ({ changed, sourceFails, reason }) => {
|
|
const browser = await startFakeCdpBrowser({
|
|
eventsBeforeReply: ["DOMSnapshot.captureSnapshot"],
|
|
commandEvents: (command, origin) =>
|
|
changed && command.method === "DOMSnapshot.captureSnapshot"
|
|
? [
|
|
frameNavigated(command, {
|
|
id: "frame-main",
|
|
loaderId: "reloaded-document",
|
|
url: origin + "/reloaded",
|
|
}),
|
|
]
|
|
: undefined,
|
|
commandError: ({ method }) =>
|
|
sourceFails && method === "Debugger.getScriptSource"
|
|
? { code: -32000, message: "No script for id" }
|
|
: undefined,
|
|
});
|
|
trackBrowser(browser);
|
|
const result = await new CdpBrowserProvider().inspectPage(
|
|
inspectWebPageInputSchema.parse({
|
|
cdp_endpoint: browser.endpoint,
|
|
target_id: "allowed-page",
|
|
observation_ms: 0,
|
|
include_script_sources: true,
|
|
}),
|
|
);
|
|
if (reason === null) {
|
|
if (!result.ok) throw result.error;
|
|
expect(
|
|
result.value.scripts.items.some(({ source }) => source.included),
|
|
).toBe(true);
|
|
return;
|
|
}
|
|
expect(result).toMatchObject({ ok: false, error: { reason } });
|
|
if (result.ok) throw new Error("Expected a capture refusal");
|
|
if (changed) {
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "target_unavailable",
|
|
message: expect.stringContaining("target changed"),
|
|
remediation: {
|
|
action: expect.stringContaining("Retry after navigation settles"),
|
|
},
|
|
});
|
|
expect(result.error.cause).toMatchObject({ reason: "protocol_error" });
|
|
}
|
|
expect(browser.commands.map(({ method }) => method)).toContain(
|
|
"Target.detachFromTarget",
|
|
);
|
|
},
|
|
);
|