Skip to content

Commit 847c301

Browse files
committed
test: More tests for toggle functionality and fix id types
Branch: InitialCLI Signed-off-by: Gabe Goodhart <ghart@us.ibm.com>
1 parent dd2a9ea commit 847c301

6 files changed

Lines changed: 674 additions & 71 deletions

File tree

tests/commands/resources/test_a2a.py

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class TestA2aCommands:
3636

3737
def test_a2a_list_success(self, mock_console) -> None:
3838
"""Test a2a list command."""
39-
mock_agents = [{"id": 1, "name": "agent1", "url": "http://example.com", "description": "desc1", "enabled": True}]
39+
mock_agents = [{"id": "agent-1", "name": "agent1", "url": "http://example.com", "description": "desc1", "enabled": True}]
4040

4141
with patch("cforge.commands.resources.a2a.get_console", return_value=mock_console):
4242
with patch("cforge.commands.resources.a2a.make_authenticated_request", return_value=mock_agents):
@@ -68,9 +68,49 @@ def test_a2a_list_error(self, mock_console) -> None:
6868
with pytest.raises(typer.Exit):
6969
a2a_list(json_output=False)
7070

71+
def test_a2a_list_with_active_only_true(self, mock_console) -> None:
72+
"""Test a2a list with --active-only flag set to True."""
73+
mock_agents = [{"id": "agent-1", "name": "agent1", "enabled": True}]
74+
75+
with patch("cforge.commands.resources.a2a.get_console", return_value=mock_console):
76+
with patch("cforge.commands.resources.a2a.make_authenticated_request", return_value=mock_agents) as mock_req:
77+
with patch("cforge.commands.resources.a2a.print_table"):
78+
a2a_list(active_only=True, json_output=False)
79+
80+
# Verify that include_inactive=False was passed to API
81+
call_args = mock_req.call_args
82+
assert call_args[1]["params"]["include_inactive"] is False
83+
84+
def test_a2a_list_with_active_only_false(self, mock_console) -> None:
85+
"""Test a2a list with --active-only flag set to False (default)."""
86+
mock_agents = [{"id": "agent-1", "name": "agent1", "enabled": True}, {"id": "agent-2", "name": "agent2", "enabled": False}]
87+
88+
with patch("cforge.commands.resources.a2a.get_console", return_value=mock_console):
89+
with patch("cforge.commands.resources.a2a.make_authenticated_request", return_value=mock_agents) as mock_req:
90+
with patch("cforge.commands.resources.a2a.print_table"):
91+
a2a_list(active_only=False, json_output=False)
92+
93+
# Verify that include_inactive=True was passed to API
94+
call_args = mock_req.call_args
95+
assert call_args[1]["params"]["include_inactive"] is True
96+
97+
def test_a2a_list_default_shows_all(self, mock_console) -> None:
98+
"""Test a2a list default behavior shows all agents."""
99+
mock_agents = [{"id": "agent-1", "name": "agent1", "enabled": True}, {"id": "agent-2", "name": "agent2", "enabled": False}]
100+
101+
with patch("cforge.commands.resources.a2a.get_console", return_value=mock_console):
102+
with patch("cforge.commands.resources.a2a.make_authenticated_request", return_value=mock_agents) as mock_req:
103+
with patch("cforge.commands.resources.a2a.print_table"):
104+
# Call with explicit active_only=False (default value)
105+
a2a_list(active_only=False, json_output=False)
106+
107+
# Verify that include_inactive=True was passed to API (default behavior)
108+
call_args = mock_req.call_args
109+
assert call_args[1]["params"]["include_inactive"] is True
110+
71111
def test_a2a_get_success(self, mock_console) -> None:
72112
"""Test a2a get command."""
73-
mock_agent = {"id": 1, "name": "test"}
113+
mock_agent = {"id": "agent-1", "name": "test"}
74114

75115
with patch("cforge.commands.resources.a2a.get_console", return_value=mock_console):
76116
with patch("cforge.commands.resources.a2a.make_authenticated_request", return_value=mock_agent):
@@ -79,7 +119,7 @@ def test_a2a_get_success(self, mock_console) -> None:
79119

80120
def test_a2a_create_from_file(self, mock_console) -> None:
81121
"""Test a2a create from file."""
82-
mock_result = {"id": 1, "name": "new_agent"}
122+
mock_result = {"id": "agent-1", "name": "new_agent"}
83123

84124
with tempfile.TemporaryDirectory() as temp_dir:
85125
data_file = Path(temp_dir) / "agent.json"
@@ -98,7 +138,7 @@ def test_a2a_create_file_not_found(self, mock_console) -> None:
98138

99139
def test_a2a_create_interactive(self, mock_console) -> None:
100140
"""Test a2a create interactive mode."""
101-
mock_result = {"id": 1, "name": "new_agent"}
141+
mock_result = {"id": "agent-1", "name": "new_agent"}
102142

103143
with patch("cforge.commands.resources.a2a.get_console", return_value=mock_console):
104144
with patch("cforge.commands.resources.a2a.prompt_for_schema", return_value={"name": "test", "url": "http://example.com"}):
@@ -108,7 +148,7 @@ def test_a2a_create_interactive(self, mock_console) -> None:
108148

109149
def test_a2a_create_with_options(self, mock_console) -> None:
110150
"""Test a2a create with command-line options."""
111-
mock_result = {"id": 1, "name": "new_agent"}
151+
mock_result = {"id": "agent-1", "name": "new_agent"}
112152

113153
with patch("cforge.commands.resources.a2a.get_console", return_value=mock_console):
114154
with patch("cforge.commands.resources.a2a.prompt_for_schema", return_value={"name": "test", "url": "http://example.com", "description": "desc"}) as mock_prompt:
@@ -124,7 +164,7 @@ def test_a2a_create_with_options(self, mock_console) -> None:
124164

125165
def test_a2a_update_success(self, mock_console) -> None:
126166
"""Test a2a update command."""
127-
mock_result = {"id": 1, "name": "updated"}
167+
mock_result = {"id": "agent-1", "name": "updated"}
128168

129169
with tempfile.TemporaryDirectory() as temp_dir:
130170
data_file = Path(temp_dir) / "update.json"
@@ -193,6 +233,67 @@ def test_a2a_get_error(self, mock_console) -> None:
193233
with pytest.raises(typer.Exit):
194234
a2a_get(agent_id="1")
195235

236+
def test_a2a_toggle_from_disabled_to_enabled(self, mock_console) -> None:
237+
"""Test toggling an A2A agent from disabled to enabled."""
238+
with patch("cforge.commands.resources.a2a.get_console", return_value=mock_console):
239+
# Create side_effect list with two responses
240+
side_effects = [{"id": "1", "name": "test", "enabled": False}, {"id": "1", "name": "test", "enabled": True}] # GET current status # POST toggle result
241+
with patch("cforge.commands.resources.a2a.make_authenticated_request", side_effect=side_effects) as mock_req:
242+
with patch("cforge.commands.resources.a2a.print_json"):
243+
a2a_toggle(agent_id="1")
244+
245+
# Verify two calls were made
246+
assert mock_req.call_count == 2
247+
248+
# Verify first call was GET to check current status
249+
get_call = mock_req.call_args_list[0]
250+
assert get_call[0][0] == "GET"
251+
assert get_call[0][1] == "/a2a/1"
252+
253+
# Verify second call was POST with activate=True
254+
post_call = mock_req.call_args_list[1]
255+
assert post_call[0][0] == "POST"
256+
assert post_call[0][1] == "/a2a/1/toggle"
257+
assert post_call[1]["params"]["activate"] is True
258+
259+
def test_a2a_toggle_from_enabled_to_disabled(self, mock_console) -> None:
260+
"""Test toggling an A2A agent from enabled to disabled."""
261+
with patch("cforge.commands.resources.a2a.get_console", return_value=mock_console):
262+
# Create side_effect list with two responses
263+
side_effects = [{"id": "1", "name": "test", "enabled": True}, {"id": "1", "name": "test", "enabled": False}] # GET current status # POST toggle result
264+
with patch("cforge.commands.resources.a2a.make_authenticated_request", side_effect=side_effects) as mock_req:
265+
with patch("cforge.commands.resources.a2a.print_json"):
266+
a2a_toggle(agent_id="1")
267+
268+
# Verify two calls were made
269+
assert mock_req.call_count == 2
270+
271+
# Verify first call was GET to check current status
272+
get_call = mock_req.call_args_list[0]
273+
assert get_call[0][0] == "GET"
274+
assert get_call[0][1] == "/a2a/1"
275+
276+
# Verify second call was POST with activate=False
277+
post_call = mock_req.call_args_list[1]
278+
assert post_call[0][0] == "POST"
279+
assert post_call[0][1] == "/a2a/1/toggle"
280+
assert post_call[1]["params"]["activate"] is False
281+
282+
def test_a2a_toggle_detects_current_status(self, mock_console) -> None:
283+
"""Test that toggle command detects current status before toggling."""
284+
with patch("cforge.commands.resources.a2a.get_console", return_value=mock_console):
285+
with patch("cforge.commands.resources.a2a.make_authenticated_request") as mock_req:
286+
# Mock an agent that is currently enabled
287+
mock_req.side_effect = [{"id": "1", "name": "test", "enabled": True}, {"id": "1", "name": "test", "enabled": False}]
288+
with patch("cforge.commands.resources.a2a.print_json"):
289+
a2a_toggle(agent_id="1")
290+
291+
# Verify GET was called first to detect current status
292+
calls = mock_req.call_args_list
293+
assert len(calls) == 2
294+
assert calls[0][0][0] == "GET" # First call is GET
295+
assert calls[1][0][0] == "POST" # Second call is POST
296+
196297
def test_a2a_toggle_error(self, mock_console) -> None:
197298
"""Test a2a toggle error handling."""
198299
with patch("cforge.commands.resources.a2a.get_console", return_value=mock_console):

tests/commands/resources/test_mcp_servers.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,40 @@ def test_mcp_servers_list_error(self, mock_console) -> None:
6161
with pytest.raises(typer.Exit):
6262
mcp_servers_list(json_output=False)
6363

64+
def test_mcp_servers_list_with_active_only_true(self, mock_console) -> None:
65+
"""Test mcp-servers list with --active-only flag set to True."""
66+
mock_servers = [{"id": "test-server-1234", "name": "server1", "enabled": True}]
67+
68+
with patch_functions("cforge.commands.resources.mcp_servers", make_authenticated_request=mock_servers, print_table=None) as mocks:
69+
mcp_servers_list(active_only=True, json_output=False)
70+
71+
# Verify that include_inactive=False was passed to API
72+
call_args = mocks.make_authenticated_request.call_args
73+
assert call_args[1]["params"]["include_inactive"] is False
74+
75+
def test_mcp_servers_list_with_active_only_false(self, mock_console) -> None:
76+
"""Test mcp-servers list with --active-only flag set to False (default)."""
77+
mock_servers = [{"id": "test-server-1234", "name": "server1", "enabled": True}, {"id": "test-server-5678", "name": "server2", "enabled": False}]
78+
79+
with patch_functions("cforge.commands.resources.mcp_servers", make_authenticated_request=mock_servers, print_table=None) as mocks:
80+
mcp_servers_list(active_only=False, json_output=False)
81+
82+
# Verify that include_inactive=True was passed to API
83+
call_args = mocks.make_authenticated_request.call_args
84+
assert call_args[1]["params"]["include_inactive"] is True
85+
86+
def test_mcp_servers_list_default_shows_all(self, mock_console) -> None:
87+
"""Test mcp-servers list default behavior shows all servers."""
88+
mock_servers = [{"id": "test-server-1234", "name": "server1", "enabled": True}, {"id": "test-server-5678", "name": "server2", "enabled": False}]
89+
90+
with patch_functions("cforge.commands.resources.mcp_servers", make_authenticated_request=mock_servers, print_table=None, get_console=mock_console) as mocks:
91+
# Call with explicit active_only=False (default value)
92+
mcp_servers_list(active_only=False, json_output=False)
93+
94+
# Verify that include_inactive=True was passed to API (default behavior)
95+
call_args = mocks.make_authenticated_request.call_args
96+
assert call_args[1]["params"]["include_inactive"] is True
97+
6498
def test_mcp_servers_get_success(self, mock_console) -> None:
6599
"""Test mcp-servers get command."""
66100
mock_server = {"id": "test-server-1234", "name": "test"}
@@ -172,6 +206,83 @@ def test_mcp_servers_get_error(self, mock_console) -> None:
172206
with pytest.raises(typer.Exit):
173207
mcp_servers_get(mcp_server_id="test-server-123")
174208

209+
def test_mcp_servers_toggle_from_disabled_to_enabled(self, mock_console) -> None:
210+
"""Test toggling an MCP server from disabled to enabled."""
211+
with patch_functions(
212+
"cforge.commands.resources.mcp_servers",
213+
get_console=mock_console,
214+
make_authenticated_request={
215+
"side_effect": [
216+
{"id": "test-server-123", "name": "test", "enabled": False}, # GET current status
217+
{"gateway": {"id": "test-server-123", "name": "test", "enabled": True}}, # POST toggle result
218+
]
219+
},
220+
print_json=None,
221+
) as mocks:
222+
223+
mcp_servers_toggle(mcp_server_id="test-server-123")
224+
225+
# Verify two calls were made
226+
assert mocks.make_authenticated_request.call_count == 2
227+
228+
# Verify first call was GET to check current status
229+
get_call = mocks.make_authenticated_request.call_args_list[0]
230+
assert get_call[0][0] == "GET"
231+
assert get_call[0][1] == "/gateways/test-server-123"
232+
233+
# Verify second call was POST with activate=True
234+
post_call = mocks.make_authenticated_request.call_args_list[1]
235+
assert post_call[0][0] == "POST"
236+
assert post_call[0][1] == "/gateways/test-server-123/toggle"
237+
assert post_call[1]["params"]["activate"] is True
238+
239+
def test_mcp_servers_toggle_from_enabled_to_disabled(self, mock_console) -> None:
240+
"""Test toggling an MCP server from enabled to disabled."""
241+
with patch_functions(
242+
"cforge.commands.resources.mcp_servers",
243+
get_console=mock_console,
244+
make_authenticated_request={
245+
"side_effect": [
246+
{"id": "test-server-123", "name": "test", "enabled": True}, # GET current status
247+
{"gateway": {"id": "test-server-123", "name": "test", "enabled": False}}, # POST toggle result
248+
]
249+
},
250+
print_json=None,
251+
) as mocks:
252+
253+
mcp_servers_toggle(mcp_server_id="test-server-123")
254+
255+
# Verify two calls were made
256+
assert mocks.make_authenticated_request.call_count == 2
257+
258+
# Verify first call was GET to check current status
259+
get_call = mocks.make_authenticated_request.call_args_list[0]
260+
assert get_call[0][0] == "GET"
261+
assert get_call[0][1] == "/gateways/test-server-123"
262+
263+
# Verify second call was POST with activate=False
264+
post_call = mocks.make_authenticated_request.call_args_list[1]
265+
assert post_call[0][0] == "POST"
266+
assert post_call[0][1] == "/gateways/test-server-123/toggle"
267+
assert post_call[1]["params"]["activate"] is False
268+
269+
def test_mcp_servers_toggle_detects_current_status(self, mock_console) -> None:
270+
"""Test that toggle command detects current status before toggling."""
271+
with patch_functions(
272+
"cforge.commands.resources.mcp_servers",
273+
get_console=mock_console,
274+
make_authenticated_request={"side_effect": [{"id": "test-server-123", "name": "test", "enabled": True}, {"gateway": {"id": "test-server-123", "name": "test", "enabled": False}}]},
275+
print_json=None,
276+
) as mocks:
277+
278+
mcp_servers_toggle(mcp_server_id="test-server-123")
279+
280+
# Verify GET was called first to detect current status
281+
calls = mocks.make_authenticated_request.call_args_list
282+
assert len(calls) == 2
283+
assert calls[0][0][0] == "GET" # First call is GET
284+
assert calls[1][0][0] == "POST" # Second call is POST
285+
175286
def test_mcp_servers_toggle_error(self, mock_console) -> None:
176287
"""Test mcp-servers toggle error handling."""
177288
with patch_functions("cforge.commands.resources.mcp_servers", make_authenticated_request={"side_effect": Exception("API error")}):

0 commit comments

Comments
 (0)