|
| 1 | +/** |
| 2 | + * Unit/integration tests for disputeService + disputeHandlers. |
| 3 | + * |
| 4 | + * Uses an in-memory MongoDB for persistence (project convention). The |
| 5 | + * outbound notification webhook is mocked via axios. |
| 6 | + */ |
| 7 | + |
| 8 | +import mongoose from 'mongoose'; |
| 9 | +import { MongoMemoryServer } from 'mongodb-memory-server'; |
| 10 | +import axios from 'axios'; |
| 11 | + |
| 12 | +jest.mock('../src/config/logger', () => ({ |
| 13 | + info: jest.fn(), |
| 14 | + warn: jest.fn(), |
| 15 | + error: jest.fn(), |
| 16 | + debug: jest.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +jest.mock('axios'); |
| 20 | + |
| 21 | +import { Dispute, DisputeStatus } from '../src/models/Dispute'; |
| 22 | +import { disputeService } from '../src/services/disputeService'; |
| 23 | +import { |
| 24 | + handleDisputeOpened, |
| 25 | + handleDisputeResolved, |
| 26 | + dispatchDisputeEvent, |
| 27 | +} from '../src/indexer/disputeHandlers'; |
| 28 | +import env from '../src/config/env'; |
| 29 | + |
| 30 | +const mockedAxiosPost = axios.post as jest.Mock; |
| 31 | + |
| 32 | +let mongoServer: MongoMemoryServer; |
| 33 | +const SETUP_TIMEOUT = 120_000; |
| 34 | + |
| 35 | +beforeAll(async () => { |
| 36 | + mongoServer = await MongoMemoryServer.create(); |
| 37 | + await mongoose.connect(mongoServer.getUri()); |
| 38 | +}, SETUP_TIMEOUT); |
| 39 | + |
| 40 | +afterEach(async () => { |
| 41 | + jest.clearAllMocks(); |
| 42 | + const collections = mongoose.connection.collections; |
| 43 | + for (const key in collections) { |
| 44 | + await collections[key].deleteMany({}); |
| 45 | + } |
| 46 | +}); |
| 47 | + |
| 48 | +afterAll(async () => { |
| 49 | + await mongoose.disconnect(); |
| 50 | + await mongoServer.stop(); |
| 51 | +}, 15_000); |
| 52 | + |
| 53 | +describe('handleDisputeOpened', () => { |
| 54 | + it('creates a local Dispute record from the on-chain event', async () => { |
| 55 | + await handleDisputeOpened({ |
| 56 | + disputeId: 'dispute-1', |
| 57 | + deliveryId: 'delivery-1', |
| 58 | + openedBy: 'GABC...OPENER', |
| 59 | + reason: 'Package damaged', |
| 60 | + ledgerSequence: 1000, |
| 61 | + }); |
| 62 | + |
| 63 | + const dispute = await Dispute.findOne({ disputeId: 'dispute-1' }); |
| 64 | + expect(dispute).not.toBeNull(); |
| 65 | + expect(dispute?.status).toBe(DisputeStatus.OPEN); |
| 66 | + expect(dispute?.deliveryId).toBe('delivery-1'); |
| 67 | + expect(dispute?.openedLedger).toBe(1000); |
| 68 | + }); |
| 69 | + |
| 70 | + it('is idempotent when the same disputeId is processed twice', async () => { |
| 71 | + const input = { |
| 72 | + disputeId: 'dispute-2', |
| 73 | + deliveryId: 'delivery-2', |
| 74 | + openedBy: 'GABC...OPENER', |
| 75 | + ledgerSequence: 500, |
| 76 | + }; |
| 77 | + |
| 78 | + await handleDisputeOpened(input); |
| 79 | + await handleDisputeOpened(input); |
| 80 | + |
| 81 | + const count = await Dispute.countDocuments({ disputeId: 'dispute-2' }); |
| 82 | + expect(count).toBe(1); |
| 83 | + }); |
| 84 | + |
| 85 | + it('ignores a malformed payload without throwing', async () => { |
| 86 | + await expect( |
| 87 | + handleDisputeOpened({ |
| 88 | + disputeId: '', |
| 89 | + deliveryId: 'delivery-3', |
| 90 | + openedBy: 'GABC', |
| 91 | + ledgerSequence: 1, |
| 92 | + }), |
| 93 | + ).resolves.not.toThrow(); |
| 94 | + |
| 95 | + const count = await Dispute.countDocuments(); |
| 96 | + expect(count).toBe(0); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe('handleDisputeResolved', () => { |
| 101 | + it('marks an existing dispute resolved', async () => { |
| 102 | + await Dispute.create({ |
| 103 | + disputeId: 'dispute-4', |
| 104 | + deliveryId: 'delivery-4', |
| 105 | + openedBy: 'GABC...OPENER', |
| 106 | + status: DisputeStatus.OPEN, |
| 107 | + openedLedger: 100, |
| 108 | + }); |
| 109 | + |
| 110 | + await handleDisputeResolved({ |
| 111 | + disputeId: 'dispute-4', |
| 112 | + resolution: 'Refund issued', |
| 113 | + ledgerSequence: 200, |
| 114 | + }); |
| 115 | + |
| 116 | + const dispute = await Dispute.findOne({ disputeId: 'dispute-4' }); |
| 117 | + expect(dispute?.status).toBe(DisputeStatus.RESOLVED); |
| 118 | + expect(dispute?.resolution).toBe('Refund issued'); |
| 119 | + expect(dispute?.resolvedLedger).toBe(200); |
| 120 | + expect(dispute?.resolvedAt).toBeInstanceOf(Date); |
| 121 | + }); |
| 122 | + |
| 123 | + it('does not throw when resolving a dispute that was never opened locally', async () => { |
| 124 | + await expect( |
| 125 | + handleDisputeResolved({ |
| 126 | + disputeId: 'unknown-dispute', |
| 127 | + resolution: 'n/a', |
| 128 | + ledgerSequence: 1, |
| 129 | + }), |
| 130 | + ).resolves.not.toThrow(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('notifies the configured webhook on resolution', async () => { |
| 134 | + const originalWebhookUrl = env.DISPUTE_NOTIFICATION_WEBHOOK_URL; |
| 135 | + (env as { DISPUTE_NOTIFICATION_WEBHOOK_URL: string }).DISPUTE_NOTIFICATION_WEBHOOK_URL = |
| 136 | + 'https://example.com/dispute-webhook'; |
| 137 | + mockedAxiosPost.mockResolvedValue({ status: 200 }); |
| 138 | + |
| 139 | + try { |
| 140 | + await Dispute.create({ |
| 141 | + disputeId: 'dispute-5', |
| 142 | + deliveryId: 'delivery-5', |
| 143 | + openedBy: 'GABC...OPENER', |
| 144 | + status: DisputeStatus.OPEN, |
| 145 | + openedLedger: 100, |
| 146 | + }); |
| 147 | + |
| 148 | + await handleDisputeResolved({ |
| 149 | + disputeId: 'dispute-5', |
| 150 | + resolution: 'Refund issued', |
| 151 | + ledgerSequence: 200, |
| 152 | + }); |
| 153 | + |
| 154 | + expect(mockedAxiosPost).toHaveBeenCalledWith( |
| 155 | + 'https://example.com/dispute-webhook', |
| 156 | + expect.objectContaining({ event: 'dispute_resolved', disputeId: 'dispute-5' }), |
| 157 | + ); |
| 158 | + } finally { |
| 159 | + (env as { DISPUTE_NOTIFICATION_WEBHOOK_URL: string }).DISPUTE_NOTIFICATION_WEBHOOK_URL = |
| 160 | + originalWebhookUrl; |
| 161 | + } |
| 162 | + }); |
| 163 | +}); |
| 164 | + |
| 165 | +describe('dispatchDisputeEvent', () => { |
| 166 | + it('routes dispute_opened to handleDisputeOpened', async () => { |
| 167 | + await dispatchDisputeEvent('dispute_opened', { |
| 168 | + disputeId: 'dispute-6', |
| 169 | + deliveryId: 'delivery-6', |
| 170 | + openedBy: 'GABC', |
| 171 | + ledgerSequence: 1, |
| 172 | + }); |
| 173 | + |
| 174 | + const dispute = await Dispute.findOne({ disputeId: 'dispute-6' }); |
| 175 | + expect(dispute).not.toBeNull(); |
| 176 | + }); |
| 177 | + |
| 178 | + it('ignores unknown event types', async () => { |
| 179 | + await expect( |
| 180 | + dispatchDisputeEvent('something_else', { |
| 181 | + disputeId: 'dispute-7', |
| 182 | + deliveryId: 'delivery-7', |
| 183 | + openedBy: 'GABC', |
| 184 | + ledgerSequence: 1, |
| 185 | + }), |
| 186 | + ).resolves.not.toThrow(); |
| 187 | + |
| 188 | + const count = await Dispute.countDocuments({ disputeId: 'dispute-7' }); |
| 189 | + expect(count).toBe(0); |
| 190 | + }); |
| 191 | +}); |
| 192 | + |
| 193 | +describe('disputeService.listDisputes / getDisputeById', () => { |
| 194 | + it('paginates and filters by status', async () => { |
| 195 | + await Dispute.create([ |
| 196 | + { |
| 197 | + disputeId: 'a', |
| 198 | + deliveryId: 'd1', |
| 199 | + openedBy: 'G1', |
| 200 | + status: DisputeStatus.OPEN, |
| 201 | + openedLedger: 1, |
| 202 | + }, |
| 203 | + { |
| 204 | + disputeId: 'b', |
| 205 | + deliveryId: 'd2', |
| 206 | + openedBy: 'G2', |
| 207 | + status: DisputeStatus.RESOLVED, |
| 208 | + openedLedger: 2, |
| 209 | + resolvedLedger: 3, |
| 210 | + }, |
| 211 | + ]); |
| 212 | + |
| 213 | + const openOnly = await disputeService.listDisputes(1, 20, DisputeStatus.OPEN); |
| 214 | + expect(openOnly.data).toHaveLength(1); |
| 215 | + expect(openOnly.data[0].disputeId).toBe('a'); |
| 216 | + expect(openOnly.pagination.total).toBe(1); |
| 217 | + }); |
| 218 | + |
| 219 | + it('throws a not-found error for an unknown disputeId', async () => { |
| 220 | + await expect(disputeService.getDisputeById('does-not-exist')).rejects.toThrow(/not found/i); |
| 221 | + }); |
| 222 | +}); |
0 commit comments