From 9e9bf2a3badb59d0ce5bdb47c4e9d09ffb259e69 Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Mon, 27 Jul 2026 01:17:32 +0530 Subject: refactor(cdp): extract attachTo, getWindowId, evaluateJson helpers Target.attachToTarget was decoded at 5 sites, Browser.getWindowForTarget at 5, and Runtime.evaluate unwrapped at another. Each is now one typed helper. Deletes the dead cdp/window-binding.ts. Co-Authored-By: Claude Opus 5 (1M context) --- test/cdp/evaluate-test.ts | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/cdp/evaluate-test.ts (limited to 'test/cdp') diff --git a/test/cdp/evaluate-test.ts b/test/cdp/evaluate-test.ts new file mode 100644 index 0000000..237ab40 --- /dev/null +++ b/test/cdp/evaluate-test.ts @@ -0,0 +1,39 @@ +import { test, describe } from "node:test"; +import assert from "node:assert/strict"; +import { evaluateJson } from "../../src/cdp/evaluate"; +import type { CdpSession } from "../../src/cdp/session"; +import { ok } from "../../src/util/result"; + +const isNumberArray = (v: unknown): v is ReadonlyArray => + Array.isArray(v) && v.every((x) => typeof x === "number"); + +const fakeSession = (payload: unknown): CdpSession => + ({ + call: async () => ok(payload), + }) as unknown as CdpSession; + +describe("evaluateJson", () => { + test("unwraps result.value and applies the guard", async () => { + const s = fakeSession({ result: { type: "object", value: [1, 2, 3] } }); + const r = await evaluateJson(s, "[1,2,3]", isNumberArray); + assert.equal(r.success, true); + if (r.success) assert.deepEqual(r.data, [1, 2, 3]); + }); + + test("surfaces a page exception as an error", async () => { + const s = fakeSession({ + result: { type: "undefined" }, + exceptionDetails: { text: "ReferenceError: x is not defined" }, + }); + const r = await evaluateJson(s, "x", isNumberArray); + assert.equal(r.success, false); + if (!r.success) assert.match(r.error.message, /ReferenceError/); + }); + + test("rejects a value that fails the guard", async () => { + const s = fakeSession({ result: { type: "string", value: "nope" } }); + const r = await evaluateJson(s, "'nope'", isNumberArray); + assert.equal(r.success, false); + if (!r.success) assert.equal(r.error.kind, "invalid_response"); + }); +}); -- cgit v1.2.3