test: avoid extension count filter allocations · openclaw/openclaw@7645824
steipete
·
2026-05-09
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -127,7 +127,9 @@ describe("Feishu monitor startup preflight", () => {
|
127 | 127 | try { |
128 | 128 | await waitForStartedAccount(started, "beta"); |
129 | 129 | expect(started).toEqual(["alpha", "beta"]); |
130 | | -expect(started.filter((accountId) => accountId === "alpha")).toHaveLength(1); |
| 130 | +expect(started.reduce((count, accountId) => count + (accountId === "alpha" ? 1 : 0), 0)).toBe( |
| 131 | +1, |
| 132 | +); |
131 | 133 | } finally { |
132 | 134 | releaseStartedBetaProbe(); |
133 | 135 | abortController.abort(); |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -507,6 +507,6 @@ describe("persistAllowAlways", () => {
|
507 | 507 | }; |
508 | 508 | }; |
509 | 509 | const list = root.plugins.entries["file-transfer"].config.nodes.n1.allowReadPaths; |
510 | | -expect(list.filter((p) => p === "/tmp/x").length).toBe(1); |
| 510 | +expect(list.reduce((count, p) => count + (p === "/tmp/x" ? 1 : 0), 0)).toBe(1); |
511 | 511 | }); |
512 | 512 | }); |
| Original file line number | Diff line number | Diff line change |
|---|
@@ -6,6 +6,16 @@ import {
|
6 | 6 | WebSocketClosedBeforeOpenError, |
7 | 7 | } from "./monitor-websocket.js"; |
8 | 8 | |
| 9 | +function countMatching<T>(items: readonly T[], predicate: (item: T) => boolean): number { |
| 10 | +let count = 0; |
| 11 | +for (const item of items) { |
| 12 | +if (predicate(item)) { |
| 13 | +count += 1; |
| 14 | +} |
| 15 | +} |
| 16 | +return count; |
| 17 | +} |
| 18 | + |
9 | 19 | class FakeWebSocket implements MattermostWebSocketLike { |
10 | 20 | public readonly sent: string[] = []; |
11 | 21 | public pingCalls = 0; |
@@ -172,8 +182,8 @@ describe("mattermost websocket monitor", () => {
|
172 | 182 | data: { token: "token" }, |
173 | 183 | seq: 1, |
174 | 184 | }); |
175 | | -expect(patches.filter((patch) => patch.connected === true)).toHaveLength(1); |
176 | | -expect(patches.filter((patch) => patch.connected === false)).toHaveLength(2); |
| 185 | +expect(countMatching(patches, (patch) => patch.connected === true)).toBe(1); |
| 186 | +expect(countMatching(patches, (patch) => patch.connected === false)).toBe(2); |
177 | 187 | }); |
178 | 188 | |
179 | 189 | it("dispatches reaction events to the reaction handler", async () => { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -25,13 +25,15 @@ describe("Ollama provider", () => {
|
25 | 25 | const fetchCallUrls = (fetchMock: ReturnType<typeof vi.fn>): string[] => |
26 | 26 | fetchMock.mock.calls.map(([input]) => String(input)); |
27 | 27 | |
| 28 | +const countFetchCallUrls = (fetchMock: ReturnType<typeof vi.fn>, suffix: string): number => |
| 29 | +fetchCallUrls(fetchMock).reduce((count, url) => count + (url.endsWith(suffix) ? 1 : 0), 0); |
| 30 | + |
28 | 31 | const expectDiscoveryCallCounts = ( |
29 | 32 | fetchMock: ReturnType<typeof vi.fn>, |
30 | 33 | params: { tags: number; show: number }, |
31 | 34 | ) => { |
32 | | -const urls = fetchCallUrls(fetchMock); |
33 | | -expect(urls.filter((url) => url.endsWith("/api/tags"))).toHaveLength(params.tags); |
34 | | -expect(urls.filter((url) => url.endsWith("/api/show"))).toHaveLength(params.show); |
| 35 | +expect(countFetchCallUrls(fetchMock, "/api/tags")).toBe(params.tags); |
| 36 | +expect(countFetchCallUrls(fetchMock, "/api/show")).toBe(params.show); |
35 | 37 | }; |
36 | 38 | |
37 | 39 | async function withOllamaApiKey<T>(run: () => Promise<T>): Promise<T> { |
@@ -148,7 +150,7 @@ describe("Ollama provider", () => {
|
148 | 150 | env: { OLLAMA_API_KEY: "test-key" }, |
149 | 151 | }); |
150 | 152 | |
151 | | -expect(fetchCallUrls(fetchMock).filter((url) => url.endsWith("/api/tags"))).toHaveLength(1); |
| 153 | +expect(countFetchCallUrls(fetchMock, "/api/tags")).toBe(1); |
152 | 154 | |
153 | 155 | // Native API strips /v1 suffix via resolveOllamaApiBase() |
154 | 156 | expect(provider?.baseUrl).toBe("http://192.168.20.14:11434"); |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。