- 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
51 lines
1.5 KiB
TypeScript
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)
|
|
})
|
|
})
|