|
| 1 | +import supertest from 'supertest'; |
| 2 | +import { Prisma } from '@prisma/client'; |
| 3 | +import { Keypair } from '@stellar/stellar-base'; |
| 4 | + |
| 5 | +// Mock Prisma so the integration test exercises the full HTTP + dispatch path |
| 6 | +// without requiring a live database, matching the suite's mocking conventions. |
| 7 | +jest.mock('../../utils/prisma.utils', () => ({ |
| 8 | + prisma: { |
| 9 | + alert: { |
| 10 | + create: jest.fn(), |
| 11 | + findFirst: jest.fn(), |
| 12 | + findMany: jest.fn(), |
| 13 | + delete: jest.fn(), |
| 14 | + update: jest.fn(), |
| 15 | + }, |
| 16 | + }, |
| 17 | +})); |
| 18 | + |
| 19 | +jest.mock('../../utils/logger.utils', () => ({ |
| 20 | + logger: { info: jest.fn(), warn: jest.fn(), error: jest.fn() }, |
| 21 | +})); |
| 22 | + |
| 23 | +import app from '../../app'; |
| 24 | +import { prisma } from '../../utils/prisma.utils'; |
| 25 | +import { evaluateTradeForAlerts } from './alert.service'; |
| 26 | +import { envConfig } from '../../config'; |
| 27 | + |
| 28 | +const mockPrisma = prisma as unknown as { |
| 29 | + alert: { |
| 30 | + create: jest.Mock; |
| 31 | + findFirst: jest.Mock; |
| 32 | + findMany: jest.Mock; |
| 33 | + delete: jest.Mock; |
| 34 | + update: jest.Mock; |
| 35 | + }; |
| 36 | +}; |
| 37 | + |
| 38 | +const walletAddress = Keypair.random().publicKey(); |
| 39 | + |
| 40 | +function decimal(v: string): Prisma.Decimal { |
| 41 | + return new Prisma.Decimal(v); |
| 42 | +} |
| 43 | + |
| 44 | +beforeEach(() => { |
| 45 | + jest.clearAllMocks(); |
| 46 | +}); |
| 47 | + |
| 48 | +describe('POST /api/v1/alerts', () => { |
| 49 | + it('registers an alert and returns a unique alert ID', async () => { |
| 50 | + mockPrisma.alert.create.mockResolvedValue({ |
| 51 | + id: 'alert-generated-id', |
| 52 | + creatorId: 'creator-1', |
| 53 | + walletAddress, |
| 54 | + targetPrice: decimal('15'), |
| 55 | + direction: 'ABOVE', |
| 56 | + callbackUrl: 'https://example.com/hook', |
| 57 | + status: 'PENDING', |
| 58 | + createdAt: new Date(), |
| 59 | + }); |
| 60 | + |
| 61 | + const res = await supertest(app) |
| 62 | + .post('/api/v1/alerts') |
| 63 | + .send({ |
| 64 | + creator_id: 'creator-1', |
| 65 | + wallet_address: walletAddress, |
| 66 | + target_price: '15', |
| 67 | + direction: 'above', |
| 68 | + callback_url: 'https://example.com/hook', |
| 69 | + }); |
| 70 | + |
| 71 | + expect(res.status).toBe(201); |
| 72 | + expect(res.body.success).toBe(true); |
| 73 | + expect(res.body.data.id).toBe('alert-generated-id'); |
| 74 | + expect(res.body.data.direction).toBe('above'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('returns 400 on invalid body (bad direction / url / price)', async () => { |
| 78 | + const res = await supertest(app) |
| 79 | + .post('/api/v1/alerts') |
| 80 | + .send({ |
| 81 | + creator_id: 'creator-1', |
| 82 | + wallet_address: walletAddress, |
| 83 | + target_price: '-5', |
| 84 | + direction: 'sideways', |
| 85 | + callback_url: 'not-a-url', |
| 86 | + }); |
| 87 | + |
| 88 | + expect(res.status).toBe(400); |
| 89 | + expect(mockPrisma.alert.create).not.toHaveBeenCalled(); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe('DELETE /api/v1/alerts/:id', () => { |
| 94 | + it('cancels a pending alert before it fires', async () => { |
| 95 | + mockPrisma.alert.findFirst.mockResolvedValue({ id: 'alert-1', status: 'PENDING' }); |
| 96 | + mockPrisma.alert.delete.mockResolvedValue({ id: 'alert-1' }); |
| 97 | + |
| 98 | + const res = await supertest(app).delete('/api/v1/alerts/alert-1'); |
| 99 | + |
| 100 | + expect(res.status).toBe(200); |
| 101 | + expect(res.body.success).toBe(true); |
| 102 | + expect(mockPrisma.alert.delete).toHaveBeenCalledWith({ where: { id: 'alert-1' } }); |
| 103 | + }); |
| 104 | + |
| 105 | + it('returns 404 for a non-existent alert', async () => { |
| 106 | + mockPrisma.alert.findFirst.mockResolvedValue(null); |
| 107 | + |
| 108 | + const res = await supertest(app).delete('/api/v1/alerts/missing-id'); |
| 109 | + |
| 110 | + expect(res.status).toBe(404); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +describe('alert trigger evaluation', () => { |
| 115 | + afterEach(() => { |
| 116 | + jest.restoreAllMocks(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('fires on an above trigger and deletes the alert (one-shot)', async () => { |
| 120 | + mockPrisma.alert.findMany.mockResolvedValue([ |
| 121 | + { |
| 122 | + id: 'alert-above', |
| 123 | + creatorId: 'creator-1', |
| 124 | + walletAddress, |
| 125 | + targetPrice: decimal('10'), |
| 126 | + direction: 'ABOVE', |
| 127 | + callbackUrl: 'https://example.com/hook', |
| 128 | + status: 'PENDING', |
| 129 | + }, |
| 130 | + ]); |
| 131 | + mockPrisma.alert.delete.mockResolvedValue({ id: 'alert-above' }); |
| 132 | + const mockFetch = jest.fn().mockResolvedValue({ ok: true, status: 200, statusText: 'OK' }); |
| 133 | + (global.fetch as jest.Mock) = mockFetch; |
| 134 | + |
| 135 | + await evaluateTradeForAlerts({ |
| 136 | + creatorId: 'creator-1', |
| 137 | + price: '11', |
| 138 | + timestamp: new Date().toISOString(), |
| 139 | + }); |
| 140 | + |
| 141 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 142 | + expect(mockPrisma.alert.delete).toHaveBeenCalledWith({ where: { id: 'alert-above' } }); |
| 143 | + }); |
| 144 | + |
| 145 | + it('fires on a below trigger and deletes the alert (one-shot)', async () => { |
| 146 | + mockPrisma.alert.findMany.mockResolvedValue([ |
| 147 | + { |
| 148 | + id: 'alert-below', |
| 149 | + creatorId: 'creator-1', |
| 150 | + walletAddress, |
| 151 | + targetPrice: decimal('10'), |
| 152 | + direction: 'BELOW', |
| 153 | + callbackUrl: 'https://example.com/hook', |
| 154 | + status: 'PENDING', |
| 155 | + }, |
| 156 | + ]); |
| 157 | + mockPrisma.alert.delete.mockResolvedValue({ id: 'alert-below' }); |
| 158 | + const mockFetch = jest.fn().mockResolvedValue({ ok: true, status: 200, statusText: 'OK' }); |
| 159 | + (global.fetch as jest.Mock) = mockFetch; |
| 160 | + |
| 161 | + await evaluateTradeForAlerts({ |
| 162 | + creatorId: 'creator-1', |
| 163 | + price: '9', |
| 164 | + timestamp: new Date().toISOString(), |
| 165 | + }); |
| 166 | + |
| 167 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 168 | + expect(mockPrisma.alert.delete).toHaveBeenCalledWith({ where: { id: 'alert-below' } }); |
| 169 | + }); |
| 170 | + |
| 171 | + it('does not fire when price moves in the opposite direction', async () => { |
| 172 | + mockPrisma.alert.findMany.mockResolvedValue([ |
| 173 | + { |
| 174 | + id: 'alert-above', |
| 175 | + creatorId: 'creator-1', |
| 176 | + walletAddress, |
| 177 | + targetPrice: decimal('10'), |
| 178 | + direction: 'ABOVE', |
| 179 | + callbackUrl: 'https://example.com/hook', |
| 180 | + status: 'PENDING', |
| 181 | + }, |
| 182 | + ]); |
| 183 | + const mockFetch = jest.fn(); |
| 184 | + (global.fetch as jest.Mock) = mockFetch; |
| 185 | + |
| 186 | + await evaluateTradeForAlerts({ |
| 187 | + creatorId: 'creator-1', |
| 188 | + price: '5', |
| 189 | + timestamp: new Date().toISOString(), |
| 190 | + }); |
| 191 | + |
| 192 | + expect(mockFetch).not.toHaveBeenCalled(); |
| 193 | + expect(mockPrisma.alert.delete).not.toHaveBeenCalled(); |
| 194 | + }); |
| 195 | + |
| 196 | + describe('failed delivery retry', () => { |
| 197 | + beforeEach(() => { |
| 198 | + jest.useFakeTimers(); |
| 199 | + }); |
| 200 | + |
| 201 | + afterEach(() => { |
| 202 | + jest.useRealTimers(); |
| 203 | + }); |
| 204 | + |
| 205 | + it('retries failed delivery up to 3 times then marks the alert failed', async () => { |
| 206 | + mockPrisma.alert.findMany.mockResolvedValue([ |
| 207 | + { |
| 208 | + id: 'alert-fail', |
| 209 | + creatorId: 'creator-1', |
| 210 | + walletAddress, |
| 211 | + targetPrice: decimal('10'), |
| 212 | + direction: 'ABOVE', |
| 213 | + callbackUrl: 'https://nonexistent.example.com/fail', |
| 214 | + status: 'PENDING', |
| 215 | + }, |
| 216 | + ]); |
| 217 | + mockPrisma.alert.update.mockResolvedValue({}); |
| 218 | + const mockFetch = jest.fn().mockRejectedValue(new Error('Network error')); |
| 219 | + (global.fetch as jest.Mock) = mockFetch; |
| 220 | + |
| 221 | + const promise = evaluateTradeForAlerts({ |
| 222 | + creatorId: 'creator-1', |
| 223 | + price: '20', |
| 224 | + timestamp: new Date().toISOString(), |
| 225 | + }); |
| 226 | + |
| 227 | + for (let i = 0; i < envConfig.WEBHOOK_RETRY_MAX_ATTEMPTS; i++) { |
| 228 | + await jest.advanceTimersByTimeAsync( |
| 229 | + Math.pow(2, i) * envConfig.WEBHOOK_RETRY_BASE_DELAY_MS |
| 230 | + ); |
| 231 | + } |
| 232 | + |
| 233 | + await promise; |
| 234 | + |
| 235 | + expect(mockFetch).toHaveBeenCalledTimes(envConfig.WEBHOOK_RETRY_MAX_ATTEMPTS); |
| 236 | + expect(mockPrisma.alert.delete).not.toHaveBeenCalled(); |
| 237 | + expect(mockPrisma.alert.update).toHaveBeenLastCalledWith( |
| 238 | + expect.objectContaining({ |
| 239 | + data: expect.objectContaining({ status: 'FAILED' }), |
| 240 | + }) |
| 241 | + ); |
| 242 | + }); |
| 243 | + }); |
| 244 | +}); |
0 commit comments