Files
gruperly/apps/backend/test/health-check.test.ts
Jose Selesan 4b1f356fab refactor: migrate backend to pnpm monorepo with Hono module architecture
- Replace Bun with pnpm 9 + Turborepo + tsx; apps/api renamed to apps/backend
- Split backend into http/, modules/, lib/ layers mirroring tai-specguard
- Add use-case + Result pattern, Problem Details RFC 7807, basePath /api/v1
- Mount Better Auth at /api/v1/auth, keep session-auth whitelist
- Split Prisma schema into prisma/models/*, generate into generated/
- Rework packages/shared into lib/ + schemas/ with pagination DTOs
- Implement health, groups, students, payments, waitlist modules
- Add vitest suite with prisma mocks (21 tests), biome lint
- Point web client to /api/v1/auth and /api/v1/groups/from-organization
2026-09-14 09:18:40 -03:00

45 lines
1.2 KiB
TypeScript

import { Hono } from 'hono';
import { beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('@/lib/prisma', () => ({
default: {
$queryRaw: vi.fn(),
},
getPrismaClient: vi.fn(),
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 () => {
vi.mocked(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 () => {
vi.mocked(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,
});
});
});