|
| 1 | +import { describe, expect, test, beforeEach, vi } from "vitest"; |
| 2 | +import { SlackClientProvider } from "../src/providers/slack-client.provider"; |
| 3 | +import { SlackConfig } from "../src/types/slack-types"; |
| 4 | +import { WebClient } from "@slack/web-api"; |
| 5 | +import type { |
| 6 | + AuthTestResponse, |
| 7 | + ChatPostMessageResponse, |
| 8 | +} from "@slack/web-api"; |
| 9 | + |
| 10 | +vi.mock("@slack/web-api"); |
| 11 | + |
| 12 | +// Mock setup functions |
| 13 | +const createMockSlackResponse = (ok: boolean, additionalData = {}) => ({ |
| 14 | + ok, |
| 15 | + ...additionalData, |
| 16 | +}); |
| 17 | + |
| 18 | +const getMockWebClient = () => { |
| 19 | + return { |
| 20 | + auth: { |
| 21 | + test: vi.fn(), |
| 22 | + }, |
| 23 | + chat: { |
| 24 | + postMessage: vi.fn(), |
| 25 | + }, |
| 26 | + } as unknown as WebClient; |
| 27 | +}; |
| 28 | + |
| 29 | +describe("SlackClientProvider", () => { |
| 30 | + let provider: SlackClientProvider; |
| 31 | + let mockWebClient: WebClient; |
| 32 | + let mockConfig: SlackConfig; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + vi.clearAllMocks(); |
| 36 | + mockConfig = { |
| 37 | + appId: "test-app-id", |
| 38 | + clientId: "test-client-id", |
| 39 | + clientSecret: "test-client-secret", |
| 40 | + signingSecret: "test-signing-secret", |
| 41 | + verificationToken: "test-verification-token", |
| 42 | + botToken: "test-bot-token", |
| 43 | + botId: "test-bot-id", |
| 44 | + }; |
| 45 | + mockWebClient = getMockWebClient(); |
| 46 | + provider = new SlackClientProvider(mockConfig); |
| 47 | + // @ts-ignore - setting mock client for testing |
| 48 | + provider['client'] = mockWebClient; |
| 49 | + }); |
| 50 | + |
| 51 | + describe("Initialization", () => { |
| 52 | + test("should create a provider instance with default retry options", () => { |
| 53 | + expect(provider).toBeInstanceOf(SlackClientProvider); |
| 54 | + const context = provider.getContext(); |
| 55 | + expect(context).toHaveProperty("client"); |
| 56 | + expect(context).toHaveProperty("config"); |
| 57 | + expect(context.config).toEqual(mockConfig); |
| 58 | + }); |
| 59 | + |
| 60 | + test("should create a provider instance with custom retry options", () => { |
| 61 | + const retryOptions = { |
| 62 | + maxRetries: 5, |
| 63 | + initialDelay: 2000, |
| 64 | + maxDelay: 10000, |
| 65 | + }; |
| 66 | + const providerWithOptions = new SlackClientProvider(mockConfig, retryOptions); |
| 67 | + // @ts-ignore - setting mock client for testing |
| 68 | + providerWithOptions['client'] = mockWebClient; |
| 69 | + |
| 70 | + expect(providerWithOptions).toBeInstanceOf(SlackClientProvider); |
| 71 | + const context = providerWithOptions.getContext(); |
| 72 | + expect(context).toHaveProperty("client"); |
| 73 | + expect(context).toHaveProperty("config"); |
| 74 | + expect(context.config).toEqual(mockConfig); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe("Connection Validation", () => { |
| 79 | + test("should validate connection successfully", async () => { |
| 80 | + const mockResponse = createMockSlackResponse(true, { |
| 81 | + user_id: "test-bot-id", |
| 82 | + }) as AuthTestResponse; |
| 83 | + const mockTest = mockWebClient.auth.test as vi.Mock; |
| 84 | + mockTest.mockResolvedValue(mockResponse); |
| 85 | + |
| 86 | + const result = await provider.validateConnection(); |
| 87 | + expect(result).toBe(true); |
| 88 | + }); |
| 89 | + |
| 90 | + test("should handle failed validation", async () => { |
| 91 | + const mockResponse = createMockSlackResponse(false) as AuthTestResponse; |
| 92 | + const mockTest = mockWebClient.auth.test as vi.Mock; |
| 93 | + mockTest.mockResolvedValue(mockResponse); |
| 94 | + |
| 95 | + const result = await provider.validateConnection(); |
| 96 | + expect(result).toBe(false); |
| 97 | + }); |
| 98 | + |
| 99 | + test("should handle connection errors", async () => { |
| 100 | + const mockTest = mockWebClient.auth.test as vi.Mock; |
| 101 | + mockTest.mockRejectedValue(new Error("Connection failed")); |
| 102 | + |
| 103 | + const result = await provider.validateConnection(); |
| 104 | + expect(result).toBe(false); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe("Message Sending", () => { |
| 109 | + const channelId = "test-channel"; |
| 110 | + const text = "Hello, world!"; |
| 111 | + |
| 112 | + test("should successfully send a message", async () => { |
| 113 | + const expectedResponse = createMockSlackResponse(true, { |
| 114 | + ts: "1234567890.123456", |
| 115 | + }) as ChatPostMessageResponse; |
| 116 | + const mockPostMessage = mockWebClient.chat.postMessage as vi.Mock; |
| 117 | + mockPostMessage.mockResolvedValue(expectedResponse); |
| 118 | + |
| 119 | + const result = await provider.sendMessage(channelId, text); |
| 120 | + expect(result.ok).toBe(true); |
| 121 | + expect(mockPostMessage).toHaveBeenCalledWith({ |
| 122 | + channel: channelId, |
| 123 | + text: text, |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + test("should handle rate limiting", async () => { |
| 128 | + const mockResponse = createMockSlackResponse(true) as ChatPostMessageResponse; |
| 129 | + const mockPostMessage = mockWebClient.chat.postMessage as vi.Mock; |
| 130 | + |
| 131 | + mockPostMessage |
| 132 | + .mockRejectedValueOnce(new Error("rate_limited")) |
| 133 | + .mockResolvedValueOnce(mockResponse); |
| 134 | + |
| 135 | + const result = await provider.sendMessage(channelId, text); |
| 136 | + expect(result.ok).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + test("should handle network errors with retry", async () => { |
| 140 | + const mockResponse = createMockSlackResponse(true) as ChatPostMessageResponse; |
| 141 | + const mockPostMessage = mockWebClient.chat.postMessage as vi.Mock; |
| 142 | + |
| 143 | + mockPostMessage |
| 144 | + .mockRejectedValueOnce(new Error("network_error")) |
| 145 | + .mockResolvedValueOnce(mockResponse); |
| 146 | + |
| 147 | + const result = await provider.sendMessage(channelId, text); |
| 148 | + expect(result.ok).toBe(true); |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments