/** * Unit tests for the queue consumer handlers. * * The downstream side effects are mocked, so nothing is sent and no notification * implementation runs: * - `sendAdminNotification` is a jest.fn (asserted, never executed) * - `handleOrderPlaced` / `handleOrderCancelled` are jest.fn (asserted, never executed) */ jest.mock('@/src/lib/notif-job', () => ({ sendAdminNotification: jest.fn(async () => undefined), })) jest.mock('@/src/lib/post-order-handler', () => ({ handleOrderPlaced: jest.fn(async () => undefined), handleOrderCancelled: jest.fn(async () => undefined), })) import { handleNotifQueue, handleOrderCancelledQueue, handleOrderPlacedQueue, } from '@/src/lib/queue-consumer' import { sendAdminNotification } from '@/src/lib/notif-job' import { handleOrderCancelled, handleOrderPlaced } from '@/src/lib/post-order-handler' const mockedSendAdminNotification = sendAdminNotification as jest.MockedFunction< typeof sendAdminNotification > const mockedHandleOrderPlaced = handleOrderPlaced as jest.MockedFunction const mockedHandleOrderCancelled = handleOrderCancelled as jest.MockedFunction< typeof handleOrderCancelled > function batch(...messages: any[]): any { return { messages: messages.map((body) => ({ body })) } } beforeEach(() => { jest.clearAllMocks() jest.spyOn(console, 'log').mockImplementation(() => undefined) }) afterEach(() => { jest.restoreAllMocks() }) describe('handleNotifQueue', () => { const payload = { name: 'send-admin-notification', jobData: { tokens: ['tok-1', 'tok-2'], title: 'New order', body: 'Order 41', }, } it('calls sendAdminNotification with the payload and null fallbacks', async () => { await handleNotifQueue(batch(payload)) expect(mockedSendAdminNotification).toHaveBeenCalledTimes(1) expect(mockedSendAdminNotification).toHaveBeenCalledWith({ tokens: ['tok-1', 'tok-2'], title: 'New order', body: 'Order 41', imageUrl: null, orderId: null, complaintId: null, }) }) it('passes imageUrl and orderId when present', async () => { await handleNotifQueue( batch({ ...payload, jobData: { ...payload.jobData, imageUrl: 'https://x/y.png', orderId: 41 }, }) ) expect(mockedSendAdminNotification).toHaveBeenCalledWith( expect.objectContaining({ imageUrl: 'https://x/y.png', orderId: 41 }) ) }) it('passes complaintId when present', async () => { await handleNotifQueue( batch({ ...payload, jobData: { ...payload.jobData, complaintId: 9 }, }) ) expect(mockedSendAdminNotification).toHaveBeenCalledWith( expect.objectContaining({ complaintId: 9 }) ) }) it('processes only the valid message in a mixed batch', async () => { await handleNotifQueue( batch( null, { name: 'other-job', jobData: { tokens: ['x'] } }, { name: 'send-admin-notification', jobData: { tokens: [] } }, payload ) ) expect(mockedSendAdminNotification).toHaveBeenCalledTimes(1) }) it('does nothing for empty bodies or missing tokens', async () => { await handleNotifQueue( batch( null, {}, { name: 'send-admin-notification' }, { name: 'send-admin-notification', jobData: { tokens: [] } } ) ) expect(mockedSendAdminNotification).not.toHaveBeenCalled() }) it('handles a batch with no messages', async () => { await handleNotifQueue({}) expect(mockedSendAdminNotification).not.toHaveBeenCalled() }) it('propagates a rejection so the queue retries', async () => { mockedSendAdminNotification.mockRejectedValueOnce(new Error('push failed')) await expect(handleNotifQueue(batch(payload))).rejects.toThrow('push failed') }) }) describe('handleOrderPlacedQueue', () => { it('delegates orderIds to handleOrderPlaced', async () => { await handleOrderPlacedQueue(batch({ orderIds: [10, 11] })) expect(mockedHandleOrderPlaced).toHaveBeenCalledWith([10, 11]) }) it('processes each valid message', async () => { await handleOrderPlacedQueue(batch({ orderIds: [1] }, { orderIds: [2] })) expect(mockedHandleOrderPlaced).toHaveBeenNthCalledWith(1, [1]) expect(mockedHandleOrderPlaced).toHaveBeenNthCalledWith(2, [2]) }) it('skips empty bodies and non-array orderIds', async () => { await handleOrderPlacedQueue( batch(null, {}, { orderIds: 'nope' }, { orderIds: null }, { other: true }) ) expect(mockedHandleOrderPlaced).not.toHaveBeenCalled() }) it('handles a batch with no messages', async () => { await handleOrderPlacedQueue({}) expect(mockedHandleOrderPlaced).not.toHaveBeenCalled() }) it('propagates a rejection so the queue retries', async () => { mockedHandleOrderPlaced.mockRejectedValueOnce(new Error('placed failed')) await expect(handleOrderPlacedQueue(batch({ orderIds: [1] }))).rejects.toThrow( 'placed failed' ) }) }) describe('handleOrderCancelledQueue', () => { const body = { orderId: 77, cancelledBy: 'user', reason: 'changed mind', cancelledAt: '2026-10-20T10:00:00.000Z', } it('delegates the mapped cancellation payload', async () => { await handleOrderCancelledQueue(batch(body)) expect(mockedHandleOrderCancelled).toHaveBeenCalledTimes(1) expect(mockedHandleOrderCancelled).toHaveBeenCalledWith({ orderId: 77, cancelledBy: 'user', reason: 'changed mind', cancelledAt: '2026-10-20T10:00:00.000Z', }) }) it('processes each valid message', async () => { await handleOrderCancelledQueue( batch(body, { ...body, orderId: 78, reason: 'other' }) ) expect(mockedHandleOrderCancelled).toHaveBeenNthCalledWith( 1, expect.objectContaining({ orderId: 77 }) ) expect(mockedHandleOrderCancelled).toHaveBeenNthCalledWith( 2, expect.objectContaining({ orderId: 78, reason: 'other' }) ) }) it('skips empty bodies and messages without an orderId', async () => { await handleOrderCancelledQueue( batch(null, {}, { orderId: 0 }, { reason: 'no id', cancelledBy: 'user' }) ) expect(mockedHandleOrderCancelled).not.toHaveBeenCalled() }) it('handles a batch with no messages', async () => { await handleOrderCancelledQueue({}) expect(mockedHandleOrderCancelled).not.toHaveBeenCalled() }) it('propagates a rejection so the queue retries', async () => { mockedHandleOrderCancelled.mockRejectedValueOnce(new Error('cancel failed')) await expect(handleOrderCancelledQueue(batch(body))).rejects.toThrow('cancel failed') }) })