headroom/tests/test_cli/test_wrap_vscode.py
JD Davis 1aa701adaa
fix(vscode): persist compatible Claude modes and route Copilot CAPI (#2986)
## Description

Fixes #2492, #2028, and #2827.

Claude daemon workers consume project settings rather than reliably
inheriting wrapper environment state, while the Claude VS Code webview
cannot render deferred-tool response blocks. Separately, recent Copilot
Chat versions use the whole CAPI override for generation; the legacy
proxy override alone only sends model discovery through Headroom.

This PR carries both integrations through to the actual consumers
instead of only changing their launch-time surface configuration.

## Type of Change

- [x] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update
- [ ] Build / CI

## Changes Made

- Persist the resolved Claude ENABLE_TOOL_SEARCH value into project
settings for daemon workers and restore it transactionally after wrap
exits.
- Use compatibility-safe Foundry and Claude VS Code defaults while
preserving explicit user choices.
- Configure both Copilot overrideProxyUrl and overrideCapiUrl in the
reversible managed VS Code settings block.
- Route Copilot unprefixed POST /chat/completions and HTTP /responses
requests through the real compression handlers.
- Keep /responses out of the Codex WebSocket aliases because Copilot and
Codex use different WebSocket wire protocols.
- Extend wrap E2E assertions for both the Claude webview mode and
Copilot CAPI routing.

## Testing

- [x] 127 combined Claude, Copilot, route-integration, and MCP
dependency-contract tests pass.
- [x] Ruff check passes on all changed Python files.
- [x] Ruff format check passes.
- [x] Python compilation and git diff --check pass.

## Runtime Safety

Standalone Claude CLI defaults remain unchanged. Explicit Claude
tool-search values retain precedence, and project settings are restored
through the existing cleanup path. Copilot model/session helper
endpoints continue through generic passthrough, while only validated
HTTP generation paths receive explicit compression routes. Existing
Codex WebSocket behavior is unchanged.

## Review Readiness

- [x] Current main and MCP v1 compatibility retained
- [x] Worker-facing Claude persistence covered
- [x] Reversible Copilot and Claude settings behavior covered
- [x] Copilot generation routes covered at registration and proxy
integration layers
- [x] Ready for review
2026-08-13 15:06:41 -05:00

82 lines
2.9 KiB
Python

"""CLI coverage for transparent VS Code Copilot setup and undo."""
from __future__ import annotations
from pathlib import Path
from unittest.mock import patch
from click.testing import CliRunner
from headroom.cli.main import main
from headroom.copilot_auth import CopilotSubscriptionTokenResolution
def _resolution() -> CopilotSubscriptionTokenResolution:
return CopilotSubscriptionTokenResolution(
token="copilot-token",
source="test",
confidence="test",
api_url="https://api.githubcopilot.com",
token_fingerprint="sha256:test",
)
def test_wrap_vscode_configures_actual_port_and_seeds_subscription(tmp_path: Path) -> None:
path = tmp_path / "settings.json"
captured = {}
def fake_watcher(**kwargs): # noqa: ANN003, ANN202
captured.update(kwargs)
kwargs["print_setup_lines"](9999)
with (
patch(
"headroom.cli.wrap._require_copilot_subscription_resolution", return_value=_resolution()
),
patch("headroom.cli.wrap._run_proxy_only_watcher", side_effect=fake_watcher),
):
result = CliRunner().invoke(main, ["wrap", "vscode", "--settings-file", str(path)])
assert result.exit_code == 0, result.output
settings = path.read_text(encoding="utf-8")
assert "http://127.0.0.1:9999/" in settings
assert "model" not in settings.lower()
assert "normal model picker" in result.output
assert captured["openai_api_url"] == "https://api.githubcopilot.com"
assert captured["copilot_api_token"] == "copilot-token"
def test_wrap_vscode_no_configure_prints_transparent_settings(tmp_path: Path) -> None:
path = tmp_path / "settings.json"
def fake_watcher(**kwargs): # noqa: ANN003, ANN202
kwargs["print_setup_lines"](8787)
with (
patch(
"headroom.cli.wrap._require_copilot_subscription_resolution", return_value=_resolution()
),
patch("headroom.cli.wrap._run_proxy_only_watcher", side_effect=fake_watcher),
):
result = CliRunner().invoke(
main,
["wrap", "vscode", "--no-configure", "--settings-file", str(path)],
)
assert result.exit_code == 0, result.output
assert not path.exists()
assert "overrideProxyUrl" in result.output
assert "overrideCapiUrl" in result.output
assert "overrideAuthType" in result.output
def test_unwrap_vscode_removes_only_managed_settings(tmp_path: Path) -> None:
path = tmp_path / "settings.json"
original = '{\n "editor.fontSize": 14\n}\n'
path.write_text(original, encoding="utf-8")
from headroom.providers.copilot.vscode import configure_vscode_proxy_settings
configure_vscode_proxy_settings(path, "http://127.0.0.1:8787")
result = CliRunner().invoke(main, ["unwrap", "vscode", "--settings-file", str(path)])
assert result.exit_code == 0, result.output
assert path.read_text(encoding="utf-8") == original