diff --git a/gateway/tests/test_health_public_probes.py b/gateway/tests/test_health_public_probes.py index 25c80a075..9e79792a4 100644 --- a/gateway/tests/test_health_public_probes.py +++ b/gateway/tests/test_health_public_probes.py @@ -11,12 +11,14 @@ import secrets from types import SimpleNamespace +from unittest.mock import AsyncMock import httpx import pytest import stripe from httpx import ASGITransport, AsyncClient +from app.core import health as health_module from app.core.config import get_settings from app.core.health import ( PUBLIC_DEPENDENCY_KEYS, @@ -137,3 +139,64 @@ async def test_gather_without_only_runs_every_probe(probe_counts): "sentinel", } assert probe_counts == {"stripe": 1, "sentinel": 1} + + +@pytest.fixture +def dependency_probes(monkeypatch): + """Replace every probe so call assertions never depend on configuration.""" + probes = {} + for name in ( + "postgres", + "redis", + "mqtt", + "stripe", + "llm", + "upstream_mcp", + "signing_key", + "sentinel", + ): + probe = AsyncMock(return_value={"status": "up"}) + monkeypatch.setattr(health_module, f"_check_{name}", probe) + probes[name] = probe + monkeypatch.setattr(get_settings(), "ENABLE_PROOF_SURFACES", False) + return probes + + +@pytest.mark.anyio +@pytest.mark.parametrize("public_status", ["up", "down"]) +async def test_public_route_skips_discarded_probe_callables( + client, dependency_probes, public_status +): + dependency_probes["postgres"].return_value = {"status": public_status} + for name in ("mqtt", "stripe", "llm", "sentinel"): + dependency_probes[name].side_effect = RuntimeError("dependency unavailable") + + resp = await client.get("/health/dependencies") + + assert resp.status_code == 200 + for name, probe in dependency_probes.items(): + if name in PUBLIC_DEPENDENCY_KEYS: + probe.assert_awaited_once() + else: + probe.assert_not_called() + + # Preserve the response produced by the original full report projection, + # including public failures and the exclusion of discarded failures. + full = await gather_dependency_report() + expected = build_public_dependency_report(full) + assert _without_latency(resp.json()) == _without_latency(expected) + assert resp.json()["status"] == ("healthy" if public_status == "up" else "degraded") + + +@pytest.mark.anyio +async def test_detailed_route_awaits_every_probe_callable( + client, dependency_probes, monkeypatch +): + monkeypatch.setattr(get_settings(), "ENABLE_PROOF_SURFACES", True) + + resp = await client.get("/health/dependencies") + + assert resp.status_code == 200 + assert set(resp.json()["dependencies"]) == set(dependency_probes) + for probe in dependency_probes.values(): + probe.assert_awaited_once()