|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { HealthController } from './health.controller'; |
| 3 | +import { HealthService } from './health.service'; |
| 4 | +import { HealthCheckResult } from './health.interfaces'; |
| 5 | + |
| 6 | +describe('HealthController', () => { |
| 7 | + let controller: HealthController; |
| 8 | + let healthService: HealthService; |
| 9 | + |
| 10 | + const mockHealthService = { |
| 11 | + getBasicHealth: jest.fn(), |
| 12 | + getLivenessHealth: jest.fn(), |
| 13 | + getReadinessHealth: jest.fn(), |
| 14 | + getDetailedHealth: jest.fn(), |
| 15 | + isAppShuttingDown: jest.fn(), |
| 16 | + }; |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + const module: TestingModule = await Test.createTestingModule({ |
| 20 | + controllers: [HealthController], |
| 21 | + providers: [ |
| 22 | + { |
| 23 | + provide: HealthService, |
| 24 | + useValue: mockHealthService, |
| 25 | + }, |
| 26 | + ], |
| 27 | + }).compile(); |
| 28 | + |
| 29 | + controller = module.get<HealthController>(HealthController); |
| 30 | + healthService = module.get<HealthService>(HealthService); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + jest.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('GET /health', () => { |
| 38 | + it('should return basic health check', async () => { |
| 39 | + const expectedResult: HealthCheckResult = { |
| 40 | + status: 'healthy', |
| 41 | + version: '1.0.0', |
| 42 | + uptime: 3600, |
| 43 | + timestamp: '2023-01-01T00:00:00.000Z', |
| 44 | + }; |
| 45 | + |
| 46 | + mockHealthService.isAppShuttingDown.mockReturnValue(false); |
| 47 | + mockHealthService.getBasicHealth.mockResolvedValue(expectedResult); |
| 48 | + |
| 49 | + const result = await controller.getBasicHealth(); |
| 50 | + |
| 51 | + expect(result).toEqual(expectedResult); |
| 52 | + expect(healthService.isAppShuttingDown).toHaveBeenCalled(); |
| 53 | + expect(healthService.getBasicHealth).toHaveBeenCalled(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should return 403 when app is shutting down', async () => { |
| 57 | + mockHealthService.isAppShuttingDown.mockReturnValue(true); |
| 58 | + |
| 59 | + await expect(controller.getBasicHealth()).rejects.toThrow('Application is shutting down'); |
| 60 | + expect(healthService.isAppShuttingDown).toHaveBeenCalled(); |
| 61 | + expect(healthService.getBasicHealth).not.toHaveBeenCalled(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('GET /health/live', () => { |
| 66 | + it('should return liveness health check', async () => { |
| 67 | + const expectedResult: HealthCheckResult = { |
| 68 | + status: 'healthy', |
| 69 | + version: '1.0.0', |
| 70 | + uptime: 3600, |
| 71 | + timestamp: '2023-01-01T00:00:00.000Z', |
| 72 | + }; |
| 73 | + |
| 74 | + mockHealthService.isAppShuttingDown.mockReturnValue(false); |
| 75 | + mockHealthService.getLivenessHealth.mockResolvedValue(expectedResult); |
| 76 | + |
| 77 | + const result = await controller.getLivenessHealth(); |
| 78 | + |
| 79 | + expect(result).toEqual(expectedResult); |
| 80 | + expect(healthService.isAppShuttingDown).toHaveBeenCalled(); |
| 81 | + expect(healthService.getLivenessHealth).toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should return 403 when app is shutting down', async () => { |
| 85 | + mockHealthService.isAppShuttingDown.mockReturnValue(true); |
| 86 | + |
| 87 | + await expect(controller.getLivenessHealth()).rejects.toThrow('Application is shutting down'); |
| 88 | + expect(healthService.isAppShuttingDown).toHaveBeenCalled(); |
| 89 | + expect(healthService.getLivenessHealth).not.toHaveBeenCalled(); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('GET /health/ready', () => { |
| 94 | + it('should return readiness health check when healthy', async () => { |
| 95 | + const expectedResult: HealthCheckResult = { |
| 96 | + status: 'healthy', |
| 97 | + version: '1.0.0', |
| 98 | + uptime: 3600, |
| 99 | + timestamp: '2023-01-01T00:00:00.000Z', |
| 100 | + checks: { |
| 101 | + database: { status: 'healthy', responseTime: 5 }, |
| 102 | + redis: { status: 'healthy', responseTime: 2 }, |
| 103 | + memory: { status: 'healthy', responseTime: 1 }, |
| 104 | + filesystem: { status: 'healthy', responseTime: 1 }, |
| 105 | + }, |
| 106 | + }; |
| 107 | + |
| 108 | + mockHealthService.isAppShuttingDown.mockReturnValue(false); |
| 109 | + mockHealthService.getReadinessHealth.mockResolvedValue(expectedResult); |
| 110 | + |
| 111 | + const result = await controller.getReadinessHealth(); |
| 112 | + |
| 113 | + expect(result).toEqual(expectedResult); |
| 114 | + expect(healthService.isAppShuttingDown).toHaveBeenCalled(); |
| 115 | + expect(healthService.getReadinessHealth).toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should return 403 when app is shutting down', async () => { |
| 119 | + mockHealthService.isAppShuttingDown.mockReturnValue(true); |
| 120 | + |
| 121 | + await expect(controller.getReadinessHealth()).rejects.toThrow('Application is shutting down'); |
| 122 | + expect(healthService.isAppShuttingDown).toHaveBeenCalled(); |
| 123 | + expect(healthService.getReadinessHealth).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should return 403 when service is not ready', async () => { |
| 127 | + const expectedResult: HealthCheckResult = { |
| 128 | + status: 'unhealthy', |
| 129 | + version: '1.0.0', |
| 130 | + uptime: 3600, |
| 131 | + timestamp: '2023-01-01T00:00:00.000Z', |
| 132 | + checks: { |
| 133 | + database: { status: 'unhealthy', error: 'Connection failed' }, |
| 134 | + redis: { status: 'healthy', responseTime: 2 }, |
| 135 | + memory: { status: 'healthy', responseTime: 1 }, |
| 136 | + filesystem: { status: 'healthy', responseTime: 1 }, |
| 137 | + }, |
| 138 | + }; |
| 139 | + |
| 140 | + mockHealthService.isAppShuttingDown.mockReturnValue(false); |
| 141 | + mockHealthService.getReadinessHealth.mockResolvedValue(expectedResult); |
| 142 | + |
| 143 | + await expect(controller.getReadinessHealth()).rejects.toThrow('Service not ready'); |
| 144 | + expect(healthService.isAppShuttingDown).toHaveBeenCalled(); |
| 145 | + expect(healthService.getReadinessHealth).toHaveBeenCalled(); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('GET /health/detailed', () => { |
| 150 | + const originalEnv = process.env; |
| 151 | + |
| 152 | + beforeEach(() => { |
| 153 | + process.env = { ...originalEnv, ADMIN_HEALTH_KEY: 'test-admin-key' }; |
| 154 | + }); |
| 155 | + |
| 156 | + afterEach(() => { |
| 157 | + process.env = originalEnv; |
| 158 | + }); |
| 159 | + |
| 160 | + it('should return detailed health check with valid admin key', async () => { |
| 161 | + const expectedResult: HealthCheckResult = { |
| 162 | + status: 'healthy', |
| 163 | + version: '1.0.0', |
| 164 | + uptime: 3600, |
| 165 | + timestamp: '2023-01-01T00:00:00.000Z', |
| 166 | + checks: { |
| 167 | + database: { status: 'healthy', responseTime: 5, details: { connected: true } }, |
| 168 | + redis: { status: 'healthy', responseTime: 2, details: { connected: true } }, |
| 169 | + memory: { status: 'healthy', responseTime: 1, details: { usagePercent: '45%' } }, |
| 170 | + filesystem: { status: 'healthy', responseTime: 1, details: { writable: true } }, |
| 171 | + }, |
| 172 | + }; |
| 173 | + |
| 174 | + mockHealthService.isAppShuttingDown.mockReturnValue(false); |
| 175 | + mockHealthService.getDetailedHealth.mockResolvedValue(expectedResult); |
| 176 | + |
| 177 | + const result = await controller.getDetailedHealth('test-admin-key'); |
| 178 | + |
| 179 | + expect(result).toEqual(expectedResult); |
| 180 | + expect(healthService.isAppShuttingDown).toHaveBeenCalled(); |
| 181 | + expect(healthService.getDetailedHealth).toHaveBeenCalled(); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should return 403 with invalid admin key', async () => { |
| 185 | + await expect(controller.getDetailedHealth('invalid-key')).rejects.toThrow('Admin access required'); |
| 186 | + expect(healthService.isAppShuttingDown).not.toHaveBeenCalled(); |
| 187 | + expect(healthService.getDetailedHealth).not.toHaveBeenCalled(); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should return 403 when app is shutting down', async () => { |
| 191 | + mockHealthService.isAppShuttingDown.mockReturnValue(true); |
| 192 | + |
| 193 | + await expect(controller.getDetailedHealth('test-admin-key')).rejects.toThrow('Application is shutting down'); |
| 194 | + expect(healthService.isAppShuttingDown).toHaveBeenCalled(); |
| 195 | + expect(healthService.getDetailedHealth).not.toHaveBeenCalled(); |
| 196 | + }); |
| 197 | + }); |
| 198 | +}); |
0 commit comments