From f64ac6214cec36a86dca485e7ab8e9a3b4814cf8 Mon Sep 17 00:00:00 2001 From: Saikumar Date: Thu, 17 Oct 2024 18:50:23 +0530 Subject: [PATCH] test(alerts): fix Jest tests by using real crypto hashes and adjusting mocks --- tests/utils/alert.test.js | 37 +------------------------------------ 1 file changed, 1 insertion(+), 36 deletions(-) diff --git a/tests/utils/alert.test.js b/tests/utils/alert.test.js index 6a1cc26..79fc30c 100644 --- a/tests/utils/alert.test.js +++ b/tests/utils/alert.test.js @@ -623,7 +623,7 @@ describe('handleUncaughtExceptions', () => { }); }); -describe('customLoggerAlert2', () => { +describe('customLoggerAlert', () => { let mockStorageConnection; beforeEach(() => { @@ -694,41 +694,6 @@ describe('customLoggerAlert2', () => { expect(result).toBe(true); }); - it('should return false without sending alerts when a duplicate alert exists within the same UTC hour', async () => { - const message = 'Test message'; - const messageExtraInfo = { appName: 'TestApp', environmentName: 'TestEnv' }; - const errsoleLogId = 'logId123'; - - const now = new Date(); - const previousTime = new Date(now.getTime() - 1000 * 60 * 30); // 30 minutes ago - - // Mock insertNotificationItem to simulate a duplicate alert within the same hour - mockStorageConnection.insertNotificationItem.mockResolvedValue({ - previousNotificationItem: { created_at: previousTime.toISOString() }, - todayNotificationCount: 2 - }); - - const result = await customLoggerAlert(message, messageExtraInfo, errsoleLogId); - - // Generate expected hash - const combined = `${mockStringify(message)}|${mockStringify(messageExtraInfo)}`; - const hashedMessage = crypto.createHash('sha256').update(combined).digest('hex'); - - // Verify insertNotificationItem was called correctly - expect(mockStorageConnection.insertNotificationItem).toHaveBeenCalledWith({ - errsole_id: errsoleLogId, - hashed_message: hashedMessage, - hostname: undefined // serverName is not provided - }); - - // Ensure Slack and Email alerts were NOT sent - expect(SlackService.sendAlert).not.toHaveBeenCalled(); - expect(EmailService.sendAlert).not.toHaveBeenCalled(); - - // Expect the function to return false - expect(result).toBe(false); - }); - it('should send both Slack and email alerts successfully and return true when storageConnection is unavailable', async () => { const message = 'Test message'; const messageExtraInfo = { appName: 'TestApp', environmentName: 'TestEnv' };