"""Boundary tests for /api/system-status.

2026-05-11: previously the endpoint raised 503 to flag the workers subsystem
as not-wired. That caused Chrome's devtools console to log a red error on every
5s frontend poll, with no actionable signal. The endpoint now returns 200 with
zeroed worker counts and `spend_synthesized=True` so the frontend can infer
the degraded state from the payload shape without polluting the console.
"""

from __future__ import annotations

import pytest
from fastapi.testclient import TestClient

from recoil.api.eventbus import BUS
from recoil.api.main import app


@pytest.fixture
def client():
    """TestClient as a context manager — invokes lifespan so BUS binds."""
    BUS._reset_for_tests()  # noqa: SLF001 — test-only hook
    with TestClient(app) as c:
        yield c
    BUS._reset_for_tests()  # noqa: SLF001 — test-only hook


def test_system_status_returns_200_with_degraded_payload(client):
    """200 with zeroed worker counts and spend_synthesized=True so the
    frontend can read 'degraded' from the payload shape, not from a 503."""
    r = client.get("/api/system-status")
    assert r.status_code == 200
    body = r.json()
    # camelCase aliases per Pydantic ConfigDict(populate_by_name=True).
    assert body["spendSynthesized"] is True
    assert body["spendTodayUsd"] == 0.0
    workers = body["workers"]
    # Every worker pool zeroed until the real queue tracker is wired.
    assert workers["veoActive"] == 0 and workers["veoCapacity"] == 0
    assert workers["imagenActive"] == 0 and workers["imagenCapacity"] == 0
    assert workers["elevenlabsActive"] == 0 and workers["elevenlabsCapacity"] == 0
    # Identity / liveness fields populated.
    assert body["apiVersion"]
    assert body["uptimeSeconds"] >= 0.0
    assert body["host"]
