import type { IncomingMessage, ServerResponse } from "node:http"; import { describe, expect, test, vi } from "vitest"; import { createWorkerHealthHandler } from "./health-server.js"; function createMockResponse() { const captured = { statusCode: 200, headers: {} as Record, body: "" as string, ended: false, headersSent: false, }; const res = { get statusCode() { return captured.statusCode; }, set statusCode(code: number) { captured.statusCode = code; }, get headersSent() { return captured.headersSent; }, setHeader(name: string, value: string) { captured.headers[name.toLowerCase()] = value; }, end(body?: string) { captured.headersSent = true; if (body !== undefined) captured.body = body; captured.ended = true; }, } as unknown as ServerResponse; return { res, captured }; } function createMockReq(url: string): IncomingMessage { return { url } as IncomingMessage; } describe("worker health handler", () => { test("/healthz returns 200 alive without invoking probes", async () => { const redisProbe = vi.fn(); const databaseProbe = vi.fn(); const handler = createWorkerHealthHandler({ redisProbe, databaseProbe, workerGroup: "general" }); const { res, captured } = createMockResponse(); await handler(createMockReq("/healthz"), res); expect(captured.statusCode).toBe(200); expect(JSON.parse(captured.body)).toEqual({ status: "alive", workerGroup: "general" }); expect(redisProbe).not.toHaveBeenCalled(); expect(databaseProbe).not.toHaveBeenCalled(); }); test("/readyz returns 200 when both probes succeed", async () => { const handler = createWorkerHealthHandler({ redisProbe: async () => ({ ok: true }) as const, databaseProbe: async () => ({ ok: true }) as const, workerGroup: "mail", }); const { res, captured } = createMockResponse(); await handler(createMockReq("/readyz"), res); expect(captured.statusCode).toBe(200); const body = JSON.parse(captured.body); expect(body.status).toBe("ready"); expect(body.workerGroup).toBe("mail"); expect(body.checks).toEqual({ redis: "ok", database: "ok" }); }); test("/readyz clears timeout timers after fast probes complete", async () => { vi.useFakeTimers(); try { const handler = createWorkerHealthHandler({ redisProbe: async () => ({ ok: true }) as const, databaseProbe: async () => ({ ok: true }) as const, }); const { res } = createMockResponse(); await handler(createMockReq("/readyz"), res); expect(vi.getTimerCount()).toBe(0); } finally { vi.useRealTimers(); } }); test("/readyz returns 503 when redis probe fails", async () => { const handler = createWorkerHealthHandler({ redisProbe: async () => ({ ok: false, error: "ECONNREFUSED" }) as const, databaseProbe: async () => ({ ok: true }) as const, }); const { res, captured } = createMockResponse(); await handler(createMockReq("/readyz"), res); expect(captured.statusCode).toBe(503); const body = JSON.parse(captured.body); expect(body.status).toBe("not_ready"); expect(body.checks.redis).toBe("error"); expect(body.checks.database).toBe("ok"); }); test("/readyz returns 503 when database probe throws", async () => { const handler = createWorkerHealthHandler({ redisProbe: async () => ({ ok: true }) as const, databaseProbe: async () => { throw new Error("pg disconnected"); }, }); const { res, captured } = createMockResponse(); await handler(createMockReq("/readyz"), res); expect(captured.statusCode).toBe(503); expect(JSON.parse(captured.body).checks.database).toBe("error"); }); test("/readyz includes required runtime probes in readiness status", async () => { const handler = createWorkerHealthHandler({ redisProbe: async () => ({ ok: true }) as const, databaseProbe: async () => ({ ok: true }) as const, readinessProbes: { outboxDispatcher: async () => ({ ok: true }) as const, workflowTrigger: async () => ({ ok: false, error: "subscriber disabled" }) as const, }, workerGroup: "general", }); const { res, captured } = createMockResponse(); await handler(createMockReq("/readyz"), res); expect(captured.statusCode).toBe(503); expect(JSON.parse(captured.body)).toMatchObject({ status: "not_ready", workerGroup: "general", checks: { redis: "ok", database: "ok", outboxDispatcher: "ok", workflowTrigger: "error", }, }); }); test("/readyz times out a hanging probe", async () => { const handler = createWorkerHealthHandler({ redisProbe: () => new Promise(() => undefined), databaseProbe: async () => ({ ok: true }) as const, probeTimeoutMs: 20, }); const { res, captured } = createMockResponse(); await handler(createMockReq("/readyz"), res); expect(captured.statusCode).toBe(503); expect(JSON.parse(captured.body).checks.redis).toBe("error"); }); test("unknown routes return 404 JSON", async () => { const handler = createWorkerHealthHandler({ redisProbe: async () => ({ ok: true }) as const, databaseProbe: async () => ({ ok: true }) as const, }); const { res, captured } = createMockResponse(); await handler(createMockReq("/wat"), res); expect(captured.statusCode).toBe(404); expect(JSON.parse(captured.body).error.code).toBe("not_found"); }); test("/healthz omits workerGroup when none is configured", async () => { const handler = createWorkerHealthHandler({ redisProbe: async () => ({ ok: true }) as const, databaseProbe: async () => ({ ok: true }) as const, }); const { res, captured } = createMockResponse(); await handler(createMockReq("/healthz"), res); expect(JSON.parse(captured.body)).toEqual({ status: "alive" }); }); });