|
| 1 | +import { ContractEventsProcessor } from './contract-events.processor'; |
| 2 | +import { Logger } from '@nestjs/common'; |
| 3 | + |
| 4 | +describe('ContractEventsProcessor', () => { |
| 5 | + let processor: ContractEventsProcessor; |
| 6 | + let prisma: any; |
| 7 | + |
| 8 | + const mockJob = (data: any) => |
| 9 | + ({ |
| 10 | + data, |
| 11 | + id: 'test-job-id', |
| 12 | + }) as any; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + prisma = { |
| 16 | + processedEvent: { |
| 17 | + findUnique: jest.fn(), |
| 18 | + create: jest.fn(), |
| 19 | + }, |
| 20 | + donation: { |
| 21 | + updateMany: jest.fn(), |
| 22 | + }, |
| 23 | + milestone: { |
| 24 | + updateMany: jest.fn(), |
| 25 | + }, |
| 26 | + }; |
| 27 | + processor = new ContractEventsProcessor(prisma); |
| 28 | + jest.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined); |
| 29 | + jest.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + jest.restoreAllMocks(); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('idempotency', () => { |
| 37 | + it('skips already-processed (txHash, eventType) pairs', async () => { |
| 38 | + prisma.processedEvent.findUnique.mockResolvedValue({ |
| 39 | + txHash: 'abc', |
| 40 | + eventType: 'DonationReceived', |
| 41 | + processedAt: new Date(), |
| 42 | + }); |
| 43 | + |
| 44 | + const result = await processor.processEvent( |
| 45 | + mockJob({ txHash: 'abc', eventType: 'DonationReceived' }), |
| 46 | + ); |
| 47 | + |
| 48 | + expect(result).toEqual({ skipped: true, reason: 'duplicate' }); |
| 49 | + expect(prisma.donation.updateMany).not.toHaveBeenCalled(); |
| 50 | + expect(prisma.processedEvent.create).not.toHaveBeenCalled(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('processes a new event and records idempotency key', async () => { |
| 54 | + prisma.processedEvent.findUnique.mockResolvedValue(null); |
| 55 | + prisma.processedEvent.create.mockResolvedValue({}); |
| 56 | + prisma.donation.updateMany.mockResolvedValue({ count: 1 }); |
| 57 | + |
| 58 | + await processor.processEvent( |
| 59 | + mockJob({ |
| 60 | + txHash: 'abc', |
| 61 | + eventType: 'DonationReceived', |
| 62 | + contractId: 'CA...', |
| 63 | + topics: ['DonationReceived', 'G...DONOR'], |
| 64 | + value: { amount: '100' }, |
| 65 | + ledger: 12345, |
| 66 | + pagingToken: '123-456', |
| 67 | + createdAt: '2026-06-22T00:00:00Z', |
| 68 | + }), |
| 69 | + ); |
| 70 | + |
| 71 | + expect(prisma.donation.updateMany).toHaveBeenCalledWith({ |
| 72 | + where: { txHash: 'abc', status: 'PENDING' }, |
| 73 | + data: { status: 'CONFIRMED', confirmedAt: expect.any(Date) }, |
| 74 | + }); |
| 75 | + expect(prisma.processedEvent.create).toHaveBeenCalledWith({ |
| 76 | + data: { txHash: 'abc', eventType: 'DonationReceived' }, |
| 77 | + }); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('event routing', () => { |
| 82 | + it('updates milestone status on MilestoneReleased', async () => { |
| 83 | + prisma.processedEvent.findUnique.mockResolvedValue(null); |
| 84 | + prisma.processedEvent.create.mockResolvedValue({}); |
| 85 | + prisma.milestone.updateMany.mockResolvedValue({ count: 1 }); |
| 86 | + |
| 87 | + await processor.processEvent( |
| 88 | + mockJob({ |
| 89 | + txHash: 'def', |
| 90 | + eventType: 'MilestoneReleased', |
| 91 | + contractId: 'CA...', |
| 92 | + topics: ['MilestoneReleased'], |
| 93 | + value: null, |
| 94 | + ledger: 12346, |
| 95 | + pagingToken: '124-456', |
| 96 | + createdAt: '2026-06-22T00:00:01Z', |
| 97 | + }), |
| 98 | + ); |
| 99 | + |
| 100 | + expect(prisma.milestone.updateMany).toHaveBeenCalledWith({ |
| 101 | + where: { txHash: 'def', status: 'PENDING' }, |
| 102 | + data: { status: 'COMPLETED', completedAt: expect.any(Date) }, |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('warns on unknown event types without crashing', async () => { |
| 107 | + prisma.processedEvent.findUnique.mockResolvedValue(null); |
| 108 | + prisma.processedEvent.create.mockResolvedValue({}); |
| 109 | + const warnSpy = jest.spyOn(Logger.prototype, 'warn'); |
| 110 | + |
| 111 | + await processor.processEvent( |
| 112 | + mockJob({ |
| 113 | + txHash: 'xyz', |
| 114 | + eventType: 'UnknownEvent', |
| 115 | + contractId: 'CA...', |
| 116 | + topics: ['UnknownEvent'], |
| 117 | + value: null, |
| 118 | + ledger: 12347, |
| 119 | + pagingToken: '125-456', |
| 120 | + createdAt: '2026-06-22T00:00:02Z', |
| 121 | + }), |
| 122 | + ); |
| 123 | + |
| 124 | + expect(warnSpy).toHaveBeenCalledWith( |
| 125 | + expect.stringContaining('Unknown event type "UnknownEvent"'), |
| 126 | + ); |
| 127 | + expect(prisma.processedEvent.create).toHaveBeenCalled(); |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments