|
| 1 | +""" |
| 2 | +Test for OpenCode manager |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +import os |
| 7 | +import tempfile |
| 8 | +from pathlib import Path |
| 9 | +from unittest.mock import patch |
| 10 | + |
| 11 | +from mcpm.clients.managers.opencode import OpenCodeManager |
| 12 | + |
| 13 | + |
| 14 | +def test_opencode_manager_initialization(): |
| 15 | + """Test OpenCodeManager initialization with default and override paths.""" |
| 16 | + manager = OpenCodeManager() |
| 17 | + assert manager.client_key == "opencode" |
| 18 | + assert manager.display_name == "OpenCode" |
| 19 | + assert manager.download_url == "https://github.com/sst/opencode" |
| 20 | + assert manager.config_path == str(Path.home() / ".config" / "opencode" / "opencode.json") |
| 21 | + |
| 22 | + custom_path = "/tmp/custom_opencode.json" |
| 23 | + manager = OpenCodeManager(config_path_override=custom_path) |
| 24 | + assert manager.config_path == custom_path |
| 25 | + |
| 26 | + |
| 27 | +def test_opencode_manager_uses_mcp_key_not_mcpservers(): |
| 28 | + """Test that OpenCode uses the `mcp` key (not `mcpServers`). |
| 29 | +
|
| 30 | + This is the OpenCode-specific configuration shape — the JSON file |
| 31 | + has a top-level `mcp` object instead of the `mcpServers` standard |
| 32 | + used by Claude Code, Cursor, Cline, etc. |
| 33 | + """ |
| 34 | + manager = OpenCodeManager() |
| 35 | + assert manager.configure_key_name == "mcp" |
| 36 | + |
| 37 | + |
| 38 | +def test_opencode_manager_get_empty_config(): |
| 39 | + """Test OpenCodeManager _get_empty_config method returns the right shape.""" |
| 40 | + manager = OpenCodeManager() |
| 41 | + config = manager._get_empty_config() |
| 42 | + assert "mcp" in config |
| 43 | + assert config["mcp"] == {} |
| 44 | + # Sanity: no mcpServers key (the standard one). |
| 45 | + assert "mcpServers" not in config |
| 46 | + |
| 47 | + |
| 48 | +def test_opencode_manager_is_client_installed_true(): |
| 49 | + """Test is_client_installed returns True when `opencode` binary is on PATH.""" |
| 50 | + manager = OpenCodeManager() |
| 51 | + with patch("shutil.which", return_value="/usr/local/bin/opencode") as mock_which: |
| 52 | + assert manager.is_client_installed() is True |
| 53 | + mock_which.assert_called_with("opencode") |
| 54 | + |
| 55 | + |
| 56 | +def test_opencode_manager_is_client_installed_false(): |
| 57 | + """Test is_client_installed returns False when `opencode` is not on PATH.""" |
| 58 | + manager = OpenCodeManager() |
| 59 | + with patch("shutil.which", return_value=None) as mock_which: |
| 60 | + assert manager.is_client_installed() is False |
| 61 | + mock_which.assert_called_with("opencode") |
| 62 | + |
| 63 | + |
| 64 | +def test_opencode_manager_is_client_installed_windows(): |
| 65 | + """Test that is_client_installed handles Windows PATHEXT via shutil.which.""" |
| 66 | + manager = OpenCodeManager() |
| 67 | + # shutil.which() handles Windows PATHEXT automatically, so the manager |
| 68 | + # always searches for "opencode" (no .exe / .cmd suffix). This keeps |
| 69 | + # the manager OS-agnostic and matches the convention used by |
| 70 | + # CodexCliManager, QwenCliManager, etc. |
| 71 | + with patch("shutil.which", return_value="C:\\Users\\user\\AppData\\Roaming\\npm\\opencode.cmd") as mock_which: |
| 72 | + assert manager.is_client_installed() is True |
| 73 | + mock_which.assert_called_with("opencode") |
| 74 | + |
| 75 | + |
| 76 | +def test_opencode_manager_get_client_info(): |
| 77 | + """Test OpenCodeManager get_client_info method returns expected metadata.""" |
| 78 | + manager = OpenCodeManager() |
| 79 | + info = manager.get_client_info() |
| 80 | + assert info["name"] == "OpenCode" |
| 81 | + assert info["download_url"] == "https://github.com/sst/opencode" |
| 82 | + assert info["config_file"] == str(Path.home() / ".config" / "opencode" / "opencode.json") |
| 83 | + assert "Open-source AI coding agent" in info["description"] |
| 84 | + |
| 85 | + |
| 86 | +def test_opencode_manager_loads_existing_mcp_section(): |
| 87 | + """Test that loading an existing config preserves the `mcp` section content.""" |
| 88 | + with tempfile.NamedTemporaryFile(delete=False, suffix=".json", mode="w") as f: |
| 89 | + json.dump( |
| 90 | + { |
| 91 | + "mcp": { |
| 92 | + "memory": { |
| 93 | + "command": "npx", |
| 94 | + "args": ["-y", "@modelcontextprotocol/server-memory"], |
| 95 | + } |
| 96 | + } |
| 97 | + }, |
| 98 | + f, |
| 99 | + ) |
| 100 | + temp_path = f.name |
| 101 | + |
| 102 | + try: |
| 103 | + manager = OpenCodeManager(config_path_override=temp_path) |
| 104 | + config = manager._load_config() |
| 105 | + assert "mcp" in config |
| 106 | + assert "memory" in config["mcp"] |
| 107 | + assert config["mcp"]["memory"]["command"] == "npx" |
| 108 | + finally: |
| 109 | + os.unlink(temp_path) |
| 110 | + |
| 111 | + |
| 112 | +def test_opencode_manager_creates_empty_mcp_section_for_missing_file(): |
| 113 | + """Test that loading a nonexistent file returns an empty `mcp` section.""" |
| 114 | + nonexistent_path = "/tmp/nonexistent-opencode-config-test.json" |
| 115 | + manager = OpenCodeManager(config_path_override=nonexistent_path) |
| 116 | + config = manager._load_config() |
| 117 | + assert "mcp" in config |
| 118 | + assert config["mcp"] == {} |
| 119 | + |
| 120 | + |
| 121 | +def test_opencode_manager_adds_server_under_mcp_key(): |
| 122 | + """Test that adding a server writes it under the `mcp` key (not `mcpServers`).""" |
| 123 | + from mcpm.core.schema import STDIOServerConfig |
| 124 | + |
| 125 | + with tempfile.NamedTemporaryFile(delete=False, suffix=".json", mode="w") as f: |
| 126 | + json.dump({"mcp": {}}, f) |
| 127 | + temp_path = f.name |
| 128 | + |
| 129 | + try: |
| 130 | + manager = OpenCodeManager(config_path_override=temp_path) |
| 131 | + server_config = STDIOServerConfig(name="test-server", command="echo", args=["hello"]) |
| 132 | + success = manager.add_server(server_config) |
| 133 | + assert success is True |
| 134 | + |
| 135 | + # Verify the server was written under `mcp`, not `mcpServers`. |
| 136 | + with open(temp_path) as f: |
| 137 | + saved = json.load(f) |
| 138 | + assert "mcp" in saved |
| 139 | + assert "test-server" in saved["mcp"] |
| 140 | + assert saved["mcp"]["test-server"]["command"] == "echo" |
| 141 | + assert "mcpServers" not in saved |
| 142 | + finally: |
| 143 | + os.unlink(temp_path) |
0 commit comments