Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions gateway/tests/test_health_public_probes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Loading