fix(browser): preserve command failures and reject malformed replies

This commit is contained in:
morluto
2026-10-09 04:12:48 +08:00
parent bc4aa46d7c
commit 6b92002597
5 changed files with 117 additions and 22 deletions
@@ -269,6 +269,32 @@ describe("CdpBrowserProvider: protocol failures and cancellation cleanup", () =>
"Accessibility.getFullAXTree was unavailable from this browser target.",
);
const invalidAccessibilityParams = await startFakeCdpBrowser({
commandError: (command) =>
command.method === "Accessibility.getFullAXTree"
? { code: -32_602, message: "Invalid params" }
: undefined,
});
trackBrowser(invalidAccessibilityParams);
const invalidParamsResult = await new CdpBrowserProvider().inspectPage(
inspectWebPageInputSchema.parse({
cdp_endpoint: invalidAccessibilityParams.endpoint,
allowed_origins: [invalidAccessibilityParams.allowedOrigin],
target_id: "allowed-page",
observation_ms: 0,
}),
);
expect(invalidParamsResult).toMatchObject({
ok: false,
error: {
_tag: "BrowserObservationError",
reason: "protocol_error",
command: "Accessibility.getFullAXTree",
code: -32_602,
reportedMessage: "Invalid params",
},
});
for (const [options, reason] of [
[{ oversizedDiscovery: true }, "invalid_endpoint_response"],
[{ invalidBrowserWebSocket: true }, "invalid_endpoint_response"],
@@ -32,6 +32,8 @@ describe("CDP connection", () => {
expect(frames).toMatchObject({
frameTree: { frame: { id: "frame-main" } },
});
browser.emitRawMessage(JSON.stringify({ id: 999 }));
await expect(connection.send("Runtime.enable")).resolves.toEqual({});
} finally {
await connection.close();
}
@@ -84,6 +86,57 @@ describe("CDP connection", () => {
}
});
it.each([
["missing result and error", (id: number) => ({ id })],
[
"both result and error",
(id: number) => ({
id,
result: {},
error: { code: -32_602, message: "Invalid params" },
}),
],
[
"malformed error",
(id: number) => ({
id,
error: { code: "bad", message: "Invalid params" },
}),
],
["invalid result", (id: number) => ({ id, result: [] })],
["invalid session id", (id: number) => ({ id, result: {}, sessionId: 7 })],
])(
"fails the connection for a correlated reply with %s",
async (_case, reply) => {
const browser = await startFakeCdpBrowser({
hangOnMethod: "Page.enable",
});
browsers.push(browser);
const connection = await CdpConnection.connect(
browser.browserWebSocketUrl,
"observe_web_session",
);
try {
const pending = connection.send("Page.enable");
const rejection = expect(pending).rejects.toMatchObject({
_tag: "BrowserObservationError",
reason: "protocol_error",
});
await vi.waitFor(() => expect(browser.commands).toHaveLength(1));
const command = browser.commands[0];
if (command === undefined)
throw new Error("Fake browser received no command");
browser.emitRawMessage(JSON.stringify(reply(command.id)));
await rejection;
await expect(connection.send("Runtime.enable")).rejects.toMatchObject({
reason: "protocol_error",
});
} finally {
await connection.close();
}
},
);
it("preserves the selected payload limit reason for pending and subsequent commands", async () => {
const browser = await startFakeCdpBrowser({
commandResult: () => ({ oversized: "x".repeat(2_048) }),