test: expand accountId routing coverage

Co-authored-by: adam91holt <adam91holt@users.noreply.github.com>
This commit is contained in:
Peter Steinberger
2026-01-17 04:33:15 +00:00
parent 19ee6699d2
commit 49ecbd8fea
3 changed files with 113 additions and 0 deletions

View File

@@ -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);

View File

@@ -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<string, unknown>;
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-"));

View File

@@ -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,
});
});
});