"""Cubre el endpoint /health: respuesta 200 con la BD viva, 503 cuando cae.""" from unittest.mock import patch from fastapi.testclient import TestClient def test_health_db_up_returns_200(client: TestClient) -> None: response = client.get("/health") assert response.status_code == 200 assert response.json() == {"status": "ok", "db": "up"} def test_health_db_down_returns_503(client: TestClient) -> None: with patch("app.main.db_alive", return_value=False): response = client.get("/health") assert response.status_code == 503 body = response.json() assert body["status"] == "degraded" assert body["db"] == "down" def test_health_propagates_correlation_id(client: TestClient) -> None: cid = "test-cid-12345" response = client.get("/health", headers={"X-Correlation-ID": cid}) assert response.headers.get("X-Correlation-ID") == cid def test_health_generates_correlation_id_when_absent(client: TestClient) -> None: response = client.get("/health") assert response.headers.get("X-Correlation-ID")