railyard/apps/console/lib/rail-status.test.ts
caelum0x c3b7edcf6d feat(console): GET /api/v1/rail/status, one aggregated health check for the rail
Consumer products (Rally, Menivor, Scribase, ...) and operators currently
have no single place to ask "is the rail actually up" -- they'd have to
probe Mint, Rago, Relay and Abacus separately before routing real traffic.

Adds lib/rail-status.ts (pure, injectable fetch per the repo's own transport
invariant) that checks every configured service in parallel with a bounded
per-service timeout, and GET /api/v1/rail/status that wires it to four new
optional env vars (MINT_HEALTH_URL, RAGO_HEALTH_URL, RELAY_HEALTH_URL,
ABACUS_HEALTH_URL -- unset services read "not_configured" and are excluded
from the verdict, so this degrades cleanly on any partial deployment).
Overall reads ok/degraded/down/unconfigured; HTTP status is 200 for
ok/degraded and 503 for down/unconfigured so a caller that only checks the
status code still gets the right answer without parsing the body.

TDD: 7 new tests in lib/rail-status.test.ts (all services healthy, one down
degrades the rest, a thrown network error reads down not an unhandled
rejection, a hung service times out instead of blocking forever, an unset
service skips the network call and is excluded from the verdict, zero
configured services reads unconfigured not a false ok, every service down
reads down). Full console suite: 166 pass (164 + the pre-existing env-only
failure in add-app-component.test.ts, unrelated -- missing root node_modules
in this sandbox, not a code regression).

Not deployed: the console Worker's deploy needs
~/.config/products-keys/railyard-console.env (not present on this machine)
and a working wrangler/pnpm toolchain (corepack's pnpm cache is broken here,
and the workspace root node_modules does not exist), so this could not be
shipped to railyard.ereboslab.com from this session -- noted in NOTES.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-21 18:39:35 +03:00

107 lines
4.3 KiB
TypeScript

// Railyard rail status (one aggregated health check across Mint/Rago/Relay/
// Abacus) -- lets a consumer product or an operator ask ONE endpoint instead
// of probing four hosts directly before routing real traffic at the rail.
import { test } from "node:test";
import assert from "node:assert/strict";
import { checkRailStatus } from "./rail-status.ts";
const NOW = new Date("2026-10-21T12:00:00Z");
test("every service healthy reads overall ok, each with its own latency", async () => {
const fetchImpl = (async (url: string) => new Response("ok", { status: 200 })) as typeof fetch;
const result = await checkRailStatus({
services: [
{ name: "mint", url: "https://mint.ereboslab.com/healthz" },
{ name: "rago", url: "https://rago.ereboslab.com/health" },
],
fetchImpl,
now: () => NOW,
});
assert.equal(result.overall, "ok");
assert.equal(result.checked_at, NOW.toISOString());
assert.deepEqual(
result.services.map((s) => [s.service, s.state, s.http_status]),
[
["mint", "ok", 200],
["rago", "ok", 200],
],
);
for (const s of result.services) assert.equal(typeof s.latency_ms, "number");
});
test("a non-2xx response from one service is down, not thrown; overall is degraded when at least one is still ok", async () => {
const fetchImpl = (async (url: string) => (url.includes("relay") ? new Response("bad", { status: 500 }) : new Response("ok", { status: 200 }))) as typeof fetch;
const result = await checkRailStatus({
services: [
{ name: "mint", url: "https://mint.ereboslab.com/healthz" },
{ name: "relay", url: "https://relay.ereboslab.com/health" },
],
fetchImpl,
now: () => NOW,
});
assert.equal(result.overall, "degraded");
const relay = result.services.find((s) => s.service === "relay");
assert.equal(relay?.state, "down");
assert.equal(relay?.http_status, 500);
});
test("a thrown network error (DNS, connection refused) reads down, not an unhandled rejection", async () => {
const fetchImpl = (async () => {
throw new TypeError("fetch failed");
}) as typeof fetch;
const result = await checkRailStatus({ services: [{ name: "mint", url: "https://mint.ereboslab.com/healthz" }], fetchImpl, now: () => NOW });
assert.equal(result.overall, "down");
assert.deepEqual(
result.services.map((s) => [s.service, s.state, s.http_status]),
[["mint", "down", null]],
);
});
test("a slow service that never answers is down once the timeout elapses, not hung forever", async () => {
const fetchImpl = ((_url: string, init?: RequestInit) =>
new Promise<Response>((_resolve, reject) => {
init?.signal?.addEventListener("abort", () => reject(new DOMException("aborted", "AbortError")));
})) as typeof fetch;
const result = await checkRailStatus({ services: [{ name: "mint", url: "https://mint.ereboslab.com/healthz" }], fetchImpl, timeoutMs: 20, now: () => NOW });
assert.equal(result.services[0].state, "down");
});
test("an unset service url reads not_configured and never calls fetch; it is excluded from the overall verdict", async () => {
let calls = 0;
const fetchImpl = (async () => {
calls++;
return new Response("ok", { status: 200 });
}) as typeof fetch;
const result = await checkRailStatus({
services: [
{ name: "mint", url: "https://mint.ereboslab.com/healthz" },
{ name: "turnstile", url: null },
],
fetchImpl,
now: () => NOW,
});
assert.equal(calls, 1);
assert.equal(result.services.find((s) => s.service === "turnstile")?.state, "not_configured");
assert.equal(result.overall, "ok");
});
test("no service configured at all reads unconfigured, not a false ok", async () => {
const fetchImpl = (async () => new Response("ok", { status: 200 })) as typeof fetch;
const result = await checkRailStatus({ services: [{ name: "mint", url: null }], fetchImpl, now: () => NOW });
assert.equal(result.overall, "unconfigured");
});
test("every configured service down reads overall down", async () => {
const fetchImpl = (async () => new Response("bad", { status: 503 })) as typeof fetch;
const result = await checkRailStatus({
services: [
{ name: "mint", url: "https://mint.ereboslab.com/healthz" },
{ name: "rago", url: "https://rago.ereboslab.com/health" },
],
fetchImpl,
now: () => NOW,
});
assert.equal(result.overall, "down");
});