From 2b202ffe7c9687f47dabcab99994f821a823e600 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 12 May 2026 00:47:07 +0100 Subject: [PATCH] test: guard slack dispatch mock calls --- .../slack/src/message-action-dispatch.test.ts | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/extensions/slack/src/message-action-dispatch.test.ts b/extensions/slack/src/message-action-dispatch.test.ts index 81909977d11..2023080347c 100644 --- a/extensions/slack/src/message-action-dispatch.test.ts +++ b/extensions/slack/src/message-action-dispatch.test.ts @@ -12,12 +12,24 @@ function slackConfig() { return { channels: { slack: { botToken: "tok" } } }; } +function firstInvokeCall(invoke: ReturnType) { + const [call] = invoke.mock.calls; + if (!call) { + throw new Error("expected first Slack action invoke"); + } + return call; +} + function expectForwardedCfg(invoke: ReturnType, cfg: unknown) { - expect(invoke.mock.calls[0]?.[1]).toBe(cfg); + expect(firstInvokeCall(invoke)[1]).toBe(cfg); +} + +function expectNoForwardedToolContext(invoke: ReturnType) { + expect(firstInvokeCall(invoke)[2]).toBeUndefined(); } function firstAction(invoke: ReturnType) { - const action = invoke.mock.calls[0]?.[0]; + const action = firstInvokeCall(invoke)[0]; if (!action || typeof action !== "object") { throw new Error("expected first invoke action"); } @@ -154,7 +166,7 @@ describe("handleSlackMessageAction", () => { expect(actionsBlock.type).toBe("actions"); expect(elementAt(actionsBlock, 0).value).toBe("approve"); expectForwardedCfg(invoke, cfg); - expect(invoke.mock.calls[0]?.[2]).toBeUndefined(); + expectNoForwardedToolContext(invoke); }); it("passes replyBroadcast through for Slack thread sends", async () => { @@ -183,7 +195,7 @@ describe("handleSlackMessageAction", () => { expect(action.threadTs).toBe("111.222"); expect(action.replyBroadcast).toBe(true); expectForwardedCfg(invoke, cfg); - expect(invoke.mock.calls[0]?.[2]).toBeUndefined(); + expectNoForwardedToolContext(invoke); }); it("passes topLevel through so same-channel Slack sends can suppress thread inheritance", async () => { @@ -211,7 +223,7 @@ describe("handleSlackMessageAction", () => { expect(action.threadTs).toBeUndefined(); expect(action.topLevel).toBe(true); expectForwardedCfg(invoke, cfg); - expect(invoke.mock.calls[0]?.[2]).toBeUndefined(); + expectNoForwardedToolContext(invoke); }); it("treats threadId null as a Slack top-level send request", async () => { @@ -237,7 +249,7 @@ describe("handleSlackMessageAction", () => { expect(action.threadTs).toBeUndefined(); expect(action.topLevel).toBe(true); expectForwardedCfg(invoke, cfg); - expect(invoke.mock.calls[0]?.[2]).toBeUndefined(); + expectNoForwardedToolContext(invoke); }); it("maps upload-file to the internal uploadFile action", async () => { @@ -270,7 +282,7 @@ describe("handleSlackMessageAction", () => { expect(action.title).toBe("Build Screenshot"); expect(action.threadTs).toBe("111.222"); expectForwardedCfg(invoke, cfg); - expect(invoke.mock.calls[0]?.[2]).toBeUndefined(); + expectNoForwardedToolContext(invoke); }); it("rejects replyBroadcast for upload-file", async () => { @@ -318,7 +330,7 @@ describe("handleSlackMessageAction", () => { expect(action.initialComment).toBe("chart attached"); expect(action.threadTs).toBe("333.444"); expectForwardedCfg(invoke, cfg); - expect(invoke.mock.calls[0]?.[2]).toBeUndefined(); + expectNoForwardedToolContext(invoke); }); it("maps upload-file path alias to filePath", async () => { @@ -345,7 +357,7 @@ describe("handleSlackMessageAction", () => { expect(action.filePath).toBe("/tmp/report.txt"); expect(action.initialComment).toBe("path alias"); expectForwardedCfg(invoke, cfg); - expect(invoke.mock.calls[0]?.[2]).toBeUndefined(); + expectNoForwardedToolContext(invoke); }); it("forwards messageId for read actions", async () => { @@ -368,7 +380,7 @@ describe("handleSlackMessageAction", () => { expect(action.action).toBe("readMessages"); expect(action.channelId).toBe("C1"); expect(action.messageId).toBe("1712345678.654321"); - expect(invoke.mock.calls[0]?.[1]).toEqual({}); + expect(firstInvokeCall(invoke)[1]).toEqual({}); }); it("requires filePath, path, or media for upload-file", async () => {