"""Configurable topology: DISABLED_SERVICES, vLLM container override, and the extra-vLLM probe. All offline — the disabled checks short-circuit before any network call, and the probes are exercised only on the not-configured path. """ import asyncio from app.config import Settings from app.health import ( check_embeddings, check_kokoro, check_parakeet, check_qdrant, check_vllm, probe_vllm_endpoint, ) from app.services import services_from_settings def _settings(monkeypatch, **env) -> Settings: # Pin the topology env vars under test; default the rest to blank so a stray # value in the real environment can't leak into the assertion. keys = [ "SPARK1_HOST", "SPARK1_USER", "SPARK2_HOST", "SPARK2_USER", "DISABLED_SERVICES", "VLLM_CONTAINER", ] for k in keys: monkeypatch.delenv(k, raising=False) for k, v in env.items(): monkeypatch.setenv(k, v) return Settings.from_env() # ---- DISABLED_SERVICES parsing ---- def test_disabled_services_parsed_lowercased_and_trimmed(monkeypatch): s = _settings(monkeypatch, DISABLED_SERVICES="parakeet, Kokoro ,,") assert s.disabled_services == frozenset({"parakeet", "kokoro"}) def test_disabled_services_blank_is_empty(monkeypatch): assert _settings(monkeypatch).disabled_services == frozenset() # ---- vLLM container override ---- def test_vllm_container_defaults_to_vllm_node(monkeypatch): assert _settings(monkeypatch).vllm_container == "vllm_node" def test_vllm_container_override(monkeypatch): assert _settings(monkeypatch, VLLM_CONTAINER="vllm-gemma4").vllm_container == "vllm-gemma4" def test_vllm_container_invalid_falls_back(monkeypatch): # A malformed value (space / shell metachar) is rejected at the boundary and # falls back to the default rather than crashing startup or reaching a sink. assert _settings(monkeypatch, VLLM_CONTAINER="bad name; rm -rf").vllm_container == "vllm_node" # ---- services map honors the disable list ---- def test_services_from_settings_drops_disabled(monkeypatch): s = _settings( monkeypatch, SPARK1_HOST="10.0.0.1", SPARK1_USER="u", SPARK2_HOST="10.0.0.2", SPARK2_USER="u", DISABLED_SERVICES="parakeet,qdrant", ) svcs = services_from_settings(s) assert "parakeet" not in svcs and "qdrant" not in svcs assert "kokoro" in svcs and "embeddings" in svcs def test_custom_vllm_service_registered(monkeypatch): from app import custom_services monkeypatch.setattr(custom_services, "load_custom_services", lambda: [ {"key": "vllm-spark2", "kind": "vllm", "host": "10.0.0.2", "user": "u", "container": "vllm_node", "port": 8000}, ]) s = _settings(monkeypatch, SPARK1_HOST="10.0.0.1", SPARK1_USER="u", SPARK2_HOST="10.0.0.2", SPARK2_USER="u") svc = services_from_settings(s)["vllm-spark2"] assert svc.kind == "vllm" and svc.port == 8000 and svc.container == "vllm_node" def test_custom_service_colliding_with_builtin_is_ignored(monkeypatch): # A custom entry can't shadow a built-in key — the built-in wins. from app import custom_services monkeypatch.setattr(custom_services, "load_custom_services", lambda: [ {"key": "parakeet", "kind": "vllm", "host": "10.0.0.9", "user": "u", "port": 8000}, ]) s = _settings(monkeypatch, SPARK1_HOST="10.0.0.1", SPARK1_USER="u", SPARK2_HOST="10.0.0.2", SPARK2_USER="u") assert services_from_settings(s)["parakeet"].kind == "stt" # ---- disabled health checks short-circuit (no network) ---- def test_disabled_check_returns_disabled_verdict(monkeypatch): s = _settings( monkeypatch, SPARK2_HOST="10.0.0.2", SPARK2_USER="u", # host set, but disable wins DISABLED_SERVICES="parakeet,kokoro,embeddings,qdrant", ) for check in (check_parakeet, check_kokoro, check_embeddings, check_qdrant): r = asyncio.run(check(s)) assert r == {"ok": False, "disabled": True, "error": "disabled", "base_url": None} # ---- vLLM probe: not-configured path is pure ---- def test_probe_vllm_endpoint_unconfigured(monkeypatch): r = asyncio.run(probe_vllm_endpoint("", 8000)) assert r["ok"] is False and "not configured" in r["error"] def test_check_vllm_unconfigured_without_spark1(monkeypatch): s = _settings(monkeypatch) # no SPARK1_HOST r = asyncio.run(check_vllm(s)) assert r["ok"] is False and "spark1 not configured" in r["error"]