|
| 1 | +# Copyright 2025 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 __future__ import annotations |
| 16 | + |
| 17 | +from unittest import mock |
| 18 | + |
| 19 | +from google.adk.agents.invocation_context import InvocationContext |
| 20 | +from google.adk.agents.llm_agent import LlmAgent |
| 21 | +from google.adk.agents.run_config import RunConfig |
| 22 | +from google.adk.agents.sequential_agent import SequentialAgent |
| 23 | +from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService |
| 24 | +from google.adk.memory.in_memory_memory_service import InMemoryMemoryService |
| 25 | +from google.adk.plugins.plugin_manager import PluginManager |
| 26 | +from google.adk.sessions.in_memory_session_service import InMemorySessionService |
| 27 | +from google.adk.tools.enterprise_search_agent_tool import create_enterprise_search_agent |
| 28 | +from google.adk.tools.enterprise_search_agent_tool import EnterpriseSearchAgentTool |
| 29 | +from google.adk.tools.tool_context import ToolContext |
| 30 | +from pytest import mark |
| 31 | + |
| 32 | + |
| 33 | +async def _create_tool_context() -> ToolContext: |
| 34 | + session_service = InMemorySessionService() |
| 35 | + session = await session_service.create_session( |
| 36 | + app_name='test_app', user_id='test_user' |
| 37 | + ) |
| 38 | + agent = SequentialAgent(name='test_agent') |
| 39 | + invocation_context = InvocationContext( |
| 40 | + invocation_id='invocation_id', |
| 41 | + agent=agent, |
| 42 | + session=session, |
| 43 | + session_service=session_service, |
| 44 | + artifact_service=InMemoryArtifactService(), |
| 45 | + memory_service=InMemoryMemoryService(), |
| 46 | + plugin_manager=PluginManager(), |
| 47 | + run_config=RunConfig(), |
| 48 | + ) |
| 49 | + return ToolContext(invocation_context=invocation_context) |
| 50 | + |
| 51 | + |
| 52 | +class TestEnterpriseSearchAgentTool: |
| 53 | + """Test the EnterpriseSearchAgentTool class.""" |
| 54 | + |
| 55 | + def test_create_enterprise_search_agent(self): |
| 56 | + """Test that create_enterprise_search_agent creates a valid agent.""" |
| 57 | + agent = create_enterprise_search_agent('gemini-pro') |
| 58 | + assert isinstance(agent, LlmAgent) |
| 59 | + assert agent.name == 'enterprise_search_agent' |
| 60 | + assert 'enterprise_web_search' in [t.name for t in agent.tools] |
| 61 | + |
| 62 | + def test_enterprise_search_agent_tool_init(self): |
| 63 | + """Test initialization of EnterpriseSearchAgentTool.""" |
| 64 | + mock_agent = mock.MagicMock(spec=LlmAgent) |
| 65 | + mock_agent.name = 'test_agent' |
| 66 | + mock_agent.description = 'test_description' |
| 67 | + tool = EnterpriseSearchAgentTool(mock_agent) |
| 68 | + assert tool.agent == mock_agent |
| 69 | + |
| 70 | + @mark.asyncio |
| 71 | + @mock.patch('google.adk.tools.enterprise_search_agent_tool.Runner') |
| 72 | + async def test_run_async_succeeds(self, mock_runner_class): |
| 73 | + """Test that run_async executes the sub-agent and returns the result.""" |
| 74 | + # Arrange |
| 75 | + mock_agent = mock.MagicMock(spec=LlmAgent) |
| 76 | + mock_agent.name = 'enterprise_search_agent' |
| 77 | + mock_agent.description = 'test_description' |
| 78 | + mock_agent.input_schema = None |
| 79 | + mock_agent.output_schema = None |
| 80 | + |
| 81 | + tool = EnterpriseSearchAgentTool(mock_agent) |
| 82 | + tool_context = await _create_tool_context() |
| 83 | + |
| 84 | + async def mock_run_async_gen(): |
| 85 | + yield mock.MagicMock( |
| 86 | + actions=mock.MagicMock(state_delta={'key': 'value'}), content=None |
| 87 | + ) |
| 88 | + yield mock.MagicMock( |
| 89 | + actions=mock.MagicMock(state_delta=None), |
| 90 | + content=mock.MagicMock(parts=[mock.MagicMock(text='test response')]), |
| 91 | + ) |
| 92 | + |
| 93 | + mock_runner_instance = mock.MagicMock() |
| 94 | + mock_runner_instance.run_async.return_value = mock_run_async_gen() |
| 95 | + mock_runner_instance.session_service = mock.AsyncMock() |
| 96 | + mock_runner_instance.session_service.create_session.return_value = ( |
| 97 | + tool_context._invocation_context.session |
| 98 | + ) |
| 99 | + mock_runner_class.return_value = mock_runner_instance |
| 100 | + |
| 101 | + # Act |
| 102 | + result = await tool.run_async( |
| 103 | + args={'request': 'test query'}, tool_context=tool_context |
| 104 | + ) |
| 105 | + |
| 106 | + # Assert |
| 107 | + mock_runner_class.assert_called_once() |
| 108 | + mock_runner_instance.run_async.assert_called_once() |
| 109 | + assert tool_context.state['key'] == 'value' |
| 110 | + assert result == 'test response' |
0 commit comments