|
| 1 | +import type { WebClient } from "@slack/web-api"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { reactSlackMessage } from "./actions.js"; |
| 4 | + |
| 5 | +function createClient() { |
| 6 | + return { |
| 7 | + reactions: { |
| 8 | + add: vi.fn(async () => ({})), |
| 9 | + }, |
| 10 | + } as unknown as WebClient & { |
| 11 | + reactions: { |
| 12 | + add: ReturnType<typeof vi.fn>; |
| 13 | + }; |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +function slackPlatformError(error: string) { |
| 18 | + return Object.assign(new Error(`An API error occurred: ${error}`), { |
| 19 | + data: { |
| 20 | + ok: false, |
| 21 | + error, |
| 22 | + }, |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +describe("reactSlackMessage", () => { |
| 27 | + it("treats already_reacted as idempotent success", async () => { |
| 28 | + const client = createClient(); |
| 29 | + client.reactions.add.mockRejectedValueOnce(slackPlatformError("already_reacted")); |
| 30 | + |
| 31 | + await expect( |
| 32 | + reactSlackMessage("C1", "123.456", ":white_check_mark:", { |
| 33 | + client, |
| 34 | + token: "xoxb-test", |
| 35 | + }), |
| 36 | + ).resolves.toBeUndefined(); |
| 37 | + |
| 38 | + expect(client.reactions.add).toHaveBeenCalledWith({ |
| 39 | + channel: "C1", |
| 40 | + timestamp: "123.456", |
| 41 | + name: "white_check_mark", |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it("propagates unrelated reaction add errors", async () => { |
| 46 | + const client = createClient(); |
| 47 | + client.reactions.add.mockRejectedValueOnce(slackPlatformError("invalid_name")); |
| 48 | + |
| 49 | + await expect( |
| 50 | + reactSlackMessage("C1", "123.456", "not-an-emoji", { |
| 51 | + client, |
| 52 | + token: "xoxb-test", |
| 53 | + }), |
| 54 | + ).rejects.toMatchObject({ |
| 55 | + data: { |
| 56 | + error: "invalid_name", |
| 57 | + }, |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
0 commit comments