From 8de02e6074eea8d2600b0253b0ce138a15607001 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 18 Jan 2026 23:25:16 +0000 Subject: [PATCH] test: stabilize sessions_send waits --- src/agents/clawdbot-tools.sessions.test.ts | 42 +++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src/agents/clawdbot-tools.sessions.test.ts b/src/agents/clawdbot-tools.sessions.test.ts index 3f465a867..d6879a167 100644 --- a/src/agents/clawdbot-tools.sessions.test.ts +++ b/src/agents/clawdbot-tools.sessions.test.ts @@ -22,6 +22,20 @@ vi.mock("../config/config.js", async (importOriginal) => { import { createClawdbotTools } from "./clawdbot-tools.js"; +const waitForCalls = async ( + getCount: () => number, + count: number, + timeoutMs = 2000, +) => { + const start = Date.now(); + while (getCount() < count) { + if (Date.now() - start > timeoutMs) { + throw new Error(`timed out waiting for ${count} calls`); + } + await new Promise((resolve) => setTimeout(resolve, 0)); + } +}; + describe("sessions tools", () => { it("uses number (not integer) in tool schemas for Gemini compatibility", () => { const tools = createClawdbotTools(); @@ -240,8 +254,18 @@ describe("sessions tools", () => { runId: "run-1", delivery: { status: "pending", mode: "announce" }, }); - await new Promise((resolve) => setTimeout(resolve, 0)); - await new Promise((resolve) => setTimeout(resolve, 0)); + await waitForCalls( + () => calls.filter((call) => call.method === "agent").length, + 4, + ); + await waitForCalls( + () => calls.filter((call) => call.method === "agent.wait").length, + 4, + ); + await waitForCalls( + () => calls.filter((call) => call.method === "chat.history").length, + 4, + ); const waitPromise = tool.execute("call6", { sessionKey: "main", @@ -255,8 +279,18 @@ describe("sessions tools", () => { delivery: { status: "pending", mode: "announce" }, }); expect(typeof (waited.details as { runId?: string }).runId).toBe("string"); - await new Promise((resolve) => setTimeout(resolve, 0)); - await new Promise((resolve) => setTimeout(resolve, 0)); + await waitForCalls( + () => calls.filter((call) => call.method === "agent").length, + 8, + ); + await waitForCalls( + () => calls.filter((call) => call.method === "agent.wait").length, + 8, + ); + await waitForCalls( + () => calls.filter((call) => call.method === "chat.history").length, + 8, + ); const agentCalls = calls.filter((call) => call.method === "agent"); const waitCalls = calls.filter((call) => call.method === "agent.wait");