@@ -41,10 +41,16 @@ def test_add_server(windsurf_manager, monkeypatch, tmp_path):
4141 )
4242
4343 # Mock prompt_toolkit's prompt to return our test values
44- with patch ("prompt_toolkit.PromptSession.prompt" , side_effect = ["json" , "test-api-key" ]):
44+ # Note: With --force, the CLI will look for env vars for required args instead of prompting
45+ monkeypatch .setenv ("fmt" , "json" )
46+ monkeypatch .setenv ("API_KEY" , "test-api-key" )
47+
48+ # We still patch PromptSession to ensure it's NOT called (or ignored if called incorrectly)
49+ with patch ("prompt_toolkit.PromptSession.prompt" ) as mock_prompt :
4550 runner = CliRunner ()
4651 result = runner .invoke (install , ["server-test" , "--force" , "--alias" , "test" ])
4752 assert result .exit_code == 0
53+ mock_prompt .assert_not_called ()
4854
4955 # Check that the server was added to global configuration with alias
5056 server = global_config_manager .get_server ("test" )
@@ -95,8 +101,13 @@ def test_add_server_with_missing_arg(windsurf_manager, monkeypatch, tmp_path):
95101
96102 # Instead of mocking Console and Progress, we'll mock key methods directly
97103 # This is a simpler approach that avoids complex mock setup
104+
105+ # Set environment variables for required arguments
106+ monkeypatch .setenv ("fmt" , "json" )
107+ monkeypatch .setenv ("API_KEY" , "test-api-key" )
108+
98109 with (
99- patch ("prompt_toolkit.PromptSession.prompt" , side_effect = [ "json" , "test-api-key" ]) ,
110+ patch ("prompt_toolkit.PromptSession.prompt" ) as mock_prompt ,
100111 patch ("rich.progress.Progress.start" ),
101112 patch ("rich.progress.Progress.stop" ),
102113 patch ("rich.progress.Progress.add_task" ),
@@ -111,6 +122,7 @@ def test_add_server_with_missing_arg(windsurf_manager, monkeypatch, tmp_path):
111122 print (f"Output: { result .stdout } " )
112123
113124 assert result .exit_code == 0
125+ mock_prompt .assert_not_called ()
114126
115127 # Check that the server was added with alias and the missing argument is replaced with empty string
116128 server = global_config_manager .get_server ("test-missing-arg" )
@@ -166,8 +178,12 @@ def test_add_server_with_empty_args(windsurf_manager, monkeypatch, tmp_path):
166178 )
167179
168180 # Mock prompt responses for required arguments only
181+ monkeypatch .setenv ("fmt" , "json" )
182+ monkeypatch .setenv ("API_KEY" , "test-api-key" )
183+ # OPTIONAL is not set, simulating empty/missing optional arg
184+
169185 with (
170- patch ("prompt_toolkit.PromptSession.prompt" , side_effect = [ "json" , "test-api-key" ]) ,
186+ patch ("prompt_toolkit.PromptSession.prompt" ) as mock_prompt ,
171187 patch ("rich.progress.Progress.start" ),
172188 patch ("rich.progress.Progress.stop" ),
173189 patch ("rich.progress.Progress.add_task" ),
@@ -176,6 +192,7 @@ def test_add_server_with_empty_args(windsurf_manager, monkeypatch, tmp_path):
176192 result = runner .invoke (install , ["server-test" , "--force" , "--alias" , "test-empty-args" ])
177193
178194 assert result .exit_code == 0
195+ mock_prompt .assert_not_called ()
179196
180197 # Check that the server was added and optional arguments are empty
181198 server = global_config_manager .get_server ("test-empty-args" )
@@ -269,14 +286,18 @@ def test_add_server_with_configured_npx(windsurf_manager, monkeypatch, tmp_path)
269286 )
270287
271288 # Mock Rich's progress display to prevent 'Only one live display may be active at once' error
289+ monkeypatch .setenv ("fmt" , "json" )
290+ monkeypatch .setenv ("API_KEY" , "test-api-key" )
291+
272292 with (
273293 patch ("rich.progress.Progress.__enter__" , return_value = Mock ()),
274294 patch ("rich.progress.Progress.__exit__" ),
275- patch ("prompt_toolkit.PromptSession.prompt" , side_effect = [ "json" , "test-api-key" ]) ,
295+ patch ("prompt_toolkit.PromptSession.prompt" ) as mock_prompt ,
276296 ):
277297 runner = CliRunner ()
278298 result = runner .invoke (install , ["server-test" , "--force" , "--alias" , "test" ])
279299 assert result .exit_code == 0
300+ mock_prompt .assert_not_called ()
280301
281302 # Check that the server was added with alias
282303 server = global_config_manager .get_server ("test" )
@@ -384,27 +405,19 @@ def test_add_server_with_filtered_arguments(windsurf_manager, monkeypatch, tmp_p
384405
385406 # Mock prompt_toolkit's prompt to return our test values
386407 # Should only be called once for API_KEY since that's the only referenced variable
387- prompt_calls = []
388-
389- def mock_prompt_func (* args , ** kwargs ):
390- prompt_calls .append (kwargs .get ("message" , "" ))
391- return "test-api-key"
392-
408+ # With force=True, we use env vars
409+ monkeypatch .setenv ("API_KEY" , "test-api-key" )
410+
393411 with (
394- patch ("prompt_toolkit.PromptSession.prompt" , side_effect = mock_prompt_func ) ,
412+ patch ("prompt_toolkit.PromptSession.prompt" ) as mock_prompt ,
395413 patch ("rich.progress.Progress.start" ),
396414 patch ("rich.progress.Progress.stop" ),
397415 patch ("rich.progress.Progress.add_task" ),
398416 ):
399417 runner = CliRunner ()
400418 result = runner .invoke (install , ["test-server" , "--force" ])
401419 assert result .exit_code == 0
402-
403- # Check that only API_KEY was prompted for
404- assert len (prompt_calls ) == 1
405- assert "API_KEY" in str (prompt_calls [0 ])
406- assert "DATABASE_URL" not in str (prompt_calls [0 ])
407- assert "UNUSED_VAR" not in str (prompt_calls [0 ])
420+ mock_prompt .assert_not_called ()
408421
409422 # Check that the server was added correctly
410423 server = global_config_manager .get_server ("test-server" )
@@ -449,20 +462,23 @@ def test_add_http_server_with_headers(windsurf_manager, monkeypatch, tmp_path):
449462 )
450463
451464 # Mock prompt_toolkit's prompt to return our test values
465+ monkeypatch .setenv ("API_TOKEN" , "test-token-123" )
466+
452467 with (
453- patch ("prompt_toolkit.PromptSession.prompt" , side_effect = [ "test-token-123" ]) ,
468+ patch ("prompt_toolkit.PromptSession.prompt" ) as mock_prompt ,
454469 patch ("rich.progress.Progress.start" ),
455470 patch ("rich.progress.Progress.stop" ),
456471 patch ("rich.progress.Progress.add_task" ),
457472 ):
458473 runner = CliRunner ()
459474 result = runner .invoke (install , ["api-server" , "--force" ])
460475 assert result .exit_code == 0
476+ mock_prompt .assert_not_called ()
461477
462478 # Check that the server was added to global configuration as RemoteServerConfig
463479 server = global_config_manager .get_server ("api-server" )
464480 assert server is not None
465481 assert isinstance (server , RemoteServerConfig )
466482 assert server .url == "https://api.example.com/mcp"
467483 # Check headers were properly set with variable replacement
468- assert server .headers == {"Authorization" : "Bearer test-token-123" , "X-API-Version" : "1.0" }
484+ assert server .headers == {"Authorization" : "Bearer test-token-123" , "X-API-Version" : "1.0" }
0 commit comments