# tests/test_config.py """Unit tests for the GPU_SERVER_URL / CF_ORCH_URL resolution in app/config.py. app/config.py resolves GPU_SERVER_URL at *module import time* (module-level code, not a function), so each test reloads the module after adjusting env vars via monkeypatch, then reloads it again afterward with the real environment restored so later tests see normal module state. """ from __future__ import annotations import importlib import os import pytest import app.config as config @pytest.fixture(autouse=True) def _restore_config_module(): yield # config.py's write-back (`os.environ["CF_ORCH_URL"] = GPU_SERVER_URL`) is a # module-level side effect monkeypatch never sees, so it can't auto-revert # it — without this explicit cleanup, a value set here leaks into every # other test in the session (e.g. shelve tests' get_llm_config() checks). os.environ.pop("CF_ORCH_URL", None) os.environ.pop("GPU_SERVER_URL", None) os.environ.pop("CF_LICENSE_KEY", None) importlib.reload(config) def _clear_orch_env(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.delenv("GPU_SERVER_URL", raising=False) monkeypatch.delenv("CF_ORCH_URL", raising=False) monkeypatch.delenv("CF_LICENSE_KEY", raising=False) def test_gpu_server_url_prefers_explicit_env_var(monkeypatch): _clear_orch_env(monkeypatch) monkeypatch.setenv("GPU_SERVER_URL", "http://my-rig:7700") monkeypatch.setenv("CF_ORCH_URL", "http://should-be-ignored:7700") importlib.reload(config) assert config.GPU_SERVER_URL == "http://my-rig:7700" def test_gpu_server_url_falls_back_to_cf_orch_url_legacy_alias(monkeypatch): _clear_orch_env(monkeypatch) monkeypatch.setenv("CF_ORCH_URL", "http://legacy-coordinator:7700") importlib.reload(config) assert config.GPU_SERVER_URL == "http://legacy-coordinator:7700" def test_gpu_server_url_defaults_when_license_key_present(monkeypatch): _clear_orch_env(monkeypatch) monkeypatch.setenv("CF_LICENSE_KEY", "test-key-123") importlib.reload(config) assert config.GPU_SERVER_URL == "https://orch.circuitforge.tech" def test_gpu_server_url_none_when_nothing_set(monkeypatch): _clear_orch_env(monkeypatch) importlib.reload(config) assert config.GPU_SERVER_URL is None def test_gpu_server_url_writes_back_to_cf_orch_url_for_legacy_callers(monkeypatch): """get_llm_config() and app/api/chat.py read os.environ["CF_ORCH_URL"] directly — the write-back must happen so they keep working without code changes.""" import os _clear_orch_env(monkeypatch) monkeypatch.setenv("GPU_SERVER_URL", "http://my-rig:7700") importlib.reload(config) assert os.environ.get("CF_ORCH_URL") == "http://my-rig:7700" def test_gpu_server_url_unset_does_not_write_back(monkeypatch): import os _clear_orch_env(monkeypatch) importlib.reload(config) assert "CF_ORCH_URL" not in os.environ