Files
familysync/apps/api/tests/health.test.ts
T
Lucas Berger f31711af27 test(01-01): add failing health test (RED gate)
- Tests GET /health returns 200 { ok, db } on success and 503 on DB error
- Fails because src/index.ts and src/routes/health.ts don't exist yet
2026-06-04 09:50:43 -04:00

51 lines
1.5 KiB
TypeScript

/**
* GET /health — end-to-end skeleton test
*
* TDD RED: Tests written before implementation.
* These will fail until Task 2 creates apps/api/src/routes/health.ts
* and apps/api/src/index.ts.
*
* Behavior (from plan):
* - GET /health returns 200 with { ok: true, db: "up" } after a real DB round-trip
* - GET /health returns 503 if the DB round-trip throws
*/
import { describe, it, expect, vi, beforeEach } from 'vitest'
// We test the health route by importing the Hono app and calling it directly
// (no real DB needed — we mock the db module)
describe('GET /health', () => {
beforeEach(() => {
vi.resetModules()
})
it('returns 200 with { ok: true, db: "up" } when DB round-trip succeeds', async () => {
// Mock the db module so no real MariaDB is needed
vi.mock('../src/db/client.js', () => ({
db: {
execute: vi.fn().mockResolvedValue([[]]),
},
}))
const { app } = await import('../src/index.js')
const res = await app.request('/health')
expect(res.status).toBe(200)
const body = await res.json()
expect(body.ok).toBe(true)
expect(body.db).toBe('up')
})
it('returns 503 when DB round-trip throws', async () => {
vi.mock('../src/db/client.js', () => ({
db: {
execute: vi.fn().mockRejectedValue(new Error('DB connection failed')),
},
}))
const { app } = await import('../src/index.js')
const res = await app.request('/health')
expect(res.status).toBe(503)
})
})