Skip to content

Commit

Permalink
test(alerts): fix Jest tests by using real crypto hashes and adjustin…
Browse files Browse the repository at this point in the history
…g mocks
  • Loading branch information
SaiKumar2121 committed Oct 17, 2024
1 parent 2cac741 commit f64ac62
Showing 1 changed file with 1 addition and 36 deletions.
37 changes: 1 addition & 36 deletions tests/utils/alert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ describe('handleUncaughtExceptions', () => {
});
});

describe('customLoggerAlert2', () => {
describe('customLoggerAlert', () => {
let mockStorageConnection;

beforeEach(() => {
Expand Down Expand Up @@ -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' };
Expand Down

0 comments on commit f64ac62

Please sign in to comment.