diff options
| author | amankumarsingh77 <amankumarsingh7702@gmail.com> | 2026-07-27 23:02:59 +0530 |
|---|---|---|
| committer | Aman Kumar <145339995+amankumarsingh77@users.noreply.github.com> | 2026-08-02 19:12:46 +0530 |
| commit | 44e3271c148e991e5547f36e3cfe596d71c91cfc (patch) | |
| tree | 9835c1d35d7db96ed89a0d2f0d8d2fbe13305943 /test | |
| parent | 9e9bf2a3badb59d0ce5bdb47c4e9d09ffb259e69 (diff) | |
fix(cdp): let evaluateJson target a specific session
Diffstat (limited to 'test')
| -rw-r--r-- | test/cdp/evaluate-test.ts | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/test/cdp/evaluate-test.ts b/test/cdp/evaluate-test.ts index 237ab40..cffd513 100644 --- a/test/cdp/evaluate-test.ts +++ b/test/cdp/evaluate-test.ts @@ -2,7 +2,7 @@ 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"; +import { err, ok } from "../../src/util/result"; const isNumberArray = (v: unknown): v is ReadonlyArray<number> => Array.isArray(v) && v.every((x) => typeof x === "number"); @@ -12,6 +12,23 @@ const fakeSession = (payload: unknown): CdpSession => call: async () => ok(payload), }) as unknown as CdpSession; +type Call = { method: string; args: unknown[] }; + +const recordingSession = (result: unknown): { session: CdpSession; calls: Call[] } => { + const calls: Call[] = []; + const session = { + call: async (...args: unknown[]) => { + calls.push({ method: "call", args }); + return result; + }, + callOnTarget: async (...args: unknown[]) => { + calls.push({ method: "callOnTarget", args }); + return result; + }, + } as unknown as CdpSession; + return { session, calls }; +}; + describe("evaluateJson", () => { test("unwraps result.value and applies the guard", async () => { const s = fakeSession({ result: { type: "object", value: [1, 2, 3] } }); @@ -27,7 +44,10 @@ describe("evaluateJson", () => { }); const r = await evaluateJson(s, "x", isNumberArray); assert.equal(r.success, false); - if (!r.success) assert.match(r.error.message, /ReferenceError/); + if (!r.success) { + assert.equal(r.error.kind, "remote_error"); + assert.match(r.error.message, /ReferenceError/); + } }); test("rejects a value that fails the guard", async () => { @@ -36,4 +56,42 @@ describe("evaluateJson", () => { assert.equal(r.success, false); if (!r.success) assert.equal(r.error.kind, "invalid_response"); }); + + test("dispatches via callOnTarget when opts.sessionId is set", async () => { + const { session, calls } = recordingSession(ok({ result: { type: "object", value: [4, 5] } })); + const r = await evaluateJson(session, "[4,5]", isNumberArray, { sessionId: "sess-1" }); + assert.equal(r.success, true); + if (r.success) assert.deepEqual(r.data, [4, 5]); + assert.equal(calls.length, 1); + assert.equal(calls[0]?.method, "callOnTarget"); + const [, , sessionIdArg] = calls[0]?.args ?? []; + assert.equal(sessionIdArg, "sess-1"); + }); + + test("dispatches via call when no opts are given", async () => { + const { session, calls } = recordingSession(ok({ result: { type: "object", value: [1] } })); + const r = await evaluateJson(session, "[1]", isNumberArray); + assert.equal(r.success, true); + assert.equal(calls.length, 1); + assert.equal(calls[0]?.method, "call"); + assert.equal(calls.some((c) => c.method === "callOnTarget"), false); + }); + + test("passes timeoutMs through to the transport options", async () => { + const { session, calls } = recordingSession(ok({ result: { type: "object", value: [1] } })); + await evaluateJson(session, "[1]", isNumberArray, { timeoutMs: 5000 }); + const [, , optsArg] = calls[0]?.args ?? []; + assert.deepEqual(optsArg, { timeoutMs: 5000 }); + }); + + test("passes through a transport failure unchanged", async () => { + const failure = err({ kind: "timeout" as const, message: "timed out" }); + const { session } = recordingSession(failure); + const r = await evaluateJson(session, "[1]", isNumberArray); + assert.equal(r.success, false); + if (!r.success) { + assert.equal(r.error.kind, "timeout"); + assert.equal(r.error.message, "timed out"); + } + }); }); |
