"""plausible — INSTALL overlay (Phase 1d): reuse generic serving, then assert plausible's own
/api/health reports BOTH datastores + the sites_cache as ready.

We do NOT probe `/`: under the headless CI config (DISABLE_AUTH=true, no user) plausible's `/`
auth_controller 500s (it tries to render a dashboard for a non-existent session/user), so `/` is not
a valid readiness signal. /api/health is plausible's dedicated readiness endpoint and is a STRONGER
assertion than a bare status code — it confirms the postgres metadata store, the ClickHouse events
store, and the in-memory sites_cache are all reachable, which is exactly what "installed and serving"
means for an analytics app."""

import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "runner"))
from harness import generic  # noqa: E402
from harness import http as harness_http  # noqa: E402


def test_serving(live_app, meta):
    generic.assert_serving(live_app, meta)

    # plausible-specific readiness: /api/health -> 200 with every subsystem "ok".
    status, body = harness_http.retry_http_get(
        f"https://{live_app}/api/health", expect_status=(200,), max_wait=60, interval=3
    )
    assert status == 200, f"/api/health on {live_app} returned HTTP {status} (expected 200)"
    assert isinstance(body, dict), f"/api/health body is not JSON object: {body!r}"
    for subsystem in ("clickhouse", "postgres", "sites_cache"):
        assert body.get(subsystem) == "ok", (
            f"/api/health subsystem {subsystem!r} = {body.get(subsystem)!r} (expected 'ok'); "
            f"full body: {body!r}"
        )
