test: avoid core count filter predicates

This commit is contained in:
Peter Steinberger
2026-05-08 22:08:55 +01:00
parent 7188ab7f6b
commit 27ddb6bea2
5 changed files with 40 additions and 8 deletions

View File

@@ -1062,7 +1062,7 @@ describe("sessions tools", () => {
});
await vi.waitFor(
() => {
expect(calls.filter((call) => call.method === "agent")).toHaveLength(3);
expect(countMatching(calls, (call) => call.method === "agent")).toBe(3);
},
{ timeout: 2_000, interval: 5 },
);

View File

@@ -52,6 +52,16 @@ vi.mock("./tools/agent-step.js", () => ({
const callGatewayMock = getCallGatewayMock();
const RUN_TIMEOUT_SECONDS = 1;
function countMatching<T>(items: readonly T[], predicate: (item: T) => boolean): number {
let count = 0;
for (const item of items) {
if (predicate(item)) {
count += 1;
}
}
return count;
}
function buildDiscordCleanupHooks(onDelete: (key: string | undefined) => void) {
return {
onAgentSubagentSpawn: (params: unknown) => {
@@ -235,7 +245,7 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
() =>
ctx.waitCalls.some((call) => call.runId === child.runId) &&
patchCalls.some((call) => call.label === "my-task") &&
ctx.calls.filter((call) => call.method === "agent").length >= 2,
countMatching(ctx.calls, (call) => call.method === "agent") >= 2,
);
if (!child.sessionKey) {
throw new Error("missing child sessionKey");
@@ -371,7 +381,7 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
await waitForSessionsSpawnEvent(
"lifecycle cleanup",
() => ctx.calls.filter((call) => call.method === "agent").length >= 2 && Boolean(deletedKey),
() => countMatching(ctx.calls, (call) => call.method === "agent") >= 2 && Boolean(deletedKey),
);
const childWait = ctx.waitCalls.find((call) => call.runId === child.runId);
@@ -437,7 +447,7 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
);
await waitForSessionsSpawnEvent(
"main agent cleanup trigger",
() => ctx.calls.filter((call) => call.method === "agent").length >= 2,
() => countMatching(ctx.calls, (call) => call.method === "agent") >= 2,
);
await waitForSessionsSpawnEvent("delete cleanup", () => Boolean(deletedKey));
@@ -563,7 +573,7 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
await waitForSessionsSpawnEvent(
"account-aware lifecycle announce",
() => ctx.calls.filter((call) => call.method === "agent").length >= 2,
() => countMatching(ctx.calls, (call) => call.method === "agent") >= 2,
);
await waitForRunCleanup(child.sessionKey);

View File

@@ -20,6 +20,16 @@ function expectSomeScriptContaining(scripts: string[], needle: string) {
expect(scripts.some((script) => script.includes(needle))).toBe(true);
}
function countMatching<T>(items: readonly T[], predicate: (item: T) => boolean): number {
let count = 0;
for (const item of items) {
if (predicate(item)) {
count += 1;
}
}
return count;
}
describe("sandbox fs bridge shell compatibility", () => {
installFsBridgeTestHarness();
@@ -157,7 +167,9 @@ describe("sandbox fs bridge shell compatibility", () => {
await bridge.rename({ from: "a.txt", to: "nested/b.txt" });
const scripts = getScriptsFromCalls();
expect(scripts.filter((script) => script.includes("operation = sys.argv[1]")).length).toBe(3);
expect(countMatching(scripts, (script) => script.includes("operation = sys.argv[1]"))).toBe(
3,
);
expectNoScriptsContaining(scripts, 'mkdir -p -- "$2"');
expectNoScriptsContaining(scripts, 'rm -f -- "$2"');
expectNoScriptsContaining(scripts, 'mv -- "$3" "$2/$4"');

View File

@@ -5,6 +5,16 @@ import { createAcpTestConfig as createCfg } from "./test-fixtures/acp-runtime.js
type Delivery = { kind: string; text?: string };
function countMatching<T>(items: readonly T[], predicate: (item: T) => boolean): number {
let count = 0;
for (const item of items) {
if (predicate(item)) {
count += 1;
}
}
return count;
}
function createProjectorHarness(
cfgOverrides?: Parameters<typeof createCfg>[0],
opts?: { onProgress?: () => void },
@@ -567,7 +577,7 @@ describe("createAcpReplyProjector", () => {
});
await projector.flush(true);
expect(deliveries.filter((entry) => entry.kind === "tool").length).toBe(4);
expect(countMatching(deliveries, (entry) => entry.kind === "tool")).toBe(4);
expect(deliveries[0]).toEqual({
kind: "tool",
text: prefixSystemMessage("available commands updated"),

View File

@@ -1233,7 +1233,7 @@ describe("task-registry", () => {
await maybeDeliverTaskTerminalUpdate(spawnedTask.taskId);
expect(hoisted.sendMessageMock).toHaveBeenCalledTimes(1);
expect(listTaskRecords().filter((task) => task.runId === "run-shared-delivery")).toHaveLength(
expect(countMatching(listTaskRecords(), (task) => task.runId === "run-shared-delivery")).toBe(
1,
);
expect(findTaskByRunId("run-shared-delivery")).toMatchObject({