|
| 1 | +import { createHandler } from 'apis/sqs-handler'; |
| 2 | +import { Logger } from 'utils'; |
| 3 | +import { SQSEvent, SQSRecord } from 'aws-lambda'; |
| 4 | +import { mock } from 'jest-mock-extended'; |
| 5 | + |
| 6 | +const logger = mock<Logger>(); |
| 7 | + |
| 8 | +const event = { |
| 9 | + sourceEventId: 'test-event-id', |
| 10 | +}; |
| 11 | + |
| 12 | +const sqsRecord1: SQSRecord = { |
| 13 | + messageId: '1', |
| 14 | + receiptHandle: 'abc', |
| 15 | + body: JSON.stringify(event), |
| 16 | + attributes: { |
| 17 | + ApproximateReceiveCount: '1', |
| 18 | + SentTimestamp: '2025-07-03T14:23:30Z', |
| 19 | + SenderId: 'sender-id', |
| 20 | + ApproximateFirstReceiveTimestamp: '2025-07-03T14:23:30Z', |
| 21 | + }, |
| 22 | + messageAttributes: {}, |
| 23 | + md5OfBody: '', |
| 24 | + eventSource: 'aws:sqs', |
| 25 | + eventSourceARN: '', |
| 26 | + awsRegion: '', |
| 27 | +}; |
| 28 | + |
| 29 | +const singleRecordEvent: SQSEvent = { |
| 30 | + Records: [sqsRecord1], |
| 31 | +}; |
| 32 | + |
| 33 | +const handler = createHandler({ |
| 34 | + logger, |
| 35 | +}); |
| 36 | + |
| 37 | +describe('SQS Handler', () => { |
| 38 | + beforeEach(() => { |
| 39 | + jest.clearAllMocks(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('processes a single record', async () => { |
| 43 | + const response = await handler(singleRecordEvent); |
| 44 | + |
| 45 | + expect(logger.info).toHaveBeenCalledWith( |
| 46 | + 'Received SQS Event of 1 record(s)', |
| 47 | + ); |
| 48 | + expect(logger.info).toHaveBeenCalledWith( |
| 49 | + '1 of 1 records processed successfully', |
| 50 | + ); |
| 51 | + expect(response).toEqual({ batchItemFailures: [] }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should return failed items to the queue if an error occurs while processing them', async () => { |
| 55 | + singleRecordEvent.Records[0].body = 'not-json'; |
| 56 | + |
| 57 | + const result = await handler(singleRecordEvent); |
| 58 | + |
| 59 | + expect(logger.warn).toHaveBeenCalledWith({ |
| 60 | + error: `Unexpected token 'o', "not-json" is not valid JSON`, |
| 61 | + description: 'Failed processing message', |
| 62 | + messageId: '1', |
| 63 | + }); |
| 64 | + |
| 65 | + expect(logger.info).toHaveBeenCalledWith( |
| 66 | + '0 of 1 records processed successfully', |
| 67 | + ); |
| 68 | + |
| 69 | + expect(result).toEqual({ |
| 70 | + batchItemFailures: [{ itemIdentifier: '1' }], |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
0 commit comments