From 57ca857d031690c771c202eb45ab141757b71278 Mon Sep 17 00:00:00 2001 From: Superray23 Date: Fri, 21 Aug 2026 00:09:47 +0100 Subject: [PATCH] Test: Add unit tests for claim redemption webhook triggers and update docs (#176) --- README.md | 12 +----- .../claim-redemption.provider.spec.ts | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 27f3283..28278ae 100644 --- a/README.md +++ b/README.md @@ -20,17 +20,7 @@ The Bridgelet SDK is a NestJS-based backend service that manages the lifecycle o The following services/imports are currently **commented out** to allow `npm run start:dev` to run without errors. These are **NOT removed** and **MUST be restored** once proper implementations exist. -### Missing Services: - -1. **WebhooksService** (referenced in `src/modules/claims/providers/claim-redemption.provider.ts`) - - **Location:** `src/modules/webhooks/` (does not exist yet) - - **What was commented out:** - - Constructor dependency injection (line ~25) - - Webhook trigger for `sweep.completed` event (line ~106) - - Webhook trigger for `sweep.failed` event (line ~137) - - **Why:** Service implementation does not exist, causing TypeScript compilation errors - - **Impact:** Webhook notifications will NOT fire when claims are redeemed or when sweeps fail - - **Restoration required:** Once `WebhooksService` is implemented in `src/modules/webhooks/`, uncomment all marked sections +### Temporary Development Notes: ### How to Find Temporary Changes: diff --git a/src/modules/claims/providers/claim-redemption.provider.spec.ts b/src/modules/claims/providers/claim-redemption.provider.spec.ts index 0298c8c..18be95e 100644 --- a/src/modules/claims/providers/claim-redemption.provider.spec.ts +++ b/src/modules/claims/providers/claim-redemption.provider.spec.ts @@ -214,6 +214,23 @@ describe('ClaimRedemptionProvider', () => { }); }); + it('should trigger sweep.completed webhook event on successful redemption', async () => { + await provider.redeemClaim(VALID_TOKEN, VALID_DESTINATION); + + expect(mockWebhooksService.triggerEvent).toHaveBeenCalledWith( + 'sweep.completed', + expect.objectContaining({ + accountId: mockAccount.id, + amount: mockAccount.amount, + asset: mockAccount.asset, + destination: VALID_DESTINATION, + txHash: mockSweepResult.txHash, + sweptAt: mockClaim.claimedAt, + metadata: mockAccount.metadata, + }), + ); + }); + it('should acquire SELECT FOR UPDATE lock before setting status to CLAIMING', async () => { // Rebuild with a spy-able dataSource so we can inspect the qb inside the callback const ds = makeHappyPathDataSource(); @@ -371,6 +388,30 @@ describe('ClaimRedemptionProvider', () => { p.redeemClaim(VALID_TOKEN, VALID_DESTINATION), ).rejects.toThrow('Stellar network error'); }); + + it('should trigger sweep.failed webhook event when sweep fails', async () => { + const ds = makeHappyPathDataSource(); + const p = await buildModule(ds); + mockSweepsService.executeSweep.mockRejectedValue( + new Error('Stellar network error'), + ); + + await expect( + p.redeemClaim(VALID_TOKEN, VALID_DESTINATION), + ).rejects.toThrow('Stellar network error'); + + expect(mockWebhooksService.triggerEvent).toHaveBeenCalledWith( + 'sweep.failed', + expect.objectContaining({ + accountId: mockAccount.id, + amount: mockAccount.amount, + asset: mockAccount.asset, + destination: VALID_DESTINATION, + error: 'Stellar network error', + timestamp: expect.any(Date), + }), + ); + }); }); describe('redeemClaim - propagates TokenVerification errors (issue #167)', () => {