import { Test, TestingModule } from "@nestjs/testing"; import { HealthController } from "./health.controller"; import { PrismaService } from "../prisma/prisma.service"; describe("HealthController", () => { let controller: HealthController; let prismaService: { client: { $queryRaw: jest.Mock } }; beforeEach(async () => { prismaService = { client: { $queryRaw: jest.fn().mockResolvedValue([{ "?column?": 1 }]), }, }; const module: TestingModule = await Test.createTestingModule({ controllers: [HealthController], providers: [ { provide: PrismaService, useValue: prismaService, }, ], }).compile(); controller = module.get(HealthController); }); it("debe estar definido", () => { expect(controller).toBeDefined(); }); it("retorna status 'ok' cuando la DB responde", async () => { const result = await controller.health(); expect(result.status).toBe("ok"); expect(result.checks.database).toBe("ok"); }); it("retorna status 'degraded' cuando la DB falla", async () => { prismaService.client.$queryRaw.mockRejectedValueOnce(new Error("DB down")); const result = await controller.health(); expect(result.status).toBe("degraded"); expect(result.checks.database).toBe("error"); }); it("el response incluye service, version y timestamp", async () => { const result = await controller.health(); expect(result.service).toBe("moraworld-api"); expect(result.version).toBe("0.1.0"); expect(result.timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T/); }); it("timestamp es una fecha ISO vĂ¡lida", async () => { const result = await controller.health(); const date = new Date(result.timestamp); expect(date.getTime()).not.toBeNaN(); }); });