import { beforeEach, describe, expect, it, mock, vi } from 'bun:test'; import { Hono } from 'hono'; mock.module('@/lib/prisma', () => ({ default: { $queryRaw: mock(), }, getPrismaClient: mock(), UnitOfWork: class {}, })); import prisma from '@/lib/prisma'; import { healthCheckRoutes } from '@/modules/health-check'; const app = new Hono(); app.route('/health', healthCheckRoutes); describe('health check routes', () => { beforeEach(() => { vi.clearAllMocks(); }); it('reports ok when the database answers', async () => { prisma.$queryRaw.mockResolvedValue([{ '?column?': 1 }]); const res = await app.request('/health'); expect(res.status).toBe(200); const body = await res.json(); expect(body).toMatchObject({ status: 'ok', checks: { database: 'ok' } }); expect(typeof body.timestamp).toBe('string'); }); it('reports degraded when the database is unreachable', async () => { prisma.$queryRaw.mockRejectedValue(new Error('connection refused')); const res = await app.request('/health'); expect(res.status).toBe(503); expect(await res.json()).toMatchObject({ code: 'database_unavailable', status: 503, }); }); });