From ce515dbf4de89d3eab6961b599fe099f4bbf0aff Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 8 May 2026 22:05:41 +0100 Subject: [PATCH] test: avoid misc count filter allocations --- src/auto-reply/reply/session-transcript-replay.test.ts | 2 +- src/cli/run-main.exit.test.ts | 7 ++++++- src/commands/models/list.status.test.ts | 4 +++- src/daemon/launchd.integration.e2e.test.ts | 2 +- src/gateway/gateway-misc.test.ts | 4 +++- src/logging/diagnostic-memory.test.ts | 7 ++++++- src/plugin-sdk/memory-host-events.test.ts | 6 ++++-- src/plugin-state/plugin-state-store.test.ts | 2 +- src/tui/tui-event-handlers.test.ts | 2 +- test/scripts/docker-e2e-plan.test.ts | 8 ++++---- 10 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/auto-reply/reply/session-transcript-replay.test.ts b/src/auto-reply/reply/session-transcript-replay.test.ts index 76d83a62a89..34961b862fd 100644 --- a/src/auto-reply/reply/session-transcript-replay.test.ts +++ b/src/auto-reply/reply/session-transcript-replay.test.ts @@ -73,7 +73,7 @@ describe("replayRecentUserAssistantMessages", () => { .split(/\r?\n/) .filter((line) => line.trim().length > 0) .map((line) => JSON.parse(line)); - expect(records.filter((r) => r.type === "session")).toHaveLength(1); + expect(records.reduce((count, r) => count + (r.type === "session" ? 1 : 0), 0)).toBe(1); expect(records[0]).toMatchObject({ id: "existing" }); expect(records[1].message.role).toBe("user"); }); diff --git a/src/cli/run-main.exit.test.ts b/src/cli/run-main.exit.test.ts index 4510e918356..09b81a5f028 100644 --- a/src/cli/run-main.exit.test.ts +++ b/src/cli/run-main.exit.test.ts @@ -582,7 +582,12 @@ describe("runCli exit behavior", () => { try { const runPromise = runCli(["node", "openclaw", "plugins", "marketplace", "list"]); await vi.waitFor(() => { - expect(processOnceSpy.mock.calls.filter(([event]) => event === "exit")).toHaveLength(2); + expect( + processOnceSpy.mock.calls.reduce( + (count, [event]) => count + (event === "exit" ? 1 : 0), + 0, + ), + ).toBe(2); }); const exitHandler = processOnceSpy.mock.calls.find(([event]) => event === "exit")?.[1]; diff --git a/src/commands/models/list.status.test.ts b/src/commands/models/list.status.test.ts index 371ae1f0cbe..e4aaeadac71 100644 --- a/src/commands/models/list.status.test.ts +++ b/src/commands/models/list.status.test.ts @@ -585,7 +585,9 @@ describe("modelsStatusCommand auth overview", () => { await modelsStatusCommand({ json: true }, aliasRuntime as never); const aliasPayload = JSON.parse(String((aliasRuntime.log as Mock).mock.calls[0]?.[0])); const providers = aliasPayload.auth.providers as Array<{ provider: string }>; - expect(providers.filter((provider) => provider.provider === "zai")).toHaveLength(1); + expect( + providers.reduce((count, provider) => count + (provider.provider === "zai" ? 1 : 0), 0), + ).toBe(1); expect(providers.map((provider) => provider.provider)).not.toContain("z.ai"); } finally { if (originalLoadConfig) { diff --git a/src/daemon/launchd.integration.e2e.test.ts b/src/daemon/launchd.integration.e2e.test.ts index ef4bc98dd4e..5f4dbf4725b 100644 --- a/src/daemon/launchd.integration.e2e.test.ts +++ b/src/daemon/launchd.integration.e2e.test.ts @@ -294,7 +294,7 @@ describeLaunchdIntegration("launchd integration", () => { }); const events = await fs.readFile(eventsPath, "utf8"); const lines = events.trim().split(/\r?\n/).filter(Boolean); - expect(lines.filter((line) => line.startsWith("start "))).toHaveLength(1); + expect(lines.reduce((count, line) => count + (line.startsWith("start ") ? 1 : 0), 0)).toBe(1); const signalLines = lines.filter((line) => /^(SIGHUP|SIGINT|SIGTERM) /.test(line)); expect(signalLines).toEqual([]); }, 60_000); diff --git a/src/gateway/gateway-misc.test.ts b/src/gateway/gateway-misc.test.ts index 20410d0af4c..285eb01df7d 100644 --- a/src/gateway/gateway-misc.test.ts +++ b/src/gateway/gateway-misc.test.ts @@ -631,7 +631,9 @@ describe("gateway broadcaster", () => { reason: "ws_send_buffer_drop", }), ); - expect(events.filter((event) => event.type === "payload.large")).toHaveLength(1); + expect( + events.reduce((count, event) => count + (event.type === "payload.large" ? 1 : 0), 0), + ).toBe(1); } finally { stop(); resetDiagnosticEventsForTest(); diff --git a/src/logging/diagnostic-memory.test.ts b/src/logging/diagnostic-memory.test.ts index 2a1b224b500..bc0af7485f3 100644 --- a/src/logging/diagnostic-memory.test.ts +++ b/src/logging/diagnostic-memory.test.ts @@ -149,6 +149,11 @@ describe("diagnostic memory", () => { } stop(); - expect(events.filter((event) => event.type === "diagnostic.memory.pressure")).toHaveLength(1); + expect( + events.reduce( + (count, event) => count + (event.type === "diagnostic.memory.pressure" ? 1 : 0), + 0, + ), + ).toBe(1); }); }); diff --git a/src/plugin-sdk/memory-host-events.test.ts b/src/plugin-sdk/memory-host-events.test.ts index 92fa5c6844b..fa2f8269c74 100644 --- a/src/plugin-sdk/memory-host-events.test.ts +++ b/src/plugin-sdk/memory-host-events.test.ts @@ -135,8 +135,10 @@ describe("createClaimableDedupe", () => { await expect(dedupe.claim("line:evt-1")).resolves.toEqual({ kind: "duplicate" }); const claims = await Promise.all([dedupe.claim("line:race-1"), dedupe.claim("line:race-1")]); - expect(claims.filter((claim) => claim.kind === "claimed")).toHaveLength(1); - expect(claims.filter((claim) => claim.kind === "inflight")).toHaveLength(1); + const countClaimKind = (kind: (typeof claims)[number]["kind"]) => + claims.reduce((count, claim) => count + (claim.kind === kind ? 1 : 0), 0); + expect(countClaimKind("claimed")).toBe(1); + expect(countClaimKind("inflight")).toBe(1); const waitingClaim = claims.find((claim) => claim.kind === "inflight"); await expect(dedupe.commit("line:race-1")).resolves.toBe(true); diff --git a/src/plugin-state/plugin-state-store.test.ts b/src/plugin-state/plugin-state-store.test.ts index a613fc93908..cab16747406 100644 --- a/src/plugin-state/plugin-state-store.test.ts +++ b/src/plugin-state/plugin-state-store.test.ts @@ -150,7 +150,7 @@ describe("plugin state keyed store", () => { ), ); - expect(attempts.filter(Boolean)).toHaveLength(1); + expect(attempts.reduce((count, attempt) => count + (attempt ? 1 : 0), 0)).toBe(1); const stored = await store.lookup("claim"); if (stored === undefined) { throw new Error("expected winning plugin-state claim"); diff --git a/src/tui/tui-event-handlers.test.ts b/src/tui/tui-event-handlers.test.ts index 620c3779041..9289f465d76 100644 --- a/src/tui/tui-event-handlers.test.ts +++ b/src/tui/tui-event-handlers.test.ts @@ -1226,7 +1226,7 @@ describe("tui-event-handlers: streaming watchdog", () => { vi.advanceTimersByTime(10_000); const statusCalls = setActivityStatus.mock.calls.map((c) => c[0]); - expect(statusCalls.filter((s) => s === "idle").length).toBe(1); + expect(statusCalls.reduce((count, s) => count + (s === "idle" ? 1 : 0), 0)).toBe(1); expect(chatLog.addSystem).not.toHaveBeenCalledWith(expectedTimeoutMessage); expect(state.activeChatRunId).toBeNull(); diff --git a/test/scripts/docker-e2e-plan.test.ts b/test/scripts/docker-e2e-plan.test.ts index 0d53e3b99d3..80f929657c4 100644 --- a/test/scripts/docker-e2e-plan.test.ts +++ b/test/scripts/docker-e2e-plan.test.ts @@ -60,10 +60,10 @@ describe("scripts/lib/docker-e2e-plan", () => { expect(plan.lanes.map((lane) => lane.name)).toContain("commitments-safety"); expect(plan.lanes.map((lane) => lane.name)).toContain("bundled-plugin-install-uninstall-0"); expect(plan.lanes.map((lane) => lane.name)).toContain("bundled-plugin-install-uninstall-23"); - expect(plan.lanes.filter((lane) => lane.name === "install-e2e-openai")).toHaveLength(1); - expect( - plan.lanes.filter((lane) => lane.name === "bundled-plugin-install-uninstall-0"), - ).toHaveLength(1); + const countLane = (name: string) => + plan.lanes.reduce((count, lane) => count + (lane.name === name ? 1 : 0), 0); + expect(countLane("install-e2e-openai")).toBe(1); + expect(countLane("bundled-plugin-install-uninstall-0")).toBe(1); expect(plan.lanes.map((lane) => lane.name)).not.toContain("bundled-plugin-install-uninstall"); expect(plan.lanes.map((lane) => lane.name)).not.toContain("bundled-channel-deps"); expect(plan.lanes.map((lane) => lane.name)).not.toContain("openwebui");