From b484c017807acb902eaaaa4e242f8b79f8e8eba6 Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Tue, 28 Jul 2026 22:29:16 +0530 Subject: refactor: act on the fallow audit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unused exports it found that my own sweep missed, because I had grepped by name and these names occur as local variables elsewhere: deadline and the Deadline type in util/time.ts were genuinely unreferenced and are gone; isWireMessage, resolveUserDataDir, CLEAR_SELECTION_LABEL and the ExpandableText type are used only inside their own file and no longer export. isWireMessage became internal when server.ts moved to the shared serialize/deserialize codec. Duplication: console-buffer and network-buffer were the same capacity-bounded map with an overflow flag, sharing eviction, the drain tail and clear() across three clone groups. That is now cdp/record-store.ts, with tests — neither buffer had any. The three Network follow-up events also repeated the same decode-and-find preamble; one `update` helper covers them, and drops the redundant optional chaining that survived a `!id` guard. formatBytes existed verbatim in js.ts and network.ts; it is now util/bytes.ts. Duplication overall: 2.7% to 2.0%, and none of the remainder is in src. .fallowrc.json records the two entry points the tool cannot infer — the pi extension (declared under pi.extensions) and the daemon (spawned by path) — without which five reachable files read as dead. sharp is ignored because it is loaded through a variable specifier in a dynamic import so it can stay optional. agentDir is exported solely for a test's dynamic import, so it is suppressed by name rather than globally enabling ignoreExportsUsedInFile, which would have hidden four of the five findings above. Not acted on: the "split high-impact file" suggestions for session.ts, client.ts and ref-resolve.ts (119 LOC) are size and fan-in heuristics with no defect behind them, and splitting them would add the indirection this refactor set out to remove. The claim that state.ts is 100% dead is an artifact of the missing entry point. Co-Authored-By: Claude Opus 5 (1M context) --- test/cdp/record-store-test.ts | 95 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 test/cdp/record-store-test.ts (limited to 'test/cdp') diff --git a/test/cdp/record-store-test.ts b/test/cdp/record-store-test.ts new file mode 100644 index 0000000..c259f87 --- /dev/null +++ b/test/cdp/record-store-test.ts @@ -0,0 +1,95 @@ +import { test, describe } from "node:test"; +import assert from "node:assert/strict"; +import { createRecordStore } from "../../src/cdp/record-store"; + +const fill = (store: ReturnType>, n: number): void => { + for (let i = 1; i <= n; i++) store.set(i, `r${i}`); +}; + +describe("record store capacity", () => { + test("holds up to capacity without reporting a loss", () => { + const store = createRecordStore(3); + fill(store, 3); + assert.equal([...store.values()].length, 3); + assert.equal(store.page([...store.values()], undefined).bufferOverflowed, false); + }); + + test("evicts the oldest past capacity and reports the loss", () => { + const store = createRecordStore(3); + fill(store, 5); + assert.deepEqual([...store.values()], ["r3", "r4", "r5"]); + assert.equal(store.page([...store.values()], undefined).bufferOverflowed, true); + }); + + test("the overflow flag is taken, so a second drain reports no new loss", () => { + const store = createRecordStore(2); + fill(store, 4); + assert.equal(store.page([], undefined).bufferOverflowed, true); + assert.equal(store.page([], undefined).bufferOverflowed, false); + }); + + test("clear resets both the records and the overflow flag", () => { + const store = createRecordStore(2); + fill(store, 4); + store.clear(); + assert.equal([...store.values()].length, 0); + assert.equal(store.page([], undefined).bufferOverflowed, false); + }); + + test("get returns the stored object by reference so callers can mutate in place", () => { + const store = createRecordStore(4); + store.set("a", {}); + const held = store.get("a"); + assert.ok(held); + held.status = 200; + assert.equal(store.get("a")?.status, 200); + }); + + test("delete removes a record without flagging an overflow", () => { + const store = createRecordStore(4); + fill(store, 2); + store.delete(1); + assert.deepEqual([...store.values()], ["r2"]); + assert.equal(store.page([], undefined).bufferOverflowed, false); + }); + + test("re-setting an existing key does not grow the store", () => { + const store = createRecordStore(4); + store.set(1, "first"); + store.set(1, "second"); + assert.deepEqual([...store.values()], ["second"]); + }); +}); + +describe("record store paging", () => { + test("total counts every match while records carry only the page", () => { + const store = createRecordStore(100); + const matched = Array.from({ length: 120 }, (_, i) => `r${i}`); + const page = store.page(matched, 10); + assert.equal(page.total, 120); + assert.equal(page.records.length, 10); + }); + + test("the page is the newest records, not the oldest", () => { + const store = createRecordStore(10); + const page = store.page(["a", "b", "c"], 2); + assert.deepEqual(page.records, ["b", "c"]); + }); + + test("an absent limit defaults to 50", () => { + const store = createRecordStore(600); + const matched = Array.from({ length: 80 }, (_, i) => `r${i}`); + assert.equal(store.page(matched, undefined).records.length, 50); + }); + + test("a limit above the 500 ceiling is capped", () => { + const store = createRecordStore(1000); + const matched = Array.from({ length: 700 }, (_, i) => `r${i}`); + assert.equal(store.page(matched, 9_000).records.length, 500); + }); + + test("a limit larger than the match count returns every match", () => { + const store = createRecordStore(10); + assert.equal(store.page(["a", "b"], 50).records.length, 2); + }); +}); -- cgit v1.2.3