test: add route tests for signup, recover, health
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 12m35s
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 12m35s
This commit is contained in:
parent
c01e88686a
commit
1fe3f3746a
6 changed files with 554 additions and 0 deletions
68
src/__tests__/health.test.ts
Normal file
68
src/__tests__/health.test.ts
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
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");
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue