|
| 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 | +"""Unit tests for BaseLlmFlow toolset integration.""" |
| 16 | + |
| 17 | +from unittest.mock import AsyncMock |
| 18 | + |
| 19 | +from google.adk.agents import Agent |
| 20 | +from google.adk.flows.llm_flows.base_llm_flow import BaseLlmFlow |
| 21 | +from google.adk.models.llm_request import LlmRequest |
| 22 | +from google.adk.models.llm_response import LlmResponse |
| 23 | +from google.adk.tools.base_toolset import BaseToolset |
| 24 | +from google.genai import types |
| 25 | +import pytest |
| 26 | + |
| 27 | +from ... import testing_utils |
| 28 | + |
| 29 | + |
| 30 | +class BaseLlmFlowForTesting(BaseLlmFlow): |
| 31 | + """Test implementation of BaseLlmFlow for testing purposes.""" |
| 32 | + |
| 33 | + pass |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.asyncio |
| 37 | +async def test_preprocess_calls_toolset_process_llm_request(): |
| 38 | + """Test that _preprocess_async calls process_llm_request on toolsets.""" |
| 39 | + |
| 40 | + # Create a mock toolset that tracks if process_llm_request was called |
| 41 | + class _MockToolset(BaseToolset): |
| 42 | + |
| 43 | + def __init__(self): |
| 44 | + super().__init__() |
| 45 | + self.process_llm_request_called = False |
| 46 | + self.process_llm_request = AsyncMock(side_effect=self._track_call) |
| 47 | + |
| 48 | + async def _track_call(self, **kwargs): |
| 49 | + self.process_llm_request_called = True |
| 50 | + |
| 51 | + async def get_tools(self, readonly_context=None): |
| 52 | + return [] |
| 53 | + |
| 54 | + async def close(self): |
| 55 | + pass |
| 56 | + |
| 57 | + mock_toolset = _MockToolset() |
| 58 | + |
| 59 | + # Create a mock model that returns a simple response |
| 60 | + mock_response = LlmResponse( |
| 61 | + content=types.Content( |
| 62 | + role='model', parts=[types.Part.from_text(text='Test response')] |
| 63 | + ), |
| 64 | + partial=False, |
| 65 | + ) |
| 66 | + |
| 67 | + mock_model = testing_utils.MockModel.create(responses=[mock_response]) |
| 68 | + |
| 69 | + # Create agent with the mock toolset |
| 70 | + agent = Agent(name='test_agent', model=mock_model, tools=[mock_toolset]) |
| 71 | + invocation_context = await testing_utils.create_invocation_context( |
| 72 | + agent=agent, user_content='test message' |
| 73 | + ) |
| 74 | + |
| 75 | + flow = BaseLlmFlowForTesting() |
| 76 | + |
| 77 | + # Call _preprocess_async |
| 78 | + llm_request = LlmRequest() |
| 79 | + events = [] |
| 80 | + async for event in flow._preprocess_async(invocation_context, llm_request): |
| 81 | + events.append(event) |
| 82 | + |
| 83 | + # Verify that process_llm_request was called on the toolset |
| 84 | + assert mock_toolset.process_llm_request_called |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.asyncio |
| 88 | +async def test_preprocess_handles_mixed_tools_and_toolsets(): |
| 89 | + """Test that _preprocess_async properly handles both tools and toolsets.""" |
| 90 | + from google.adk.tools.base_tool import BaseTool |
| 91 | + from google.adk.tools.function_tool import FunctionTool |
| 92 | + |
| 93 | + # Create a mock tool |
| 94 | + class _MockTool(BaseTool): |
| 95 | + |
| 96 | + def __init__(self): |
| 97 | + super().__init__(name='mock_tool', description='Mock tool') |
| 98 | + self.process_llm_request_called = False |
| 99 | + self.process_llm_request = AsyncMock(side_effect=self._track_call) |
| 100 | + |
| 101 | + async def _track_call(self, **kwargs): |
| 102 | + self.process_llm_request_called = True |
| 103 | + |
| 104 | + async def call(self, **kwargs): |
| 105 | + return 'mock result' |
| 106 | + |
| 107 | + # Create a mock toolset |
| 108 | + class _MockToolset(BaseToolset): |
| 109 | + |
| 110 | + def __init__(self): |
| 111 | + super().__init__() |
| 112 | + self.process_llm_request_called = False |
| 113 | + self.process_llm_request = AsyncMock(side_effect=self._track_call) |
| 114 | + |
| 115 | + async def _track_call(self, **kwargs): |
| 116 | + self.process_llm_request_called = True |
| 117 | + |
| 118 | + async def get_tools(self, readonly_context=None): |
| 119 | + return [] |
| 120 | + |
| 121 | + async def close(self): |
| 122 | + pass |
| 123 | + |
| 124 | + def _test_function(): |
| 125 | + """Test function tool.""" |
| 126 | + return 'function result' |
| 127 | + |
| 128 | + mock_tool = _MockTool() |
| 129 | + mock_toolset = _MockToolset() |
| 130 | + |
| 131 | + # Create agent with mixed tools and toolsets |
| 132 | + agent = Agent( |
| 133 | + name='test_agent', tools=[mock_tool, _test_function, mock_toolset] |
| 134 | + ) |
| 135 | + |
| 136 | + invocation_context = await testing_utils.create_invocation_context( |
| 137 | + agent=agent, user_content='test message' |
| 138 | + ) |
| 139 | + |
| 140 | + flow = BaseLlmFlowForTesting() |
| 141 | + |
| 142 | + # Call _preprocess_async |
| 143 | + llm_request = LlmRequest() |
| 144 | + events = [] |
| 145 | + async for event in flow._preprocess_async(invocation_context, llm_request): |
| 146 | + events.append(event) |
| 147 | + |
| 148 | + # Verify that process_llm_request was called on both tools and toolsets |
| 149 | + assert mock_tool.process_llm_request_called |
| 150 | + assert mock_toolset.process_llm_request_called |
0 commit comments