import { ServiceUnavailableException } from '@nestjs/common'; import { HealthController } from './health.controller'; import { PrismaService } from '../prisma/prisma.service'; describe('HealthController', () => { let controller: HealthController; let prisma: { $queryRaw: jest.Mock }; beforeEach(() => { prisma = { $queryRaw: jest.fn() }; controller = new HealthController(prisma as unknown as PrismaService); }); it('liveness returns ok without touching the database', () => { expect(controller.liveness()).toEqual({ status: 'ok' }); expect(prisma.$queryRaw).not.toHaveBeenCalled(); }); it('readiness returns ok when the database responds', async () => { prisma.$queryRaw.mockResolvedValueOnce([{ '?column?': 1 }]); await expect(controller.readiness()).resolves.toEqual({ status: 'ok' }); }); it('readiness throws ServiceUnavailableException when the database is unreachable', async () => { prisma.$queryRaw.mockRejectedValueOnce(new Error('connection refused')); await expect(controller.readiness()).rejects.toBeInstanceOf( ServiceUnavailableException, ); }); });