|
| 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 unittest import mock |
| 16 | + |
| 17 | +from google.adk.agents import Agent |
| 18 | +from google.adk.agents.live_request_queue import LiveRequest |
| 19 | +from google.adk.agents.live_request_queue import LiveRequestQueue |
| 20 | +from google.adk.agents.run_config import RunConfig |
| 21 | +from google.adk.flows.llm_flows.base_llm_flow import BaseLlmFlow |
| 22 | +from google.adk.models.llm_request import LlmRequest |
| 23 | +from google.genai import types |
| 24 | +import pytest |
| 25 | + |
| 26 | +from ... import testing_utils |
| 27 | + |
| 28 | + |
| 29 | +class TestBaseLlmFlow(BaseLlmFlow): |
| 30 | + """Test implementation of BaseLlmFlow for testing purposes.""" |
| 31 | + |
| 32 | + pass |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def test_blob(): |
| 37 | + """Test blob for audio data.""" |
| 38 | + return types.Blob(data=b'\x00\xFF\x00\xFF', mime_type='audio/pcm') |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def mock_llm_connection(): |
| 43 | + """Mock LLM connection for testing.""" |
| 44 | + connection = mock.AsyncMock() |
| 45 | + connection.send_realtime = mock.AsyncMock() |
| 46 | + return connection |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.asyncio |
| 50 | +async def test_send_to_model_with_disabled_vad(test_blob, mock_llm_connection): |
| 51 | + """Test _send_to_model with automatic_activity_detection.disabled=True.""" |
| 52 | + # Create LlmRequest with disabled VAD |
| 53 | + realtime_input_config = types.RealtimeInputConfig( |
| 54 | + automatic_activity_detection=types.AutomaticActivityDetection( |
| 55 | + disabled=True |
| 56 | + ) |
| 57 | + ) |
| 58 | + |
| 59 | + # Create invocation context with live request queue |
| 60 | + agent = Agent(name='test_agent', model='mock') |
| 61 | + invocation_context = await testing_utils.create_invocation_context( |
| 62 | + agent=agent, |
| 63 | + user_content='', |
| 64 | + run_config=RunConfig(realtime_input_config=realtime_input_config), |
| 65 | + ) |
| 66 | + invocation_context.live_request_queue = LiveRequestQueue() |
| 67 | + |
| 68 | + # Create flow and start _send_to_model task |
| 69 | + flow = TestBaseLlmFlow() |
| 70 | + |
| 71 | + # Send a blob to the queue |
| 72 | + live_request = LiveRequest(blob=test_blob) |
| 73 | + invocation_context.live_request_queue.send(live_request) |
| 74 | + invocation_context.live_request_queue.close() |
| 75 | + |
| 76 | + # Run _send_to_model |
| 77 | + await flow._send_to_model(mock_llm_connection, invocation_context) |
| 78 | + |
| 79 | + mock_llm_connection.send_realtime.assert_called_once_with(test_blob) |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.asyncio |
| 83 | +async def test_send_to_model_with_enabled_vad(test_blob, mock_llm_connection): |
| 84 | + """Test _send_to_model with automatic_activity_detection.disabled=False. |
| 85 | +
|
| 86 | + Custom VAD activity signal is not supported so we should still disable it. |
| 87 | + """ |
| 88 | + # Create LlmRequest with enabled VAD |
| 89 | + realtime_input_config = types.RealtimeInputConfig( |
| 90 | + automatic_activity_detection=types.AutomaticActivityDetection( |
| 91 | + disabled=False |
| 92 | + ) |
| 93 | + ) |
| 94 | + |
| 95 | + # Create invocation context with live request queue |
| 96 | + agent = Agent(name='test_agent', model='mock') |
| 97 | + invocation_context = await testing_utils.create_invocation_context( |
| 98 | + agent=agent, user_content='' |
| 99 | + ) |
| 100 | + invocation_context.live_request_queue = LiveRequestQueue() |
| 101 | + |
| 102 | + # Create flow and start _send_to_model task |
| 103 | + flow = TestBaseLlmFlow() |
| 104 | + |
| 105 | + # Send a blob to the queue |
| 106 | + live_request = LiveRequest(blob=test_blob) |
| 107 | + invocation_context.live_request_queue.send(live_request) |
| 108 | + invocation_context.live_request_queue.close() |
| 109 | + |
| 110 | + # Run _send_to_model |
| 111 | + await flow._send_to_model(mock_llm_connection, invocation_context) |
| 112 | + |
| 113 | + mock_llm_connection.send_realtime.assert_called_once_with(test_blob) |
| 114 | + |
| 115 | + |
| 116 | +@pytest.mark.asyncio |
| 117 | +async def test_send_to_model_without_realtime_config( |
| 118 | + test_blob, mock_llm_connection |
| 119 | +): |
| 120 | + """Test _send_to_model without realtime_input_config (default behavior).""" |
| 121 | + # Create invocation context with live request queue |
| 122 | + agent = Agent(name='test_agent', model='mock') |
| 123 | + invocation_context = await testing_utils.create_invocation_context( |
| 124 | + agent=agent, user_content='' |
| 125 | + ) |
| 126 | + invocation_context.live_request_queue = LiveRequestQueue() |
| 127 | + |
| 128 | + # Create flow and start _send_to_model task |
| 129 | + flow = TestBaseLlmFlow() |
| 130 | + |
| 131 | + # Send a blob to the queue |
| 132 | + live_request = LiveRequest(blob=test_blob) |
| 133 | + invocation_context.live_request_queue.send(live_request) |
| 134 | + invocation_context.live_request_queue.close() |
| 135 | + |
| 136 | + # Run _send_to_model |
| 137 | + await flow._send_to_model(mock_llm_connection, invocation_context) |
| 138 | + |
| 139 | + mock_llm_connection.send_realtime.assert_called_once_with(test_blob) |
| 140 | + |
| 141 | + |
| 142 | +@pytest.mark.asyncio |
| 143 | +async def test_send_to_model_with_none_automatic_activity_detection( |
| 144 | + test_blob, mock_llm_connection |
| 145 | +): |
| 146 | + """Test _send_to_model with automatic_activity_detection=None.""" |
| 147 | + # Create LlmRequest with None automatic_activity_detection |
| 148 | + realtime_input_config = types.RealtimeInputConfig( |
| 149 | + automatic_activity_detection=None |
| 150 | + ) |
| 151 | + |
| 152 | + # Create invocation context with live request queue |
| 153 | + agent = Agent(name='test_agent', model='mock') |
| 154 | + invocation_context = await testing_utils.create_invocation_context( |
| 155 | + agent=agent, |
| 156 | + user_content='', |
| 157 | + run_config=RunConfig(realtime_input_config=realtime_input_config), |
| 158 | + ) |
| 159 | + invocation_context.live_request_queue = LiveRequestQueue() |
| 160 | + |
| 161 | + # Create flow and start _send_to_model task |
| 162 | + flow = TestBaseLlmFlow() |
| 163 | + |
| 164 | + # Send a blob to the queue |
| 165 | + live_request = LiveRequest(blob=test_blob) |
| 166 | + invocation_context.live_request_queue.send(live_request) |
| 167 | + invocation_context.live_request_queue.close() |
| 168 | + |
| 169 | + # Run _send_to_model |
| 170 | + await flow._send_to_model(mock_llm_connection, invocation_context) |
| 171 | + |
| 172 | + mock_llm_connection.send_realtime.assert_called_once_with(test_blob) |
| 173 | + |
| 174 | + |
| 175 | +@pytest.mark.asyncio |
| 176 | +async def test_send_to_model_with_text_content(mock_llm_connection): |
| 177 | + """Test _send_to_model with text content (not blob).""" |
| 178 | + # Create invocation context with live request queue |
| 179 | + agent = Agent(name='test_agent', model='mock') |
| 180 | + invocation_context = await testing_utils.create_invocation_context( |
| 181 | + agent=agent, user_content='' |
| 182 | + ) |
| 183 | + invocation_context.live_request_queue = LiveRequestQueue() |
| 184 | + |
| 185 | + # Create flow and start _send_to_model task |
| 186 | + flow = TestBaseLlmFlow() |
| 187 | + |
| 188 | + # Send text content to the queue |
| 189 | + content = types.Content( |
| 190 | + role='user', parts=[types.Part.from_text(text='Hello')] |
| 191 | + ) |
| 192 | + live_request = LiveRequest(content=content) |
| 193 | + invocation_context.live_request_queue.send(live_request) |
| 194 | + invocation_context.live_request_queue.close() |
| 195 | + |
| 196 | + # Run _send_to_model |
| 197 | + await flow._send_to_model(mock_llm_connection, invocation_context) |
| 198 | + |
| 199 | + # Verify send_content was called instead of send_realtime |
| 200 | + mock_llm_connection.send_content.assert_called_once_with(content) |
| 201 | + mock_llm_connection.send_realtime.assert_not_called() |
0 commit comments