From b3d3f3636001b84d08ebdf29616cf1b1224d7590 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 16 Feb 2026 05:20:22 +0000 Subject: [PATCH] test: speed up slack slash monitor tests --- src/slack/monitor/slash.test.ts | 161 +++++++++++++------------------- 1 file changed, 65 insertions(+), 96 deletions(-) diff --git a/src/slack/monitor/slash.test.ts b/src/slack/monitor/slash.test.ts index f637847c116..bd7dd629832 100644 --- a/src/slack/monitor/slash.test.ts +++ b/src/slack/monitor/slash.test.ts @@ -252,15 +252,7 @@ describe("Slack native command argument menus", () => { }); }); -function createPolicyHarness(overrides?: { - groupPolicy?: "open" | "allowlist"; - channelsConfig?: Record; - channelId?: string; - channelName?: string; - allowFrom?: string[]; - useAccessGroups?: boolean; - resolveChannelName?: () => Promise<{ name?: string; type?: string }>; -}) { +function createPolicyHarness() { const commands = new Map Promise>(); const postEphemeral = vi.fn().mockResolvedValue({ ok: true }); const app = { @@ -270,24 +262,21 @@ function createPolicyHarness(overrides?: { }, }; - const channelId = overrides?.channelId ?? "C_UNLISTED"; - const channelName = overrides?.channelName ?? "unlisted"; - const ctx = { cfg: { commands: { native: false } }, runtime: {}, botToken: "bot-token", botUserId: "bot", teamId: "T1", - allowFrom: overrides?.allowFrom ?? ["*"], + allowFrom: ["*"], dmEnabled: true, dmPolicy: "open", groupDmEnabled: false, groupDmChannels: [], defaultRequireMention: true, - groupPolicy: overrides?.groupPolicy ?? "open", - useAccessGroups: overrides?.useAccessGroups ?? true, - channelsConfig: overrides?.channelsConfig, + groupPolicy: "open", + useAccessGroups: true, + channelsConfig: undefined, slashCommand: { enabled: true, name: "openclaw", @@ -297,14 +286,21 @@ function createPolicyHarness(overrides?: { textLimit: 4000, app, isChannelAllowed: () => true, - resolveChannelName: - overrides?.resolveChannelName ?? (async () => ({ name: channelName, type: "channel" })), + resolveChannelName: async () => ({ name: "unlisted", type: "channel" }), resolveUserName: async () => ({ name: "Ada" }), - } as unknown; + } as Record; const account = { accountId: "acct", config: { commands: { native: false } } } as unknown; - return { commands, ctx, account, postEphemeral, channelId, channelName }; + return { commands, ctx, account, postEphemeral }; +} + +function resetPolicyHarness(harness: ReturnType) { + harness.ctx.allowFrom = ["*"]; + harness.ctx.groupPolicy = "open"; + harness.ctx.useAccessGroups = true; + harness.ctx.channelsConfig = undefined; + harness.ctx.resolveChannelName = async () => ({ name: "unlisted", type: "channel" }); } async function runSlashHandler(params: { @@ -343,20 +339,28 @@ async function runSlashHandler(params: { } describe("slack slash commands channel policy", () => { + let harness: ReturnType; + + beforeAll(async () => { + harness = createPolicyHarness(); + await registerCommands(harness.ctx, harness.account); + }); + + beforeEach(() => { + harness.postEphemeral.mockClear(); + resetPolicyHarness(harness); + }); + it("allows unlisted channels when groupPolicy is open", async () => { - const { commands, ctx, account, channelId, channelName } = createPolicyHarness({ - groupPolicy: "open", - channelsConfig: { C_LISTED: { requireMention: true } }, - channelId: "C_UNLISTED", - channelName: "unlisted", - }); - await registerCommands(ctx, account); + harness.ctx.groupPolicy = "open"; + harness.ctx.channelsConfig = { C_LISTED: { requireMention: true } }; + harness.ctx.resolveChannelName = async () => ({ name: "unlisted", type: "channel" }); const { respond } = await runSlashHandler({ - commands, + commands: harness.commands, command: { - channel_id: channelId, - channel_name: channelName, + channel_id: "C_UNLISTED", + channel_name: "unlisted", }, }); @@ -367,19 +371,15 @@ describe("slack slash commands channel policy", () => { }); it("blocks explicitly denied channels when groupPolicy is open", async () => { - const { commands, ctx, account, channelId, channelName } = createPolicyHarness({ - groupPolicy: "open", - channelsConfig: { C_DENIED: { allow: false } }, - channelId: "C_DENIED", - channelName: "denied", - }); - await registerCommands(ctx, account); + harness.ctx.groupPolicy = "open"; + harness.ctx.channelsConfig = { C_DENIED: { allow: false } }; + harness.ctx.resolveChannelName = async () => ({ name: "denied", type: "channel" }); const { respond } = await runSlashHandler({ - commands, + commands: harness.commands, command: { - channel_id: channelId, - channel_name: channelName, + channel_id: "C_DENIED", + channel_name: "denied", }, }); @@ -391,19 +391,15 @@ describe("slack slash commands channel policy", () => { }); it("blocks unlisted channels when groupPolicy is allowlist", async () => { - const { commands, ctx, account, channelId, channelName } = createPolicyHarness({ - groupPolicy: "allowlist", - channelsConfig: { C_LISTED: { requireMention: true } }, - channelId: "C_UNLISTED", - channelName: "unlisted", - }); - await registerCommands(ctx, account); + harness.ctx.groupPolicy = "allowlist"; + harness.ctx.channelsConfig = { C_LISTED: { requireMention: true } }; + harness.ctx.resolveChannelName = async () => ({ name: "unlisted", type: "channel" }); const { respond } = await runSlashHandler({ - commands, + commands: harness.commands, command: { - channel_id: channelId, - channel_name: channelName, + channel_id: "C_UNLISTED", + channel_name: "unlisted", }, }); @@ -416,41 +412,24 @@ describe("slack slash commands channel policy", () => { }); describe("slack slash commands access groups", () => { - it("fails closed when channel type lookup returns empty for channels", async () => { - const { commands, ctx, account, channelId, channelName } = createPolicyHarness({ - allowFrom: [], - channelId: "C_UNKNOWN", - channelName: "unknown", - resolveChannelName: async () => ({}), - }); - await registerCommands(ctx, account); + let harness: ReturnType; - const { respond } = await runSlashHandler({ - commands, - command: { - channel_id: channelId, - channel_name: channelName, - }, - }); + beforeAll(async () => { + harness = createPolicyHarness(); + await registerCommands(harness.ctx, harness.account); + }); - expect(dispatchMock).not.toHaveBeenCalled(); - expect(respond).toHaveBeenCalledWith({ - text: "You are not authorized to use this command.", - response_type: "ephemeral", - }); + beforeEach(() => { + harness.postEphemeral.mockClear(); + resetPolicyHarness(harness); }); it("still treats D-prefixed channel ids as DMs when lookup fails", async () => { - const { commands, ctx, account } = createPolicyHarness({ - allowFrom: [], - channelId: "D123", - channelName: "notdirectmessage", - resolveChannelName: async () => ({}), - }); - await registerCommands(ctx, account); + harness.ctx.allowFrom = []; + harness.ctx.resolveChannelName = async () => ({}); const { respond } = await runSlashHandler({ - commands, + commands: harness.commands, command: { channel_id: "D123", channel_name: "notdirectmessage", @@ -468,16 +447,11 @@ describe("slack slash commands access groups", () => { }); it("computes CommandAuthorized for DM slash commands when dmPolicy is open", async () => { - const { commands, ctx, account } = createPolicyHarness({ - allowFrom: ["U_OWNER"], - channelId: "D999", - channelName: "directmessage", - resolveChannelName: async () => ({ name: "directmessage", type: "im" }), - }); - await registerCommands(ctx, account); + harness.ctx.allowFrom = ["U_OWNER"]; + harness.ctx.resolveChannelName = async () => ({ name: "directmessage", type: "im" }); await runSlashHandler({ - commands, + commands: harness.commands, command: { user_id: "U_ATTACKER", user_name: "Mallory", @@ -494,19 +468,14 @@ describe("slack slash commands access groups", () => { }); it("enforces access-group gating when lookup fails for private channels", async () => { - const { commands, ctx, account, channelId, channelName } = createPolicyHarness({ - allowFrom: [], - channelId: "G123", - channelName: "private", - resolveChannelName: async () => ({}), - }); - await registerCommands(ctx, account); + harness.ctx.allowFrom = []; + harness.ctx.resolveChannelName = async () => ({}); const { respond } = await runSlashHandler({ - commands, + commands: harness.commands, command: { - channel_id: channelId, - channel_name: channelName, + channel_id: "G123", + channel_name: "private", }, });