import { BadRequestException } from '@nestjs/common'; import { GatewayCallbackController } from './gateway-callback.controller'; describe('GatewayCallbackController', () => { const sendChain = { handleSubmitResult: jest.fn(), handleSubmitSegmentResult: jest.fn(), intakeReceipt: jest.fn(), handleReceipt: jest.fn(), handleUplink: jest.fn(), recordGatewaySubmitDeadLetter: jest.fn(), }; const protocolLogs = { record: jest.fn() }; const prisma = { $queryRaw: jest.fn(), getPoolState: jest.fn().mockReturnValue({ max: 16, total: 2, idle: 1, waiting: 0 }) }; const controller = new GatewayCallbackController(sendChain as never, protocolLogs as never, prisma as never); beforeEach(() => { jest.clearAllMocks(); process.env.PROTOCOL_LOG_CALLBACK_TRACKING_ENABLED = 'true'; }); afterAll(() => { delete process.env.PROTOCOL_LOG_CALLBACK_TRACKING_ENABLED; }); it('keeps Submit result persistence on the callback process without duplicating protocol logs', async () => { sendChain.handleSubmitResult.mockResolvedValue({ accepted: true }); await expect(controller.submitResult({ messageId: 'MSG-1', channelId: 'channel-1', gatewayMessageId: '1', sequenceId: 1, submitStatus: 'accepted', })).resolves.toEqual({ accepted: true }); expect(sendChain.handleSubmitResult).toHaveBeenCalledTimes(1); expect(protocolLogs.record).not.toHaveBeenCalled(); }); it('durably intakes receipts before recording the callback protocol event', async () => { sendChain.intakeReceipt.mockResolvedValue({ accepted: true, inboxId: 'inbox-1' }); await controller.receiptIntake({ messageId: 'MSG-1', channelId: 'channel-1', gatewayMessageId: '1', phoneNumber: '13800000001', receiptStatus: 'delivered', rawStatus: 'DELIVRD', }); expect(sendChain.intakeReceipt).toHaveBeenCalledTimes(1); expect(protocolLogs.record).toHaveBeenCalledWith(expect.objectContaining({ eventType: 'deliver_receipt', messageId: 'MSG-1', status: 'success', })); }); it('rejects inbound/client packet logs from the isolated supplier callback surface', () => { expect(() => controller.protocolLog({ protocol: 'cmpp', direction: 'client_to_platform', eventType: 'submit', status: 'success', })).toThrow(BadRequestException); }); it('returns an independent result for every event in a callback batch', async () => { sendChain.handleSubmitResult.mockResolvedValue({ accepted: true }); sendChain.handleUplink.mockResolvedValue({ accepted: true }); await expect(controller.batch({ batchId: 'CB-1', gatewayInstanceId: 'gateway-1', events: [ { eventId: 'EV-1', type: 'submit_result', payload: { messageId: 'MSG-1', channelId: 'channel-1', submitStatus: 'accepted' } }, { eventId: 'EV-2', type: 'uplink', payload: { messageId: 'MSG-1', channelId: 'channel-1', content: '1' } }, { eventId: 'EV-3', type: 'unsupported', payload: {} }, ], })).resolves.toEqual({ batchId: 'CB-1', results: [ { eventId: 'EV-1', accepted: true }, { eventId: 'EV-2', accepted: true }, { eventId: 'EV-3', accepted: false, retryable: false, errorCode: 'INVALID_EVENT' }, ] }); }); });