|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +import { NotificationConnector } from '@/connectors/notification/notification-connector'; |
| 4 | +import type { ITelegramConnector } from '@/connectors/telegram/interfaces/telegram-connector.interface'; |
| 5 | + |
| 6 | +describe('NotificationConnector', () => { |
| 7 | + let notificationConnector: NotificationConnector; |
| 8 | + let mockTelegramConnector: ITelegramConnector; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + vi.clearAllMocks(); |
| 12 | + |
| 13 | + mockTelegramConnector = { |
| 14 | + sendMessage: vi.fn().mockResolvedValue(undefined), |
| 15 | + } as unknown as ITelegramConnector; |
| 16 | + |
| 17 | + notificationConnector = new NotificationConnector(mockTelegramConnector); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('sendNotification', () => { |
| 21 | + it('should send notification successfully', async () => { |
| 22 | + const result = await notificationConnector.sendNotification(123456, 'Test message'); |
| 23 | + |
| 24 | + expect(result).toBe(true); |
| 25 | + expect(mockTelegramConnector.sendMessage).toHaveBeenCalledWith(123456, 'Test message'); |
| 26 | + expect(mockTelegramConnector.sendMessage).toHaveBeenCalledTimes(1); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should retry on failure', async () => { |
| 30 | + vi.mocked(mockTelegramConnector.sendMessage) |
| 31 | + .mockRejectedValueOnce(new Error('Temporary error')) |
| 32 | + .mockRejectedValueOnce(new Error('Temporary error')) |
| 33 | + .mockResolvedValueOnce(undefined); |
| 34 | + |
| 35 | + const result = await notificationConnector.sendNotification(123456, 'Test message'); |
| 36 | + |
| 37 | + expect(result).toBe(true); |
| 38 | + expect(mockTelegramConnector.sendMessage).toHaveBeenCalledTimes(3); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should return false after max retries', async () => { |
| 42 | + vi.mocked(mockTelegramConnector.sendMessage).mockRejectedValue(new Error('Persistent error')); |
| 43 | + |
| 44 | + const result = await notificationConnector.sendNotification(123456, 'Test message'); |
| 45 | + |
| 46 | + expect(result).toBe(false); |
| 47 | + expect(mockTelegramConnector.sendMessage).toHaveBeenCalledTimes(3); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should not retry if user blocked the bot', async () => { |
| 51 | + vi.mocked(mockTelegramConnector.sendMessage).mockRejectedValueOnce( |
| 52 | + new Error('Bot was blocked by the user'), |
| 53 | + ); |
| 54 | + |
| 55 | + const result = await notificationConnector.sendNotification(123456, 'Test message'); |
| 56 | + |
| 57 | + expect(result).toBe(false); |
| 58 | + expect(mockTelegramConnector.sendMessage).toHaveBeenCalledTimes(1); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should detect various blocked user errors', async () => { |
| 62 | + const blockedErrors = [ |
| 63 | + 'Bot was blocked by the user', |
| 64 | + 'User is deactivated', |
| 65 | + 'Chat not found', |
| 66 | + 'Forbidden: bot was blocked', |
| 67 | + ]; |
| 68 | + |
| 69 | + for (const errorMessage of blockedErrors) { |
| 70 | + vi.clearAllMocks(); |
| 71 | + vi.mocked(mockTelegramConnector.sendMessage).mockRejectedValueOnce(new Error(errorMessage)); |
| 72 | + |
| 73 | + const result = await notificationConnector.sendNotification(123456, 'Test message'); |
| 74 | + |
| 75 | + expect(result).toBe(false); |
| 76 | + expect(mockTelegramConnector.sendMessage).toHaveBeenCalledTimes(1); |
| 77 | + } |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('sendBatchNotifications', () => { |
| 82 | + it('should send notifications in batches', async () => { |
| 83 | + const notifications = Array.from({ length: 75 }, (_, i) => ({ |
| 84 | + userId: 100 + i, |
| 85 | + message: `Message ${i}`, |
| 86 | + })); |
| 87 | + |
| 88 | + await notificationConnector.sendBatchNotifications(notifications); |
| 89 | + |
| 90 | + // Should be called 75 times (once for each notification) |
| 91 | + expect(mockTelegramConnector.sendMessage).toHaveBeenCalledTimes(75); |
| 92 | + |
| 93 | + // Check first and last calls |
| 94 | + expect(mockTelegramConnector.sendMessage).toHaveBeenNthCalledWith(1, 100, 'Message 0'); |
| 95 | + expect(mockTelegramConnector.sendMessage).toHaveBeenNthCalledWith(75, 174, 'Message 74'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should handle failures gracefully in batch', async () => { |
| 99 | + // Set up mixed success/failure responses |
| 100 | + vi.mocked(mockTelegramConnector.sendMessage) |
| 101 | + .mockResolvedValueOnce(undefined) // First message success |
| 102 | + .mockRejectedValue(new Error('Failed')); // All others fail |
| 103 | + |
| 104 | + const notifications = [ |
| 105 | + { userId: 111, message: 'Message 1' }, |
| 106 | + { userId: 222, message: 'Message 2' }, |
| 107 | + { userId: 333, message: 'Message 3' }, |
| 108 | + ]; |
| 109 | + |
| 110 | + // Just verify it completes without throwing |
| 111 | + await expect( |
| 112 | + notificationConnector.sendBatchNotifications(notifications), |
| 113 | + ).resolves.toBeUndefined(); |
| 114 | + |
| 115 | + // Verify at least the first successful call was made |
| 116 | + expect(mockTelegramConnector.sendMessage).toHaveBeenCalledWith(111, 'Message 1'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should delay between batches', async () => { |
| 120 | + const notifications = Array.from({ length: 31 }, (_, i) => ({ |
| 121 | + userId: 100 + i, |
| 122 | + message: `Message ${i}`, |
| 123 | + })); |
| 124 | + |
| 125 | + const start = Date.now(); |
| 126 | + await notificationConnector.sendBatchNotifications(notifications); |
| 127 | + const duration = Date.now() - start; |
| 128 | + |
| 129 | + // Should have delayed at least 1000ms between batches |
| 130 | + expect(duration).toBeGreaterThanOrEqual(1000); |
| 131 | + expect(mockTelegramConnector.sendMessage).toHaveBeenCalledTimes(31); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should handle empty notifications array', async () => { |
| 135 | + await notificationConnector.sendBatchNotifications([]); |
| 136 | + |
| 137 | + expect(mockTelegramConnector.sendMessage).not.toHaveBeenCalled(); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments