mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-08-10 14:27:00 -04:00
## Description `headroom proxy` can wedge when code-aware compression enters the tree-sitter Perl external scanner and the native scan keeps the GIL indefinitely. Current main still has two routes into that scanner: explicit `perl` or `pl` hints flow through `CodeAwareCompressor`, and `detect_language()` can nominate Perl on non-Perl code because its prefilter matches generic sigils such as decorators, JSDoc tags, and shell variables before phase 2 parses every surviving candidate grammar. This change quarantines Perl at the code-aware compression funnel without widening scope. Perl remains recognized at the input boundary, but live proxy compression no longer requests a Perl parser. Non-Perl code keeps its existing code-aware behavior. Real Perl falls back through the existing safe Kompress or passthrough contract instead of entering tree-sitter. The diff stays inside `headroom/transforms/code_compressor.py` plus focused parser-safety regressions. Refs #2185 ## Type of Change - [x] 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 - [ ] Code refactoring (no functional changes) ## Changes Made - Added a Perl quarantine list in `headroom/transforms/code_compressor.py` and used it to stop Perl candidate parsing during language detection. - Added a hard `_get_parser()` guard so no live code-aware path can construct a Perl parser. - Routed resolved explicit Perl hints through the existing safe fallback or passthrough contract before AST compression. - Added focused parser-safety regressions for non-Perl candidate bleed, explicit `perl` and `pl`, inferred real Perl, mixed fenced Perl content, fallback-disabled passthrough, and non-Perl negative space. ## Testing - [x] Unit tests pass (`uv run pytest tests/test_perl_scanner_safety.py -q`) - [x] Linting passes (`uv run ruff check headroom/transforms/code_compressor.py tests/test_perl_scanner_safety.py`) - [ ] Type checking passes (`uv run mypy headroom`) - [x] New tests added for new functionality when applicable - [ ] Manual testing performed ### Test Output ```text uv run pytest tests/test_perl_scanner_safety.py -q 8 passed in 1.93s uv run ruff check headroom/transforms/code_compressor.py tests/test_perl_scanner_safety.py All checks passed! uv run ruff format headroom/transforms/code_compressor.py tests/test_perl_scanner_safety.py --check 2 files already formatted ``` ## Real Behavior Proof - Environment: Windows, Python from the synced `uv` environment, `dev` and `code` extras installed, no provider call - Exact command / steps: Run `uv run pytest tests/test_perl_scanner_safety.py -q`. - Observed result: `8 passed in 1.93s`; the suite proves explicit `perl` and `pl`, inferred real Perl, mixed fenced Perl, and non-Perl negative-space routes all avoid Perl parser entry. - Not tested: the reporter's macOS payload and long-running concurrent workload ## 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 - [x] 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 have updated the CHANGELOG.md if applicable ## Additional Notes - Use `Refs #2185`, not `Closes #2185`. The wedge surface is this PR's scope, but #2185 also carries a separate orphaned `headroom mcp serve` report that this slice does not address. - This is a reachability fix. It does not repair the upstream Perl scanner and it does not harden other grammars against the same class of native wedge. - `CHANGELOG.md` remains unchanged because Headroom generates release notes from conventional commits. - Merged PR https://github.com/headroomlabs-ai/headroom/pull/2114 addresses a different cooperative compression stall and stays separate from this native parser-entry slice. --------- Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
504 lines
18 KiB
Python
504 lines
18 KiB
Python
"""Tests for `headroom wrap zcode` and `headroom unwrap zcode` commands.
|
|
|
|
ZCode is a desktop Electron app (zcode.z.ai) with no CLI binary. The wrap
|
|
command follows the Pattern-B (proxy-only watcher) approach: it starts the
|
|
proxy, injects RTK guidance into AGENTS.md at the project root, and prints
|
|
the ZCode settings the user should configure in the app's settings UI.
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
@pytest.fixture
|
|
def runner() -> CliRunner:
|
|
return CliRunner()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Wrap: --prepare-only RTK injection into AGENTS.md
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_prepare_only_injects_rtk_into_agents_md(
|
|
runner: CliRunner,
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""``wrap zcode --prepare-only`` writes the RTK block to AGENTS.md at cwd."""
|
|
monkeypatch.chdir(tmp_path)
|
|
monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)
|
|
|
|
with patch.object(wrap_mod, "_ensure_rtk_binary", return_value=Path("/tmp/rtk")):
|
|
result = runner.invoke(main, ["wrap", "zcode", "--prepare-only"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
marker = tmp_path / "AGENTS.md"
|
|
assert marker.exists(), "AGENTS.md should be created"
|
|
content = marker.read_text(encoding="utf-8")
|
|
assert wrap_mod._RTK_MARKER in content
|
|
assert "RTK (Rust Token Killer)" in content
|
|
|
|
|
|
def test_prepare_only_idempotent_no_duplicate_block(
|
|
runner: CliRunner,
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Running prepare-only twice must not duplicate the RTK block in AGENTS.md."""
|
|
monkeypatch.chdir(tmp_path)
|
|
monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)
|
|
|
|
with patch.object(wrap_mod, "_ensure_rtk_binary", return_value=Path("/tmp/rtk")):
|
|
runner.invoke(main, ["wrap", "zcode", "--prepare-only"])
|
|
runner.invoke(main, ["wrap", "zcode", "--prepare-only"])
|
|
|
|
content = (tmp_path / "AGENTS.md").read_text(encoding="utf-8")
|
|
assert content.count(wrap_mod._RTK_MARKER) == 1
|
|
|
|
|
|
def test_no_context_tool_does_not_create_agents_md(
|
|
runner: CliRunner,
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""--no-context-tool must not create AGENTS.md and must not invoke rtk."""
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
with patch.object(wrap_mod, "_ensure_rtk_binary") as ensure:
|
|
result = runner.invoke(main, ["wrap", "zcode", "--prepare-only", "--no-context-tool"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert not (tmp_path / "AGENTS.md").exists()
|
|
ensure.assert_not_called()
|
|
|
|
|
|
def test_preserves_existing_agents_md_content(
|
|
runner: CliRunner,
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Pre-existing AGENTS.md content must be preserved when RTK is appended."""
|
|
monkeypatch.chdir(tmp_path)
|
|
monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)
|
|
agents_md = tmp_path / "AGENTS.md"
|
|
original = "# Project conventions\n\nAlways use Python 3.12.\n"
|
|
agents_md.write_text(original, encoding="utf-8")
|
|
|
|
with patch.object(wrap_mod, "_ensure_rtk_binary", return_value=Path("/tmp/rtk")):
|
|
result = runner.invoke(main, ["wrap", "zcode", "--prepare-only"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
content = agents_md.read_text(encoding="utf-8")
|
|
assert "Always use Python 3.12." in content
|
|
assert wrap_mod._RTK_MARKER in content
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Wrap: setup instructions output
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_wrap_prints_proxy_urls(
|
|
runner: CliRunner,
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""The wrap command must print the proxy URLs for ZCode configuration."""
|
|
monkeypatch.chdir(tmp_path)
|
|
monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)
|
|
|
|
fake_rtk = Path("/tmp/rtk")
|
|
|
|
def fake_watcher(**kwargs): # noqa: ANN003
|
|
print_fn = kwargs.get("print_setup_lines")
|
|
if callable(print_fn):
|
|
print_fn(kwargs["port"])
|
|
|
|
with patch.object(wrap_mod, "_ensure_rtk_binary", return_value=fake_rtk):
|
|
with patch.object(wrap_mod, "_run_proxy_only_watcher", side_effect=fake_watcher):
|
|
result = runner.invoke(main, ["wrap", "zcode", "--port", "9000"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert "http://127.0.0.1:9000/v1" in result.output
|
|
assert "http://127.0.0.1:9000" in result.output
|
|
assert "Settings > Model Settings" in result.output
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Unwrap: RTK removal from AGENTS.md
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_unwrap_removes_rtk_from_agents_md(
|
|
runner: CliRunner,
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""``unwrap zcode`` removes RTK instructions from AGENTS.md."""
|
|
monkeypatch.chdir(tmp_path)
|
|
agents_md = tmp_path / "AGENTS.md"
|
|
agents_md.write_text(
|
|
"# Project\n\nSome content.\n\n" + wrap_mod.RTK_INSTRUCTIONS_BLOCK + "\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
with patch.object(wrap_mod, "_stop_local_proxy_for_unwrap", return_value="stopped"):
|
|
result = runner.invoke(main, ["unwrap", "zcode"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert "Removed Headroom rtk instructions" in result.output
|
|
content = agents_md.read_text(encoding="utf-8")
|
|
assert wrap_mod._RTK_MARKER not in content
|
|
assert "Some content." in content
|
|
|
|
|
|
def test_unwrap_deletes_empty_agents_md(
|
|
runner: CliRunner,
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""``unwrap zcode`` deletes AGENTS.md if it only contained RTK instructions."""
|
|
monkeypatch.chdir(tmp_path)
|
|
agents_md = tmp_path / "AGENTS.md"
|
|
agents_md.write_text(wrap_mod.RTK_INSTRUCTIONS_BLOCK + "\n", encoding="utf-8")
|
|
|
|
with patch.object(wrap_mod, "_stop_local_proxy_for_unwrap", return_value="stopped"):
|
|
result = runner.invoke(main, ["unwrap", "zcode"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert not agents_md.exists(), "AGENTS.md should be deleted when only RTK content"
|
|
|
|
|
|
def test_unwrap_noop_when_no_markers(
|
|
runner: CliRunner,
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""``unwrap zcode`` is a safe no-op when AGENTS.md has no Headroom markers."""
|
|
monkeypatch.chdir(tmp_path)
|
|
agents_md = tmp_path / "AGENTS.md"
|
|
agents_md.write_text("# Project\n\nSome content.\n", encoding="utf-8")
|
|
|
|
with patch.object(wrap_mod, "_stop_local_proxy_for_unwrap", return_value="stopped"):
|
|
result = runner.invoke(main, ["unwrap", "zcode"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert "Nothing to undo" in result.output
|
|
content = agents_md.read_text(encoding="utf-8")
|
|
assert content == "# Project\n\nSome content.\n"
|
|
|
|
|
|
def test_unwrap_noop_when_no_agents_md(
|
|
runner: CliRunner,
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""``unwrap zcode`` is a safe no-op when AGENTS.md does not exist."""
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
with patch.object(wrap_mod, "_stop_local_proxy_for_unwrap", return_value="stopped"):
|
|
result = runner.invoke(main, ["unwrap", "zcode"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert "Nothing to undo" in result.output
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Runtime: proxy targets
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_build_proxy_targets() -> None:
|
|
"""build_proxy_targets returns correct OpenAI and Anthropic URLs."""
|
|
from headroom.providers.zcode.runtime import build_proxy_targets
|
|
|
|
targets = build_proxy_targets(8787)
|
|
assert targets.openai_base_url == "http://127.0.0.1:8787/v1"
|
|
assert targets.anthropic_base_url == "http://127.0.0.1:8787"
|
|
|
|
|
|
def test_build_proxy_targets_custom_port() -> None:
|
|
"""build_proxy_targets respects custom port."""
|
|
from headroom.providers.zcode.runtime import build_proxy_targets
|
|
|
|
targets = build_proxy_targets(9999)
|
|
assert targets.openai_base_url == "http://127.0.0.1:9999/v1"
|
|
assert targets.anthropic_base_url == "http://127.0.0.1:9999"
|
|
|
|
|
|
def test_render_setup_lines_includes_mcp_instruction() -> None:
|
|
"""render_setup_lines includes the MCP paste JSON for user convenience."""
|
|
from headroom.providers.zcode.runtime import render_setup_lines
|
|
|
|
lines = render_setup_lines(8787)
|
|
joined = "\n".join(lines)
|
|
assert "headroom" in joined.lower()
|
|
assert "MCP" in joined
|
|
assert '"stdio"' in joined
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Runtime: upstream detection
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_detect_upstream_from_config(tmp_path: Path) -> None:
|
|
"""detect_upstream reads config.json and returns the enabled provider."""
|
|
from headroom.providers.zcode.runtime import detect_upstream
|
|
|
|
config = tmp_path / "config.json"
|
|
config.write_text(
|
|
'{"provider": {"zai": {"name": "Z.ai", "kind": "anthropic", '
|
|
'"enabled": true, "options": {"baseURL": "https://api.z.ai/api/anthropic"}}}}',
|
|
encoding="utf-8",
|
|
)
|
|
upstream = detect_upstream(config)
|
|
assert upstream.provider_name == "Z.ai"
|
|
assert upstream.base_url == "https://api.z.ai/api/anthropic"
|
|
assert upstream.kind == "anthropic"
|
|
|
|
|
|
def test_detect_upstream_openai_compatible(tmp_path: Path) -> None:
|
|
"""detect_upstream handles OpenAI-compatible providers."""
|
|
from headroom.providers.zcode.runtime import detect_upstream
|
|
|
|
config = tmp_path / "config.json"
|
|
config.write_text(
|
|
'{"provider": {"custom": {"name": "Custom", "kind": "openai", '
|
|
'"enabled": true, "options": {"baseURL": "https://my-api.example.com/v1"}}}}',
|
|
encoding="utf-8",
|
|
)
|
|
upstream = detect_upstream(config)
|
|
assert upstream.kind == "openai"
|
|
assert upstream.base_url == "https://my-api.example.com/v1"
|
|
|
|
|
|
def test_detect_upstream_disabled_provider_ignored(tmp_path: Path) -> None:
|
|
"""detect_upstream skips disabled providers."""
|
|
from headroom.providers.zcode.runtime import detect_upstream
|
|
|
|
config = tmp_path / "config.json"
|
|
config.write_text(
|
|
'{"provider": {"zai": {"name": "Z.ai", "kind": "anthropic", '
|
|
'"enabled": false, "options": {"baseURL": "https://api.z.ai/api/anthropic"}}}}',
|
|
encoding="utf-8",
|
|
)
|
|
upstream = detect_upstream(config)
|
|
assert upstream.provider_name == "Z.ai (default)"
|
|
assert upstream.base_url == "https://api.z.ai/api/anthropic"
|
|
|
|
|
|
def test_detect_upstream_no_baseurl_skips(tmp_path: Path) -> None:
|
|
"""detect_upstream skips providers with empty or missing baseURL."""
|
|
from headroom.providers.zcode.runtime import detect_upstream
|
|
|
|
config = tmp_path / "config.json"
|
|
config.write_text(
|
|
'{"provider": {"zai": {"name": "Z.ai", "kind": "anthropic", '
|
|
'"enabled": true, "options": {"baseURL": ""}}}}',
|
|
encoding="utf-8",
|
|
)
|
|
upstream = detect_upstream(config)
|
|
assert upstream.provider_name == "Z.ai (default)"
|
|
assert upstream.base_url == "https://api.z.ai/api/anthropic"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"bad_options",
|
|
[
|
|
None,
|
|
[],
|
|
42,
|
|
"just a string",
|
|
],
|
|
ids=["null", "list", "int", "string"],
|
|
)
|
|
def test_detect_upstream_malformed_options_skips(tmp_path: Path, bad_options: object) -> None:
|
|
"""detect_upstream falls back when provider options is not a dict."""
|
|
import json as _json
|
|
|
|
from headroom.providers.zcode.runtime import detect_upstream
|
|
|
|
config = tmp_path / "config.json"
|
|
config.write_text(
|
|
_json.dumps(
|
|
{
|
|
"provider": {
|
|
"bad": {
|
|
"name": "Bad Provider",
|
|
"kind": "anthropic",
|
|
"enabled": True,
|
|
"options": bad_options,
|
|
}
|
|
}
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
upstream = detect_upstream(config)
|
|
assert upstream.provider_name == "Z.ai (default)"
|
|
assert upstream.base_url == "https://api.z.ai/api/anthropic"
|
|
|
|
|
|
def test_detect_upstream_missing_file_fallback(tmp_path: Path) -> None:
|
|
"""detect_upstream falls back to default when config file is missing."""
|
|
from headroom.providers.zcode.runtime import detect_upstream
|
|
|
|
config = tmp_path / "nonexistent.json"
|
|
upstream = detect_upstream(config)
|
|
assert upstream.provider_name == "Z.ai (default)"
|
|
assert upstream.base_url == "https://api.z.ai/api/anthropic"
|
|
assert upstream.kind == "anthropic"
|
|
|
|
|
|
def test_detect_upstream_invalid_json_fallback(tmp_path: Path) -> None:
|
|
"""detect_upstream falls back to default on malformed JSON."""
|
|
from headroom.providers.zcode.runtime import detect_upstream
|
|
|
|
config = tmp_path / "config.json"
|
|
config.write_text("not json at all", encoding="utf-8")
|
|
upstream = detect_upstream(config)
|
|
assert upstream.provider_name == "Z.ai (default)"
|
|
assert upstream.base_url == "https://api.z.ai/api/anthropic"
|
|
|
|
|
|
def test_detect_upstream_no_providers_fallback(tmp_path: Path) -> None:
|
|
"""detect_upstream falls back when config has no provider section."""
|
|
from headroom.providers.zcode.runtime import detect_upstream
|
|
|
|
config = tmp_path / "config.json"
|
|
config.write_text('{"settings": {}}', encoding="utf-8")
|
|
upstream = detect_upstream(config)
|
|
assert upstream.provider_name == "Z.ai (default)"
|
|
assert upstream.base_url == "https://api.z.ai/api/anthropic"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Runtime: upstream to proxy URLs
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_upstream_to_proxy_urls_anthropic() -> None:
|
|
"""upstream_to_proxy_urls returns (url, None) for anthropic upstream."""
|
|
from headroom.providers.zcode.runtime import ZCodeUpstream, upstream_to_proxy_urls
|
|
|
|
upstream = ZCodeUpstream(
|
|
provider_name="Z.ai", base_url="https://api.z.ai/api/anthropic", kind="anthropic"
|
|
)
|
|
anthropic_url, openai_url = upstream_to_proxy_urls(upstream)
|
|
assert anthropic_url == "https://api.z.ai/api/anthropic"
|
|
assert openai_url is None
|
|
|
|
|
|
def test_upstream_to_proxy_urls_openai() -> None:
|
|
"""upstream_to_proxy_urls returns (None, url) for openai-compatible upstream."""
|
|
from headroom.providers.zcode.runtime import ZCodeUpstream, upstream_to_proxy_urls
|
|
|
|
upstream = ZCodeUpstream(
|
|
provider_name="Custom",
|
|
base_url="https://my-api.example.com/v1",
|
|
kind="openai",
|
|
)
|
|
anthropic_url, openai_url = upstream_to_proxy_urls(upstream)
|
|
assert anthropic_url is None
|
|
assert openai_url == "https://my-api.example.com/v1"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Wrap: upstream detection integration
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_wrap_zcode_detects_upstream(
|
|
runner: CliRunner,
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""wrap zcode detects upstream and prints detected provider in setup."""
|
|
monkeypatch.chdir(tmp_path)
|
|
monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)
|
|
|
|
config = tmp_path / "config.json"
|
|
config.write_text(
|
|
'{"provider": {"zai": {"name": "Z.ai Coding", "kind": "anthropic", '
|
|
'"enabled": true, "options": {"baseURL": "https://api.z.ai/api/anthropic"}}}}',
|
|
encoding="utf-8",
|
|
)
|
|
|
|
from headroom.providers.zcode.runtime import detect_upstream, upstream_to_proxy_urls
|
|
|
|
upstream = detect_upstream(config)
|
|
assert upstream.provider_name == "Z.ai Coding"
|
|
assert upstream.base_url == "https://api.z.ai/api/anthropic"
|
|
|
|
anthropic_url, openai_url = upstream_to_proxy_urls(upstream)
|
|
assert anthropic_url == "https://api.z.ai/api/anthropic"
|
|
assert openai_url is None
|
|
|
|
|
|
def test_wrap_zcode_passes_upstream_to_watcher(
|
|
runner: CliRunner,
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""wrap zcode forwards detected upstream URLs to _run_proxy_only_watcher."""
|
|
monkeypatch.chdir(tmp_path)
|
|
monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)
|
|
|
|
from headroom.providers.zcode.runtime import ZCodeUpstream
|
|
|
|
captured: dict[str, object] = {}
|
|
|
|
def fake_watcher(**kwargs): # noqa: ANN003
|
|
captured.update(kwargs)
|
|
|
|
fake_upstream = ZCodeUpstream(
|
|
provider_name="Z.ai", base_url="https://api.z.ai/api/anthropic", kind="anthropic"
|
|
)
|
|
|
|
with patch.object(wrap_mod, "_ensure_rtk_binary", return_value=Path("/tmp/rtk")):
|
|
with patch.object(wrap_mod, "_detect_zcode_upstream", return_value=fake_upstream):
|
|
with patch.object(wrap_mod, "_run_proxy_only_watcher", side_effect=fake_watcher):
|
|
runner.invoke(main, ["wrap", "zcode", "--port", "9000"])
|
|
|
|
assert captured.get("anthropic_api_url") == "https://api.z.ai/api/anthropic"
|
|
assert captured.get("openai_api_url") is None
|
|
assert captured.get("port") == 9000
|
|
|
|
|
|
def test_wrap_zcode_passes_openai_upstream(
|
|
runner: CliRunner,
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""wrap zcode forwards OpenAI-compatible upstream URLs correctly."""
|
|
monkeypatch.chdir(tmp_path)
|
|
monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)
|
|
|
|
from headroom.providers.zcode.runtime import ZCodeUpstream
|
|
|
|
captured: dict[str, object] = {}
|
|
|
|
def fake_watcher(**kwargs): # noqa: ANN003
|
|
captured.update(kwargs)
|
|
|
|
fake_upstream = ZCodeUpstream(
|
|
provider_name="Custom", base_url="https://my-api.example.com/v1", kind="openai"
|
|
)
|
|
|
|
with patch.object(wrap_mod, "_ensure_rtk_binary", return_value=Path("/tmp/rtk")):
|
|
with patch.object(wrap_mod, "_detect_zcode_upstream", return_value=fake_upstream):
|
|
with patch.object(wrap_mod, "_run_proxy_only_watcher", side_effect=fake_watcher):
|
|
runner.invoke(main, ["wrap", "zcode", "--port", "9000"])
|
|
|
|
assert captured.get("anthropic_api_url") is None
|
|
assert captured.get("openai_api_url") == "https://my-api.example.com/v1"
|