|
| 1 | +import { Logger } from '@nestjs/common'; |
| 2 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 3 | +import { getRepositoryToken } from '@nestjs/typeorm'; |
| 4 | +import { AnalyticsEvent } from '../entities/analytics-event.entity'; |
| 5 | +import { DailyActiveUser } from '../entities/daily-active-user.entity'; |
| 6 | +import { DailyActiveUsersRollupJob } from './daily-active-users-rollup.job'; |
| 7 | + |
| 8 | +describe('DailyActiveUsersRollupJob', () => { |
| 9 | + let job: DailyActiveUsersRollupJob; |
| 10 | + const mockAnalyticsEventRepo = { |
| 11 | + find: jest.fn<Promise<Pick<AnalyticsEvent, 'userId'>[]>, [unknown]>(), |
| 12 | + }; |
| 13 | + const mockDailyActiveUserRepo = { |
| 14 | + delete: jest.fn<Promise<unknown>, [unknown]>(), |
| 15 | + create: jest.fn( |
| 16 | + (data: Partial<DailyActiveUser>) => data as DailyActiveUser, |
| 17 | + ), |
| 18 | + save: jest.fn<Promise<DailyActiveUser[]>, [DailyActiveUser[]]>(), |
| 19 | + }; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + |
| 24 | + const module: TestingModule = await Test.createTestingModule({ |
| 25 | + providers: [ |
| 26 | + DailyActiveUsersRollupJob, |
| 27 | + { |
| 28 | + provide: getRepositoryToken(AnalyticsEvent), |
| 29 | + useValue: mockAnalyticsEventRepo, |
| 30 | + }, |
| 31 | + { |
| 32 | + provide: getRepositoryToken(DailyActiveUser), |
| 33 | + useValue: mockDailyActiveUserRepo, |
| 34 | + }, |
| 35 | + ], |
| 36 | + }).compile(); |
| 37 | + |
| 38 | + job = module.get<DailyActiveUsersRollupJob>(DailyActiveUsersRollupJob); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should be defined', () => { |
| 42 | + expect(job).toBeDefined(); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('rollupForDate', () => { |
| 46 | + it('dedupes users who fired multiple events on the same day', async () => { |
| 47 | + mockAnalyticsEventRepo.find.mockResolvedValue([ |
| 48 | + { userId: 'user-1' }, |
| 49 | + { userId: 'user-1' }, |
| 50 | + { userId: 'user-2' }, |
| 51 | + { userId: 'user-1' }, |
| 52 | + { userId: 'user-3' }, |
| 53 | + { userId: 'user-2' }, |
| 54 | + ] as AnalyticsEvent[]); |
| 55 | + |
| 56 | + const result = await job.rollupForDate( |
| 57 | + new Date('2026-07-22T12:00:00.000Z'), |
| 58 | + ); |
| 59 | + |
| 60 | + expect(result).toEqual({ date: '2026-07-22', rowsWritten: 3 }); |
| 61 | + expect(mockDailyActiveUserRepo.save).toHaveBeenCalledTimes(1); |
| 62 | + |
| 63 | + const savedRows = mockDailyActiveUserRepo.save.mock.calls[0][0]; |
| 64 | + const savedUserIds = savedRows.map((row) => row.userId).sort(); |
| 65 | + expect(savedUserIds).toEqual(['user-1', 'user-2', 'user-3']); |
| 66 | + savedRows.forEach((row) => expect(row.date).toBe('2026-07-22')); |
| 67 | + }); |
| 68 | + |
| 69 | + it('queries raw events for the correct UTC day bounds', async () => { |
| 70 | + mockAnalyticsEventRepo.find.mockResolvedValue([]); |
| 71 | + |
| 72 | + await job.rollupForDate(new Date('2026-07-22T12:00:00.000Z')); |
| 73 | + |
| 74 | + expect(mockAnalyticsEventRepo.find).toHaveBeenCalledWith( |
| 75 | + expect.objectContaining({ |
| 76 | + select: ['userId'], |
| 77 | + where: expect.objectContaining({ timestamp: expect.anything() }), |
| 78 | + }), |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('is idempotent: re-running for the same day replaces rather than duplicates rows', async () => { |
| 83 | + mockAnalyticsEventRepo.find.mockResolvedValue([ |
| 84 | + { userId: 'user-1' }, |
| 85 | + { userId: 'user-2' }, |
| 86 | + ] as AnalyticsEvent[]); |
| 87 | + |
| 88 | + await job.rollupForDate(new Date('2026-07-22T00:00:00.000Z')); |
| 89 | + await job.rollupForDate(new Date('2026-07-22T00:00:00.000Z')); |
| 90 | + |
| 91 | + expect(mockDailyActiveUserRepo.delete).toHaveBeenCalledTimes(2); |
| 92 | + expect(mockDailyActiveUserRepo.delete).toHaveBeenNthCalledWith(1, { |
| 93 | + date: '2026-07-22', |
| 94 | + }); |
| 95 | + expect(mockDailyActiveUserRepo.delete).toHaveBeenNthCalledWith(2, { |
| 96 | + date: '2026-07-22', |
| 97 | + }); |
| 98 | + expect(mockDailyActiveUserRepo.save).toHaveBeenCalledTimes(2); |
| 99 | + }); |
| 100 | + |
| 101 | + it('writes no rows and skips save when no users were active', async () => { |
| 102 | + mockAnalyticsEventRepo.find.mockResolvedValue([]); |
| 103 | + |
| 104 | + const result = await job.rollupForDate( |
| 105 | + new Date('2026-07-22T00:00:00.000Z'), |
| 106 | + ); |
| 107 | + |
| 108 | + expect(result.rowsWritten).toBe(0); |
| 109 | + expect(mockDailyActiveUserRepo.delete).toHaveBeenCalledWith({ |
| 110 | + date: '2026-07-22', |
| 111 | + }); |
| 112 | + expect(mockDailyActiveUserRepo.save).not.toHaveBeenCalled(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('logs a summary of rows written on completion', async () => { |
| 116 | + mockAnalyticsEventRepo.find.mockResolvedValue([ |
| 117 | + { userId: 'user-1' }, |
| 118 | + ] as AnalyticsEvent[]); |
| 119 | + const loggerSpy = jest.spyOn(Logger.prototype, 'log'); |
| 120 | + |
| 121 | + await job.rollupForDate(new Date('2026-07-22T00:00:00.000Z')); |
| 122 | + |
| 123 | + expect(loggerSpy).toHaveBeenCalledWith( |
| 124 | + expect.stringContaining('2026-07-22'), |
| 125 | + ); |
| 126 | + expect(loggerSpy).toHaveBeenCalledWith( |
| 127 | + expect.stringContaining('1 rows written'), |
| 128 | + ); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments