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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
test(cli): parametrize the registry-target wrap tests (#4119)
Tejas Chopra9266b0300dc9
diff --git a/tests/test_cli/test_wrap_goose.py b/tests/test_cli/test_wrap_goose.pydeleted file mode 100644index 24f2ad496..000000000--- a/tests/test_cli/test_wrap_goose.py+++ /dev/null@@ -1,71 +0,0 @@-"""Tests for `headroom wrap goose` command (PR-G1, Phase G).--Covers goose-specific behavior: the OPENAI/ANTHROPIC env-var fan-out for-the child binary launch, and the goose-binary-not-found error path.-"""--from __future__ import annotations--from pathlib import Path-from unittest.mock import patch--import pytest-from click.testing import CliRunner--from headroom.cli import wrap as wrap_mod-from headroom.cli.main import main---def runner() -> CliRunner:- return CliRunner()---def test_wrap_goose_sets_provider_envs(- runner: CliRunner,- tmp_path: Path,- monkeypatch: pytest.MonkeyPatch,-) -> None:- """OPENAI_BASE_URL, OPENAI_API_BASE, ANTHROPIC_BASE_URL, ANTHROPIC_HOST are set.-- Goose's Anthropic provider reads ANTHROPIC_HOST rather than- ANTHROPIC_BASE_URL, so the value is asserted rather than mere presence.- """- monkeypatch.chdir(tmp_path)- monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)-- captured: dict[str, object] = {}-- def fake_launch_tool(**kwargs): # noqa: ANN003- captured.update(kwargs)-- with patch.object(wrap_mod.shutil, "which", return_value="goose"):- with patch.object(wrap_mod, "_launch_tool", side_effect=fake_launch_tool):- result = runner.invoke(main, ["wrap", "goose", "--port", "9000", "--", "session"])-- assert result.exit_code == 0, result.output- env = captured["env"]- assert isinstance(env, dict)- assert env["OPENAI_BASE_URL"] == "http://127.0.0.1:9000/v1"- assert env["OPENAI_API_BASE"] == "http://127.0.0.1:9000/v1"- assert env["ANTHROPIC_BASE_URL"] == "http://127.0.0.1:9000"- assert env["ANTHROPIC_HOST"] == "http://127.0.0.1:9000"- assert captured["tool_label"] == "GOOSE"- assert captured["agent_type"] == "goose"- assert captured["args"] == ("session",)---def test_wrap_goose_missing_binary_errors_clearly(- runner: CliRunner,- tmp_path: Path,- monkeypatch: pytest.MonkeyPatch,-) -> None:- """If the goose binary is missing the command must fail with a clear error."""- monkeypatch.chdir(tmp_path)- monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)-- with patch.object(wrap_mod.shutil, "which", return_value=None):- result = runner.invoke(main, ["wrap", "goose"])-- assert result.exit_code == 1- assert "'goose' not found in PATH" in result.outputdiff --git a/tests/test_cli/test_wrap_openclaude.py b/tests/test_cli/test_wrap_openclaude.pydeleted file mode 100644index 4877879da..000000000--- a/tests/test_cli/test_wrap_openclaude.py+++ /dev/null@@ -1,61 +0,0 @@-"""Tests for `headroom wrap openclaude` command (issue #1411)."""--from __future__ import annotations--from pathlib import Path-from unittest.mock import patch-from urllib.parse import quote--import pytest-from click.testing import CliRunner--from headroom.cli.main import main--OPENCLAUDE_BINARY = "openclaude"-OPENCLAUDE_MODEL_ARG = "gpt-4o"---def _expected_project_prefix() -> str:- return f"/p/{quote(Path.cwd().name, safe='')}"---def runner() -> CliRunner:- return CliRunner()---def test_wrap_openclaude_routes_proxy_envs(- runner: CliRunner, tmp_path: Path, monkeypatch: pytest.MonkeyPatch-) -> None:- monkeypatch.chdir(tmp_path)- captured: dict[str, object] = {}-- def fake_launch_tool(**kwargs): # noqa: ANN003- captured.update(kwargs)-- with patch("headroom.cli.wrap.shutil.which", return_value=OPENCLAUDE_BINARY):- with patch("headroom.cli.wrap._launch_tool", side_effect=fake_launch_tool):- result = runner.invoke(- main,- ["wrap", "openclaude", "--", "--model", OPENCLAUDE_MODEL_ARG],- )-- assert result.exit_code == 0, result.output- env = captured["env"]- assert isinstance(env, dict)- prefix = _expected_project_prefix()- assert env["OPENAI_API_BASE"] == f"http://127.0.0.1:8787{prefix}/v1"- assert env["ANTHROPIC_BASE_URL"] == f"http://127.0.0.1:8787{prefix}"- assert captured["tool_label"] == "OPENCLAUDE"- assert captured["agent_type"] == "openclaude"- assert captured["args"] == ("--model", OPENCLAUDE_MODEL_ARG)---def test_wrap_openclaude_missing_binary_errors(- runner: CliRunner, tmp_path: Path, monkeypatch: pytest.MonkeyPatch-) -> None:- monkeypatch.chdir(tmp_path)- with patch("headroom.cli.wrap.shutil.which", return_value=None):- result = runner.invoke(main, ["wrap", "openclaude"])- assert result.exit_code == 1- assert "openclaude" in result.output.lower()diff --git a/tests/test_cli/test_wrap_openhands.py b/tests/test_cli/test_wrap_openhands.pydeleted file mode 100644index 99fe42870..000000000--- a/tests/test_cli/test_wrap_openhands.py+++ /dev/null@@ -1,84 +0,0 @@-"""Tests for `headroom wrap openhands` command (PR-G1, Phase G)."""--from __future__ import annotations--from pathlib import Path-from unittest.mock import patch--import pytest-from click.testing import CliRunner--from headroom.cli import wrap as wrap_mod-from headroom.cli.main import main---def runner() -> CliRunner:- return CliRunner()---def test_wrap_openhands_prepare_only_succeeds_unpatched(- runner: CliRunner,- tmp_path: Path,- monkeypatch: pytest.MonkeyPatch,-) -> None:- """`wrap openhands --prepare-only` must succeed with nothing patched.-- The subcommand used to hard-fail by default because a CLI context tool was- a required dependency of the prelude. Nothing is fetched or written now, so- the bare invocation has to exit 0 on its own.- """- monkeypatch.chdir(tmp_path)- monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)-- result = runner.invoke(main, ["wrap", "openhands", "--prepare-only"])-- assert result.exit_code == 0, result.output---def test_wrap_openhands_sets_provider_envs(- runner: CliRunner,- tmp_path: Path,- monkeypatch: pytest.MonkeyPatch,-) -> None:- """OPENAI_BASE_URL, ANTHROPIC_BASE_URL, LLM_BASE_URL are set on launch."""- monkeypatch.chdir(tmp_path)- monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)-- captured: dict[str, object] = {}-- def fake_launch_tool(**kwargs): # noqa: ANN003- captured.update(kwargs)-- with patch.object(wrap_mod.shutil, "which", return_value="openhands"):- with patch.object(wrap_mod, "_launch_tool", side_effect=fake_launch_tool):- result = runner.invoke(- main, ["wrap", "openhands", "--port", "9000", "--", "--task", "demo"]- )-- assert result.exit_code == 0, result.output- env = captured["env"]- assert isinstance(env, dict)- assert env["OPENAI_BASE_URL"] == "http://127.0.0.1:9000/v1"- assert env["OPENAI_API_BASE"] == "http://127.0.0.1:9000/v1"- assert env["ANTHROPIC_BASE_URL"] == "http://127.0.0.1:9000"- assert env["LLM_BASE_URL"] == "http://127.0.0.1:9000/v1"- assert captured["tool_label"] == "OPENHANDS"- assert captured["agent_type"] == "openhands"- assert captured["args"] == ("--task", "demo")---def test_wrap_openhands_missing_binary_errors_clearly(- runner: CliRunner,- tmp_path: Path,- monkeypatch: pytest.MonkeyPatch,-) -> None:- """If the openhands binary is missing the command must fail with a clear error."""- monkeypatch.chdir(tmp_path)- monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)-- with patch.object(wrap_mod.shutil, "which", return_value=None):- result = runner.invoke(main, ["wrap", "openhands"])-- assert result.exit_code == 1- assert "'openhands' not found in PATH" in result.outputdiff --git a/tests/test_cli/test_wrap_registry.py b/tests/test_cli/test_wrap_registry.pyindex 6f229b243..a22d7ef69 100644--- a/tests/test_cli/test_wrap_registry.py+++ b/tests/test_cli/test_wrap_registry.py@@ -1,20 +1,123 @@-"""Tests for the declarative WrapTarget registry.+"""Tests for the declarative WrapTarget registry and its generated commands.-The per-tool suites (test_wrap_goose / _openhands / _openclaude) still cover-env routing, missing binaries, and --prepare-only end to end against the-generated commands. These tests pin what they don't: the banner, parity with-the aider builder openclaude used to call, and the option surface.+Every target gets the same end-to-end checks (missing binary, --prepare-only,+env routing on launch, Bob included); the rest pins the banner, parity with the aider builder+openclaude used to call, and the option surface. Bob's preflight has its own+suite in test_wrap_bob."""from __future__ import annotations+from pathlib import Path+from urllib.parse import quote++import pytestfrom click.testing import CliRunnerimport headroom.cli.wrap as wrap_mod+from headroom.cli.main import mainfrom headroom.cli.wrap import wrapfrom headroom.providers.wrap_registry import WRAP_TARGETS, build_launch_env[email protected]("name", sorted(WRAP_TARGETS))+def test_missing_binary_fails_with_install_hint(+ name: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ monkeypatch.chdir(tmp_path)+ monkeypatch.setattr(wrap_mod.shutil, "which", lambda _name: None)++ result = CliRunner().invoke(main, ["wrap", name])++ target = WRAP_TARGETS[name]+ assert result.exit_code == 1+ assert f"'{target.binary}' not found in PATH" in result.output+ assert target.install_hint in result.output++[email protected]("name", sorted(WRAP_TARGETS))+def test_prepare_only_succeeds_unpatched(+ name: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """`--prepare-only` fetches and writes nothing, so it exits 0 on its own."""+ monkeypatch.chdir(tmp_path)++ result = CliRunner().invoke(main, ["wrap", name, "--prepare-only"])++ assert result.exit_code == 0, result.output+++def _project_prefix(cwd: Path) -> str:+ return f"/p/{quote(cwd.name, safe='')}"+++ ("name", "args", "expected_env"),+ [+ (+ "bob",+ ("--port", "9000", "--", "-p", "hello"),+ lambda cwd: {+ # Bare origin: Bob appends /inference/v1/... itself.+ "BOB_GATEWAY_URL": f"http://127.0.0.1:9000{_project_prefix(cwd)}",+ },+ ),+ (+ "goose",+ ("--port", "9000", "--", "session"),+ lambda _cwd: {+ "OPENAI_BASE_URL": "http://127.0.0.1:9000/v1",+ "OPENAI_API_BASE": "http://127.0.0.1:9000/v1",+ "ANTHROPIC_BASE_URL": "http://127.0.0.1:9000",+ # Goose's Anthropic provider reads ANTHROPIC_HOST, not _BASE_URL.+ "ANTHROPIC_HOST": "http://127.0.0.1:9000",+ },+ ),+ (+ "openhands",+ ("--port", "9000", "--", "--task", "demo"),+ lambda _cwd: {+ "OPENAI_BASE_URL": "http://127.0.0.1:9000/v1",+ "OPENAI_API_BASE": "http://127.0.0.1:9000/v1",+ "ANTHROPIC_BASE_URL": "http://127.0.0.1:9000",+ "LLM_BASE_URL": "http://127.0.0.1:9000/v1",+ },+ ),+ (+ "openclaude",+ ("--", "--model", "gpt-4o"),+ lambda cwd: {+ "OPENAI_API_BASE": f"http://127.0.0.1:8787{_project_prefix(cwd)}/v1",+ "ANTHROPIC_BASE_URL": f"http://127.0.0.1:8787{_project_prefix(cwd)}",+ },+ ),+ ],+)+def test_launch_routes_tool_env_through_proxy(+ name: str,+ args: tuple[str, ...],+ expected_env,+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.chdir(tmp_path)+ # Bob's preflight reads ~/.bob/settings; keep it off the real home.+ monkeypatch.setattr(Path, "home", lambda: tmp_path)+ monkeypatch.setattr(wrap_mod.shutil, "which", lambda binary: f"/usr/bin/{binary}")+ captured: dict = {}+ monkeypatch.setattr(wrap_mod, "_launch_tool", lambda **kw: captured.update(kw))++ result = CliRunner().invoke(main, ["wrap", name, *args])++ assert result.exit_code == 0, result.output+ env = captured["env"]+ for key, value in expected_env(Path.cwd()).items():+ assert env[key] == value, key+ assert captured["tool_label"] == name.upper()+ assert captured["agent_type"] == name+ assert captured["args"] == tuple(args[args.index("--") + 1 :])++def test_goose_banner_hides_openai_api_base_alias():_, display = build_launch_env(WRAP_TARGETS["goose"], 8787, environ={}, project="p")# Goose has no project prefix; its endpoint override is visible in the banner.