blitz_api/tests/routers/test_system.py
fusion44 6e3238a5a7
feat(api): implement GET /system/health readiness endpoint (#145)
Make /system/health unauthenticated, compute real readiness from the
shared startup state via build_health_info, and return 503 (body still a
SystemHealthInfo) when a subsystem is not ready. Drop the now-dead
per-backend get_system_health delegation.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-12 18:41:18 +02:00

52 lines
1.6 KiB
Python

from starlette.testclient import TestClient
from app.main import app
from tests.routers.utils import call_route
import app.main as main
from app.api.models import StartupState
client = TestClient(app)
def test_route_authentications_latest():
prefixes = ["/system"]
for prefix in prefixes:
call_route(client, f"{prefix}/refresh-token", method="p")
def _set_status(bitcoin, lightning):
main.api_startup_status.bitcoin = bitcoin
main.api_startup_status.lightning = lightning
def test_health_is_unauthenticated_and_200_when_ready():
_set_status(StartupState.DONE, StartupState.DONE)
# no `with`: don't start lifespan/background tasks
c = TestClient(main.app)
resp = c.get("/system/health") # no Authorization header
assert resp.status_code == 200
body = resp.json()
assert body["healthy"] is True
assert body["subsystems"] == []
def test_health_503_when_not_ready_body_is_health_info():
_set_status(StartupState.BOOTSTRAPPING, StartupState.DONE)
c = TestClient(main.app)
resp = c.get("/system/health")
assert resp.status_code == 503
body = resp.json()
# body is a SystemHealthInfo, NOT an ErrorMessage
assert body["healthy"] is False
assert "error_code" not in body
assert body["message"] == "bitcoind not ready: bootstrapping"
def test_health_verbose_lists_subsystems():
_set_status(StartupState.DONE, StartupState.DONE)
c = TestClient(main.app)
resp = c.get("/system/health?verbose=true")
assert resp.status_code == 200
names = [s["name"] for s in resp.json()["subsystems"]]
assert names == ["api", "bitcoind", "lightning"]