import { describe, expect, it } from 'vitest'; import { buildHealthReport, isHealthRequest } from './health.js'; describe('buildHealthReport', () => { it('reports ok with HTTP 200 when the database is reachable', () => { const { httpStatus, body } = buildHealthReport('1.2.3', { ok: true }); expect(httpStatus).toBe(200); expect(body.status).toBe('ok'); expect(body.service).toBe('collab'); expect(body.version).toBe('1.2.3'); expect(body.checks).toEqual([{ name: 'database', status: 'ok' }]); expect(typeof body.time).toBe('string'); }); it('reports unhealthy with HTTP 503 and the detail when the database is down', () => { const { httpStatus, body } = buildHealthReport('1.2.3', { ok: false, detail: 'connection refused', }); expect(httpStatus).toBe(503); expect(body.status).toBe('unhealthy'); expect(body.checks).toEqual([ { name: 'database', status: 'failed', detail: 'connection refused' }, ]); }); }); describe('isHealthRequest', () => { it('matches the internal and proxied health paths on GET/HEAD', () => { expect(isHealthRequest('GET', '/healthz')).toBe(true); expect(isHealthRequest('HEAD', '/healthz')).toBe(true); expect(isHealthRequest('GET', '/healthz?probe=1')).toBe(true); expect(isHealthRequest('GET', '/collab/healthz')).toBe(true); expect(isHealthRequest('GET', '/collab/healthz/')).toBe(true); }); it('does not match other methods, the WebSocket path, or the root', () => { expect(isHealthRequest('POST', '/healthz')).toBe(false); expect(isHealthRequest('GET', '/collab')).toBe(false); expect(isHealthRequest('GET', '/')).toBe(false); expect(isHealthRequest('GET', undefined)).toBe(false); }); });