|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { getDataSourceToken } from '@nestjs/typeorm'; |
| 3 | +import type { Response } from 'express'; |
| 4 | +import { HealthController } from './health.controller'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Minimal Express Response mock. The controller uses |
| 8 | + * `passthrough: true` and only ever calls `res.status(...)`, so other |
| 9 | + * members of the Response surface are irrelevant for unit tests. |
| 10 | + * |
| 11 | + * The `as unknown as Response` cast happens at the call site, not in the |
| 12 | + * factory, so the cast is local and obvious — easier for a reviewer to |
| 13 | + * trace than piping it through a structural Pick<Response, 'status'> |
| 14 | + * type that still leaves the rest of the Response surface unfilled. |
| 15 | + */ |
| 16 | +function buildResMock(): { status: jest.Mock } { |
| 17 | + return { |
| 18 | + status: jest.fn().mockReturnThis(), |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +describe('HealthController (unit)', () => { |
| 23 | + let controller: HealthController; |
| 24 | + let queryMock: jest.Mock; |
| 25 | + let res: { status: jest.Mock }; |
| 26 | + |
| 27 | + beforeEach(async () => { |
| 28 | + queryMock = jest.fn(); |
| 29 | + res = buildResMock(); |
| 30 | + |
| 31 | + const moduleRef: TestingModule = await Test.createTestingModule({ |
| 32 | + controllers: [HealthController], |
| 33 | + providers: [ |
| 34 | + { |
| 35 | + provide: getDataSourceToken(), |
| 36 | + useValue: { query: queryMock }, |
| 37 | + }, |
| 38 | + ], |
| 39 | + }).compile(); |
| 40 | + |
| 41 | + controller = moduleRef.get(HealthController); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('HTTP status ↔ body.status contract', () => { |
| 45 | + it('returns 200 and body.status="ok" when DB + PostGIS both succeed', async () => { |
| 46 | + queryMock.mockResolvedValue([{ version: '3.4.0' }]); |
| 47 | + |
| 48 | + const body = await controller.check(res as unknown as Response); |
| 49 | + |
| 50 | + expect(res.status).toHaveBeenCalledWith(200); |
| 51 | + expect(body).toEqual({ |
| 52 | + status: 'ok', |
| 53 | + timestamp: expect.any(String), |
| 54 | + services: { |
| 55 | + database: { status: 'ok' }, |
| 56 | + postgis: { status: 'ok', message: expect.stringMatching(/^PostGIS /) }, |
| 57 | + }, |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns 503 and body.status="degraded" when the database probe fails', async () => { |
| 62 | + queryMock.mockRejectedValue(new Error('connection terminated')); |
| 63 | + |
| 64 | + const body = await controller.check(res as unknown as Response); |
| 65 | + |
| 66 | + expect(res.status).toHaveBeenCalledWith(503); |
| 67 | + expect(body.status).toBe('degraded'); |
| 68 | + expect(body.services.database).toEqual({ |
| 69 | + status: 'error', |
| 70 | + message: 'connection terminated', |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns 503 and body.status="degraded" when only PostGIS is missing', async () => { |
| 75 | + queryMock.mockImplementation((sql: string) => { |
| 76 | + if (typeof sql === 'string' && sql.includes('postgis_lib_version')) { |
| 77 | + return Promise.reject(new Error('function postgis_lib_version() does not exist')); |
| 78 | + } |
| 79 | + return Promise.resolve([]); |
| 80 | + }); |
| 81 | + |
| 82 | + const body = await controller.check(res as unknown as Response); |
| 83 | + |
| 84 | + expect(res.status).toHaveBeenCalledWith(503); |
| 85 | + expect(body.status).toBe('degraded'); |
| 86 | + expect(body.services.database.status).toBe('ok'); |
| 87 | + expect(body.services.postgis.status).toBe('error'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('envelope invariants', () => { |
| 92 | + it('emits a parseable ISO-8601 timestamp every call', async () => { |
| 93 | + queryMock.mockResolvedValue([{ version: '3.4.0' }]); |
| 94 | + |
| 95 | + const body = await controller.check(res as unknown as Response); |
| 96 | + const stamp = Date.parse(body.timestamp); |
| 97 | + |
| 98 | + expect(Number.isFinite(stamp)).toBe(true); |
| 99 | + expect(Math.abs(Date.now() - stamp)).toBeLessThan(5_000); |
| 100 | + }); |
| 101 | + |
| 102 | + it('returns the full envelope shape on the happy path (no body truncation)', async () => { |
| 103 | + queryMock.mockResolvedValue([{ version: '3.4.0' }]); |
| 104 | + |
| 105 | + const body = await controller.check(res as unknown as Response); |
| 106 | + |
| 107 | + expect(body).toEqual({ |
| 108 | + status: 'ok', |
| 109 | + timestamp: expect.any(String), |
| 110 | + services: { |
| 111 | + database: { status: 'ok' }, |
| 112 | + postgis: { status: 'ok', message: expect.stringMatching(/^PostGIS /) }, |
| 113 | + }, |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + it('returns the full envelope shape on the degraded path (no body truncation)', async () => { |
| 118 | + // First query (`SELECT 1`) fails imitating a DB outage, second |
| 119 | + // query (`SELECT postgis_lib_version()`) succeeds imitating an |
| 120 | + // outage that took DB pool offline but left postgis_lib_version |
| 121 | + // safely resolvable. We assert BOTH halves of the degraded body |
| 122 | + // (the failing and the still-healthy sub-service) come through. |
| 123 | + queryMock |
| 124 | + .mockRejectedValueOnce(new Error('outage')) |
| 125 | + .mockResolvedValueOnce([{ version: '3.4.0' }]); |
| 126 | + |
| 127 | + const fresh = buildResMock(); |
| 128 | + const body = await controller.check(fresh as unknown as Response); |
| 129 | + |
| 130 | + expect(fresh.status).toHaveBeenCalledWith(503); |
| 131 | + expect(body).toEqual({ |
| 132 | + status: 'degraded', |
| 133 | + timestamp: expect.any(String), |
| 134 | + services: { |
| 135 | + database: { status: 'error', message: 'outage' }, |
| 136 | + postgis: { status: 'ok', message: expect.stringMatching(/^PostGIS /) }, |
| 137 | + }, |
| 138 | + }); |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments