pagepiper/tests/test_config.py
pyr0ball 10cde880b6 fix: clean up CF_ORCH_URL/GPU_SERVER_URL env leak in test_config.py
Found during freeze-branch integration testing (PR #12 + #14 + #15
combined): app/config.py's write-back (os.environ["CF_ORCH_URL"] =
GPU_SERVER_URL) is a module-level side effect monkeypatch never tracks,
so it wasn't being reverted between tests. Once any test_config.py test
set a truthy GPU_SERVER_URL/CF_LICENSE_KEY, the resulting CF_ORCH_URL
wrote leaked into every later test in the same pytest session.

This silently broke all five "skips_embeddings_without_ollama_url"
tests across the shelve_*.py suite (pdf, docx, odt, ods, xlsx) — their
get_llm_config() check no longer saw a clean "nothing configured" state,
so they attempted embedding instead of skipping it. Neither PR #12 nor
#14 alone exercised this combination in the same test session, so it
only surfaced once merged together.

Fix: explicitly pop CF_ORCH_URL/GPU_SERVER_URL/CF_LICENSE_KEY from
os.environ in the autouse teardown fixture before reloading app.config,
rather than relying on monkeypatch to catch a write it never made itself.
2026-07-10 19:24:59 -07:00

94 lines
2.9 KiB
Python

# 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