|
| 1 | +import { WebClient } from "@slack/web-api"; |
| 2 | +import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { slackActionRuntime } from "./action-runtime.js"; |
| 5 | +import { |
| 6 | + listSlackReactions, |
| 7 | + removeOwnSlackReactions, |
| 8 | + type SlackMessageSummary, |
| 9 | +} from "./actions.js"; |
| 10 | +import { createSlackActions } from "./channel-actions.js"; |
| 11 | + |
| 12 | +type SlackReaction = NonNullable<SlackMessageSummary["reactions"]>[number]; |
| 13 | + |
| 14 | +const slackConfig = { |
| 15 | + channels: { slack: { botToken: "xoxb-local-proof", groupPolicy: "open" } }, |
| 16 | +} as OpenClawConfig; |
| 17 | + |
| 18 | +function createSlackReactionClient(reactions: SlackReaction[]) { |
| 19 | + const calls: Array<{ method: string; body: URLSearchParams }> = []; |
| 20 | + const client = new WebClient("xoxb-local-proof", { |
| 21 | + retryConfig: { retries: 0 }, |
| 22 | + fetch: async (input, init) => { |
| 23 | + const method = new URL(String(input)).pathname.split("/").at(-1) ?? ""; |
| 24 | + const requestBody = init?.body; |
| 25 | + if (typeof requestBody !== "string") { |
| 26 | + throw new Error("Slack reaction requests must use URL-encoded request bodies."); |
| 27 | + } |
| 28 | + const body = new URLSearchParams(requestBody); |
| 29 | + calls.push({ method, body }); |
| 30 | + const result = |
| 31 | + method === "reactions.get" |
| 32 | + ? { ok: true, channel: "C1", message: { reactions } } |
| 33 | + : method === "auth.test" |
| 34 | + ? { ok: true, user_id: "UBOT" } |
| 35 | + : method === "reactions.remove" |
| 36 | + ? { ok: true } |
| 37 | + : null; |
| 38 | + if (!result) { |
| 39 | + throw new Error(`Unexpected Slack API method: ${method}`); |
| 40 | + } |
| 41 | + return new Response(JSON.stringify(result), { |
| 42 | + status: 200, |
| 43 | + headers: { "content-type": "application/json" }, |
| 44 | + }); |
| 45 | + }, |
| 46 | + }); |
| 47 | + return { client, calls }; |
| 48 | +} |
| 49 | + |
| 50 | +async function readSlackReactionsThroughPublicAction(params: { |
| 51 | + client: WebClient; |
| 52 | + limit?: number; |
| 53 | +}) { |
| 54 | + vi.spyOn(slackActionRuntime, "resolveSlackConversationInfo").mockResolvedValue({ |
| 55 | + type: "channel", |
| 56 | + }); |
| 57 | + vi.spyOn(slackActionRuntime, "listSlackReactions").mockImplementation( |
| 58 | + (channelId, messageId, options) => |
| 59 | + listSlackReactions(channelId, messageId, { ...options, client: params.client }), |
| 60 | + ); |
| 61 | + const result = await createSlackActions("slack").handleAction?.({ |
| 62 | + action: "reactions", |
| 63 | + cfg: slackConfig, |
| 64 | + conversationReadOrigin: "direct-operator", |
| 65 | + params: { |
| 66 | + channelId: "C1", |
| 67 | + messageId: "123.456", |
| 68 | + ...(params.limit === undefined ? {} : { limit: params.limit }), |
| 69 | + }, |
| 70 | + } as never); |
| 71 | + const content = result?.content[0]; |
| 72 | + if (!content || content.type !== "text") { |
| 73 | + throw new Error("Slack reactions did not return a text tool result."); |
| 74 | + } |
| 75 | + return JSON.parse(content.text) as { ok: boolean; reactions: SlackReaction[] }; |
| 76 | +} |
| 77 | + |
| 78 | +describe("Slack reaction user limits", () => { |
| 79 | + afterEach(() => { |
| 80 | + vi.restoreAllMocks(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("limits users per emoji through the public action without changing reaction facts", async () => { |
| 84 | + const { client, calls } = createSlackReactionClient([ |
| 85 | + { name: "eyes", count: 3, users: ["U1", "U2", "UBOT"] }, |
| 86 | + { name: "wave", count: 2, users: ["U3", "U4"] }, |
| 87 | + { name: "heart", count: 5 }, |
| 88 | + { name: "party", count: 0, users: [] }, |
| 89 | + ]); |
| 90 | + |
| 91 | + const result = await readSlackReactionsThroughPublicAction({ client, limit: 1 }); |
| 92 | + |
| 93 | + expect(result).toEqual({ |
| 94 | + ok: true, |
| 95 | + reactions: [ |
| 96 | + { name: "eyes", count: 3, users: ["U1"] }, |
| 97 | + { name: "wave", count: 2, users: ["U3"] }, |
| 98 | + { name: "heart", count: 5 }, |
| 99 | + { name: "party", count: 0, users: [] }, |
| 100 | + ], |
| 101 | + }); |
| 102 | + expect(calls).toHaveLength(1); |
| 103 | + expect(calls[0]?.method).toBe("reactions.get"); |
| 104 | + expect(calls[0]?.body.get("full")).toBe("true"); |
| 105 | + expect(calls[0]?.body.has("limit")).toBe(false); |
| 106 | + }); |
| 107 | + |
| 108 | + it.each([ |
| 109 | + { name: "omitted", limit: undefined }, |
| 110 | + { name: "larger than the hard cap", limit: 500 }, |
| 111 | + { name: "the largest safe integer", limit: Number.MAX_SAFE_INTEGER }, |
| 112 | + ])("caps $name user limits at 100 users per emoji", async ({ limit }) => { |
| 113 | + const users = Array.from({ length: 101 }, (_, index) => `U${index + 1}`); |
| 114 | + const { client } = createSlackReactionClient([{ name: "eyes", count: 101, users }]); |
| 115 | + |
| 116 | + const result = await readSlackReactionsThroughPublicAction({ client, limit }); |
| 117 | + |
| 118 | + expect(result.reactions).toEqual([{ name: "eyes", count: 101, users: users.slice(0, 100) }]); |
| 119 | + expect(users).toHaveLength(101); |
| 120 | + }); |
| 121 | + |
| 122 | + it.each(["reactions", "read"] as const)( |
| 123 | + "authorizes public %s targets before inspecting malformed limits", |
| 124 | + async (action) => { |
| 125 | + const restrictedConfig = { |
| 126 | + channels: { |
| 127 | + slack: { |
| 128 | + botToken: "xoxb-local-proof", |
| 129 | + groupPolicy: "allowlist", |
| 130 | + channels: { C_ALLOWED: { enabled: true } }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + } as OpenClawConfig; |
| 134 | + const { client, calls } = createSlackReactionClient([]); |
| 135 | + const reactionLookup = vi |
| 136 | + .spyOn(slackActionRuntime, "listSlackReactions") |
| 137 | + .mockImplementation((channelId, messageId, options) => |
| 138 | + listSlackReactions(channelId, messageId, { ...options, client }), |
| 139 | + ); |
| 140 | + const messageLookup = vi.spyOn(slackActionRuntime, "readSlackMessages"); |
| 141 | + |
| 142 | + await expect( |
| 143 | + createSlackActions("slack").handleAction?.({ |
| 144 | + action, |
| 145 | + cfg: restrictedConfig, |
| 146 | + params: { channelId: "C_FORBIDDEN", messageId: "123.456", limit: 0 }, |
| 147 | + } as never), |
| 148 | + ).rejects.toThrow("Slack read target channel is not allowed."); |
| 149 | + expect(reactionLookup).not.toHaveBeenCalled(); |
| 150 | + expect(messageLookup).not.toHaveBeenCalled(); |
| 151 | + expect(calls).toEqual([]); |
| 152 | + }, |
| 153 | + ); |
| 154 | + |
| 155 | + it.each([ |
| 156 | + { action: "reactions", limit: 0 }, |
| 157 | + { action: "reactions", limit: -1 }, |
| 158 | + { action: "reactions", limit: 1.5 }, |
| 159 | + { action: "reactions", limit: Number.NaN }, |
| 160 | + { action: "reactions", limit: Number.POSITIVE_INFINITY }, |
| 161 | + { action: "reactions", limit: Number.NEGATIVE_INFINITY }, |
| 162 | + { action: "reactions", limit: Number.MAX_SAFE_INTEGER + 1 }, |
| 163 | + { action: "read", limit: 0 }, |
| 164 | + ])( |
| 165 | + "rejects invalid public $action limit $limit before Slack API work", |
| 166 | + async ({ action, limit }) => { |
| 167 | + const { client, calls } = createSlackReactionClient([]); |
| 168 | + const conversationLookup = vi |
| 169 | + .spyOn(slackActionRuntime, "resolveSlackConversationInfo") |
| 170 | + .mockResolvedValue({ type: "channel" }); |
| 171 | + const reactionLookup = vi |
| 172 | + .spyOn(slackActionRuntime, "listSlackReactions") |
| 173 | + .mockImplementation((channelId, messageId, options) => |
| 174 | + listSlackReactions(channelId, messageId, { ...options, client }), |
| 175 | + ); |
| 176 | + const messageLookup = vi.spyOn(slackActionRuntime, "readSlackMessages"); |
| 177 | + |
| 178 | + await expect( |
| 179 | + createSlackActions("slack").handleAction?.({ |
| 180 | + action, |
| 181 | + cfg: slackConfig, |
| 182 | + params: { channelId: "C1", messageId: "123.456", limit }, |
| 183 | + } as never), |
| 184 | + ).rejects.toThrow("limit must be a positive integer."); |
| 185 | + expect(conversationLookup).toHaveBeenCalledOnce(); |
| 186 | + expect(reactionLookup).not.toHaveBeenCalled(); |
| 187 | + expect(messageLookup).not.toHaveBeenCalled(); |
| 188 | + expect(calls).toEqual([]); |
| 189 | + }, |
| 190 | + ); |
| 191 | + |
| 192 | + it("preserves all reaction users for existing live approval helper callers", async () => { |
| 193 | + const users = [...Array.from({ length: 100 }, (_, index) => `U${index + 1}`), "U_APPROVER"]; |
| 194 | + const { client } = createSlackReactionClient([ |
| 195 | + { name: "white_check_mark", count: users.length, users }, |
| 196 | + ]); |
| 197 | + |
| 198 | + await expect(listSlackReactions("C1", "123.456", { client })).resolves.toEqual([ |
| 199 | + { name: "white_check_mark", count: users.length, users }, |
| 200 | + ]); |
| 201 | + }); |
| 202 | + |
| 203 | + it("removes the bot's own reaction when its user is beyond the public cap", async () => { |
| 204 | + const users = [...Array.from({ length: 100 }, (_, index) => `U${index + 1}`), "UBOT"]; |
| 205 | + const { client, calls } = createSlackReactionClient([ |
| 206 | + { name: "eyes", count: users.length, users }, |
| 207 | + ]); |
| 208 | + |
| 209 | + await expect(removeOwnSlackReactions("C1", "123.456", { client })).resolves.toEqual(["eyes"]); |
| 210 | + |
| 211 | + expect(calls.map(({ method }) => method)).toEqual([ |
| 212 | + "auth.test", |
| 213 | + "reactions.get", |
| 214 | + "reactions.remove", |
| 215 | + ]); |
| 216 | + expect(calls[2]?.body.get("name")).toBe("eyes"); |
| 217 | + }); |
| 218 | +}); |
0 commit comments