|
| 1 | +import { describe, test, expect } from 'bun:test' |
| 2 | + |
| 3 | +import { getHealthz } from '../_get' |
| 4 | + |
| 5 | +import type { HealthzDeps } from '../_get' |
| 6 | + |
| 7 | +describe('/api/healthz route', () => { |
| 8 | + describe('Success cases', () => { |
| 9 | + test('returns 200 with status ok and agent count', async () => { |
| 10 | + const mockGetAgentCount = async () => 42 |
| 11 | + |
| 12 | + const response = await getHealthz({ getAgentCount: mockGetAgentCount }) |
| 13 | + |
| 14 | + expect(response.status).toBe(200) |
| 15 | + const body = await response.json() |
| 16 | + expect(body.status).toBe('ok') |
| 17 | + expect(body.cached_agents).toBe(42) |
| 18 | + expect(body.timestamp).toBeDefined() |
| 19 | + expect(typeof body.timestamp).toBe('string') |
| 20 | + }) |
| 21 | + |
| 22 | + test('returns correct count when no agents exist', async () => { |
| 23 | + const mockGetAgentCount = async () => 0 |
| 24 | + |
| 25 | + const response = await getHealthz({ getAgentCount: mockGetAgentCount }) |
| 26 | + |
| 27 | + expect(response.status).toBe(200) |
| 28 | + const body = await response.json() |
| 29 | + expect(body.status).toBe('ok') |
| 30 | + expect(body.cached_agents).toBe(0) |
| 31 | + }) |
| 32 | + |
| 33 | + test('returns correct count for large number of agents', async () => { |
| 34 | + const mockGetAgentCount = async () => 10000 |
| 35 | + |
| 36 | + const response = await getHealthz({ getAgentCount: mockGetAgentCount }) |
| 37 | + |
| 38 | + expect(response.status).toBe(200) |
| 39 | + const body = await response.json() |
| 40 | + expect(body.status).toBe('ok') |
| 41 | + expect(body.cached_agents).toBe(10000) |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + describe('Error handling', () => { |
| 46 | + test('returns 200 with error flag when getAgentCount throws', async () => { |
| 47 | + const mockGetAgentCount = async () => { |
| 48 | + throw new Error('Database connection failed') |
| 49 | + } |
| 50 | + |
| 51 | + const response = await getHealthz({ getAgentCount: mockGetAgentCount }) |
| 52 | + |
| 53 | + // Should still return 200 so health check passes |
| 54 | + expect(response.status).toBe(200) |
| 55 | + const body = await response.json() |
| 56 | + expect(body.status).toBe('ok') |
| 57 | + expect(body.agent_count_error).toBe(true) |
| 58 | + expect(body.error).toBe('Database connection failed') |
| 59 | + expect(body.cached_agents).toBeUndefined() |
| 60 | + }) |
| 61 | + |
| 62 | + test('handles non-Error exceptions gracefully', async () => { |
| 63 | + const mockGetAgentCount = async () => { |
| 64 | + throw 'String error' |
| 65 | + } |
| 66 | + |
| 67 | + const response = await getHealthz({ getAgentCount: mockGetAgentCount }) |
| 68 | + |
| 69 | + expect(response.status).toBe(200) |
| 70 | + const body = await response.json() |
| 71 | + expect(body.status).toBe('ok') |
| 72 | + expect(body.agent_count_error).toBe(true) |
| 73 | + expect(body.error).toBe('Unknown error') |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + describe('Response format', () => { |
| 78 | + test('response has correct Content-Type header', async () => { |
| 79 | + const mockGetAgentCount = async () => 100 |
| 80 | + |
| 81 | + const response = await getHealthz({ getAgentCount: mockGetAgentCount }) |
| 82 | + |
| 83 | + expect(response.headers.get('content-type')).toContain('application/json') |
| 84 | + }) |
| 85 | + |
| 86 | + test('timestamp is in ISO format', async () => { |
| 87 | + const mockGetAgentCount = async () => 50 |
| 88 | + |
| 89 | + const response = await getHealthz({ getAgentCount: mockGetAgentCount }) |
| 90 | + const body = await response.json() |
| 91 | + |
| 92 | + // Verify timestamp is valid ISO date |
| 93 | + const timestamp = new Date(body.timestamp) |
| 94 | + expect(timestamp.toString()).not.toBe('Invalid Date') |
| 95 | + expect(body.timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/) |
| 96 | + }) |
| 97 | + }) |
| 98 | +}) |
0 commit comments