From f31711af27f5806ee85883d3d030cbce3728a434 Mon Sep 17 00:00:00 2001 From: Lucas Berger Date: Thu, 4 Jun 2026 09:50:43 -0400 Subject: [PATCH] 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 --- apps/api/tests/health.test.ts | 48 ++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/apps/api/tests/health.test.ts b/apps/api/tests/health.test.ts index 29430d2..0daefb0 100644 --- a/apps/api/tests/health.test.ts +++ b/apps/api/tests/health.test.ts @@ -1,16 +1,50 @@ /** - * Wave 0 stub — GET /health: 200 + real DB round-trip + * GET /health — end-to-end skeleton test * - * This test is a RED stub. Implementation lives in: - * apps/api/src/routes/health.ts (Task 2) + * 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. * - * The test will be filled GREEN in Task 2 when the health route is implemented. + * 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 } from 'vitest' +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', () => { - it.todo('returns 200 with { ok: true, db: "up" } after a real DB round-trip (Task 2)') + beforeEach(() => { + vi.resetModules() + }) - it.todo('returns 503 if the DB round-trip throws (Task 2)') + 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) + }) })