import httpx
from fastapi.testclient import TestClient

from app.main import app, get_pulp_client

client = TestClient(app)


class _FakeOnline:
    def status(self):
        return {"online": True}


class _FakeDown:
    def status(self):
        raise httpx.ConnectError("connection refused")


def test_healthz_is_liveness_always_ok():
    # Pulp 와 무관하게 앱 생존만 본다 → 항상 200 {"ok": true}
    resp = client.get("/healthz")
    assert resp.status_code == 200
    assert resp.json() == {"ok": True}


def test_pulp_status_online():
    app.dependency_overrides[get_pulp_client] = lambda: _FakeOnline()
    try:
        resp = client.get("/pulp-status")
    finally:
        app.dependency_overrides.clear()
    assert resp.status_code == 200
    assert "연결됨" in resp.text


def test_pulp_status_offline_returns_503():
    app.dependency_overrides[get_pulp_client] = lambda: _FakeDown()
    try:
        resp = client.get("/pulp-status")
    finally:
        app.dependency_overrides.clear()
    assert resp.status_code == 503
    assert "연결 실패" in resp.text
