mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 15:10:00 +08:00
## Description `headroom wrap goose`, `wrap openhands`, `wrap openclaude` and `wrap bob` are all generated from one `WrapTarget` factory (`_make_registry_command`). Despite that, goose, openhands and openclaude each had their own near-identical test file, checking a missing binary, env fan-out and `--prepare-only`. This PR folds those three files into parametrized tests in `test_wrap_registry.py`, keyed on the registry. The missing-binary, `--prepare-only` and launch-env checks now also cover `bob`. Refs #4136 ## Type of Change - [ ] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [x] Code refactoring (no functional changes) ## Changes Made - Deleted `tests/test_cli/test_wrap_goose.py`, `test_wrap_openhands.py` and `test_wrap_openclaude.py`. - Added three tests to `tests/test_cli/test_wrap_registry.py`: - `test_missing_binary_fails_with_install_hint`, run for every target. It checks exit code 1, the `'<binary>' not found in PATH` text and the target's install hint. - `test_prepare_only_succeeds_unpatched`, run for every target. - `test_launch_routes_tool_env_through_proxy`, run for all four targets: bob, goose, openhands and openclaude. The bob case asserts `BOB_GATEWAY_URL` is the bare proxy origin with the `/p/<project>` prefix. The other cases keep the exact env values the old files asserted (including goose's `ANTHROPIC_HOST`, openhands' `LLM_BASE_URL` and openclaude's `/p/<project>` prefix), plus the tool label, agent type and passthrough args. - `test_wrap_bob.py` is unchanged. It covers Bob's gateway preflight, which only Bob has. - No product code changes. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [ ] Type checking passes (`mypy headroom`) - [ ] New tests added for new functionality - [ ] Manual testing performed ### Test Output ```text $ pytest -q -p no:cacheprovider tests/test_cli/test_wrap_registry.py tests/test_cli/test_wrap_bob.py 90 passed in 2.70s $ pytest --collect-only -q tests/test_cli/test_wrap_registry.py 16 tests collected $ uvx ruff@0.16.8 check tests/test_cli/test_wrap_registry.py All checks passed! ``` ## Real Behavior Proof - Environment: macOS 25.6, Python 3.12, repo `.venv`, branch cut from origin/main ata9815bbb. - Exact command / steps: ran the two test files above. The new parametrized cases invoke `headroom wrap <target>` through `CliRunner` with `shutil.which` and `_launch_tool` patched, the same way the deleted files did. - Observed result: all cases pass. Every assertion from the three deleted files has an equivalent in the new tests. - Smoke harness (real proxy started from this checkout against a local fake upstream; temp HOME and workspace, offline): ```text branch: cleanup/cli-integrations-registry-wrap-tests @d6600e52health: 200 POST /v1/messages -> HTTP200 smoke-ok POST /v1/chat/completions -> HTTP200 smoke-ok stats: requests=2 failed=0 tokens_saved=13574 savings_percent=99.82 proxy.log WARNING/ERROR lines (<tmp>/ws/logs/proxy-50368.log): 2 headroom.proxy - WARNING - event=proxy_offline_mode air-gap active — every connection Headroom initiates is refused (telemetry, update check, license reporter, model/tokenizer/binary/dataset downloads, remote Kompress, OTLP/Langfuse export, Copilot auth, subscription polling, OpenAI embedders, Headroom Cloud compression). Still allowed on purpose: forwarding your requests to the upstream you configured, operator-configured local endpoints, loopback health probes. 4 headroom.transforms.kompress_compressor - WARNING - Kompress: artifact prefetch found no usable file for chopratejas/kompress-v2-base 4 headroom.transforms.kompress_compressor - WARNING - Kompress: artifact prefetch refused for chopratejas/kompress-v2-base (HEADROOM_OFFLINE is set): HEADROOM_OFFLINE is set: refusing outbound network access for HuggingFace download of chopratejas/kompress-v2-base/onnx/kompress-int8-wo.onnx to huggingface.co. Unset HEADROOM_OFFLINE, or turn off the feature that needs this connection. (smoke dir: <tmp>) ``` - Direct runtime command on this PR's own path (temp HOME, HEADROOM_WORKSPACE_DIR and CODEX_HOME): ```text $ HOME=<tmp>/home HEADROOM_WORKSPACE_DIR=<tmp>/ws CODEX_HOME=<tmp>/codex headroom wrap bob --prepare-only exit=0 --- stdout --- stderr $ HOME=<tmp>/home HEADROOM_WORKSPACE_DIR=<tmp>/ws CODEX_HOME=<tmp>/codex headroom wrap goose exit=1 --- stdout Error: 'goose' not found in PATH. Install Goose: https://block.github.io/goose/ --- stderr ``` - Not tested: nothing beyond unit tests; this is a test-only change. ## Runtime Rollout Safety - Rollout-managed feature(s): None. - Minimum rollout channel: N/A, test-only change. - Stable/default behavior changed: No. - Kill switch / disable path: N/A. - Unsafe override required: No. - Qualification impact: None. - Rollback path: Revert this commit. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] I did **not** edit `CHANGELOG.md` — it is generated by release-please from my Conventional Commit PR title (a CI guard enforces this) ## Screenshots (if applicable) N/A ## Additional Notes - Part of maintainer-requested cleanup #4136 (dead code, duplication, debuggability), split into small PRs for review. - `REALIGNMENT/09-phase-G-rtk-observability.md:52-53` names the deleted test files. That is an old planning doc, so I recorded it in the docs ledger and did not edit it here. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4119" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a> <!-- devin-review-badge-end --> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
183 lines
6.2 KiB
Python
183 lines
6.2 KiB
Python
"""Tests for the declarative WrapTarget registry and its generated commands.
|
|
|
|
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 pytest
|
|
from click.testing import CliRunner
|
|
|
|
import headroom.cli.wrap as wrap_mod
|
|
from headroom.cli.main import main
|
|
from headroom.cli.wrap import wrap
|
|
from headroom.providers.wrap_registry import WRAP_TARGETS, build_launch_env
|
|
|
|
|
|
@pytest.mark.parametrize("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
|
|
|
|
|
|
@pytest.mark.parametrize("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='')}"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("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.
|
|
assert display == [
|
|
"OPENAI_BASE_URL=http://127.0.0.1:8787/v1",
|
|
"ANTHROPIC_BASE_URL=http://127.0.0.1:8787",
|
|
"ANTHROPIC_HOST=http://127.0.0.1:8787",
|
|
]
|
|
|
|
|
|
def test_openhands_banner_matches_legacy_body():
|
|
_, display = build_launch_env(WRAP_TARGETS["openhands"], 9000, environ={})
|
|
assert display == [
|
|
"OPENAI_BASE_URL=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",
|
|
]
|
|
|
|
|
|
def test_openclaude_matches_legacy_aider_builder():
|
|
from headroom.providers.aider import build_launch_env as aider_build_launch_env
|
|
|
|
environ = {"PATH": "/bin"} # non-empty: the aider builder treats {} as os.environ
|
|
got = build_launch_env(WRAP_TARGETS["openclaude"], 8787, environ, project="myproj")
|
|
assert got == aider_build_launch_env(8787, environ, project="myproj")
|
|
|
|
|
|
def test_option_surface_is_preserved():
|
|
for name in WRAP_TARGETS:
|
|
params = {p.name for p in wrap.commands[name].params}
|
|
assert {
|
|
"port",
|
|
"code_graph",
|
|
"no_proxy",
|
|
"learn",
|
|
"memory",
|
|
"backend",
|
|
"anyllm_provider",
|
|
"region",
|
|
"verbose",
|
|
"prepare_only",
|
|
"tool_args",
|
|
} <= params, name
|
|
|
|
|
|
def test_launch_passes_flags_through(monkeypatch):
|
|
monkeypatch.setattr(wrap_mod.shutil, "which", lambda name: f"/usr/bin/{name}")
|
|
captured: dict = {}
|
|
monkeypatch.setattr(wrap_mod, "_launch_tool", lambda **kw: captured.update(kw))
|
|
|
|
result = CliRunner().invoke(
|
|
wrap,
|
|
["goose", "--port", "9001", "--learn", "--backend", "anyllm", "--", "session"],
|
|
)
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert captured["args"] == ("session",)
|
|
assert captured["port"] == 9001
|
|
assert captured["learn"] is True
|
|
assert captured["backend"] == "anyllm"
|
|
assert captured["tool_label"] == "GOOSE"
|
|
assert captured["agent_type"] == "goose"
|