import { describe, it, expect, beforeEach, vi } from 'vitest'; import { DiscordNotificationAdapter } from './DiscordNotificationGateway'; import { Notification } from '@core/notifications/domain/entities/Notification'; describe('DiscordNotificationAdapter', () => { const webhookUrl = 'https://discord.com/api/webhooks/123/abc'; let adapter: DiscordNotificationAdapter; beforeEach(() => { adapter = new DiscordNotificationAdapter({ webhookUrl }); vi.spyOn(console, 'log').mockImplementation(() => {}); }); const createNotification = (overrides: any = {}) => { return Notification.create({ id: 'notif-123', recipientId: 'driver-456', type: 'protest_filed', title: 'New Protest', body: 'A new protest has been filed against you.', channel: 'discord', ...overrides, }); }; describe('send', () => { it('should return success when configured', async () => { // Given const notification = createNotification(); // When const result = await adapter.send(notification); // Then expect(result.success).toBe(true); expect(result.channel).toBe('discord'); expect(result.externalId).toContain('discord-stub-'); expect(result.attemptedAt).toBeInstanceOf(Date); }); it('should return failure when not configured', async () => { // Given const unconfiguredAdapter = new DiscordNotificationAdapter(); const notification = createNotification(); // When const result = await unconfiguredAdapter.send(notification); // Then expect(result.success).toBe(false); expect(result.error).toBe('Discord webhook URL not configured'); }); }); describe('supportsChannel', () => { it('should return true for discord channel', () => { expect(adapter.supportsChannel('discord')).toBe(true); }); it('should return false for other channels', () => { expect(adapter.supportsChannel('email' as any)).toBe(false); }); }); describe('isConfigured', () => { it('should return true when webhookUrl is set', () => { expect(adapter.isConfigured()).toBe(true); }); it('should return false when webhookUrl is missing', () => { const unconfigured = new DiscordNotificationAdapter(); expect(unconfigured.isConfigured()).toBe(false); }); }); describe('setWebhookUrl', () => { it('should update the webhook URL', () => { const unconfigured = new DiscordNotificationAdapter(); unconfigured.setWebhookUrl(webhookUrl); expect(unconfigured.isConfigured()).toBe(true); }); }); });