diff --git a/tests/boundary/browser/cdpElectronProvider.test.ts b/tests/boundary/browser/cdpElectronProvider.test.ts index 6d3ada6717..5aeceb2d6b 100644 --- a/tests/boundary/browser/cdpElectronProvider.test.ts +++ b/tests/boundary/browser/cdpElectronProvider.test.ts @@ -914,56 +914,54 @@ describe("CdpElectronProvider frame capture", () => { ); }); -describe("CdpElectronProvider malformed source replies", () => { - it.each([{}, { scriptSource: null }, { scriptSource: 123 }])( - "rejects malformed Electron source replies instead of inventing source: %j", - async (reply) => { - const root = await electronFixture(); - const browser = await startFakeCdpBrowser({ - electronFileUrl: pathToFileURL(join(root, "index.html")).href, - commandResult: ({ method }) => - method === "Debugger.getScriptSource" ? reply : undefined, - }); - browsers.push(browser); - const result = await new CdpElectronProvider().inspectPage( - inspectElectronPageInputSchema.parse({ - cdp_endpoint: browser.endpoint, - target_id: "electron-page", - observation_ms: 0, - include_script_sources: true, - }), - ); +it("rejects malformed Electron source replies and detaches the selected page", async () => { + const root = await electronFixture(); + const browser = await startFakeCdpBrowser({ + electronFileUrl: pathToFileURL(join(root, "index.html")).href, + commandResult: ({ method }) => + method === "Debugger.getScriptSource" ? { scriptSource: 123 } : undefined, + }); + browsers.push(browser); + const provider = new CdpElectronProvider(); + onTestFinished(() => provider.close()); + const result = await provider.inspectPage( + inspectElectronPageInputSchema.parse({ + cdp_endpoint: browser.endpoint, + target_id: "electron-page", + observation_ms: 0, + include_script_sources: true, + }), + ); - expect(result).toMatchObject({ - ok: false, - error: { - operation: "inspect_electron_page", - reason: "protocol_error", - userMessage: expect.stringContaining( - "Debugger.getScriptSource returned a malformed result at /scriptSource", - ), - }, - }); - expect(browser.commands.map(({ method }) => method)).toContain( - "Target.detachFromTarget", - ); + expect(result).toMatchObject({ + ok: false, + error: { + operation: "inspect_electron_page", + reason: "protocol_error", + command: "Debugger.getScriptSource", + pointer: "/scriptSource", }, + }); + expect(browser.commands.map(({ method }) => method)).toContain( + "Target.detachFromTarget", ); }); -const inspectInventoryReply = async ( - method: string, - reply: (fileUrl: string) => Readonly>, +const inspectInventoryReplies = async ( + replies: Readonly< + Record Readonly>> + >, ) => { const root = await electronFixture(); const fileUrl = pathToFileURL(join(root, "index.html")).href; const browser = await startFakeCdpBrowser({ electronFileUrl: fileUrl, - commandResult: (command) => - command.method === method ? reply(fileUrl) : undefined, + commandResult: ({ method }) => replies[method]?.(fileUrl), }); browsers.push(browser); - const result = await new CdpElectronProvider().inspectPage( + const provider = new CdpElectronProvider(); + onTestFinished(() => provider.close()); + const result = await provider.inspectPage( inspectElectronPageInputSchema.parse({ cdp_endpoint: browser.endpoint, target_id: "electron-page", @@ -976,12 +974,11 @@ const inspectInventoryReply = async ( describe("CdpElectronProvider malformed inventory replies", () => { it("keeps documents unknown when the DOM string table is missing", async () => { - const inspected = await inspectInventoryReply( - "DOMSnapshot.captureSnapshot", - () => ({ + const inspected = await inspectInventoryReplies({ + "DOMSnapshot.captureSnapshot": () => ({ documents: [{ documentURL: 0, nodes: { nodeType: [] } }], }), - ); + }); expect(inspected.dom).toEqual({ total_nodes: 0, @@ -999,122 +996,122 @@ describe("CdpElectronProvider malformed inventory replies", () => { ); }); - it.each([ - ["missing type", { type: undefined }], - ["non-string type", { type: 7 }], - ["empty type", { type: "" }], - ["missing target identity", { targetId: undefined }], - ["non-string target identity", { targetId: 7 }], - ["empty target identity", { targetId: "" }], - ["missing URL", { url: undefined }], - ["non-string URL", { url: 7 }], - ["missing attached state", { attached: undefined }], - ["non-boolean attached state", { attached: "false" }], - ["non-string opener relationship", { openerId: 7 }], - ["empty opener relationship", { openerId: "" }], - ["non-string frame relationship", { parentFrameId: 7 }], - ["empty frame relationship", { parentFrameId: "" }], - ] as const)( - "retains valid workers while reporting %s before target policy", - async (_kind, malformed) => { - const inspected = await inspectInventoryReply( - "Target.getTargets", - (fileUrl) => ({ - targetInfos: [ - { - targetId: "selected-worker", - type: "worker", - url: fileUrl, - attached: false, - openerId: "electron-page", - }, - { - targetId: "malformed-worker", - type: "worker", - url: fileUrl, - attached: false, - openerId: "foreign-page", - ...malformed, - }, - { - targetId: "foreign-worker", - type: "shared_worker", - url: fileUrl, - attached: true, - openerId: "foreign-page", - }, - { - targetId: "unattributed-worker", - type: "service_worker", - url: fileUrl, - attached: false, - }, - // Non-worker rows do not need worker identity or relationship fields. - { type: "page", targetId: 7, openerId: null }, - ], - }), - ); - expect(inspected.workers).toEqual([ - expect.objectContaining({ - target_id: "selected-worker", - type: "worker", - attached: false, - opener_target_id: "electron-page", - parent_frame_id: null, - }), - ]); - const workerExclusions = inspected.completeness.excluded.filter( - ({ section }) => section === "workers", - ); - expect(workerExclusions).toHaveLength(3); - expect(workerExclusions).toEqual( - expect.arrayContaining([ - { section: "workers", reason: "invalid_protocol_value", count: 1 }, - { section: "workers", reason: "out_of_target_scope", count: 1 }, - { section: "workers", reason: "unattributed_target", count: 1 }, - ]), - ); - expect(inspected.completeness.unavailable_sections).toContain("workers"); - expect(inspected.completeness.policy_filtered_sections).toContain( - "workers", - ); - }, - ); - - it.each([ - ["dom", "DOMSnapshot.captureSnapshot", () => ({ strings: [] })], - [ - "dom", - "DOMSnapshot.captureSnapshot", - (fileUrl: string) => ({ - strings: [fileUrl], - documents: [{ documentURL: 0, nodes: { nodeType: null } }], + it("retains valid workers and distinguishes malformed target facts from policy exclusions", async () => { + const malformed = [ + { type: undefined }, + { type: 7 }, + { type: "" }, + { targetId: undefined }, + { targetId: 7 }, + { targetId: "" }, + { url: undefined }, + { url: 7 }, + { attached: undefined }, + { attached: "false" }, + { openerId: 7 }, + { openerId: "" }, + { parentFrameId: 7 }, + { parentFrameId: "" }, + ]; + const inspected = await inspectInventoryReplies({ + "Target.getTargets": (fileUrl) => ({ + targetInfos: [ + { + targetId: "selected-worker", + type: "worker", + url: fileUrl, + attached: false, + openerId: "electron-page", + }, + ...malformed.map((fields, index) => ({ + targetId: `malformed-worker-${index}`, + type: "worker", + url: fileUrl, + attached: false, + openerId: "foreign-page", + ...fields, + })), + { + targetId: "foreign-worker", + type: "shared_worker", + url: fileUrl, + attached: true, + openerId: "foreign-page", + }, + { + targetId: "unattributed-worker", + type: "service_worker", + url: fileUrl, + attached: false, + }, + // Non-worker rows do not need worker identity or relationship fields. + { type: "page", targetId: 7, openerId: null }, + ], }), - ], - [ - "resources", - "Page.getResourceTree", - (fileUrl: string) => ({ + }); + expect(inspected.workers).toEqual([ + expect.objectContaining({ + target_id: "selected-worker", + type: "worker", + attached: false, + opener_target_id: "electron-page", + parent_frame_id: null, + }), + ]); + const workerExclusions = inspected.completeness.excluded + .filter(({ section }) => section === "workers") + .map(({ reason, count }) => [reason, count]); + expect(workerExclusions).toHaveLength(3); + expect(workerExclusions).toEqual( + expect.arrayContaining([ + ["invalid_protocol_value", malformed.length], + ["out_of_target_scope", 1], + ["unattributed_target", 1], + ]), + ); + expect(inspected.completeness.unavailable_sections).toContain("workers"); + expect(inspected.completeness.policy_filtered_sections).toContain( + "workers", + ); + }); + + it("reports each malformed optional inventory without failing the observation", async () => { + const inspected = await inspectInventoryReplies({ + "DOMSnapshot.captureSnapshot": () => ({ strings: [] }), + "Page.getResourceTree": (fileUrl) => ({ frameTree: { frame: { id: "main", loaderId: "loader-main", url: fileUrl }, resources: null, }, }), - ], - ["workers", "Target.getTargets", () => ({ targetInfos: null })], - ] as const)( - "keeps a malformed %s inventory incomplete (%s)", - async (section, method, reply) => { - const inspected = await inspectInventoryReply(method, reply); - expect(inspected.completeness.status).not.toBe("complete_within_window"); - expect(inspected.completeness.excluded).toContainEqual( - expect.objectContaining({ - section, - reason: "invalid_protocol_value", - }), - ); - }, - ); + "Target.getTargets": () => ({ targetInfos: null }), + }); + expect(inspected.completeness.status).not.toBe("complete_within_window"); + for (const section of ["dom", "resources", "workers"]) { + expect(inspected.completeness.unavailable_sections).toContain(section); + expect(inspected.completeness.excluded).toContainEqual({ + section, + reason: "invalid_protocol_value", + count: null, + }); + } + }); + + it("counts malformed node columns as one unavailable DOM document", async () => { + const inspected = await inspectInventoryReplies({ + "DOMSnapshot.captureSnapshot": (fileUrl) => ({ + strings: [fileUrl], + documents: [{ documentURL: 0, nodes: { nodeType: null } }], + }), + }); + expect(inspected.dom.total_nodes).toBe(0); + expect(inspected.completeness.unavailable_sections).toContain("dom"); + expect(inspected.completeness.excluded).toContainEqual({ + section: "dom", + reason: "invalid_protocol_value", + count: 1, + }); + }); }); describe("CdpElectronProvider script source capture", () => {