From d1b57558ff40ef6d6b19ee7eff0a44f7f4a585f6 Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Mon, 27 Jul 2026 01:07:05 +0530 Subject: test(cdp): cover call/callOnTarget/callBrowser through a stub transport; collapse decode into one path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit session-call-test.ts previously only exercised decodeResult (Task 3's surface, and mostly duplicating commands-test.ts) — call, callOnTarget, callBrowser, and the session's internal req helper had zero coverage. Add a stub CdpTransport (no socket, no browser) and test: a valid payload decodes to typed data, a garbage payload becomes invalid_response without throwing, a transport-level error passes through with its original kind (not relabeled), call/callOnTarget/callBrowser route the session id correctly, and opts.timeoutMs reaches the transport only when provided. Also collapse the four near-identical transport-then-decode bodies (req, call, callOnTarget, callBrowser) into one: the three public methods now delegate to req, which is the sole place unknown ends for every CDP call the session makes, internal or public. Public signatures are unchanged. Co-Authored-By: Claude Opus 5 (1M context) --- src/cdp/session.ts | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) (limited to 'src/cdp') diff --git a/src/cdp/session.ts b/src/cdp/session.ts index 96b980f..dd5a9b3 100644 --- a/src/cdp/session.ts +++ b/src/cdp/session.ts @@ -180,16 +180,22 @@ export const createCdpSession = ( } }; - // Internal helper: the session's own CDP calls (attach/switch bookkeeping) - // go through the same decode-on-call path as the public call/callOnTarget/ - // callBrowser — the session is where `unknown` ends, including for its own - // requests, not just the ones exposed to callers. + // The one place `unknown` ends for every CDP call this session makes — its + // own internal bookkeeping calls (attach/switch) and the public + // call/callOnTarget/callBrowser all funnel through here. A single + // transport-then-decode implementation means a future change (tracing, + // error wrapping, the short-circuit on failure) lands in one place, and a + // new internal call site can't forget to decode. const req = async ( method: M, - params: ParamsOf, + params: ParamsOf | undefined, sid: string | null, + opts: { timeoutMs?: number } = {}, ): Promise, CdpError>> => { - const raw = await transport.request(method, { ...params }, { sessionId: sid }); + const raw = await transport.request(method, { ...(params ?? {}) }, { + sessionId: sid, + ...(opts.timeoutMs !== undefined ? { timeoutMs: opts.timeoutMs } : {}), + }); if (!raw.success) return raw; return decodeResult(method, raw.data); }; @@ -358,29 +364,14 @@ export const createCdpSession = ( const tab = targetId ? tabs.get(targetId) : undefined; return tab?.refSig ?? new Map(); }, - async call(method, params, opts = {}) { - const raw = await transport.request(method, { ...(params ?? {}) }, { - sessionId, - ...(opts.timeoutMs !== undefined ? { timeoutMs: opts.timeoutMs } : {}), - }); - if (!raw.success) return raw; - return decodeResult(method, raw.data); + call(method, params, opts) { + return req(method, params, sessionId, opts); }, - async callOnTarget(method, params, sid, opts = {}) { - const raw = await transport.request(method, { ...params }, { - sessionId: sid, - ...(opts.timeoutMs !== undefined ? { timeoutMs: opts.timeoutMs } : {}), - }); - if (!raw.success) return raw; - return decodeResult(method, raw.data); + callOnTarget(method, params, sid, opts) { + return req(method, params, sid, opts); }, - async callBrowser(method, params, opts = {}) { - const raw = await transport.request(method, { ...(params ?? {}) }, { - sessionId: null, - ...(opts.timeoutMs !== undefined ? { timeoutMs: opts.timeoutMs } : {}), - }); - if (!raw.success) return raw; - return decodeResult(method, raw.data); + callBrowser(method, params, opts) { + return req(method, params, null, opts); }, takeDialog() { const tab = targetId ? tabs.get(targetId) : undefined; -- cgit v1.2.3