|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from unittest.mock import MagicMock |
| 16 | +from unittest.mock import patch |
| 17 | + |
| 18 | +from google.adk.agents.remote_a2a_agent import RemoteA2aAgent |
| 19 | +from google.adk.integrations.agent_registry import AgentRegistry |
| 20 | +from google.adk.tools.mcp_tool.mcp_toolset import McpToolset |
| 21 | +import httpx |
| 22 | +import pytest |
| 23 | + |
| 24 | + |
| 25 | +class TestAgentRegistry: |
| 26 | + |
| 27 | + @pytest.fixture |
| 28 | + def registry(self): |
| 29 | + with patch("google.auth.default", return_value=(MagicMock(), "project-id")): |
| 30 | + return AgentRegistry(project_id="test-project", location="global") |
| 31 | + |
| 32 | + def test_init_raises_value_error_if_params_missing(self): |
| 33 | + with pytest.raises( |
| 34 | + ValueError, match="project_id and location must be provided" |
| 35 | + ): |
| 36 | + AgentRegistry(project_id=None, location=None) |
| 37 | + |
| 38 | + def test_get_connection_uri_mcp_interfaces_top_level(self, registry): |
| 39 | + resource_details = { |
| 40 | + "interfaces": [ |
| 41 | + {"url": "https://mcp-v1main.com", "protocolBinding": "JSONRPC"} |
| 42 | + ] |
| 43 | + } |
| 44 | + uri = registry._get_connection_uri( |
| 45 | + resource_details, protocol_binding="JSONRPC" |
| 46 | + ) |
| 47 | + assert uri == "https://mcp-v1main.com" |
| 48 | + |
| 49 | + def test_get_connection_uri_agent_nested_protocols(self, registry): |
| 50 | + resource_details = { |
| 51 | + "protocols": [{ |
| 52 | + "type": "A2A_AGENT", |
| 53 | + "interfaces": [{ |
| 54 | + "url": "https://my-agent.com", |
| 55 | + "protocolBinding": "JSONRPC", |
| 56 | + }], |
| 57 | + }] |
| 58 | + } |
| 59 | + uri = registry._get_connection_uri( |
| 60 | + resource_details, protocol_type="A2A_AGENT" |
| 61 | + ) |
| 62 | + assert uri == "https://my-agent.com" |
| 63 | + |
| 64 | + def test_get_connection_uri_filtering(self, registry): |
| 65 | + resource_details = { |
| 66 | + "protocols": [ |
| 67 | + { |
| 68 | + "type": "CUSTOM", |
| 69 | + "interfaces": [{"url": "https://custom.com"}], |
| 70 | + }, |
| 71 | + { |
| 72 | + "type": "A2A_AGENT", |
| 73 | + "interfaces": [{ |
| 74 | + "url": "https://my-agent.com", |
| 75 | + "protocolBinding": "HTTP_JSON", |
| 76 | + }], |
| 77 | + }, |
| 78 | + ] |
| 79 | + } |
| 80 | + # Filter by type |
| 81 | + uri = registry._get_connection_uri( |
| 82 | + resource_details, protocol_type="A2A_AGENT" |
| 83 | + ) |
| 84 | + assert uri == "https://my-agent.com" |
| 85 | + |
| 86 | + # Filter by binding |
| 87 | + uri = registry._get_connection_uri( |
| 88 | + resource_details, protocol_binding="HTTP_JSON" |
| 89 | + ) |
| 90 | + assert uri == "https://my-agent.com" |
| 91 | + |
| 92 | + # No match |
| 93 | + uri = registry._get_connection_uri( |
| 94 | + resource_details, protocol_type="A2A_AGENT", protocol_binding="JSONRPC" |
| 95 | + ) |
| 96 | + assert uri is None |
| 97 | + |
| 98 | + def test_get_connection_uri_returns_none_if_no_interfaces(self, registry): |
| 99 | + resource_details = {} |
| 100 | + uri = registry._get_connection_uri(resource_details) |
| 101 | + assert uri is None |
| 102 | + |
| 103 | + def test_get_connection_uri_returns_none_if_no_url_in_interfaces( |
| 104 | + self, registry |
| 105 | + ): |
| 106 | + resource_details = {"interfaces": [{"protocolBinding": "HTTP"}]} |
| 107 | + uri = registry._get_connection_uri(resource_details) |
| 108 | + assert uri is None |
| 109 | + |
| 110 | + @patch("httpx.Client") |
| 111 | + def test_list_agents(self, mock_httpx, registry): |
| 112 | + mock_response = MagicMock() |
| 113 | + mock_response.json.return_value = {"agents": []} |
| 114 | + mock_response.raise_for_status = MagicMock() |
| 115 | + mock_httpx.return_value.__enter__.return_value.get.return_value = ( |
| 116 | + mock_response |
| 117 | + ) |
| 118 | + |
| 119 | + # Mock auth refresh |
| 120 | + registry._credentials.token = "token" |
| 121 | + registry._credentials.refresh = MagicMock() |
| 122 | + |
| 123 | + agents = registry.list_agents() |
| 124 | + assert agents == {"agents": []} |
| 125 | + |
| 126 | + @patch("httpx.Client") |
| 127 | + def test_get_mcp_server(self, mock_httpx, registry): |
| 128 | + mock_response = MagicMock() |
| 129 | + mock_response.json.return_value = {"name": "test-mcp"} |
| 130 | + mock_response.raise_for_status = MagicMock() |
| 131 | + mock_httpx.return_value.__enter__.return_value.get.return_value = ( |
| 132 | + mock_response |
| 133 | + ) |
| 134 | + |
| 135 | + registry._credentials.token = "token" |
| 136 | + registry._credentials.refresh = MagicMock() |
| 137 | + |
| 138 | + server = registry.get_mcp_server("test-mcp") |
| 139 | + assert server == {"name": "test-mcp"} |
| 140 | + |
| 141 | + @patch("httpx.Client") |
| 142 | + def test_get_mcp_toolset(self, mock_httpx, registry): |
| 143 | + mock_response = MagicMock() |
| 144 | + mock_response.json.return_value = { |
| 145 | + "displayName": "TestPrefix", |
| 146 | + "interfaces": [ |
| 147 | + {"url": "https://mcp.com", "protocolBinding": "JSONRPC"} |
| 148 | + ], |
| 149 | + } |
| 150 | + mock_response.raise_for_status = MagicMock() |
| 151 | + mock_httpx.return_value.__enter__.return_value.get.return_value = ( |
| 152 | + mock_response |
| 153 | + ) |
| 154 | + |
| 155 | + registry._credentials.token = "token" |
| 156 | + registry._credentials.refresh = MagicMock() |
| 157 | + |
| 158 | + toolset = registry.get_mcp_toolset("test-mcp") |
| 159 | + assert isinstance(toolset, McpToolset) |
| 160 | + assert toolset.tool_name_prefix == "TestPrefix" |
| 161 | + |
| 162 | + @patch("httpx.Client") |
| 163 | + def test_get_remote_a2a_agent(self, mock_httpx, registry): |
| 164 | + mock_response = MagicMock() |
| 165 | + mock_response.json.return_value = { |
| 166 | + "displayName": "TestAgent", |
| 167 | + "description": "Test Desc", |
| 168 | + "agentSpec": { |
| 169 | + "a2aAgentCardUrl": "https://my-agent.com/agent-card.json" |
| 170 | + }, |
| 171 | + } |
| 172 | + mock_response.raise_for_status = MagicMock() |
| 173 | + mock_httpx.return_value.__enter__.return_value.get.return_value = ( |
| 174 | + mock_response |
| 175 | + ) |
| 176 | + |
| 177 | + registry._credentials.token = "token" |
| 178 | + registry._credentials.refresh = MagicMock() |
| 179 | + |
| 180 | + agent = registry.get_remote_a2a_agent("test-agent") |
| 181 | + assert isinstance(agent, RemoteA2aAgent) |
| 182 | + assert agent.name == "TestAgent" |
| 183 | + assert agent.description == "Test Desc" |
| 184 | + assert agent._agent_card_source == "https://my-agent.com/agent-card.json" |
| 185 | + |
| 186 | + def test_get_auth_headers(self, registry): |
| 187 | + registry._credentials.token = "fake-token" |
| 188 | + registry._credentials.refresh = MagicMock() |
| 189 | + registry._credentials.quota_project_id = "quota-project" |
| 190 | + |
| 191 | + headers = registry._get_auth_headers() |
| 192 | + assert headers["Authorization"] == "Bearer fake-token" |
| 193 | + assert headers["x-goog-user-project"] == "quota-project" |
| 194 | + |
| 195 | + @patch("httpx.Client") |
| 196 | + def test_make_request_raises_http_status_error(self, mock_httpx, registry): |
| 197 | + mock_response = MagicMock() |
| 198 | + mock_response.status_code = 404 |
| 199 | + mock_response.text = "Not Found" |
| 200 | + error = httpx.HTTPStatusError( |
| 201 | + "Error", request=MagicMock(), response=mock_response |
| 202 | + ) |
| 203 | + mock_httpx.return_value.__enter__.return_value.get.side_effect = error |
| 204 | + |
| 205 | + registry._credentials.token = "token" |
| 206 | + registry._credentials.refresh = MagicMock() |
| 207 | + |
| 208 | + with pytest.raises( |
| 209 | + RuntimeError, match="API request failed with status 404" |
| 210 | + ): |
| 211 | + registry._make_request("test-path") |
| 212 | + |
| 213 | + @patch("httpx.Client") |
| 214 | + def test_make_request_raises_request_error(self, mock_httpx, registry): |
| 215 | + error = httpx.RequestError("Connection failed", request=MagicMock()) |
| 216 | + mock_httpx.return_value.__enter__.return_value.get.side_effect = error |
| 217 | + |
| 218 | + registry._credentials.token = "token" |
| 219 | + registry._credentials.refresh = MagicMock() |
| 220 | + |
| 221 | + with pytest.raises( |
| 222 | + RuntimeError, match="API request failed \(network error\)" |
| 223 | + ): |
| 224 | + registry._make_request("test-path") |
| 225 | + |
| 226 | + @patch("httpx.Client") |
| 227 | + def test_make_request_raises_generic_exception(self, mock_httpx, registry): |
| 228 | + mock_httpx.return_value.__enter__.return_value.get.side_effect = Exception( |
| 229 | + "Generic error" |
| 230 | + ) |
| 231 | + |
| 232 | + registry._credentials.token = "token" |
| 233 | + registry._credentials.refresh = MagicMock() |
| 234 | + |
| 235 | + with pytest.raises(RuntimeError, match="API request failed: Generic error"): |
| 236 | + registry._make_request("test-path") |
0 commit comments