-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathtest_add.py
More file actions
254 lines (227 loc) · 9.98 KB
/
Copy pathtest_add.py
File metadata and controls
254 lines (227 loc) · 9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
from unittest.mock import Mock, patch
from click.testing import CliRunner
from mcpm.commands.target_operations.add import add
from mcpm.core.schema import RemoteServerConfig
from mcpm.utils.config import ConfigManager
from mcpm.utils.repository import RepositoryManager
def test_add_server(windsurf_manager, monkeypatch):
"""Test add server"""
monkeypatch.setattr(
RepositoryManager,
"_fetch_servers",
Mock(
return_value={
"server-test": {
"installations": {
"npm": {
"type": "npm",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-test", "--fmt", "${fmt}"],
"env": {"API_KEY": "${API_KEY}"},
}
},
"arguments": {
"fmt": {"type": "string", "description": "Output format", "required": True},
"API_KEY": {"type": "string", "description": "API key", "required": True},
},
}
}
),
)
# Mock prompt_toolkit's prompt to return our test values
with patch("prompt_toolkit.PromptSession.prompt", side_effect=["json", "test-api-key"]):
runner = CliRunner()
result = runner.invoke(add, ["server-test", "--force", "--alias", "test"])
assert result.exit_code == 0
# Check that the server was added with alias
server = windsurf_manager.get_server("test")
assert server is not None
assert server.command == "npx"
assert server.args == ["-y", "@modelcontextprotocol/server-test", "--fmt", "json"]
assert server.env["API_KEY"] == "test-api-key"
def test_add_server_with_missing_arg(windsurf_manager, monkeypatch):
"""Test add server with a missing argument that should be replaced with empty string"""
monkeypatch.setattr(
RepositoryManager,
"_fetch_servers",
Mock(
return_value={
"server-test": {
"installations": {
"npm": {
"type": "npm",
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-test",
"--fmt",
"${fmt}",
"--timezone",
"${TZ}", # TZ is not in the arguments list
],
"env": {"API_KEY": "${API_KEY}"},
}
},
"arguments": {
"fmt": {"type": "string", "description": "Output format", "required": True},
"API_KEY": {"type": "string", "description": "API key", "required": True},
# Deliberately not including TZ to test empty string replacement
},
}
}
),
)
# Instead of mocking Console and Progress, we'll mock key methods directly
# This is a simpler approach that avoids complex mock setup
with (
patch("prompt_toolkit.PromptSession.prompt", side_effect=["json", "test-api-key"]),
patch("rich.progress.Progress.start"),
patch("rich.progress.Progress.stop"),
patch("rich.progress.Progress.add_task"),
):
# Use CliRunner which provides its own isolated environment
runner = CliRunner()
result = runner.invoke(add, ["server-test", "--force", "--alias", "test-missing-arg"])
if result.exit_code != 0:
print(f"Exit code: {result.exit_code}")
print(f"Exception: {result.exception}")
print(f"Output: {result.stdout}")
assert result.exit_code == 0
# Check that the server was added with alias and the missing argument is replaced with empty string
server = windsurf_manager.get_server("test-missing-arg")
assert server is not None
assert server.command == "npx"
# The ${TZ} argument should be replaced with empty string since it's not in processed variables
assert server.args == ["-y", "@modelcontextprotocol/server-test", "--fmt", "json", "--timezone", ""]
assert server.env["API_KEY"] == "test-api-key"
def test_add_server_with_empty_args(windsurf_manager, monkeypatch):
"""Test add server with missing arguments that should be replaced with empty strings"""
monkeypatch.setattr(
RepositoryManager,
"_fetch_servers",
Mock(
return_value={
"server-test": {
"installations": {
"npm": {
"type": "npm",
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-test",
"--fmt",
"${fmt}",
"--optional",
"${OPTIONAL}", # Optional arg not in arguments list
"--api-key",
"${API_KEY}",
],
"env": {
"API_KEY": "${API_KEY}",
"OPTIONAL_ENV": "${OPTIONAL}", # Optional env var
},
}
},
"arguments": {
"fmt": {"type": "string", "description": "Output format", "required": True},
"API_KEY": {"type": "string", "description": "API key", "required": True},
# OPTIONAL is not listed in arguments
},
}
}
),
)
# Mock prompt responses for required arguments only
with (
patch("prompt_toolkit.PromptSession.prompt", side_effect=["json", "test-api-key"]),
patch("rich.progress.Progress.start"),
patch("rich.progress.Progress.stop"),
patch("rich.progress.Progress.add_task"),
):
runner = CliRunner()
result = runner.invoke(add, ["server-test", "--force", "--alias", "test-empty-args"])
assert result.exit_code == 0
# Check that the server was added and optional arguments are empty
server = windsurf_manager.get_server("test-empty-args")
assert server is not None
assert server.command == "npx"
# Optional arguments should be replaced with empty strings
assert server.args == [
"-y",
"@modelcontextprotocol/server-test",
"--fmt",
"json",
"--optional",
"", # ${OPTIONAL} replaced with empty string
"--api-key",
"test-api-key",
]
assert server.env == {
"API_KEY": "test-api-key",
"OPTIONAL_ENV": "", # Optional env var should be empty string
}
def test_add_sse_server_to_claude_desktop(claude_desktop_manager, monkeypatch):
"""Test add sse server to claude desktop"""
server_config = RemoteServerConfig(
name="test-sse-server", url="http://localhost:8080", headers={"Authorization": "Bearer test-api-key"}
)
claude_desktop_manager.add_server(server_config)
stored_config = claude_desktop_manager.get_server("test-sse-server")
assert stored_config is not None
assert stored_config.name == "test-sse-server"
assert stored_config.command == "uvx"
assert stored_config.args == [
"mcp-proxy",
"http://localhost:8080",
"--headers",
"Authorization",
"Bearer test-api-key",
]
def test_add_profile_to_client(windsurf_manager, monkeypatch):
profile_name = "work"
monkeypatch.setattr(ConfigManager, "get_router_config", Mock(return_value={"host": "localhost", "port": 8080}))
# test cli
runner = CliRunner()
result = runner.invoke(add, ["%" + profile_name, "--force", "--alias", "work"])
assert result.exit_code == 0
assert "Successfully added profile work to windsurf!" in result.output
profile_server = windsurf_manager.get_server("work")
assert profile_server is not None
def test_add_server_with_configured_npx(windsurf_manager, monkeypatch):
monkeypatch.setattr(ConfigManager, "get_config", Mock(return_value={"node_executable": "bunx"}))
monkeypatch.setattr(
RepositoryManager,
"_fetch_servers",
Mock(
return_value={
"server-test": {
"installations": {
"npm": {
"type": "npm",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-test", "--fmt", "${fmt}"],
"env": {"API_KEY": "${API_KEY}"},
}
},
"arguments": {
"fmt": {"type": "string", "description": "Output format", "required": True},
"API_KEY": {"type": "string", "description": "API key", "required": True},
},
}
}
),
)
# Mock Rich's progress display to prevent 'Only one live display may be active at once' error
with patch("rich.progress.Progress.__enter__", return_value=Mock()), \
patch("rich.progress.Progress.__exit__"), \
patch("prompt_toolkit.PromptSession.prompt", side_effect=["json", "test-api-key"]):
runner = CliRunner()
result = runner.invoke(add, ["server-test", "--force", "--alias", "test"])
assert result.exit_code == 0
# Check that the server was added with alias
server = windsurf_manager.get_server("test")
assert server is not None
# Should use configured node executable
assert server.command == "bunx"
assert server.args == ["-y", "@modelcontextprotocol/server-test", "--fmt", "json"]
assert server.env["API_KEY"] == "test-api-key"