forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_plugin_manager.py
More file actions
381 lines (301 loc) · 12.5 KB
/
test_plugin_manager.py
File metadata and controls
381 lines (301 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Unit tests for the PluginManager."""
from __future__ import annotations
import asyncio
from unittest.mock import AsyncMock
from unittest.mock import Mock
from google.adk.models.llm_response import LlmResponse
from google.adk.plugins.base_plugin import BasePlugin
# Assume the following path to your modules
# You might need to adjust this based on your project structure.
from google.adk.plugins.plugin_manager import PluginCallbackName
from google.adk.plugins.plugin_manager import PluginManager
import pytest
# A helper class to use in tests instead of mocks.
# This makes tests more explicit and easier to debug.
class TestPlugin(BasePlugin):
__test__ = False
"""
A test plugin that can be configured to return specific values or raise
exceptions for any callback, and it logs which callbacks were invoked.
"""
def __init__(self, name: str):
super().__init__(name)
# A log to track the names of callbacks that have been called.
self.call_log: list[PluginCallbackName] = []
# A map to configure return values for specific callbacks.
self.return_values: dict[PluginCallbackName, any] = {}
# A map to configure exceptions to be raised by specific callbacks.
self.exceptions_to_raise: dict[PluginCallbackName, Exception] = {}
async def _handle_callback(self, name: PluginCallbackName):
"""Generic handler for all callback methods."""
self.call_log.append(name)
if name in self.exceptions_to_raise:
raise self.exceptions_to_raise[name]
return self.return_values.get(name)
# Implement all callback methods from the BasePlugin interface.
async def on_user_message_callback(self, **kwargs):
return await self._handle_callback("on_user_message_callback")
async def before_run_callback(self, **kwargs):
return await self._handle_callback("before_run_callback")
async def after_run_callback(self, **kwargs):
return await self._handle_callback("after_run_callback")
async def on_event_callback(self, **kwargs):
return await self._handle_callback("on_event_callback")
async def before_agent_callback(self, **kwargs):
return await self._handle_callback("before_agent_callback")
async def after_agent_callback(self, **kwargs):
return await self._handle_callback("after_agent_callback")
async def before_tool_callback(self, **kwargs):
return await self._handle_callback("before_tool_callback")
async def after_tool_callback(self, **kwargs):
return await self._handle_callback("after_tool_callback")
async def on_tool_error_callback(self, **kwargs):
return await self._handle_callback("on_tool_error_callback")
async def before_model_callback(self, **kwargs):
return await self._handle_callback("before_model_callback")
async def after_model_callback(self, **kwargs):
return await self._handle_callback("after_model_callback")
async def on_model_error_callback(self, **kwargs):
return await self._handle_callback("on_model_error_callback")
async def on_state_change_callback(self, **kwargs):
return await self._handle_callback("on_state_change_callback")
@pytest.fixture
def service() -> PluginManager:
"""Provides a clean PluginManager instance for each test."""
return PluginManager()
@pytest.fixture
def plugin1() -> TestPlugin:
"""Provides a clean instance of our test plugin named 'plugin1'."""
return TestPlugin(name="plugin1")
@pytest.fixture
def plugin2() -> TestPlugin:
"""Provides a clean instance of our test plugin named 'plugin2'."""
return TestPlugin(name="plugin2")
def test_register_and_get_plugin(service: PluginManager, plugin1: TestPlugin):
"""Tests successful registration and retrieval of a plugin."""
service.register_plugin(plugin1)
assert len(service.plugins) == 1
assert service.plugins[0] is plugin1
assert service.get_plugin("plugin1") is plugin1
def test_register_duplicate_plugin_name_raises_value_error(
service: PluginManager, plugin1: TestPlugin
):
"""Tests that registering a plugin with a duplicate name raises an error."""
plugin1_duplicate = TestPlugin(name="plugin1")
service.register_plugin(plugin1)
with pytest.raises(
ValueError, match="Plugin with name 'plugin1' already registered."
):
service.register_plugin(plugin1_duplicate)
@pytest.mark.asyncio
async def test_early_exit_stops_subsequent_plugins(
service: PluginManager, plugin1: TestPlugin, plugin2: TestPlugin
):
"""Tests the core "early exit" logic: if a plugin returns a value,
subsequent plugins for that callback should not be executed.
"""
# Configure plugin1 to return a value, simulating a cache hit.
mock_response = Mock(spec=LlmResponse)
plugin1.return_values["before_run_callback"] = mock_response
service.register_plugin(plugin1)
service.register_plugin(plugin2)
# Execute the callback chain.
result = await service.run_before_run_callback(invocation_context=Mock())
# Assert that the final result is the one returned by the first plugin.
assert result is mock_response
# Assert that the first plugin was called.
assert "before_run_callback" in plugin1.call_log
# CRITICAL: Assert that the second plugin was never called.
assert "before_run_callback" not in plugin2.call_log
@pytest.mark.asyncio
async def test_normal_flow_all_plugins_are_called(
service: PluginManager, plugin1: TestPlugin, plugin2: TestPlugin
):
"""Tests that if no plugin returns a value, all plugins in the chain
are executed in order.
"""
# By default, plugins are configured to return None.
service.register_plugin(plugin1)
service.register_plugin(plugin2)
result = await service.run_before_run_callback(invocation_context=Mock())
# The final result should be None as no plugin interrupted the flow.
assert result is None
# Both plugins must have been called.
assert "before_run_callback" in plugin1.call_log
assert "before_run_callback" in plugin2.call_log
@pytest.mark.asyncio
async def test_plugin_exception_is_wrapped_in_runtime_error(
service: PluginManager, plugin1: TestPlugin
):
"""Tests that if a plugin callback raises an exception, the PluginManager
catches it and raises a descriptive RuntimeError.
"""
# Configure the plugin to raise an error during a specific callback.
original_exception = ValueError("Something went wrong inside the plugin!")
plugin1.exceptions_to_raise["before_run_callback"] = original_exception
service.register_plugin(plugin1)
with pytest.raises(RuntimeError) as excinfo:
await service.run_before_run_callback(invocation_context=Mock())
# Check that the error message is informative.
assert "Error in plugin 'plugin1'" in str(excinfo.value)
assert "before_run_callback" in str(excinfo.value)
# Check that the original exception is chained for better tracebacks.
assert excinfo.value.__cause__ is original_exception
@pytest.mark.asyncio
async def test_all_callbacks_are_supported(
service: PluginManager, plugin1: TestPlugin
):
"""Tests that all callbacks defined in the BasePlugin interface are supported
by the PluginManager.
"""
service.register_plugin(plugin1)
mock_context = Mock()
mock_user_message = Mock()
# Test all callbacks
await service.run_on_user_message_callback(
user_message=mock_user_message, invocation_context=mock_context
)
await service.run_before_run_callback(invocation_context=mock_context)
await service.run_after_run_callback(invocation_context=mock_context)
await service.run_on_event_callback(
invocation_context=mock_context, event=mock_context
)
await service.run_before_agent_callback(
agent=mock_context, callback_context=mock_context
)
await service.run_after_agent_callback(
agent=mock_context, callback_context=mock_context
)
await service.run_before_tool_callback(
tool=mock_context, tool_args={}, tool_context=mock_context
)
await service.run_after_tool_callback(
tool=mock_context, tool_args={}, tool_context=mock_context, result={}
)
await service.run_on_tool_error_callback(
tool=mock_context,
tool_args={},
tool_context=mock_context,
error=mock_context,
)
await service.run_before_model_callback(
callback_context=mock_context, llm_request=mock_context
)
await service.run_after_model_callback(
callback_context=mock_context, llm_response=mock_context
)
await service.run_on_model_error_callback(
callback_context=mock_context,
llm_request=mock_context,
error=mock_context,
)
await service.run_on_state_change_callback(
callback_context=mock_context,
state_delta={"key": "value"},
)
# Verify all callbacks were logged
expected_callbacks = [
"on_user_message_callback",
"before_run_callback",
"after_run_callback",
"on_event_callback",
"before_agent_callback",
"after_agent_callback",
"before_tool_callback",
"after_tool_callback",
"on_tool_error_callback",
"before_model_callback",
"after_model_callback",
"on_model_error_callback",
"on_state_change_callback",
]
assert set(plugin1.call_log) == set(expected_callbacks)
@pytest.mark.asyncio
async def test_close_calls_plugin_close(
service: PluginManager, plugin1: TestPlugin
):
"""Tests that close calls the close method on registered plugins."""
plugin1.close = AsyncMock()
service.register_plugin(plugin1)
await service.close()
plugin1.close.assert_awaited_once()
@pytest.mark.asyncio
async def test_close_raises_runtime_error_on_plugin_exception(
service: PluginManager, plugin1: TestPlugin
):
"""Tests that close raises a RuntimeError if a plugin's close fails."""
plugin1.close = AsyncMock(side_effect=ValueError("Shutdown error"))
service.register_plugin(plugin1)
with pytest.raises(
RuntimeError, match="Failed to close plugins: 'plugin1': ValueError"
):
await service.close()
plugin1.close.assert_awaited_once()
@pytest.mark.asyncio
async def test_close_with_timeout(plugin1: TestPlugin):
"""Tests that close respects the timeout and raises on failure."""
service = PluginManager(close_timeout=0.1)
async def slow_close():
await asyncio.sleep(0.2)
plugin1.close = slow_close
service.register_plugin(plugin1)
with pytest.raises(RuntimeError) as excinfo:
await service.close()
assert "Failed to close plugins: 'plugin1': TimeoutError" in str(
excinfo.value
)
# --- on_state_change_callback tests ---
@pytest.mark.asyncio
async def test_run_on_state_change_callback(
service: PluginManager, plugin1: TestPlugin
):
"""Tests that run_on_state_change_callback invokes the callback and returns None."""
service.register_plugin(plugin1)
result = await service.run_on_state_change_callback(
callback_context=Mock(),
state_delta={"key": "value"},
)
assert result is None
assert "on_state_change_callback" in plugin1.call_log
@pytest.mark.asyncio
async def test_run_on_state_change_callback_calls_all_plugins(
service: PluginManager, plugin1: TestPlugin, plugin2: TestPlugin
):
"""Tests that on_state_change_callback is called on all plugins."""
service.register_plugin(plugin1)
service.register_plugin(plugin2)
await service.run_on_state_change_callback(
callback_context=Mock(),
state_delta={"key": "value"},
)
assert "on_state_change_callback" in plugin1.call_log
assert "on_state_change_callback" in plugin2.call_log
@pytest.mark.asyncio
async def test_run_on_state_change_callback_wraps_exceptions(
service: PluginManager, plugin1: TestPlugin
):
"""Tests that exceptions in on_state_change_callback are wrapped in RuntimeError."""
original_exception = ValueError("state change error")
plugin1.exceptions_to_raise["on_state_change_callback"] = original_exception
service.register_plugin(plugin1)
with pytest.raises(RuntimeError) as excinfo:
await service.run_on_state_change_callback(
callback_context=Mock(),
state_delta={"key": "value"},
)
assert "Error in plugin 'plugin1'" in str(excinfo.value)
assert "on_state_change_callback" in str(excinfo.value)
assert excinfo.value.__cause__ is original_exception