From 49ecbd8fea34828c7af69857207c207b2f760552 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 17 Jan 2026 04:33:15 +0000 Subject: [PATCH] test: expand accountId routing coverage Co-authored-by: adam91holt --- src/agents/subagent-announce.format.test.ts | 53 +++++++++++++++++++ ...erver.agent.gateway-server-agent-a.test.ts | 46 ++++++++++++++++ src/utils/delivery-context.test.ts | 14 +++++ 3 files changed, 113 insertions(+) diff --git a/src/agents/subagent-announce.format.test.ts b/src/agents/subagent-announce.format.test.ts index 3b18f592f..0b3679c49 100644 --- a/src/agents/subagent-announce.format.test.ts +++ b/src/agents/subagent-announce.format.test.ts @@ -192,6 +192,59 @@ describe("subagent announce formatting", () => { expect(call?.params?.accountId).toBe("kev"); }); + it("splits collect-mode queues when accountId differs", async () => { + const { runSubagentAnnounceFlow } = await import("./subagent-announce.js"); + embeddedRunMock.isEmbeddedPiRunActive.mockReturnValue(true); + embeddedRunMock.isEmbeddedPiRunStreaming.mockReturnValue(false); + sessionStore = { + "agent:main:main": { + sessionId: "session-acc-split", + lastChannel: "whatsapp", + lastTo: "+1555", + queueMode: "collect", + queueDebounceMs: 80, + }, + }; + + await Promise.all([ + runSubagentAnnounceFlow({ + childSessionKey: "agent:main:subagent:test-a", + childRunId: "run-a", + requesterSessionKey: "main", + requesterDisplayKey: "main", + requesterOrigin: { accountId: "acct-a" }, + task: "do thing", + timeoutMs: 1000, + cleanup: "keep", + waitForCompletion: false, + startedAt: 10, + endedAt: 20, + outcome: { status: "ok" }, + }), + runSubagentAnnounceFlow({ + childSessionKey: "agent:main:subagent:test-b", + childRunId: "run-b", + requesterSessionKey: "main", + requesterDisplayKey: "main", + requesterOrigin: { accountId: "acct-b" }, + task: "do thing", + timeoutMs: 1000, + cleanup: "keep", + waitForCompletion: false, + startedAt: 10, + endedAt: 20, + outcome: { status: "ok" }, + }), + ]); + + await new Promise((r) => setTimeout(r, 120)); + expect(agentSpy).toHaveBeenCalledTimes(2); + const accountIds = agentSpy.mock.calls.map( + (call) => (call?.[0] as { params?: { accountId?: string } })?.params?.accountId, + ); + expect(accountIds).toEqual(expect.arrayContaining(["acct-a", "acct-b"])); + }); + it("uses requester origin for direct announce when not queued", async () => { const { runSubagentAnnounceFlow } = await import("./subagent-announce.js"); embeddedRunMock.isEmbeddedPiRunActive.mockReturnValue(false); diff --git a/src/gateway/server.agent.gateway-server-agent-a.test.ts b/src/gateway/server.agent.gateway-server-agent-a.test.ts index cab401a86..f49008cd4 100644 --- a/src/gateway/server.agent.gateway-server-agent-a.test.ts +++ b/src/gateway/server.agent.gateway-server-agent-a.test.ts @@ -197,6 +197,52 @@ describe("gateway server agent", () => { testState.allowFrom = undefined; }); + test("agent keeps explicit accountId when explicit to is provided", async () => { + testState.allowFrom = ["+1555"]; + const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-gw-")); + testState.sessionStorePath = path.join(dir, "sessions.json"); + await fs.writeFile( + testState.sessionStorePath, + JSON.stringify( + { + main: { + sessionId: "sess-main-explicit-account", + updatedAt: Date.now(), + lastChannel: "whatsapp", + lastTo: "+1555", + lastAccountId: "legacy", + }, + }, + null, + 2, + ), + "utf-8", + ); + + const { server, ws } = await startServerWithClient(); + await connectOk(ws); + + const res = await rpcReq(ws, "agent", { + message: "hi", + sessionKey: "main", + deliver: true, + to: "+1666", + accountId: "primary", + idempotencyKey: "idem-agent-explicit-account", + }); + expect(res.ok).toBe(true); + + const spy = vi.mocked(agentCommand); + const call = spy.mock.calls.at(-1)?.[0] as Record; + expectChannels(call, "whatsapp"); + expect(call.to).toBe("+1666"); + expect(call.accountId).toBe("primary"); + + ws.close(); + await server.close(); + testState.allowFrom = undefined; + }); + test("agent falls back to lastAccountId for implicit delivery", async () => { testState.allowFrom = ["+1555"]; const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-gw-")); diff --git a/src/utils/delivery-context.test.ts b/src/utils/delivery-context.test.ts index fc612ca21..7e3c4dbc7 100644 --- a/src/utils/delivery-context.test.ts +++ b/src/utils/delivery-context.test.ts @@ -42,6 +42,9 @@ describe("delivery context helpers", () => { "whatsapp|+1555|", ); expect(deliveryContextKey({ channel: "whatsapp" })).toBeUndefined(); + expect( + deliveryContextKey({ channel: "whatsapp", to: "+1555", accountId: "acct-1" }), + ).toBe("whatsapp|+1555|acct-1"); }); it("derives delivery context from a session entry", () => { @@ -57,5 +60,16 @@ describe("delivery context helpers", () => { to: "+1777", accountId: "acct-9", }); + + expect( + deliveryContextFromSession({ + channel: "telegram", + lastTo: " 123 ", + }), + ).toEqual({ + channel: "telegram", + to: "123", + accountId: undefined, + }); }); });