From 6812215d4b8eeabd3b8fb8525466a2c5ffd5d91c Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 12 May 2026 08:37:12 +0100 Subject: [PATCH] test: guard message tool mock calls --- src/agents/tools/message-tool.test.ts | 39 +++++++++++++++++---------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/agents/tools/message-tool.test.ts b/src/agents/tools/message-tool.test.ts index da13b4b13dd2..c19fb6548c72 100644 --- a/src/agents/tools/message-tool.test.ts +++ b/src/agents/tools/message-tool.test.ts @@ -103,6 +103,24 @@ const mocks = vi.hoisted(() => ({ ), })); +type RunMessageActionInput = { + agentId?: string; + cfg?: unknown; + params?: Record; + requesterSenderId?: string; + sandboxRoot?: string; + senderIsOwner?: boolean; + sessionKey?: string; + toolContext?: { + currentThreadTs?: string; + replyToMode?: string; + }; +}; + +function firstRunMessageActionInput(): RunMessageActionInput | undefined { + return mocks.runMessageAction.mock.calls.at(0)?.[0] as RunMessageActionInput | undefined; +} + const openClawToolsFactoryMocks = vi.hoisted(() => { const tool = (name: string) => ({ name, @@ -330,14 +348,7 @@ async function executeSend(params: { action: "send", ...params.action, }); - return mocks.runMessageAction.mock.calls[0]?.[0] as - | { - params?: Record; - sandboxRoot?: string; - requesterSenderId?: string; - senderIsOwner?: boolean; - } - | undefined; + return firstRunMessageActionInput(); } describe("message tool secret scoping", () => { @@ -474,7 +485,7 @@ describe("message tool secret scoping", () => { expect(secretResolveCall.allowedPaths).toEqual( new Set(["channels.discord.token", "channels.discord.accounts.ops.token"]), ); - expect(mocks.runMessageAction.mock.calls[0]?.[0]?.cfg).toBe(resolvedConfig); + expect(firstRunMessageActionInput()?.cfg).toBe(resolvedConfig); }); }); @@ -494,7 +505,7 @@ describe("message tool agent routing", () => { message: "hi", }); - const call = mocks.runMessageAction.mock.calls[0]?.[0]; + const call = firstRunMessageActionInput(); expect(call?.agentId).toBe("alpha"); expect(call?.sessionKey).toBe("agent:alpha:main"); }); @@ -517,7 +528,7 @@ describe("message tool agent routing", () => { message: "stay in thread", }); - const call = mocks.runMessageAction.mock.calls[0]?.[0]; + const call = firstRunMessageActionInput(); expect(call?.toolContext?.currentThreadTs).toBe("111.222"); expect(call?.toolContext?.replyToMode).toBe("all"); }); @@ -541,7 +552,7 @@ describe("message tool agent routing", () => { message: "send at channel level", }); - const call = mocks.runMessageAction.mock.calls[0]?.[0]; + const call = firstRunMessageActionInput(); expect(call?.toolContext?.currentThreadTs).toBe("111.222"); expect(call?.toolContext?.replyToMode).toBe("off"); }); @@ -567,7 +578,7 @@ describe("message tool agent routing", () => { message: "stay in thread", }); - const call = mocks.runMessageAction.mock.calls[0]?.[0]; + const call = firstRunMessageActionInput(); expect(call?.toolContext?.currentThreadTs).toBe("111.222"); expect(call?.toolContext?.replyToMode).toBe("all"); }); @@ -615,7 +626,7 @@ describe("message tool explicit target guard", () => { filePath: "/tmp/report.png", }); - const call = mocks.runMessageAction.mock.calls[0]?.[0]; + const call = firstRunMessageActionInput(); expect(call?.params?.target).toBe("channel:C999"); }); });