Files
wehub-resource-sync 0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:03:20 +08:00

275 lines
9.7 KiB
Python

"""Tests for Docker-bridge wrap preparation flows."""
from __future__ import annotations
import json
import subprocess
from pathlib import Path
from unittest.mock import patch
import pytest
from click.testing import CliRunner
from headroom.cli.main import main
from headroom.cli.wrap import _setup_lean_ctx_agent
@pytest.fixture(autouse=True)
def _default_context_tool(monkeypatch) -> None:
monkeypatch.delenv("HEADROOM_CONTEXT_TOOL", raising=False)
monkeypatch.delenv("LEAN_CTX_AGENT", raising=False)
monkeypatch.delenv("LEAN_CTX_DATA_DIR", raising=False)
def _set_test_home(monkeypatch, tmp_path: Path) -> None:
home = str(tmp_path)
monkeypatch.setenv("HOME", home)
monkeypatch.setenv("USERPROFILE", home)
def test_wrap_claude_prepare_only_skips_host_binary_lookup() -> None:
runner = CliRunner()
with patch("headroom.cli.wrap._prepare_wrap_rtk") as prepare_rtk:
with patch("headroom.cli.wrap.shutil.which") as which_mock:
result = runner.invoke(main, ["wrap", "claude", "--prepare-only", "--context-tool"])
assert result.exit_code == 0, result.output
prepare_rtk.assert_called_once()
which_mock.assert_not_called()
def test_wrap_claude_prepare_only_uses_lean_ctx_when_configured(monkeypatch) -> None:
runner = CliRunner()
monkeypatch.setenv("HEADROOM_CONTEXT_TOOL", "lean-ctx")
with patch("headroom.cli.wrap._prepare_wrap_rtk") as prepare_rtk:
with patch(
"headroom.cli.wrap._setup_lean_ctx_agent",
return_value=Path("lean-ctx"),
) as setup:
result = runner.invoke(main, ["wrap", "claude", "--prepare-only", "--context-tool"])
assert result.exit_code == 0, result.output
prepare_rtk.assert_not_called()
setup.assert_called_once_with("claude", verbose=False)
def test_setup_lean_ctx_agent_runs_outside_project_root(monkeypatch, tmp_path: Path) -> None:
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / ".git").mkdir()
lean_ctx = tmp_path / "lean-ctx"
lean_ctx.write_text("#!/bin/sh\n", encoding="utf-8")
calls: list[dict] = []
def fake_run(*args, **kwargs):
calls.append({"args": args, "kwargs": kwargs})
return subprocess.CompletedProcess(args[0], 0, stdout="", stderr="")
monkeypatch.chdir(project_root)
monkeypatch.setattr("headroom.lean_ctx.get_lean_ctx_path", lambda: lean_ctx)
monkeypatch.setattr("headroom.cli.wrap.subprocess.run", fake_run)
assert _setup_lean_ctx_agent("codex") == lean_ctx
assert calls
cwd = Path(calls[0]["kwargs"]["cwd"])
assert cwd != project_root
assert project_root not in cwd.parents
def test_wrap_codex_prepare_only_updates_config(monkeypatch, tmp_path: Path) -> None:
_set_test_home(monkeypatch, tmp_path)
runner = CliRunner()
with patch("headroom.cli.wrap._ensure_rtk_binary", return_value=None):
result = runner.invoke(main, ["wrap", "codex", "--prepare-only", "--port", "8787"])
assert result.exit_code == 0, result.output
config_file = tmp_path / ".codex" / "config.toml"
assert config_file.exists()
content = config_file.read_text(encoding="utf-8")
assert 'model_provider = "headroom"' in content
assert 'base_url = "http://127.0.0.1:8787/v1"' in content
def test_wrap_codex_prepare_only_uses_lean_ctx_when_configured(monkeypatch, tmp_path: Path) -> None:
_set_test_home(monkeypatch, tmp_path)
monkeypatch.setenv("HEADROOM_CONTEXT_TOOL", "lean-ctx")
runner = CliRunner()
with runner.isolated_filesystem(temp_dir=str(tmp_path)):
with patch("headroom.cli.wrap._ensure_rtk_binary") as ensure_rtk:
with patch(
"headroom.cli.wrap._setup_lean_ctx_agent",
return_value=Path("lean-ctx"),
) as setup:
result = runner.invoke(
main,
["wrap", "codex", "--prepare-only", "--no-mcp", "--no-serena"],
)
assert result.exit_code == 0, result.output
ensure_rtk.assert_not_called()
setup.assert_called_once_with("codex", verbose=False)
assert not Path("AGENTS.md").exists()
def test_wrap_codex_prepare_only_accepts_no_context_tool_alias(monkeypatch, tmp_path: Path) -> None:
_set_test_home(monkeypatch, tmp_path)
monkeypatch.setenv("HEADROOM_CONTEXT_TOOL", "lean-ctx")
runner = CliRunner()
with runner.isolated_filesystem(temp_dir=str(tmp_path)):
with patch("headroom.cli.wrap._ensure_rtk_binary") as ensure_rtk:
with patch("headroom.cli.wrap._setup_lean_ctx_agent") as setup:
result = runner.invoke(
main,
[
"wrap",
"codex",
"--prepare-only",
"--no-context-tool",
"--no-mcp",
"--no-serena",
],
)
assert result.exit_code == 0, result.output
ensure_rtk.assert_not_called()
setup.assert_not_called()
def test_wrap_aider_prepare_only_injects_conventions(monkeypatch, tmp_path: Path) -> None:
_set_test_home(monkeypatch, tmp_path)
runner = CliRunner()
with runner.isolated_filesystem(temp_dir=str(tmp_path)):
with patch("headroom.cli.wrap._ensure_rtk_binary", return_value=Path("rtk")):
result = runner.invoke(main, ["wrap", "aider", "--prepare-only"])
assert result.exit_code == 0, result.output
conventions = Path("CONVENTIONS.md")
assert conventions.exists()
assert "headroom:rtk-instructions" in conventions.read_text(encoding="utf-8")
def test_wrap_cursor_prepare_only_registers_native_hook(monkeypatch, tmp_path: Path) -> None:
# GH #756: when rtk's own `--agent cursor` hook registers successfully,
# headroom must not also inject RTK_INSTRUCTIONS_BLOCK into .cursorrules.
_set_test_home(monkeypatch, tmp_path)
runner = CliRunner()
# headroom trusts the on-disk hook, not rtk's exit code, so simulate rtk
# actually writing ~/.cursor/hooks.json when registration succeeds.
def _register(_rtk_path, *, agent):
hooks = tmp_path / ".cursor" / "hooks.json"
hooks.parent.mkdir(parents=True, exist_ok=True)
hooks.write_text('{"hooks": {"preToolUse": [{"command": "rtk hook cursor"}]}}')
return True
with runner.isolated_filesystem(temp_dir=str(tmp_path)):
with (
patch("headroom.cli.wrap._ensure_rtk_binary", return_value=Path("rtk")),
patch("headroom.rtk.installer.register_agent_hooks", side_effect=_register) as register,
):
result = runner.invoke(main, ["wrap", "cursor", "--prepare-only"])
assert result.exit_code == 0, result.output
register.assert_called_once_with(Path("rtk"), agent="cursor")
assert not Path(".cursorrules").exists()
def test_wrap_cursor_prepare_only_falls_back_to_cursorrules_when_hook_fails(
monkeypatch, tmp_path: Path
) -> None:
_set_test_home(monkeypatch, tmp_path)
runner = CliRunner()
with runner.isolated_filesystem(temp_dir=str(tmp_path)):
with (
patch("headroom.cli.wrap._ensure_rtk_binary", return_value=Path("rtk")),
patch("headroom.rtk.installer.register_agent_hooks", return_value=False),
):
result = runner.invoke(main, ["wrap", "cursor", "--prepare-only"])
assert result.exit_code == 0, result.output
cursorrules = Path(".cursorrules")
assert cursorrules.exists()
assert "headroom:rtk-instructions" in cursorrules.read_text(encoding="utf-8")
def test_wrap_cursor_prepare_only_uses_lean_ctx_when_configured(
monkeypatch, tmp_path: Path
) -> None:
_set_test_home(monkeypatch, tmp_path)
monkeypatch.setenv("HEADROOM_CONTEXT_TOOL", "lean-ctx")
runner = CliRunner()
with runner.isolated_filesystem(temp_dir=str(tmp_path)):
with patch("headroom.cli.wrap._ensure_rtk_binary") as ensure_rtk:
with patch(
"headroom.cli.wrap._setup_lean_ctx_agent",
return_value=Path("lean-ctx"),
) as setup:
result = runner.invoke(main, ["wrap", "cursor", "--prepare-only"])
assert result.exit_code == 0, result.output
ensure_rtk.assert_not_called()
setup.assert_called_once_with("cursor", verbose=False)
assert not Path(".cursorrules").exists()
def test_wrap_openclaw_prepare_only_emits_config_without_python_default() -> None:
runner = CliRunner()
result = runner.invoke(
main,
[
"wrap",
"openclaw",
"--prepare-only",
"--gateway-provider-id",
"codex",
"--gateway-provider-id",
"anthropic",
],
)
assert result.exit_code == 0, result.output
payload = json.loads(result.output)
assert payload["enabled"] is True
assert payload["config"]["proxyPort"] == 8787
assert payload["config"]["gatewayProviderIds"] == ["codex", "anthropic"]
assert "pythonPath" not in payload["config"]
def test_unwrap_openclaw_prepare_only_preserves_unmanaged_config() -> None:
runner = CliRunner()
existing_entry = json.dumps(
{
"enabled": True,
"config": {
"pythonPath": "C:\\Python312\\python.exe",
"proxyPort": 8787,
"customFlag": True,
},
}
)
result = runner.invoke(
main,
[
"unwrap",
"openclaw",
"--prepare-only",
"--existing-entry-json",
existing_entry,
],
)
assert result.exit_code == 0, result.output
payload = json.loads(result.output)
assert payload == {"enabled": False, "config": {"customFlag": True}}