from __future__ import annotations import shutil from pathlib import Path from nexus_claude_api.cli import main from nexus_claude_api.config import Settings, load_local_config from nexus_claude_api.shell import generate_claude_code_powershell def test_claude_code_command() -> None: settings = Settings.from_values( host="127.0.0.1", port=4141, api_key="test", require_api_key=False, ) command = generate_claude_code_powershell(settings) assert "ANTHROPIC_BASE_URL" in command assert "ANTHROPIC_AUTH_TOKEN='dummy'" in command assert "CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY='1'" in command assert "claude-opus-4.6" in command assert command.endswith("claude --model claude-opus-4.6") def test_claude_code_command_uses_custom_model() -> None: settings = Settings.from_values( host="127.0.0.1", port=4141, api_key="test", model="claude-opus-4.6", require_api_key=False, ) command = generate_claude_code_powershell(settings) assert command.endswith("claude --model claude-opus-4.6") def test_missing_api_key_fails(monkeypatch) -> None: tmp_path = _workspace_tmp("missing-key") monkeypatch.delenv("NEXUS_API_KEY", raising=False) monkeypatch.delenv("AWS_BEARER_TOKEN_BEDROCK", raising=False) monkeypatch.chdir(tmp_path) try: exit_code = main(["start", "--dry-run"]) finally: monkeypatch.chdir(Path(__file__).parents[1]) shutil.rmtree(tmp_path, ignore_errors=True) assert exit_code == 2 def test_local_config_api_key(monkeypatch) -> None: tmp_path = _workspace_tmp("local-config") monkeypatch.chdir(tmp_path) (tmp_path / "nexus-claude-api.local.json").write_text( '{"api_key": "local-test-key"}', encoding="utf-8", ) try: settings = Settings.from_values(require_api_key=False) assert settings.api_key == "local-test-key" assert load_local_config()["api_key"] == "local-test-key" finally: monkeypatch.chdir(Path(__file__).parents[1]) shutil.rmtree(tmp_path, ignore_errors=True) def _workspace_tmp(name: str) -> Path: path = Path(__file__).parents[1] / ".test-tmp" / name shutil.rmtree(path, ignore_errors=True) path.mkdir(parents=True, exist_ok=True) return path