def test_health_endpoint_contract(client):
    response = client.get("/api/health")
    assert response.status_code == 200
    data = response.json()
    assert data["status"] == "ok"
    assert "version" in data
    assert isinstance(data["news_count"], int)


def test_config_endpoint_exposes_footer_keys(client):
    response = client.get("/config")
    assert response.status_code == 200
    data = response.json()
    assert "github_repo_url" in data
    assert "contact_email" in data


def test_latest_news_contract(client, seeded_news):
    response = client.get("/api/news/latest?language=en")
    assert response.status_code == 200
    payload = response.json()
    assert payload["id"] == seeded_news
    assert payload["headline"]
    assert payload["summary"]
    assert payload["language"] == "en"


def test_news_pagination_and_limit_validation(client, seeded_news):
    response = client.get("/api/news?limit=10&language=en")
    assert response.status_code == 200
    payload = response.json()
    assert isinstance(payload["items"], list)
    assert payload["items"][0]["id"] == seeded_news

    invalid_limit = client.get("/api/news?limit=999")
    assert invalid_limit.status_code == 422


def test_language_fallback_defaults_to_english(client, seeded_news):
    response = client.get("/api/news/latest?language=zz")
    assert response.status_code == 200
    payload = response.json()
    assert payload["language"] == "en"
