diff options
| author | Igor Soarez <igor@soarez.org> | 2026-08-03 21:18:02 +0100 |
|---|---|---|
| committer | Igor Soarez <igor@soarez.org> | 2026-08-03 21:18:02 +0100 |
| commit | 495de0d5283dd3e4a6ef715b596c4a2892e95915 (patch) | |
| tree | aae2ec78d59cb96d8e90c56ab7626a6886d30be2 /test/endpoint.test.ts | |
Web search for pi through an existing Chrome over CDP
Attaches to a browser that is already running — never launches one —
using the same endpoint configuration as pi-browser-harness, so a single
/browser-target choice governs both packages.
One tool, castle_cdp_search, deliberately not named web_search so it
coexists with pi-web-access rather than shadowing it.
Notes from validating against castle's Chrome:
- tbs=qdr:*, the parameter Google's own Tools menu writes, renders an
empty page on this profile; the older as_qdr=* works. Any date filter
combined with udm=14 is also empty, so recency drops udm.
- Target.createTarget must not be raced against the abort signal:
raceAbort abandons the promise but cannot cancel the command, and the
command's side effect is a tab nothing is left holding.
- A search cancelled while queued has to be removed from the semaphore
queue, or the slot handed to it later is never counted back.
- A decaying rate-limit block stops serving /sorry/ and returns an empty
results page instead, indistinguishable from a genuine zero-hit search.
Diffstat (limited to 'test/endpoint.test.ts')
| -rw-r--r-- | test/endpoint.test.ts | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/test/endpoint.test.ts b/test/endpoint.test.ts new file mode 100644 index 0000000..f5c8236 --- /dev/null +++ b/test/endpoint.test.ts @@ -0,0 +1,64 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; + +import { + DEFAULT_REMOTE_CDP, + LOCAL_CDP_PORT, + describeCdpTarget, + resolveCdpTarget, +} from "../src/endpoint.ts"; + +const ok = (resolution: ReturnType<typeof resolveCdpTarget>) => { + assert.equal(resolution.kind, "ok"); + if (resolution.kind !== "ok") throw new Error("unreachable"); + return resolution.target; +}; + +test("the built-in default parses — the fallback in resolveCdpTarget is unreachable", () => { + const target = ok(resolveCdpTarget({}, null)); + assert.deepEqual(target, { host: "10.88.0.25", port: 9223, source: "default" }); + assert.equal(`${target.host}:${target.port}`, DEFAULT_REMOTE_CDP); +}); + +test("BU_CDP_HTTP wins over a stored target", () => { + const target = ok(resolveCdpTarget({ BU_CDP_HTTP: "10.88.0.9:9333" }, "10.88.0.25:9223")); + assert.deepEqual(target, { host: "10.88.0.9", port: 9333, source: "env" }); +}); + +test("a stored target is used when the environment is silent", () => { + const target = ok(resolveCdpTarget({}, "192.168.1.4:9222")); + assert.deepEqual(target, { host: "192.168.1.4", port: 9222, source: "stored" }); +}); + +test("an empty BU_CDP_HTTP does not shadow the stored target", () => { + const target = ok(resolveCdpTarget({ BU_CDP_HTTP: " " }, "192.168.1.4:9222")); + assert.equal(target.source, "stored"); +}); + +test("the local aliases all resolve to this machine", () => { + for (const alias of ["local", "localhost", "off", "none", "0", "no", "LOCAL"]) { + const target = ok(resolveCdpTarget({ BU_CDP_HTTP: alias }, null)); + assert.deepEqual(target, { host: "127.0.0.1", port: LOCAL_CDP_PORT, source: "env" }, alias); + } +}); + +test("IPv6-ish and malformed values are rejected rather than half-parsed", () => { + for (const raw of ["nonsense", "host:", ":9223", "host:0", "host:65536", "host:notaport"]) { + const resolution = resolveCdpTarget({ BU_CDP_HTTP: raw }, null); + assert.equal(resolution.kind, "invalid", raw); + } +}); + +test("a host with a port takes the last colon, so hostnames survive", () => { + const target = ok(resolveCdpTarget({ BU_CDP_HTTP: "castle.local:9223" }, null)); + assert.deepEqual(target, { host: "castle.local", port: 9223, source: "env" }); +}); + +test("describeCdpTarget names where the choice came from", () => { + assert.equal( + describeCdpTarget({ host: "10.88.0.25", port: 9223, source: "default" }), + "10.88.0.25:9223 (built-in default)", + ); + assert.equal(describeCdpTarget({ host: "h", port: 1, source: "env" }), "h:1 (BU_CDP_HTTP)"); + assert.equal(describeCdpTarget({ host: "h", port: 1, source: "stored" }), "h:1 (/browser-target)"); +}); |
