test: avoid loader count filter allocations · openclaw/openclaw@6623228
steipete
·
2026-05-09
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -738,7 +738,9 @@ describe("config io write", () => {
|
738 | 738 | ]); |
739 | 739 | await expect(fs.readFile(configPath, "utf-8")).resolves.toBe(cleanRaw); |
740 | 740 | const entries = await fs.readdir(path.dirname(configPath)); |
741 | | -expect(entries.filter((entry) => entry.includes(".clobbered."))).toHaveLength(1); |
| 741 | +expect( |
| 742 | +entries.reduce((count, entry) => count + (entry.includes(".clobbered.") ? 1 : 0), 0), |
| 743 | +).toBe(1); |
742 | 744 | expect(warn).toHaveBeenCalledWith( |
743 | 745 | expect.stringContaining("Config auto-stripped non-JSON prefix:"), |
744 | 746 | ); |
@@ -827,7 +829,9 @@ describe("config io write", () => {
|
827 | 829 | |
828 | 830 | await expect(fs.readFile(configPath, "utf-8")).resolves.toBe(originalRaw); |
829 | 831 | const entries = await fs.readdir(path.dirname(configPath)); |
830 | | -expect(entries.filter((entry) => entry.includes(".rejected."))).toHaveLength(1); |
| 832 | +expect( |
| 833 | +entries.reduce((count, entry) => count + (entry.includes(".rejected.") ? 1 : 0), 0), |
| 834 | +).toBe(1); |
831 | 835 | expect(warn).toHaveBeenCalledWith(expect.stringContaining("Config write rejected:")); |
832 | 836 | }); |
833 | 837 | }); |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -42,6 +42,16 @@ function createCustomRootCfg(customRoot: string, defaultAgentId = "ops"): OpenCl
|
42 | 42 | }; |
43 | 43 | } |
44 | 44 | |
| 45 | +function countMatching<T>(items: readonly T[], predicate: (item: T) => boolean): number { |
| 46 | +let count = 0; |
| 47 | +for (const item of items) { |
| 48 | +if (predicate(item)) { |
| 49 | +count += 1; |
| 50 | +} |
| 51 | +} |
| 52 | +return count; |
| 53 | +} |
| 54 | + |
45 | 55 | async function resolveTargetsForCustomRoot(home: string, agentIds: string[]) { |
46 | 56 | const customRoot = path.join(home, "custom-state"); |
47 | 57 | const storePaths = await createAgentSessionStores(customRoot, agentIds); |
@@ -186,7 +196,7 @@ describe("resolveAllAgentSessionStoreTargets", () => {
|
186 | 196 | const targets = await resolveAllAgentSessionStoreTargets(cfg, { env: process.env }); |
187 | 197 | |
188 | 198 | expectTargetsToContainStores(targets, storePaths); |
189 | | -expect(targets.filter((target) => target.storePath === storePaths.ops)).toHaveLength(1); |
| 199 | +expect(countMatching(targets, (target) => target.storePath === storePaths.ops)).toBe(1); |
190 | 200 | }); |
191 | 201 | }); |
192 | 202 | |
@@ -195,7 +205,7 @@ describe("resolveAllAgentSessionStoreTargets", () => {
|
195 | 205 | const { storePaths, targets } = await resolveTargetsForCustomRoot(home, ["ops", "retired"]); |
196 | 206 | |
197 | 207 | expectTargetsToContainStores(targets, storePaths); |
198 | | -expect(targets.filter((target) => target.storePath === storePaths.ops)).toHaveLength(1); |
| 208 | +expect(countMatching(targets, (target) => target.storePath === storePaths.ops)).toBe(1); |
199 | 209 | }); |
200 | 210 | }); |
201 | 211 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -292,7 +292,12 @@ describe("loader", () => {
|
292 | 292 | |
293 | 293 | const event = createInternalHookEvent("command", "new", "test-session"); |
294 | 294 | await triggerInternalHook(event); |
295 | | -expect(event.messages.filter((message) => message === "reloadable-hook")).toHaveLength(1); |
| 295 | +expect( |
| 296 | +event.messages.reduce( |
| 297 | +(count, message) => count + (message === "reloadable-hook" ? 1 : 0), |
| 298 | +0, |
| 299 | +), |
| 300 | +).toBe(1); |
296 | 301 | }); |
297 | 302 | |
298 | 303 | it("should support named exports", async () => { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -96,6 +96,16 @@ let cachedBundledMemoryDir = "";
|
96 | 96 | |
97 | 97 | type GlobalHookRunner = NonNullable<ReturnType<typeof getGlobalHookRunner>>; |
98 | 98 | |
| 99 | +function countMatching<T>(items: readonly T[], predicate: (item: T) => boolean): number { |
| 100 | +let count = 0; |
| 101 | +for (const item of items) { |
| 102 | +if (predicate(item)) { |
| 103 | +count += 1; |
| 104 | +} |
| 105 | +} |
| 106 | +return count; |
| 107 | +} |
| 108 | + |
99 | 109 | function expectGlobalHookRunner(runner: ReturnType<typeof getGlobalHookRunner>): GlobalHookRunner { |
100 | 110 | expect(runner).toEqual(expect.objectContaining({ hasHooks: expect.any(Function) })); |
101 | 111 | if (runner === null) { |
@@ -2169,7 +2179,7 @@ module.exports = { id: "throws-after-import", register() {} };`,
|
2169 | 2179 | |
2170 | 2180 | const event = createInternalHookEvent("gateway", "startup", "gateway:startup"); |
2171 | 2181 | await triggerInternalHook(event); |
2172 | | -expect(event.messages.filter((message) => message === "reload-hook-fired")).toHaveLength(1); |
| 2182 | +expect(countMatching(event.messages, (message) => message === "reload-hook-fired")).toBe(1); |
2173 | 2183 | |
2174 | 2184 | clearInternalHooks(); |
2175 | 2185 | }); |
@@ -4008,7 +4018,7 @@ module.exports = { id: "throws-after-import", register() {} };`,
|
4008 | 4018 | }); |
4009 | 4019 | } };`, |
4010 | 4020 | assert: (registry: ReturnType<typeof loadOpenClawPlugins>) => { |
4011 | | -expect(registry.channels.filter((entry) => entry.plugin.id === "demo")).toHaveLength(1); |
| 4021 | +expect(countMatching(registry.channels, (entry) => entry.plugin.id === "demo")).toBe(1); |
4012 | 4022 | expect( |
4013 | 4023 | registry.channels.find((entry) => entry.plugin.id === "demo")?.plugin.meta?.label, |
4014 | 4024 | ).toBe("Demo Duplicate"); |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。