import { SQSEvent, SQSRecord } from 'aws-lambda'; import { getRecordMessage } from '../../helper'; const tenantId = 'tenant-swg-compute-001'; const userId = 'user-swg-compute-001'; const eventTypeComputeProfile = 'compute.swg.profile'; describe('test compute-profile handler', () => { beforeEach(() => { jest.resetModules(); // Clear the module registry to ensure fresh mocks jest.clearAllMocks(); }); it('test001 should process successfully a message of type compute.swg.profile', async () => { let receivedTenantId: string | undefined; let receivedUsers: string[] | undefined; jest.doMock('@p81-common/swg-profile-compute', () => { return { UserSWGProfileService: jest.fn().mockImplementation(() => ({ compute: jest.fn((tenantId, users) => { receivedTenantId = tenantId; receivedUsers = users; }), })), }; }); const { UserSWGProfileService } = await import('@p81-common/swg-profile-compute'); // Re-import the UserSWGProfileService after mocking const { handler } = await import('@functions/swg-lambda-compute-profile'); // Re-import the handler after mocking const recordMessageSuccess: SQSRecord = getRecordMessage({ messageId: '1', body: { tenantId, users: [userId], }, eventTypeValue: eventTypeComputeProfile, }); const sqsEvent: SQSEvent = { Records: [recordMessageSuccess], }; const res = await handler(sqsEvent); expect(res).toStrictEqual({ batchItemFailures: [], }); expect(UserSWGProfileService).toHaveBeenCalledTimes(1); expect(receivedTenantId).toBe(tenantId); expect(receivedUsers).toEqual([userId]); }); it('test002 should skip invalid message', async () => { jest.doMock('@p81-common/swg-profile-compute', () => { return { UserSWGProfileService: jest.fn().mockImplementation(() => ({ compute: jest.fn(() => {}), })), }; }); const { UserSWGProfileService } = await import('@p81-common/swg-profile-compute'); const { handler } = await import('@functions/swg-lambda-compute-profile'); const recordMessageSuccess: SQSRecord = getRecordMessage({ messageId: '1', body: { invalidBody: true, }, eventTypeValue: eventTypeComputeProfile, }); const sqsEvent: SQSEvent = { Records: [recordMessageSuccess], }; await handler(sqsEvent); expect(UserSWGProfileService).not.toHaveBeenCalled(); }); it('test003 should handle errors', async () => { jest.doMock('@p81-common/swg-profile-compute', () => { return { UserSWGProfileService: jest.fn().mockImplementation(() => ({ compute: jest.fn(() => { throw new Error('Test error'); }), })), }; }); await import('@p81-common/swg-profile-compute'); // Re-import the UserSWGProfileService after mocking const { handler } = await import('@functions/swg-lambda-compute-profile'); // Re-import the handler after mocking const recordMessageSuccess: SQSRecord = getRecordMessage({ messageId: '1', body: { tenantId, users: [userId], }, eventTypeValue: eventTypeComputeProfile, }); const sqsEvent: SQSEvent = { Records: [recordMessageSuccess], }; const res = await handler(sqsEvent); expect(res).toEqual({ batchItemFailures: [ { itemIdentifier: '1', }, ], }); }); });