|
| 1 | +// Unit tests for alert.service.ts (#423) |
| 2 | +// |
| 3 | +// Covers: createAlert, listAlerts, deleteAlert. |
| 4 | +// Uses Jest mocks for prisma — no database required. |
| 5 | + |
| 6 | +import { createAlert, listAlerts, deleteAlert } from '../alert.service'; |
| 7 | +import { prisma } from '../../../utils/prisma.utils'; |
| 8 | + |
| 9 | +jest.mock('../../../utils/prisma.utils', () => ({ |
| 10 | + prisma: { |
| 11 | + priceAlert: { |
| 12 | + create: jest.fn(), |
| 13 | + findMany: jest.fn(), |
| 14 | + findFirst: jest.fn(), |
| 15 | + delete: jest.fn(), |
| 16 | + }, |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +const mockedPrisma = prisma as jest.Mocked<typeof prisma>; |
| 21 | + |
| 22 | +const VALID_ADDRESS = 'GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'; |
| 23 | + |
| 24 | +const BASE_INPUT = { |
| 25 | + creator_id: 'creator-1', |
| 26 | + wallet_address: VALID_ADDRESS, |
| 27 | + target_price: 100, |
| 28 | + direction: 'above' as const, |
| 29 | + callback_url: 'https://example.com/callback', |
| 30 | +}; |
| 31 | + |
| 32 | +const DB_ALERT = { |
| 33 | + id: 'alert-1', |
| 34 | + creatorId: 'creator-1', |
| 35 | + walletAddress: VALID_ADDRESS, |
| 36 | + targetPrice: 100, |
| 37 | + direction: 'above', |
| 38 | + callbackUrl: 'https://example.com/callback', |
| 39 | + isActive: true, |
| 40 | + triggeredAt: null, |
| 41 | + createdAt: new Date('2026-01-01T00:00:00Z'), |
| 42 | +}; |
| 43 | + |
| 44 | +describe('createAlert', () => { |
| 45 | + afterEach(() => jest.clearAllMocks()); |
| 46 | + |
| 47 | + it('calls prisma.priceAlert.create with correct data', async () => { |
| 48 | + (mockedPrisma.priceAlert.create as jest.Mock).mockResolvedValue(DB_ALERT); |
| 49 | + |
| 50 | + const result = await createAlert(BASE_INPUT); |
| 51 | + |
| 52 | + expect(mockedPrisma.priceAlert.create).toHaveBeenCalledWith({ |
| 53 | + data: { |
| 54 | + creatorId: 'creator-1', |
| 55 | + walletAddress: VALID_ADDRESS, |
| 56 | + targetPrice: 100, |
| 57 | + direction: 'above', |
| 58 | + callbackUrl: 'https://example.com/callback', |
| 59 | + }, |
| 60 | + }); |
| 61 | + expect(result).toEqual(DB_ALERT); |
| 62 | + }); |
| 63 | + |
| 64 | + it('creates a below-direction alert', async () => { |
| 65 | + const input = { ...BASE_INPUT, direction: 'below' as const, target_price: 50 }; |
| 66 | + (mockedPrisma.priceAlert.create as jest.Mock).mockResolvedValue({ |
| 67 | + ...DB_ALERT, |
| 68 | + direction: 'below', |
| 69 | + targetPrice: 50, |
| 70 | + }); |
| 71 | + |
| 72 | + const result = await createAlert(input); |
| 73 | + expect(result.direction).toBe('below'); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +describe('listAlerts', () => { |
| 78 | + afterEach(() => jest.clearAllMocks()); |
| 79 | + |
| 80 | + it('returns active alerts for a wallet address', async () => { |
| 81 | + (mockedPrisma.priceAlert.findMany as jest.Mock).mockResolvedValue([DB_ALERT]); |
| 82 | + |
| 83 | + const result = await listAlerts(VALID_ADDRESS); |
| 84 | + |
| 85 | + expect(mockedPrisma.priceAlert.findMany).toHaveBeenCalledWith({ |
| 86 | + where: { walletAddress: VALID_ADDRESS, isActive: true }, |
| 87 | + orderBy: { createdAt: 'desc' }, |
| 88 | + }); |
| 89 | + expect(result).toHaveLength(1); |
| 90 | + expect(result[0].id).toBe('alert-1'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('returns empty array when no alerts exist', async () => { |
| 94 | + (mockedPrisma.priceAlert.findMany as jest.Mock).mockResolvedValue([]); |
| 95 | + |
| 96 | + const result = await listAlerts(VALID_ADDRESS); |
| 97 | + expect(result).toEqual([]); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('deleteAlert', () => { |
| 102 | + afterEach(() => jest.clearAllMocks()); |
| 103 | + |
| 104 | + it('deletes the alert and returns its id when found', async () => { |
| 105 | + (mockedPrisma.priceAlert.findFirst as jest.Mock).mockResolvedValue(DB_ALERT); |
| 106 | + (mockedPrisma.priceAlert.delete as jest.Mock).mockResolvedValue(DB_ALERT); |
| 107 | + |
| 108 | + const result = await deleteAlert('alert-1', VALID_ADDRESS); |
| 109 | + |
| 110 | + expect(mockedPrisma.priceAlert.findFirst).toHaveBeenCalledWith({ |
| 111 | + where: { id: 'alert-1', walletAddress: VALID_ADDRESS }, |
| 112 | + }); |
| 113 | + expect(mockedPrisma.priceAlert.delete).toHaveBeenCalledWith({ |
| 114 | + where: { id: 'alert-1' }, |
| 115 | + }); |
| 116 | + expect(result).toEqual({ id: 'alert-1' }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('returns null when the alert is not found', async () => { |
| 120 | + (mockedPrisma.priceAlert.findFirst as jest.Mock).mockResolvedValue(null); |
| 121 | + |
| 122 | + const result = await deleteAlert('nonexistent', VALID_ADDRESS); |
| 123 | + |
| 124 | + expect(result).toBeNull(); |
| 125 | + expect(mockedPrisma.priceAlert.delete).not.toHaveBeenCalled(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('does not delete an alert belonging to a different wallet address', async () => { |
| 129 | + (mockedPrisma.priceAlert.findFirst as jest.Mock).mockResolvedValue(null); |
| 130 | + |
| 131 | + const result = await deleteAlert('alert-1', 'GBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB'); |
| 132 | + expect(result).toBeNull(); |
| 133 | + }); |
| 134 | +}); |
0 commit comments