From f9dcdf7f6dcdf2979693a9f179eda2b9995d57ae Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Tue, 28 Jul 2026 01:19:58 +0530 Subject: refactor: close the remaining unguarded type assertions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Types the event-queue iterator's TReturn as undefined so the terminal result needs no cast; declares the isError field the pi runtime reads by duck-typing; binds socket to a local const in the daemon transport; replaces the @ts-ignore on the optional sharp import with a non-literal specifier so src/ still typechecks where sharp was not installed. Adds src/util/guards.ts as the single home for narrowing over unknown — isRecord plus scalar/array readers — and converts the two private isObject copies in cdp/types.ts and daemon/protocol.ts along with every inline `as Record` in domains/js.ts and domains/render.ts. Gives Accessibility.getFullAXTree a real result schema, closing the last unvalidated CDP boundary: only nodeId is required, every object carries additionalProperties, and each AX value stays Unknown, so the tree decodes as a whole or fails as a whole rather than being filtered. Guards the five tool-result `details` readers, the daemon attach response, the persisted session state, the browser-namespace flag, and the fs errno narrowing, each feeding an already-present failure path. The daemon-transport socket binding is not a bug fix: send() runs synchronously inside sendWithTimeout's promise executor, so socket cannot be reassigned between the check and the write. It removes the assertion and makes the write immune to a later refactor that adds an await. Two assertions survive by design and are documented in the task report: the never->unknown re-widening in util/tool.ts, which the type-erased tool registry makes unavoidable, and the AsyncFunction constructor lookup in domains/js.ts, which JavaScript exposes no typed binding for. Co-Authored-By: Claude Opus 5 (1M context) --- test/util/guards-test.ts | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 test/util/guards-test.ts (limited to 'test/util') diff --git a/test/util/guards-test.ts b/test/util/guards-test.ts new file mode 100644 index 0000000..7c7b426 --- /dev/null +++ b/test/util/guards-test.ts @@ -0,0 +1,95 @@ +import { test, describe } from "node:test"; +import assert from "node:assert/strict"; +import { asArrayOf, asBoolean, asNumber, asRecord, asString, errnoCode, isRecord } from "../../src/util/guards"; + +describe("isRecord", () => { + test("accepts plain objects", () => { + assert.equal(isRecord({}), true); + assert.equal(isRecord({ a: 1 }), true); + }); + + test("rejects null, arrays and primitives", () => { + assert.equal(isRecord(null), false); + assert.equal(isRecord(undefined), false); + assert.equal(isRecord([]), false); + assert.equal(isRecord([{ a: 1 }]), false); + assert.equal(isRecord("s"), false); + assert.equal(isRecord(0), false); + assert.equal(isRecord(false), false); + }); +}); + +describe("scalar readers", () => { + test("asString accepts only strings", () => { + assert.equal(asString(""), ""); + assert.equal(asString("x"), "x"); + assert.equal(asString(1), undefined); + assert.equal(asString(null), undefined); + assert.equal(asString(undefined), undefined); + assert.equal(asString({}), undefined); + }); + + test("asNumber accepts only numbers, including 0 and NaN", () => { + assert.equal(asNumber(0), 0); + assert.equal(asNumber(-1.5), -1.5); + assert.equal(Number.isNaN(asNumber(Number.NaN)), true); + assert.equal(asNumber("1"), undefined); + assert.equal(asNumber(null), undefined); + }); + + test("asBoolean accepts only booleans", () => { + assert.equal(asBoolean(false), false); + assert.equal(asBoolean(true), true); + assert.equal(asBoolean(0), undefined); + assert.equal(asBoolean("true"), undefined); + }); + + test("asRecord mirrors isRecord but yields the value", () => { + assert.deepEqual(asRecord({ a: 1 }), { a: 1 }); + assert.equal(asRecord([]), undefined); + assert.equal(asRecord(null), undefined); + }); +}); + +describe("asArrayOf", () => { + test("parses every element", () => { + assert.deepEqual(asArrayOf(["a", "b"], asString), ["a", "b"]); + assert.deepEqual(asArrayOf([], asString), []); + }); + + test("rejects the whole array when any element fails, rather than dropping it", () => { + assert.equal(asArrayOf(["a", 2], asString), undefined); + assert.equal(asArrayOf([1, "b"], asString), undefined); + }); + + test("rejects non-arrays", () => { + assert.equal(asArrayOf({ 0: "a" }, asString), undefined); + assert.equal(asArrayOf(undefined, asString), undefined); + assert.equal(asArrayOf("ab", asString), undefined); + }); + + test("treats an element parsed as undefined as a failure", () => { + assert.equal(asArrayOf([undefined], asString), undefined); + }); +}); + +describe("errnoCode", () => { + test("reads .code off a real Error", () => { + const e = Object.assign(new Error("nope"), { code: "ENOENT" }); + assert.equal(errnoCode(e), "ENOENT"); + }); + + test("returns undefined for an Error without a code", () => { + assert.equal(errnoCode(new Error("plain")), undefined); + }); + + test("returns undefined for a non-string code", () => { + assert.equal(errnoCode(Object.assign(new Error("x"), { code: 13 })), undefined); + }); + + test("returns undefined for non-Errors that merely carry a code", () => { + assert.equal(errnoCode({ code: "ENOENT" }), undefined); + assert.equal(errnoCode("ENOENT"), undefined); + assert.equal(errnoCode(null), undefined); + }); +}); -- cgit v1.2.3