test: avoid misc count filter allocations

This commit is contained in:
Peter Steinberger
2026-05-08 22:05:41 +01:00
parent cd7f733a99
commit ce515dbf4d
10 changed files with 30 additions and 14 deletions

View File

@@ -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");
});

View File

@@ -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];

View File

@@ -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) {

View File

@@ -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);

View File

@@ -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();

View File

@@ -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);
});
});

View File

@@ -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);

View File

@@ -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");

View File

@@ -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();

View File

@@ -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");