|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { getRepositoryToken } from '@nestjs/typeorm'; |
| 3 | +import { AnalyticsService } from './analytics.service'; |
| 4 | +import { TransactionEntity } from '../transactions/entities/transaction.entity'; |
| 5 | +import { BudgetEntity } from '../budgets/entities/budget.entity'; |
| 6 | + |
| 7 | +describe('AnalyticsService', () => { |
| 8 | + let service: AnalyticsService; |
| 9 | + let txRepo: any; |
| 10 | + let budgetRepo: any; |
| 11 | + |
| 12 | + const mockQueryBuilder = { |
| 13 | + select: jest.fn().mockReturnThis(), |
| 14 | + addSelect: jest.fn().mockReturnThis(), |
| 15 | + where: jest.fn().mockReturnThis(), |
| 16 | + andWhere: jest.fn().mockReturnThis(), |
| 17 | + groupBy: jest.fn().mockReturnThis(), |
| 18 | + addGroupBy: jest.fn().mockReturnThis(), |
| 19 | + orderBy: jest.fn().mockReturnThis(), |
| 20 | + leftJoin: jest.fn().mockReturnThis(), |
| 21 | + limit: jest.fn().mockReturnThis(), |
| 22 | + getRawMany: jest.fn(), |
| 23 | + }; |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + txRepo = { |
| 27 | + createQueryBuilder: jest.fn(() => ({ ...mockQueryBuilder })), |
| 28 | + }; |
| 29 | + budgetRepo = { |
| 30 | + createQueryBuilder: jest.fn(() => ({ ...mockQueryBuilder })), |
| 31 | + }; |
| 32 | + |
| 33 | + const module: TestingModule = await Test.createTestingModule({ |
| 34 | + providers: [ |
| 35 | + AnalyticsService, |
| 36 | + { provide: getRepositoryToken(TransactionEntity), useValue: txRepo }, |
| 37 | + { provide: getRepositoryToken(BudgetEntity), useValue: budgetRepo }, |
| 38 | + ], |
| 39 | + }).compile(); |
| 40 | + |
| 41 | + service = module.get<AnalyticsService>(AnalyticsService); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => jest.clearAllMocks()); |
| 45 | + |
| 46 | + describe('status', () => { |
| 47 | + it('returns ready status', () => { |
| 48 | + expect(service.status()).toEqual({ module: 'analytics', status: 'ready' }); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('getSpendingTrend', () => { |
| 53 | + it('returns weekly spending buckets for the given asset', async () => { |
| 54 | + const mockRows = [ |
| 55 | + { week: '2026-08-18T00:00:00.000Z', total: '150.000000000000' }, |
| 56 | + { week: '2026-08-11T00:00:00.000Z', total: '200.500000000000' }, |
| 57 | + ]; |
| 58 | + const qb = txRepo.createQueryBuilder(); |
| 59 | + qb.getRawMany.mockResolvedValue(mockRows); |
| 60 | + |
| 61 | + const result = await service.getSpendingTrend('user-1', 'USDC', 2); |
| 62 | + |
| 63 | + expect(result).toEqual(mockRows); |
| 64 | + expect(qb.where).toHaveBeenCalledWith('t."userId" = :userId', { |
| 65 | + userId: 'user-1', |
| 66 | + }); |
| 67 | + expect(qb.andWhere).toHaveBeenCalledWith('t."asset" = :asset', { |
| 68 | + asset: 'USDC', |
| 69 | + }); |
| 70 | + expect(qb.limit).toHaveBeenCalledWith(2); |
| 71 | + }); |
| 72 | + |
| 73 | + it('defaults to 8 weeks when weeks is not specified', async () => { |
| 74 | + const qb = txRepo.createQueryBuilder(); |
| 75 | + qb.getRawMany.mockResolvedValue([]); |
| 76 | + |
| 77 | + await service.getSpendingTrend('user-1', 'USDC'); |
| 78 | + |
| 79 | + expect(qb.limit).toHaveBeenCalledWith(8); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('getCategoryBreakdown', () => { |
| 84 | + it('returns category totals with percentage computed in SQL', async () => { |
| 85 | + const mockRows = [ |
| 86 | + { category: 'groceries', total: '150.00', percentage: '60.000000' }, |
| 87 | + { category: 'entertainment', total: '100.00', percentage: '40.000000' }, |
| 88 | + ]; |
| 89 | + const qb = txRepo.createQueryBuilder(); |
| 90 | + qb.getRawMany.mockResolvedValue(mockRows); |
| 91 | + |
| 92 | + const result = await service.getCategoryBreakdown( |
| 93 | + 'user-1', |
| 94 | + 'USDC', |
| 95 | + new Date('2026-01-01'), |
| 96 | + ); |
| 97 | + |
| 98 | + expect(result).toEqual(mockRows); |
| 99 | + expect(result).toHaveLength(2); |
| 100 | + }); |
| 101 | + |
| 102 | + it('uses the since date filter', async () => { |
| 103 | + const qb = txRepo.createQueryBuilder(); |
| 104 | + qb.getRawMany.mockResolvedValue([]); |
| 105 | + |
| 106 | + const since = new Date('2026-06-01'); |
| 107 | + await service.getCategoryBreakdown('user-1', 'XLM', since); |
| 108 | + |
| 109 | + expect(qb.andWhere).toHaveBeenCalledWith( |
| 110 | + 't."createdAt" >= :since', |
| 111 | + { since }, |
| 112 | + ); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('getBudgetVsActual', () => { |
| 117 | + it('joins budgets to transactions and computes variance', async () => { |
| 118 | + const mockRows = [ |
| 119 | + { budgetName: 'Groceries', budgeted: '200.00', actual: '150.00', variance: '-50.00' }, |
| 120 | + { budgetName: 'Rent', budgeted: '500.00', actual: '500.00', variance: '0.00' }, |
| 121 | + ]; |
| 122 | + const qb = budgetRepo.createQueryBuilder(); |
| 123 | + qb.getRawMany.mockResolvedValue(mockRows); |
| 124 | + |
| 125 | + const result = await service.getBudgetVsActual('user-1'); |
| 126 | + |
| 127 | + expect(result).toEqual(mockRows); |
| 128 | + expect(result).toHaveLength(2); |
| 129 | + expect(result[0].variance).toBe('-50.00'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('returns empty array when user has no budgets', async () => { |
| 133 | + const qb = budgetRepo.createQueryBuilder(); |
| 134 | + qb.getRawMany.mockResolvedValue([]); |
| 135 | + |
| 136 | + const result = await service.getBudgetVsActual('user-no-budgets'); |
| 137 | + |
| 138 | + expect(result).toEqual([]); |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments