|
| 1 | +/** |
| 2 | + * Claim deadline processor integration tests (issue #650) — mocked Soroban RPC. |
| 3 | + */ |
| 4 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 5 | +import { ClaimStatus } from '@prisma/client'; |
| 6 | +import { ClaimDeadlineProcessorService } from '../claim-deadline.processor.service'; |
| 7 | +import { SorobanService } from '../../rpc/soroban.service'; |
| 8 | +import { PrismaService } from '../../prisma/prisma.service'; |
| 9 | +import { ConfigService } from '@nestjs/config'; |
| 10 | +import { CLAIM_VOTING_WINDOW_LEDGERS } from '../claim-deadline.constants'; |
| 11 | + |
| 12 | +const sorobanMock = { |
| 13 | + finalizeClaim: jest.fn(), |
| 14 | +}; |
| 15 | + |
| 16 | +const prismaMock = { |
| 17 | + ledgerCursor: { findUnique: jest.fn() }, |
| 18 | + claim: { |
| 19 | + findMany: jest.fn(), |
| 20 | + findUnique: jest.fn(), |
| 21 | + update: jest.fn(), |
| 22 | + updateMany: jest.fn(), |
| 23 | + }, |
| 24 | +}; |
| 25 | + |
| 26 | +describe('ClaimDeadlineProcessorService (integration)', () => { |
| 27 | + let service: ClaimDeadlineProcessorService; |
| 28 | + |
| 29 | + beforeEach(async () => { |
| 30 | + jest.clearAllMocks(); |
| 31 | + |
| 32 | + const moduleRef: TestingModule = await Test.createTestingModule({ |
| 33 | + providers: [ |
| 34 | + ClaimDeadlineProcessorService, |
| 35 | + { provide: SorobanService, useValue: sorobanMock }, |
| 36 | + { provide: PrismaService, useValue: prismaMock }, |
| 37 | + { |
| 38 | + provide: ConfigService, |
| 39 | + useValue: { get: jest.fn((key: string, def?: unknown) => (key === 'STELLAR_NETWORK' ? 'testnet' : def)) }, |
| 40 | + }, |
| 41 | + ], |
| 42 | + }).compile(); |
| 43 | + |
| 44 | + service = moduleRef.get(ClaimDeadlineProcessorService); |
| 45 | + }); |
| 46 | + |
| 47 | + it('finalizes expired claim and updates DB on successful RPC', async () => { |
| 48 | + prismaMock.ledgerCursor.findUnique.mockResolvedValue({ lastProcessedLedger: 200_000 }); |
| 49 | + prismaMock.claim.findMany.mockResolvedValue([{ id: 42, createdAtLedger: 50_000 }]); |
| 50 | + prismaMock.claim.findUnique.mockResolvedValue({ |
| 51 | + id: 42, |
| 52 | + isFinalized: false, |
| 53 | + status: 'PENDING', |
| 54 | + }); |
| 55 | + sorobanMock.finalizeClaim.mockResolvedValue({ |
| 56 | + txHash: 'abc123', |
| 57 | + ledger: 200_001, |
| 58 | + onChainStatus: 'Approved', |
| 59 | + }); |
| 60 | + |
| 61 | + const outcome = await service.processClaim(42); |
| 62 | + |
| 63 | + expect(outcome).toBe('finalized'); |
| 64 | + expect(sorobanMock.finalizeClaim).toHaveBeenCalledWith(42); |
| 65 | + expect(prismaMock.claim.update).toHaveBeenCalledWith({ |
| 66 | + where: { id: 42 }, |
| 67 | + data: expect.objectContaining({ |
| 68 | + isFinalized: true, |
| 69 | + status: ClaimStatus.APPROVED, |
| 70 | + txHash: 'abc123', |
| 71 | + }), |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('logs RPC failure and returns failed without throwing', async () => { |
| 76 | + prismaMock.claim.findUnique.mockResolvedValue({ |
| 77 | + id: 7, |
| 78 | + isFinalized: false, |
| 79 | + status: 'PENDING', |
| 80 | + }); |
| 81 | + sorobanMock.finalizeClaim.mockRejectedValue(new Error('RPC timeout')); |
| 82 | + |
| 83 | + const outcome = await service.processClaim(7); |
| 84 | + |
| 85 | + expect(outcome).toBe('failed'); |
| 86 | + expect(prismaMock.claim.update).not.toHaveBeenCalled(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('skips already-finalized claim cleanly', async () => { |
| 90 | + prismaMock.claim.findUnique.mockResolvedValue({ |
| 91 | + id: 9, |
| 92 | + isFinalized: true, |
| 93 | + status: ClaimStatus.APPROVED, |
| 94 | + }); |
| 95 | + |
| 96 | + const outcome = await service.processClaim(9); |
| 97 | + |
| 98 | + expect(outcome).toBe('skipped'); |
| 99 | + expect(sorobanMock.finalizeClaim).not.toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('scan selects claims past deadline ledger', async () => { |
| 103 | + const currentLedger = CLAIM_VOTING_WINDOW_LEDGERS + 1_000; |
| 104 | + prismaMock.ledgerCursor.findUnique.mockResolvedValue({ lastProcessedLedger: currentLedger }); |
| 105 | + prismaMock.claim.findMany.mockResolvedValue([]); |
| 106 | + |
| 107 | + await service.runScan(); |
| 108 | + |
| 109 | + expect(prismaMock.claim.findMany).toHaveBeenCalledWith( |
| 110 | + expect.objectContaining({ |
| 111 | + where: expect.objectContaining({ |
| 112 | + status: 'PENDING', |
| 113 | + isFinalized: false, |
| 114 | + createdAtLedger: { lte: currentLedger - CLAIM_VOTING_WINDOW_LEDGERS }, |
| 115 | + }), |
| 116 | + }), |
| 117 | + ); |
| 118 | + }); |
| 119 | +}); |
0 commit comments