|
| 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.cli.utils import agent_loader |
| 18 | +from google.adk.cli.utils.agent_change_handler import AgentChangeEventHandler |
| 19 | +from google.adk.cli.utils.shared_value import SharedValue |
| 20 | +import pytest |
| 21 | +from watchdog.events import FileModifiedEvent |
| 22 | + |
| 23 | + |
| 24 | +class TestAgentChangeEventHandler: |
| 25 | + """Unit tests for AgentChangeEventHandler file extension filtering.""" |
| 26 | + |
| 27 | + @pytest.fixture |
| 28 | + def mock_agent_loader(self): |
| 29 | + """Create a mock AgentLoader constrained to the public API.""" |
| 30 | + return mock.create_autospec( |
| 31 | + agent_loader.AgentLoader, instance=True, spec_set=True |
| 32 | + ) |
| 33 | + |
| 34 | + @pytest.fixture |
| 35 | + def handler(self, mock_agent_loader): |
| 36 | + """Create an AgentChangeEventHandler with mocked dependencies.""" |
| 37 | + runners_to_clean = set() |
| 38 | + current_app_name_ref = SharedValue(value="test_agent") |
| 39 | + return AgentChangeEventHandler( |
| 40 | + agent_loader=mock_agent_loader, |
| 41 | + runners_to_clean=runners_to_clean, |
| 42 | + current_app_name_ref=current_app_name_ref, |
| 43 | + ) |
| 44 | + |
| 45 | + @pytest.mark.parametrize( |
| 46 | + "file_path", |
| 47 | + [ |
| 48 | + pytest.param("/path/to/agent.py", id="python_file"), |
| 49 | + pytest.param("/path/to/config.yaml", id="yaml_file"), |
| 50 | + pytest.param("/path/to/config.yml", id="yml_file"), |
| 51 | + ], |
| 52 | + ) |
| 53 | + def test_on_modified_triggers_reload_for_supported_extensions( |
| 54 | + self, handler, mock_agent_loader, file_path |
| 55 | + ): |
| 56 | + """Verify that .py, .yaml, and .yml files trigger agent reload.""" |
| 57 | + event = FileModifiedEvent(src_path=file_path) |
| 58 | + |
| 59 | + handler.on_modified(event) |
| 60 | + |
| 61 | + mock_agent_loader.remove_agent_from_cache.assert_called_once_with( |
| 62 | + "test_agent" |
| 63 | + ) |
| 64 | + assert ( |
| 65 | + "test_agent" in handler.runners_to_clean |
| 66 | + ), f"Expected 'test_agent' in runners_to_clean for {file_path}" |
| 67 | + |
| 68 | + @pytest.mark.parametrize( |
| 69 | + "file_path", |
| 70 | + [ |
| 71 | + pytest.param("/path/to/file.json", id="json_file"), |
| 72 | + pytest.param("/path/to/file.txt", id="txt_file"), |
| 73 | + pytest.param("/path/to/file.md", id="markdown_file"), |
| 74 | + pytest.param("/path/to/file.toml", id="toml_file"), |
| 75 | + pytest.param("/path/to/.gitignore", id="gitignore_file"), |
| 76 | + pytest.param("/path/to/file", id="no_extension"), |
| 77 | + ], |
| 78 | + ) |
| 79 | + def test_on_modified_ignores_unsupported_extensions( |
| 80 | + self, handler, mock_agent_loader, file_path |
| 81 | + ): |
| 82 | + """Verify that non-py/yaml/yml files do not trigger reload.""" |
| 83 | + event = FileModifiedEvent(src_path=file_path) |
| 84 | + |
| 85 | + handler.on_modified(event) |
| 86 | + |
| 87 | + mock_agent_loader.remove_agent_from_cache.assert_not_called() |
| 88 | + assert not handler.runners_to_clean, ( |
| 89 | + f"Expected runners_to_clean to be empty for {file_path}, " |
| 90 | + f"got {handler.runners_to_clean}" |
| 91 | + ) |
0 commit comments