import { test, expect } from "vitest"; import { buildApp } from "../src/app.js"; import { loadConfig } from "../src/config.js"; test("GET /health reports process readiness without external services", async () => { const app = buildApp(loadConfig({ THT_HARNESS_DIR: "/tmp/h" })); const res = await app.inject({ method: "GET", url: "/health" }); expect(res.statusCode).toBe(200); expect(res.headers["content-type"]).toContain("application/json"); expect(res.json()).toEqual({ status: "ok" }); }); test("GET /health remains available to container probes in upstream auth mode", async () => { const app = buildApp(loadConfig({ THT_HARNESS_DIR: "/tmp/h", AUTH_MODE: "upstream", THOTH_PUBLIC_EXPOSURE: "true", THT_SESSION_STORAGE: "postgres", THT_SESSION_DB_HOST: "db.internal", THT_SESSION_DB_NAME: "thoth", THT_SESSION_RUNTIME_USER: "thoth_sessions_app", THT_SESSION_RUNTIME_PASSWORD_FILE: "/run/secrets/session_runtime_password", THT_SESSION_DB_SSLMODE: "verify-full", THT_SESSION_DB_SSLROOTCERT: "/run/secrets/session_ca.pem", })); const res = await app.inject({ method: "GET", url: "/health" }); expect(res.statusCode).toBe(200); expect(res.json()).toEqual({ status: "ok" }); }); test("SSE response headers are flushed before the first event", async () => { const app = buildApp(loadConfig({ THT_HARNESS_DIR: "/tmp/h" }), { thtRunner: { sessionShow: async () => ({ id: "header-probe" }) } as any, }); await app.listen({ port: 0, host: "127.0.0.1" }); const port = (app.server.address() as { port: number }).port; const controller = new AbortController(); try { const response = await Promise.race([ fetch(`http://127.0.0.1:${port}/sessions/header-probe/events`, { signal: controller.signal, }), new Promise((_, reject) => setTimeout(() => reject(new Error("SSE headers were not flushed")), 250), ), ]); expect(response.headers.get("content-type")).toContain("text/event-stream"); expect(response.headers.get("cache-control")).toBe("no-cache"); expect(response.headers.get("x-accel-buffering")).toBe("no"); } finally { controller.abort(); await app.close(); } });