|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { HealthController } from '../src/health/health.controller'; |
| 3 | +import { HealthCheckService } from '@nestjs/terminus'; |
| 4 | +import { |
| 5 | + DatabaseHealthIndicator, |
| 6 | + RedisHealthIndicator, |
| 7 | + StellarHealthIndicator, |
| 8 | + SorobanHealthIndicator, |
| 9 | +} from '../src/health/indicators'; |
| 10 | + |
| 11 | +const mockHealthCheck = jest.fn(); |
| 12 | +const mockDbHealth = { isHealthy: jest.fn() }; |
| 13 | +const mockRedisHealth = { isHealthy: jest.fn() }; |
| 14 | +const mockStellarHealth = { isHealthy: jest.fn() }; |
| 15 | +const mockSorobanHealth = { isHealthy: jest.fn() }; |
| 16 | + |
| 17 | +describe('HealthController (#370)', () => { |
| 18 | + let controller: HealthController; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + const module: TestingModule = await Test.createTestingModule({ |
| 23 | + controllers: [HealthController], |
| 24 | + providers: [ |
| 25 | + { provide: HealthCheckService, useValue: { check: mockHealthCheck } }, |
| 26 | + { provide: DatabaseHealthIndicator, useValue: mockDbHealth }, |
| 27 | + { provide: RedisHealthIndicator, useValue: mockRedisHealth }, |
| 28 | + { provide: StellarHealthIndicator, useValue: mockStellarHealth }, |
| 29 | + { provide: SorobanHealthIndicator, useValue: mockSorobanHealth }, |
| 30 | + ], |
| 31 | + }).compile(); |
| 32 | + |
| 33 | + controller = module.get(HealthController); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should pass startup health check when DB and cache are healthy', async () => { |
| 37 | + mockHealthCheck.mockResolvedValue({ status: 'ok', info: {}, error: {}, details: {} }); |
| 38 | + await expect(controller.onApplicationBootstrap()).resolves.not.toThrow(); |
| 39 | + expect(mockHealthCheck).toHaveBeenCalledTimes(1); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should retry and exit if dependencies remain unhealthy', async () => { |
| 43 | + const exitSpy = jest.spyOn(process, 'exit').mockImplementation((() => {}) as any); |
| 44 | + mockHealthCheck.mockRejectedValue(new Error('DB unavailable')); |
| 45 | + |
| 46 | + // Speed up retries |
| 47 | + jest.useFakeTimers(); |
| 48 | + const bootstrapPromise = controller.onApplicationBootstrap(); |
| 49 | + // Advance through all retry delays (5 retries × 3000ms) |
| 50 | + for (let i = 0; i < 5; i++) { |
| 51 | + await Promise.resolve(); |
| 52 | + jest.advanceTimersByTime(3000); |
| 53 | + } |
| 54 | + await bootstrapPromise.catch(() => {}); |
| 55 | + jest.useRealTimers(); |
| 56 | + |
| 57 | + expect(exitSpy).toHaveBeenCalledWith(1); |
| 58 | + exitSpy.mockRestore(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('GET /health should check all indicators', async () => { |
| 62 | + mockHealthCheck.mockResolvedValue({ status: 'ok', info: {}, error: {}, details: {} }); |
| 63 | + await controller.check(); |
| 64 | + expect(mockHealthCheck).toHaveBeenCalledWith(expect.arrayContaining([ |
| 65 | + expect.any(Function), |
| 66 | + expect.any(Function), |
| 67 | + expect.any(Function), |
| 68 | + expect.any(Function), |
| 69 | + ])); |
| 70 | + }); |
| 71 | + |
| 72 | + it('GET /health/readiness should check DB and cache only', async () => { |
| 73 | + mockHealthCheck.mockResolvedValue({ status: 'ok', info: {}, error: {}, details: {} }); |
| 74 | + await controller.readiness(); |
| 75 | + expect(mockHealthCheck).toHaveBeenCalledWith(expect.arrayContaining([ |
| 76 | + expect.any(Function), |
| 77 | + expect.any(Function), |
| 78 | + ])); |
| 79 | + }); |
| 80 | +}); |
0 commit comments