|
| 1 | +/** |
| 2 | + * app/api/dashboard/stats/stats.test.ts |
| 3 | + * |
| 4 | + * Unit tests for: |
| 5 | + * - lib/dashboard/stats (query layer) |
| 6 | + * - GET /api/dashboard/stats (route handler) |
| 7 | + * |
| 8 | + * The `sql` export from @/lib/db is a Proxy, so it cannot be spied on |
| 9 | + * directly. Instead we replace the entire module with a vi.mock factory |
| 10 | + * that exposes a plain vi.fn() which tests can configure per-call. |
| 11 | + */ |
| 12 | + |
| 13 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 14 | +import type { DashboardStats } from '@/lib/dashboard/stats' |
| 15 | + |
| 16 | +// ─── Module-level mock for @/lib/db ───────────────────────────────────────── |
| 17 | + |
| 18 | +const mockSqlFn = vi.fn() |
| 19 | + |
| 20 | +vi.mock('@/lib/db', () => ({ |
| 21 | + sql: mockSqlFn, |
| 22 | +})) |
| 23 | + |
| 24 | +// ─── queryStats ────────────────────────────────────────────────────────────── |
| 25 | + |
| 26 | +describe('queryStats', () => { |
| 27 | + beforeEach(() => { |
| 28 | + vi.clearAllMocks() |
| 29 | + }) |
| 30 | + |
| 31 | + it('maps DB row to DashboardStats correctly', async () => { |
| 32 | + mockSqlFn.mockResolvedValueOnce([{ |
| 33 | + active_contracts: 3, |
| 34 | + completed_contracts: 7, |
| 35 | + total_earnings: '1500.00', |
| 36 | + escrow_volume: '2500.50', |
| 37 | + }]) |
| 38 | + |
| 39 | + const { queryStats } = await import('@/lib/dashboard/stats') |
| 40 | + const result = await queryStats('user-uuid-1') |
| 41 | + |
| 42 | + expect(result).toEqual<DashboardStats>({ |
| 43 | + activeContracts: 3, |
| 44 | + completedContracts: 7, |
| 45 | + totalEarnings: '1500.00', |
| 46 | + escrowVolume: '2500.50', |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + it('returns zero values when query returns no rows', async () => { |
| 51 | + mockSqlFn.mockResolvedValueOnce([]) |
| 52 | + |
| 53 | + const { queryStats } = await import('@/lib/dashboard/stats') |
| 54 | + const result = await queryStats('user-uuid-no-data') |
| 55 | + |
| 56 | + expect(result).toEqual<DashboardStats>({ |
| 57 | + activeContracts: 0, |
| 58 | + completedContracts: 0, |
| 59 | + totalEarnings: '0', |
| 60 | + escrowVolume: '0', |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + it('handles null numeric fields by defaulting to "0"', async () => { |
| 65 | + mockSqlFn.mockResolvedValueOnce([{ |
| 66 | + active_contracts: 0, |
| 67 | + completed_contracts: 0, |
| 68 | + total_earnings: null, |
| 69 | + escrow_volume: null, |
| 70 | + }]) |
| 71 | + |
| 72 | + const { queryStats } = await import('@/lib/dashboard/stats') |
| 73 | + const result = await queryStats('user-uuid-nulls') |
| 74 | + |
| 75 | + expect(result.totalEarnings).toBe('0') |
| 76 | + expect(result.escrowVolume).toBe('0') |
| 77 | + }) |
| 78 | +}) |
| 79 | + |
| 80 | +// ─── getDashboardStats ─────────────────────────────────────────────────────── |
| 81 | + |
| 82 | +describe('getDashboardStats', () => { |
| 83 | + beforeEach(() => { |
| 84 | + vi.clearAllMocks() |
| 85 | + }) |
| 86 | + |
| 87 | + it('throws USER_NOT_FOUND when wallet is not registered', async () => { |
| 88 | + mockSqlFn.mockResolvedValueOnce([]) // getUserIdByWallet → no rows |
| 89 | + |
| 90 | + const { getDashboardStats } = await import('@/lib/dashboard/stats') |
| 91 | + await expect(getDashboardStats('GUNKNOWN')).rejects.toThrow('USER_NOT_FOUND') |
| 92 | + }) |
| 93 | + |
| 94 | + it('returns stats for a registered wallet', async () => { |
| 95 | + mockSqlFn |
| 96 | + .mockResolvedValueOnce([{ id: 'user-uuid-1' }]) // getUserIdByWallet |
| 97 | + .mockResolvedValueOnce([{ // queryStats |
| 98 | + active_contracts: 2, |
| 99 | + completed_contracts: 5, |
| 100 | + total_earnings: '800.00', |
| 101 | + escrow_volume: '400.00', |
| 102 | + }]) |
| 103 | + |
| 104 | + const { getDashboardStats } = await import('@/lib/dashboard/stats') |
| 105 | + const stats = await getDashboardStats('GABC123') |
| 106 | + |
| 107 | + expect(stats).toEqual<DashboardStats>({ |
| 108 | + activeContracts: 2, |
| 109 | + completedContracts: 5, |
| 110 | + totalEarnings: '800.00', |
| 111 | + escrowVolume: '400.00', |
| 112 | + }) |
| 113 | + }) |
| 114 | +}) |
| 115 | + |
| 116 | +// ─── Route handler ─────────────────────────────────────────────────────────── |
| 117 | + |
| 118 | +describe('GET /api/dashboard/stats route', () => { |
| 119 | + beforeEach(() => { |
| 120 | + vi.resetModules() |
| 121 | + }) |
| 122 | + |
| 123 | + it('returns 200 with stats and cache header on success', async () => { |
| 124 | + vi.doMock('@/lib/dashboard/stats', () => ({ |
| 125 | + getDashboardStats: vi.fn().mockResolvedValue({ |
| 126 | + activeContracts: 1, |
| 127 | + completedContracts: 4, |
| 128 | + totalEarnings: '300.00', |
| 129 | + escrowVolume: '100.00', |
| 130 | + } satisfies DashboardStats), |
| 131 | + })) |
| 132 | + |
| 133 | + vi.doMock('@/lib/auth/middleware', () => ({ |
| 134 | + withAuth: (handler: (req: Request, auth: { walletAddress: string }) => Promise<Response>) => |
| 135 | + (req: Request) => handler(req, { walletAddress: 'GABC123' }), |
| 136 | + })) |
| 137 | + |
| 138 | + const { GET } = await import('@/app/api/dashboard/stats/route') |
| 139 | + const req = new Request('http://localhost/api/dashboard/stats') |
| 140 | + const res = await GET(req as never) |
| 141 | + |
| 142 | + expect(res.status).toBe(200) |
| 143 | + const body = await res.json() as { data: DashboardStats; meta: { generatedAt: string } } |
| 144 | + expect(body.data).toEqual({ |
| 145 | + activeContracts: 1, |
| 146 | + completedContracts: 4, |
| 147 | + totalEarnings: '300.00', |
| 148 | + escrowVolume: '100.00', |
| 149 | + }) |
| 150 | + expect(body.meta.generatedAt).toBeDefined() |
| 151 | + expect(res.headers.get('Cache-Control')).toBe('private, max-age=60') |
| 152 | + }) |
| 153 | + |
| 154 | + it('returns 404 when wallet is not registered', async () => { |
| 155 | + vi.doMock('@/lib/dashboard/stats', () => ({ |
| 156 | + getDashboardStats: vi.fn().mockRejectedValue(new Error('USER_NOT_FOUND')), |
| 157 | + })) |
| 158 | + |
| 159 | + vi.doMock('@/lib/auth/middleware', () => ({ |
| 160 | + withAuth: (handler: (req: Request, auth: { walletAddress: string }) => Promise<Response>) => |
| 161 | + (req: Request) => handler(req, { walletAddress: 'GUNKNOWN' }), |
| 162 | + })) |
| 163 | + |
| 164 | + const { GET } = await import('@/app/api/dashboard/stats/route') |
| 165 | + const req = new Request('http://localhost/api/dashboard/stats') |
| 166 | + const res = await GET(req as never) |
| 167 | + |
| 168 | + expect(res.status).toBe(404) |
| 169 | + const body = await res.json() as { code: string } |
| 170 | + expect(body.code).toBe('USER_NOT_FOUND') |
| 171 | + }) |
| 172 | + |
| 173 | + it('returns 500 on unexpected DB error', async () => { |
| 174 | + vi.doMock('@/lib/dashboard/stats', () => ({ |
| 175 | + getDashboardStats: vi.fn().mockRejectedValue(new Error('connection refused')), |
| 176 | + })) |
| 177 | + |
| 178 | + vi.doMock('@/lib/auth/middleware', () => ({ |
| 179 | + withAuth: (handler: (req: Request, auth: { walletAddress: string }) => Promise<Response>) => |
| 180 | + (req: Request) => handler(req, { walletAddress: 'GABC123' }), |
| 181 | + })) |
| 182 | + |
| 183 | + const { GET } = await import('@/app/api/dashboard/stats/route') |
| 184 | + const req = new Request('http://localhost/api/dashboard/stats') |
| 185 | + const res = await GET(req as never) |
| 186 | + |
| 187 | + expect(res.status).toBe(500) |
| 188 | + const body = await res.json() as { code: string } |
| 189 | + expect(body.code).toBe('INTERNAL_ERROR') |
| 190 | + }) |
| 191 | +}) |
0 commit comments