aboutsummaryrefslogtreecommitdiff
path: root/test/cdp/evaluate-test.ts
blob: 237ab400f212e2f7c6f772cf3ac0892994b8666f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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<number> =>
  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");
  });
});