|
| 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.mock import Mock |
| 16 | + |
| 17 | +from google.adk.agents.base_agent import BaseAgent |
| 18 | +from google.adk.agents.invocation_context import InvocationContext |
| 19 | +from google.adk.events.event import Event |
| 20 | +from google.adk.sessions.base_session_service import BaseSessionService |
| 21 | +from google.adk.sessions.session import Session |
| 22 | +import pytest |
| 23 | + |
| 24 | + |
| 25 | +class TestInvocationContext: |
| 26 | + """Test suite for InvocationContext.""" |
| 27 | + |
| 28 | + @pytest.fixture |
| 29 | + def mock_events(self): |
| 30 | + """Create mock events for testing.""" |
| 31 | + event1 = Mock(spec=Event) |
| 32 | + event1.invocation_id = 'inv_1' |
| 33 | + event1.branch = 'agent_1' |
| 34 | + |
| 35 | + event2 = Mock(spec=Event) |
| 36 | + event2.invocation_id = 'inv_1' |
| 37 | + event2.branch = 'agent_2' |
| 38 | + |
| 39 | + event3 = Mock(spec=Event) |
| 40 | + event3.invocation_id = 'inv_2' |
| 41 | + event3.branch = 'agent_1' |
| 42 | + |
| 43 | + event4 = Mock(spec=Event) |
| 44 | + event4.invocation_id = 'inv_2' |
| 45 | + event4.branch = 'agent_2' |
| 46 | + |
| 47 | + return [event1, event2, event3, event4] |
| 48 | + |
| 49 | + @pytest.fixture |
| 50 | + def mock_invocation_context(self, mock_events): |
| 51 | + """Create a mock invocation context for testing.""" |
| 52 | + ctx = InvocationContext( |
| 53 | + session_service=Mock(spec=BaseSessionService), |
| 54 | + agent=Mock(spec=BaseAgent), |
| 55 | + invocation_id='inv_1', |
| 56 | + branch='agent_1', |
| 57 | + session=Mock(spec=Session, events=mock_events), |
| 58 | + ) |
| 59 | + return ctx |
| 60 | + |
| 61 | + def test_get_events_returns_all_events_by_default( |
| 62 | + self, mock_invocation_context, mock_events |
| 63 | + ): |
| 64 | + """Tests that get_events returns all events when no filters are applied.""" |
| 65 | + events = mock_invocation_context.get_events() |
| 66 | + assert events == mock_events |
| 67 | + |
| 68 | + def test_get_events_filters_by_current_invocation( |
| 69 | + self, mock_invocation_context, mock_events |
| 70 | + ): |
| 71 | + """Tests that get_events correctly filters by the current invocation.""" |
| 72 | + event1, event2, _, _ = mock_events |
| 73 | + events = mock_invocation_context.get_events(current_invocation=True) |
| 74 | + assert events == [event1, event2] |
| 75 | + |
| 76 | + def test_get_events_filters_by_current_branch( |
| 77 | + self, mock_invocation_context, mock_events |
| 78 | + ): |
| 79 | + """Tests that get_events correctly filters by the current branch.""" |
| 80 | + event1, _, event3, _ = mock_events |
| 81 | + events = mock_invocation_context.get_events(current_branch=True) |
| 82 | + assert events == [event1, event3] |
| 83 | + |
| 84 | + def test_get_events_filters_by_invocation_and_branch( |
| 85 | + self, mock_invocation_context, mock_events |
| 86 | + ): |
| 87 | + """Tests that get_events filters by invocation and branch.""" |
| 88 | + event1, _, _, _ = mock_events |
| 89 | + events = mock_invocation_context.get_events( |
| 90 | + current_invocation=True, |
| 91 | + current_branch=True, |
| 92 | + ) |
| 93 | + assert events == [event1] |
| 94 | + |
| 95 | + def test_get_events_with_no_events_in_session(self, mock_invocation_context): |
| 96 | + """Tests get_events when the session has no events.""" |
| 97 | + mock_invocation_context.session.events = [] |
| 98 | + events = mock_invocation_context.get_events() |
| 99 | + assert not events |
| 100 | + |
| 101 | + def test_get_events_with_no_matching_events(self, mock_invocation_context): |
| 102 | + """Tests get_events when no events match the filters.""" |
| 103 | + mock_invocation_context.invocation_id = 'inv_3' |
| 104 | + mock_invocation_context.branch = 'branch_C' |
| 105 | + |
| 106 | + # Filter by invocation |
| 107 | + events = mock_invocation_context.get_events(current_invocation=True) |
| 108 | + assert not events |
| 109 | + |
| 110 | + # Filter by branch |
| 111 | + events = mock_invocation_context.get_events(current_branch=True) |
| 112 | + assert not events |
| 113 | + |
| 114 | + # Filter by both |
| 115 | + events = mock_invocation_context.get_events( |
| 116 | + current_invocation=True, |
| 117 | + current_branch=True, |
| 118 | + ) |
| 119 | + assert not events |
0 commit comments