|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { AuthMetricsController } from './auth-metrics.controller'; |
| 3 | +import { AuthMetricsService, AuthMetricsSnapshot } from './auth-metrics.service'; |
| 4 | + |
| 5 | +const makeSnapshot = (overrides: Partial<AuthMetricsSnapshot> = {}): AuthMetricsSnapshot => ({ |
| 6 | + totalAttempts: 0, |
| 7 | + outcomes: { |
| 8 | + success_new_user: 0, |
| 9 | + success_returning_user: 0, |
| 10 | + failure_invalid_payload: 0, |
| 11 | + failure_user_inactive: 0, |
| 12 | + failure_wallet_error: 0, |
| 13 | + failure_unknown: 0, |
| 14 | + }, |
| 15 | + rateLimitHits: 0, |
| 16 | + averageLatencyMs: 0, |
| 17 | + p95LatencyMs: 0, |
| 18 | + lastResetAt: new Date('2026-01-01T00:00:00Z'), |
| 19 | + ...overrides, |
| 20 | +}); |
| 21 | + |
| 22 | +describe('AuthMetricsController', () => { |
| 23 | + let controller: AuthMetricsController; |
| 24 | + let metricsService: jest.Mocked<AuthMetricsService>; |
| 25 | + |
| 26 | + beforeEach(async () => { |
| 27 | + metricsService = { |
| 28 | + recordAttempt: jest.fn(), |
| 29 | + recordRateLimitHit: jest.fn(), |
| 30 | + getSnapshot: jest.fn(), |
| 31 | + reset: jest.fn(), |
| 32 | + } as unknown as jest.Mocked<AuthMetricsService>; |
| 33 | + |
| 34 | + const module: TestingModule = await Test.createTestingModule({ |
| 35 | + controllers: [AuthMetricsController], |
| 36 | + providers: [{ provide: AuthMetricsService, useValue: metricsService }], |
| 37 | + }).compile(); |
| 38 | + |
| 39 | + controller = module.get(AuthMetricsController); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should be defined', () => { |
| 43 | + expect(controller).toBeDefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('getMetrics()', () => { |
| 47 | + it('delegates to AuthMetricsService.getSnapshot()', () => { |
| 48 | + const snap = makeSnapshot({ totalAttempts: 42, rateLimitHits: 3 }); |
| 49 | + metricsService.getSnapshot.mockReturnValue(snap); |
| 50 | + |
| 51 | + const result = controller.getMetrics(); |
| 52 | + |
| 53 | + expect(metricsService.getSnapshot).toHaveBeenCalledTimes(1); |
| 54 | + expect(result).toEqual(snap); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns a snapshot with all expected fields', () => { |
| 58 | + const snap = makeSnapshot({ |
| 59 | + totalAttempts: 10, |
| 60 | + averageLatencyMs: 120, |
| 61 | + p95LatencyMs: 300, |
| 62 | + outcomes: { |
| 63 | + success_new_user: 3, |
| 64 | + success_returning_user: 5, |
| 65 | + failure_invalid_payload: 1, |
| 66 | + failure_user_inactive: 0, |
| 67 | + failure_wallet_error: 0, |
| 68 | + failure_unknown: 1, |
| 69 | + }, |
| 70 | + }); |
| 71 | + metricsService.getSnapshot.mockReturnValue(snap); |
| 72 | + |
| 73 | + const result = controller.getMetrics(); |
| 74 | + expect(result.totalAttempts).toBe(10); |
| 75 | + expect(result.averageLatencyMs).toBe(120); |
| 76 | + expect(result.p95LatencyMs).toBe(300); |
| 77 | + expect(result.outcomes.success_new_user).toBe(3); |
| 78 | + }); |
| 79 | + |
| 80 | + it('returns zero-state snapshot when no auth has occurred', () => { |
| 81 | + metricsService.getSnapshot.mockReturnValue(makeSnapshot()); |
| 82 | + const result = controller.getMetrics(); |
| 83 | + expect(result.totalAttempts).toBe(0); |
| 84 | + expect(result.rateLimitHits).toBe(0); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments