|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { triggerAlert } from '../monitor/alerts'; |
| 3 | +import { logger } from '../utils/logger'; |
| 4 | +import { getConfig } from '../utils/config'; |
| 5 | + |
| 6 | +// Mock config |
| 7 | +vi.mock('../utils/config', () => ({ |
| 8 | + getConfig: vi.fn(() => ({ |
| 9 | + alert_cooldown: 300, |
| 10 | + discord_webhook: 'https://discord.com/api/webhooks/dummy', |
| 11 | + })), |
| 12 | + getSMTPSettings: vi.fn(() => ({ |
| 13 | + host: '', |
| 14 | + port: 587, |
| 15 | + auth: { user: '', pass: '' }, |
| 16 | + to: '', |
| 17 | + })), |
| 18 | +})); |
| 19 | + |
| 20 | +// Mock logger |
| 21 | +vi.mock('../utils/logger', () => ({ |
| 22 | + logger: { |
| 23 | + info: vi.fn(), |
| 24 | + error: vi.fn(), |
| 25 | + }, |
| 26 | +})); |
| 27 | + |
| 28 | +// Mock fetch globally |
| 29 | +const mockFetch = vi.fn().mockResolvedValue({ ok: true }); |
| 30 | +global.fetch = mockFetch; |
| 31 | + |
| 32 | +describe('alerts monitoring', () => { |
| 33 | + beforeEach(() => { |
| 34 | + vi.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should trigger alert on first invocation', async () => { |
| 38 | + const alertId = 'container:test-first:failure'; |
| 39 | + await triggerAlert({ |
| 40 | + id: alertId, |
| 41 | + message: 'Container crashed', |
| 42 | + type: 'container', |
| 43 | + severity: 'critical', |
| 44 | + }); |
| 45 | + |
| 46 | + expect(logger.info).toHaveBeenCalledWith(expect.stringContaining(`Triggering alert for ${alertId}`)); |
| 47 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should respect cooldown and suppress alert on subsequent invocations within cooldown window', async () => { |
| 51 | + const alertId = 'container:test-cooldown:failure'; |
| 52 | + |
| 53 | + // First trigger - should send |
| 54 | + await triggerAlert({ |
| 55 | + id: alertId, |
| 56 | + message: 'Container crashed first time', |
| 57 | + type: 'container', |
| 58 | + severity: 'critical', |
| 59 | + }); |
| 60 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 61 | + |
| 62 | + // Second trigger within cooldown - should be suppressed |
| 63 | + await triggerAlert({ |
| 64 | + id: alertId, |
| 65 | + message: 'Container crashed second time', |
| 66 | + type: 'container', |
| 67 | + severity: 'critical', |
| 68 | + }); |
| 69 | + // Call count should still be 1 |
| 70 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should bypass cooldown and trigger alert on subsequent invocations if force option is true', async () => { |
| 74 | + const alertId = 'container:test-force:failure'; |
| 75 | + |
| 76 | + // First trigger - should send |
| 77 | + await triggerAlert({ |
| 78 | + id: alertId, |
| 79 | + message: 'Container crashed first time', |
| 80 | + type: 'container', |
| 81 | + severity: 'critical', |
| 82 | + }); |
| 83 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 84 | + |
| 85 | + // Second trigger with force: true - should send regardless of cooldown |
| 86 | + await triggerAlert({ |
| 87 | + id: alertId, |
| 88 | + message: 'Container crashed second time', |
| 89 | + type: 'container', |
| 90 | + severity: 'critical', |
| 91 | + }, { force: true }); |
| 92 | + |
| 93 | + // Call count should be 2 now |
| 94 | + expect(mockFetch).toHaveBeenCalledTimes(2); |
| 95 | + }); |
| 96 | +}); |
0 commit comments