import { describe, it, expect, vi, beforeEach } from "vitest"; import express from "express"; import request from "supertest"; import { getPoolStats } from "../services/browser.js"; import { pool } from "../services/db.js"; let app: express.Express; beforeEach(async () => { vi.clearAllMocks(); // Default: healthy DB const mockClient = { query: vi.fn() .mockResolvedValueOnce({ rows: [{ 1: 1 }] }) // SELECT 1 .mockResolvedValueOnce({ rows: [{ version: "PostgreSQL 17.4 on x86_64" }] }), // SELECT version() release: vi.fn(), }; vi.mocked(pool.connect).mockResolvedValue(mockClient as any); vi.mocked(getPoolStats).mockReturnValue({ poolSize: 16, totalPages: 16, availablePages: 14, queueDepth: 0, pdfCount: 5, restarting: false, uptimeMs: 60000, browsers: [], }); const { healthRouter } = await import("../routes/health.js"); app = express(); app.use("/health", healthRouter); }); describe("GET /health", () => { it("returns 200 with status ok when DB is healthy", async () => { const res = await request(app).get("/health"); expect(res.status).toBe(200); expect(res.body.status).toBe("ok"); expect(res.body.database.status).toBe("ok"); }); it("returns 503 with status degraded on DB error", async () => { vi.mocked(pool.connect).mockRejectedValue(new Error("Connection refused")); const res = await request(app).get("/health"); expect(res.status).toBe(503); expect(res.body.status).toBe("degraded"); expect(res.body.database.status).toBe("error"); }); it("includes pool stats", async () => { const res = await request(app).get("/health"); expect(res.body.pool).toMatchObject({ size: 16, available: 14, queueDepth: 0, pdfCount: 5, }); }); it("includes version", async () => { const res = await request(app).get("/health"); expect(res.body.version).toBeDefined(); expect(typeof res.body.version).toBe("string"); }); });