fix(health): report real per-dependency status instead of hardcoded ok:true
Security Scan (SIAX Cloud) / security (push) Successful in 21s
CI (SIAX Cloud) / security (push) Successful in 21s
CI (SIAX Cloud) / quality (push) Successful in 41s
CI (SIAX Cloud) / security (pull_request) Successful in 22s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 21s
CI (SIAX Cloud) / quality (pull_request) Successful in 41s
Security Scan (SIAX Cloud) / security (push) Successful in 21s
CI (SIAX Cloud) / security (push) Successful in 21s
CI (SIAX Cloud) / quality (push) Successful in 41s
CI (SIAX Cloud) / security (pull_request) Successful in 22s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 21s
CI (SIAX Cloud) / quality (pull_request) Successful in 41s
BACKLOG P0-3: /api/health only ever checked *presence* of DATABASE_URL/
REDIS_URL and always returned ok:true regardless of whether anything was
reachable -- ARCHITECTURE.md's own words, "an uptime monitor watching HTTP
200 would show green for a dying instance".
lib/health-checks.ts adds real dependency probes:
- db: a live `select 1` round trip (not just DATABASE_URL presence)
- engine: a real HTTP call to the FastAPI engine's own GET /health
- stripe: Stripe's documented lightweight balance.retrieve() liveness call
- queue: honestly reports "not_integrated" when REDIS_URL is set but
unused -- no Redis client is wired anywhere in this repo, so reporting
"ok" would fabricate a check that doesn't exist
/api/health stays a liveness check (always 200 while the process can
respond; the body's `ok` field is the honest signal). New GET /api/ready
fails closed with 503 when the load-bearing dependency (db) isn't both
configured and reachable -- the "no /ready route" gap.
contracts/openapi.yaml, docs/architecture/ARCHITECTURE.md, and docs/slo.md
updated to match. 17 new unit tests (dependency-injected fakes, no real
network/DB) + 5 route-level tests.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Route-level regression test for GET /api/health (BACKLOG P0-3): the old
|
||||
* handler hardcoded `ok: true` regardless of dependency state. These tests
|
||||
* cover the default (nothing configured) baseline against the real route
|
||||
* handler; per-dependency ok/error branching is covered at the unit level in
|
||||
* tests/lib/health-checks.test.ts (dependency-injected, no network/DB
|
||||
* needed).
|
||||
*/
|
||||
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
||||
import { GET as healthGET } from "@/app/api/health/route";
|
||||
|
||||
describe("GET /api/health", () => {
|
||||
const savedEnv = {
|
||||
DATABASE_URL: process.env.DATABASE_URL,
|
||||
POSTGRES_URL: process.env.POSTGRES_URL,
|
||||
ENGINE_URL: process.env.ENGINE_URL,
|
||||
STRIPE_SECRET_KEY: process.env.STRIPE_SECRET_KEY,
|
||||
REDIS_URL: process.env.REDIS_URL,
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
delete process.env.DATABASE_URL;
|
||||
delete process.env.POSTGRES_URL;
|
||||
delete process.env.ENGINE_URL;
|
||||
delete process.env.STRIPE_SECRET_KEY;
|
||||
delete process.env.REDIS_URL;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
for (const [k, v] of Object.entries(savedEnv)) {
|
||||
if (v === undefined) delete process.env[k];
|
||||
else process.env[k] = v;
|
||||
}
|
||||
});
|
||||
|
||||
it("REGRESSION: always returns HTTP 200 (liveness), but `ok` now reflects a real per-dependency check rather than a hardcoded true", async () => {
|
||||
const res = await healthGET();
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
// With nothing configured, nothing can be degraded -- ok:true here is
|
||||
// now an honest conclusion, not a hardcoded literal.
|
||||
expect(body.ok).toBe(true);
|
||||
expect(body.service).toBe("0pt-web");
|
||||
});
|
||||
|
||||
it("reports not_configured (not the old flat 'configured'/'not_configured' guess) for every unset dependency", async () => {
|
||||
const res = await healthGET();
|
||||
const body = await res.json();
|
||||
expect(body.db).toBe("not_configured");
|
||||
expect(body.engine).toBe("not_configured");
|
||||
expect(body.stripe).toBe("not_configured");
|
||||
expect(body.queue).toBe("not_configured");
|
||||
});
|
||||
|
||||
it("reports queue as not_integrated (never a fabricated ok) when REDIS_URL is set but unused", async () => {
|
||||
process.env.REDIS_URL = "redis://localhost:6379";
|
||||
const res = await healthGET();
|
||||
const body = await res.json();
|
||||
expect(body.queue).toBe("not_integrated");
|
||||
// Not wiring a real check for an unused integration is not itself a
|
||||
// degradation -- overall liveness stays ok.
|
||||
expect(body.ok).toBe(true);
|
||||
});
|
||||
|
||||
it("response includes a ts timestamp", async () => {
|
||||
const res = await healthGET();
|
||||
const body = await res.json();
|
||||
expect(new Date(body.ts).toString()).not.toBe("Invalid Date");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Route-level test for GET /api/ready (BACKLOG P0-3: "no /ready route
|
||||
* existed at all"). Unlike /api/health, readiness fails closed: 503 when the
|
||||
* load-bearing dependency (db) is not both configured and reachable.
|
||||
*/
|
||||
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
||||
import { GET as readyGET } from "@/app/api/ready/route";
|
||||
|
||||
describe("GET /api/ready", () => {
|
||||
const savedEnv = {
|
||||
DATABASE_URL: process.env.DATABASE_URL,
|
||||
POSTGRES_URL: process.env.POSTGRES_URL,
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
delete process.env.DATABASE_URL;
|
||||
delete process.env.POSTGRES_URL;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
for (const [k, v] of Object.entries(savedEnv)) {
|
||||
if (v === undefined) delete process.env[k];
|
||||
else process.env[k] = v;
|
||||
}
|
||||
});
|
||||
|
||||
it("fails closed with 503 when the database is not configured", async () => {
|
||||
const res = await readyGET();
|
||||
expect(res.status).toBe(503);
|
||||
const body = await res.json();
|
||||
expect(body.ready).toBe(false);
|
||||
expect(body.db).toBe("not_configured");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,185 @@
|
||||
/**
|
||||
* Unit tests for the dependency probes behind GET /api/health + GET
|
||||
* /api/ready (BACKLOG P0-3). Each check is exercised via dependency
|
||||
* injection (fake sql/fetch/stripe clients) rather than real network I/O,
|
||||
* matching the fake-over-mock-global convention used elsewhere in this repo
|
||||
* (e.g. tests/p0/document-intelligence-adapter.test.ts).
|
||||
*/
|
||||
import { describe, it, expect, afterEach } from "vitest";
|
||||
import {
|
||||
checkDatabase,
|
||||
checkEngine,
|
||||
checkStripe,
|
||||
checkQueue,
|
||||
hasDegradedDependency,
|
||||
isReady,
|
||||
type DependencyResults,
|
||||
type SqlPing,
|
||||
} from "@/lib/health-checks";
|
||||
|
||||
function jsonResponse(body: unknown, status = 200): Response {
|
||||
return {
|
||||
ok: status < 400,
|
||||
status,
|
||||
json: async () => body,
|
||||
} as unknown as Response;
|
||||
}
|
||||
|
||||
describe("checkDatabase", () => {
|
||||
const saved = process.env.DATABASE_URL;
|
||||
afterEach(() => {
|
||||
if (saved === undefined) delete process.env.DATABASE_URL;
|
||||
else process.env.DATABASE_URL = saved;
|
||||
});
|
||||
|
||||
it("reports not_configured when DATABASE_URL is unset, without calling the client", async () => {
|
||||
delete process.env.DATABASE_URL;
|
||||
let called = false;
|
||||
const fakeSql: SqlPing = async () => {
|
||||
called = true;
|
||||
return [{ "?column?": 1 }];
|
||||
};
|
||||
const result = await checkDatabase(fakeSql);
|
||||
expect(result.status).toBe("not_configured");
|
||||
expect(called).toBe(false);
|
||||
});
|
||||
|
||||
it("reports ok when the ping round trip succeeds", async () => {
|
||||
process.env.DATABASE_URL = "postgres://user:pass@localhost:5432/opt";
|
||||
const fakeSql: SqlPing = async () => [{ "?column?": 1 }];
|
||||
const result = await checkDatabase(fakeSql);
|
||||
expect(result.status).toBe("ok");
|
||||
expect(typeof result.latencyMs).toBe("number");
|
||||
});
|
||||
|
||||
it("reports error with the driver message when the ping fails", async () => {
|
||||
process.env.DATABASE_URL = "postgres://user:pass@localhost:5432/opt";
|
||||
const fakeSql: SqlPing = async () => {
|
||||
throw new Error("ECONNREFUSED 127.0.0.1:5432");
|
||||
};
|
||||
const result = await checkDatabase(fakeSql);
|
||||
expect(result.status).toBe("error");
|
||||
expect(result.detail).toContain("ECONNREFUSED");
|
||||
});
|
||||
|
||||
it("reports error on timeout rather than hanging", async () => {
|
||||
process.env.DATABASE_URL = "postgres://user:pass@localhost:5432/opt";
|
||||
const fakeSql: SqlPing = () => new Promise(() => {}); // never resolves
|
||||
const result = await checkDatabase(fakeSql, 20);
|
||||
expect(result.status).toBe("error");
|
||||
expect(result.detail).toContain("timed out");
|
||||
});
|
||||
|
||||
it("reports not_configured when DATABASE_URL is set but no client is available", async () => {
|
||||
process.env.DATABASE_URL = "postgres://user:pass@localhost:5432/opt";
|
||||
const result = await checkDatabase(null);
|
||||
expect(result.status).toBe("not_configured");
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkEngine", () => {
|
||||
const saved = process.env.ENGINE_URL;
|
||||
afterEach(() => {
|
||||
if (saved === undefined) delete process.env.ENGINE_URL;
|
||||
else process.env.ENGINE_URL = saved;
|
||||
});
|
||||
|
||||
it("reports not_configured when ENGINE_URL is unset", async () => {
|
||||
delete process.env.ENGINE_URL;
|
||||
const result = await checkEngine(async () => jsonResponse({ ok: true }));
|
||||
expect(result.status).toBe("not_configured");
|
||||
});
|
||||
|
||||
it("reports ok when the engine's /health responds 2xx", async () => {
|
||||
process.env.ENGINE_URL = "http://engine.local:8000";
|
||||
const fetchMock = (async (url: string) => {
|
||||
expect(url).toBe("http://engine.local:8000/health");
|
||||
return jsonResponse({ ok: true });
|
||||
}) as unknown as typeof fetch;
|
||||
const result = await checkEngine(fetchMock);
|
||||
expect(result.status).toBe("ok");
|
||||
});
|
||||
|
||||
it("reports error when the engine responds non-2xx", async () => {
|
||||
process.env.ENGINE_URL = "http://engine.local:8000";
|
||||
const result = await checkEngine(async () => jsonResponse({}, 500));
|
||||
expect(result.status).toBe("error");
|
||||
expect(result.detail).toContain("500");
|
||||
});
|
||||
|
||||
it("reports error when the fetch throws (host unreachable)", async () => {
|
||||
process.env.ENGINE_URL = "http://engine.local:8000";
|
||||
const result = await checkEngine(async () => {
|
||||
throw new Error("fetch failed");
|
||||
});
|
||||
expect(result.status).toBe("error");
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkStripe", () => {
|
||||
it("reports not_configured when no client is available (no STRIPE_SECRET_KEY)", async () => {
|
||||
const result = await checkStripe(null);
|
||||
expect(result.status).toBe("not_configured");
|
||||
});
|
||||
|
||||
it("reports ok when balance.retrieve() succeeds", async () => {
|
||||
const result = await checkStripe({ balance: { retrieve: async () => ({ available: [] }) } });
|
||||
expect(result.status).toBe("ok");
|
||||
});
|
||||
|
||||
it("reports error when balance.retrieve() rejects (e.g. revoked key)", async () => {
|
||||
const result = await checkStripe({
|
||||
balance: {
|
||||
retrieve: async () => {
|
||||
throw new Error("Invalid API Key provided");
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(result.status).toBe("error");
|
||||
expect(result.detail).toContain("Invalid API Key");
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkQueue", () => {
|
||||
const saved = process.env.REDIS_URL;
|
||||
afterEach(() => {
|
||||
if (saved === undefined) delete process.env.REDIS_URL;
|
||||
else process.env.REDIS_URL = saved;
|
||||
});
|
||||
|
||||
it("never claims ok — only not_configured or not_integrated (no Redis client wired in this repo)", () => {
|
||||
delete process.env.REDIS_URL;
|
||||
expect(checkQueue().status).toBe("not_configured");
|
||||
process.env.REDIS_URL = "redis://localhost:6379";
|
||||
expect(checkQueue().status).toBe("not_integrated");
|
||||
});
|
||||
});
|
||||
|
||||
describe("hasDegradedDependency / isReady", () => {
|
||||
function deps(overrides: Partial<DependencyResults> = {}): DependencyResults {
|
||||
return {
|
||||
db: { status: "not_configured" },
|
||||
engine: { status: "not_configured" },
|
||||
stripe: { status: "not_configured" },
|
||||
queue: { status: "not_configured" },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
it("hasDegradedDependency is false when nothing is configured", () => {
|
||||
expect(hasDegradedDependency(deps())).toBe(false);
|
||||
});
|
||||
|
||||
it("hasDegradedDependency is true when any dependency errored", () => {
|
||||
expect(hasDegradedDependency(deps({ engine: { status: "error", detail: "boom" } }))).toBe(true);
|
||||
});
|
||||
|
||||
it("isReady is false when db is not ok, even if nothing else is broken", () => {
|
||||
expect(isReady(deps())).toBe(false);
|
||||
expect(isReady(deps({ db: { status: "error" } }))).toBe(false);
|
||||
});
|
||||
|
||||
it("isReady is true once db is ok, regardless of optional integrations", () => {
|
||||
expect(isReady(deps({ db: { status: "ok" } }))).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user