Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(tests): scope sys.modules mutation and unpin hardcoded version

Garm1788d907f0c8
diff --git a/tests/test_cli/test_wrap_copilot.py b/tests/test_cli/test_wrap_copilot.py
index dc8bafbab..792feea84 100644
--- a/tests/test_cli/test_wrap_copilot.py
+++ b/tests/test_cli/test_wrap_copilot.py
@@ -2,27 +2,16 @@
from __future__ import annotations
-import importlib
-import sys
-import types
from pathlib import Path
from unittest.mock import patch
-import click
import pytest
from click.testing import CliRunner
+from headroom.cli import wrap as wrap_cli
+from headroom.cli.main import main
from headroom.copilot_auth import DEFAULT_API_URL
-fake_main_module = types.ModuleType("headroom.cli.main")
-fake_main_module.main = click.Group()
-sys.modules["headroom.cli.main"] = fake_main_module
-sys.modules.pop("headroom.cli", None)
-sys.modules.pop("headroom.cli.wrap", None)
-
-wrap_cli = importlib.import_module("headroom.cli.wrap")
-main = fake_main_module.main
-
@pytest.fixture
def runner() -> CliRunner:
diff --git a/tests/test_proxy_copilot_auth_hooks.py b/tests/test_proxy_copilot_auth_hooks.py
index 5a89d8017..f0191a7e3 100644
--- a/tests/test_proxy_copilot_auth_hooks.py
+++ b/tests/test_proxy_copilot_auth_hooks.py
@@ -11,20 +11,20 @@ import pytest
ROOT = Path(__file__).resolve().parents[1]
-def _load_handler_module(module_name: str, relative_path: str):
+def _load_handler_module(monkeypatch: pytest.MonkeyPatch, module_name: str, relative_path: str):
proxy_pkg = types.ModuleType("headroom.proxy")
proxy_pkg.__path__ = [str(ROOT / "headroom" / "proxy")]
- sys.modules["headroom.proxy"] = proxy_pkg
+ monkeypatch.setitem(sys.modules, "headroom.proxy", proxy_pkg)
handlers_pkg = types.ModuleType("headroom.proxy.handlers")
handlers_pkg.__path__ = [str(ROOT / "headroom" / "proxy" / "handlers")]
- sys.modules["headroom.proxy.handlers"] = handlers_pkg
+ monkeypatch.setitem(sys.modules, "headroom.proxy.handlers", handlers_pkg)
httpx_mod = types.ModuleType("httpx")
httpx_mod.ConnectError = type("ConnectError", (Exception,), {})
httpx_mod.ConnectTimeout = type("ConnectTimeout", (Exception,), {})
httpx_mod.PoolTimeout = type("PoolTimeout", (Exception,), {})
- sys.modules["httpx"] = httpx_mod
+ monkeypatch.setitem(sys.modules, "httpx", httpx_mod)
responses_mod = types.ModuleType("fastapi.responses")
@@ -40,12 +40,12 @@ def _load_handler_module(module_name: str, relative_path: str):
responses_mod.Response = Response
responses_mod.StreamingResponse = StreamingResponse
- sys.modules["fastapi.responses"] = responses_mod
+ monkeypatch.setitem(sys.modules, "fastapi.responses", responses_mod)
spec = importlib.util.spec_from_file_location(module_name, ROOT / relative_path)
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
- sys.modules[module_name] = module
+ monkeypatch.setitem(sys.modules, module_name, module)
spec.loader.exec_module(module)
return module
@@ -53,6 +53,7 @@ def _load_handler_module(module_name: str, relative_path: str):
@pytest.mark.asyncio
async def test_openai_passthrough_applies_copilot_auth(monkeypatch: pytest.MonkeyPatch) -> None:
openai_mod = _load_handler_module(
+ monkeypatch,
"tests.headroom_proxy_handlers_openai",
"headroom/proxy/handlers/openai.py",
)
@@ -110,6 +111,7 @@ async def test_openai_passthrough_applies_copilot_auth(monkeypatch: pytest.Monke
@pytest.mark.asyncio
async def test_streaming_response_applies_copilot_auth(monkeypatch: pytest.MonkeyPatch) -> None:
streaming_mod = _load_handler_module(
+ monkeypatch,
"tests.headroom_proxy_handlers_streaming",
"headroom/proxy/handlers/streaming.py",
)
diff --git a/tests/test_release_version.py b/tests/test_release_version.py
index 52bb0fcac..461e0db45 100644
--- a/tests/test_release_version.py
+++ b/tests/test_release_version.py
@@ -14,6 +14,7 @@ from headroom.release_version import (
compute_release_version,
determine_bump_level,
find_latest_release_tag,
+ get_canonical_version,
list_release_commits,
normalize_release_tag,
parse_release_tag,
@@ -177,7 +178,7 @@ def test_release_version_script_runs_directly_without_importing_headroom_package
assert output_path.read_text(encoding="utf-8").splitlines() == [
"version=0.6.0",
"npm_version=0.6.0",
- "canonical=0.5.25",
+ f"canonical={get_canonical_version(ROOT)}",
"height=0",
"bump=manual",
"previous_tag=",