26
26
from google .adk .agents .base_agent import BaseAgentState
27
27
from google .adk .agents .callback_context import CallbackContext
28
28
from google .adk .agents .invocation_context import InvocationContext
29
+ from google .adk .apps .app import ResumabilityConfig
29
30
from google .adk .events .event import Event
30
31
from google .adk .plugins .base_plugin import BasePlugin
31
32
from google .adk .plugins .plugin_manager import PluginManager
@@ -733,39 +734,6 @@ async def test_run_live_incomplete_agent(request: pytest.FixtureRequest):
733
734
[e async for e in agent .run_live (parent_ctx )]
734
735
735
736
736
- @pytest .mark .asyncio
737
- async def test_create_agent_state_event (request : pytest .FixtureRequest ):
738
- # Arrange
739
- agent = _TestingAgent (name = f'{ request .function .__name__ } _test_agent' )
740
- ctx = await _create_parent_invocation_context (
741
- request .function .__name__ , agent , branch = 'test_branch'
742
- )
743
- state = BaseAgentState ()
744
-
745
- # Act
746
- event = agent ._create_agent_state_event (ctx , state = state )
747
-
748
- # Assert
749
- assert event .invocation_id == ctx .invocation_id
750
- assert event .author == agent .name
751
- assert event .branch == 'test_branch'
752
- assert event .actions is not None
753
- assert event .actions .agent_state is not None
754
- assert event .actions .agent_state == state .model_dump (mode = 'json' )
755
- assert not event .actions .end_of_agent
756
-
757
- # Act
758
- event = agent ._create_agent_state_event (ctx , end_of_agent = True )
759
-
760
- # Assert
761
- assert event .invocation_id == ctx .invocation_id
762
- assert event .author == agent .name
763
- assert event .branch == 'test_branch'
764
- assert event .actions is not None
765
- assert event .actions .end_of_agent
766
- assert event .actions .agent_state is None
767
-
768
-
769
737
def test_set_parent_agent_for_sub_agents (request : pytest .FixtureRequest ):
770
738
sub_agents : list [BaseAgent ] = [
771
739
_TestingAgent (name = f'{ request .function .__name__ } _sub_agent_1' ),
@@ -895,7 +863,7 @@ class _TestAgentState(BaseAgentState):
895
863
896
864
897
865
@pytest .mark .asyncio
898
- async def test_load_agent_state_no_resume ():
866
+ async def test_load_agent_state_not_resumable ():
899
867
agent = BaseAgent (name = 'test_agent' )
900
868
session_service = InMemorySessionService ()
901
869
session = await session_service .create_session (
@@ -907,14 +875,15 @@ async def test_load_agent_state_no_resume():
907
875
session = session ,
908
876
session_service = session_service ,
909
877
)
910
- default_state = _TestAgentState (test_field = 'default' )
911
878
912
- state , is_resuming = agent . _load_agent_state (
913
- ctx , _TestAgentState , default_state
914
- )
879
+ # Test case 1: resumability_config is None
880
+ state = agent . _load_agent_state ( ctx , _TestAgentState )
881
+ assert state is None
915
882
916
- assert not is_resuming
917
- assert state == default_state
883
+ # Test case 2: is_resumable is False
884
+ ctx .resumability_config = ResumabilityConfig (is_resumable = False )
885
+ state = agent ._load_agent_state (ctx , _TestAgentState )
886
+ assert state is None
918
887
919
888
920
889
@pytest .mark .asyncio
@@ -929,13 +898,70 @@ async def test_load_agent_state_with_resume():
929
898
agent = agent ,
930
899
session = session ,
931
900
session_service = session_service ,
901
+ resumability_config = ResumabilityConfig (is_resumable = True ),
932
902
)
903
+
904
+ # Test case 1: agent state not in context
905
+ state = agent ._load_agent_state (ctx , _TestAgentState )
906
+ assert state is None
907
+
908
+ # Test case 2: agent state in context
933
909
persisted_state = _TestAgentState (test_field = 'resumed' )
934
910
ctx .agent_states [agent .name ] = persisted_state .model_dump (mode = 'json' )
935
911
936
- state , is_resuming = agent ._load_agent_state (
937
- ctx , _TestAgentState , _TestAgentState ()
912
+ state = agent ._load_agent_state (ctx , _TestAgentState )
913
+ assert state == persisted_state
914
+
915
+
916
+ @pytest .mark .asyncio
917
+ async def test_create_agent_state_event ():
918
+ agent = BaseAgent (name = 'test_agent' )
919
+ session_service = InMemorySessionService ()
920
+ session = await session_service .create_session (
921
+ app_name = 'test_app' , user_id = 'test_user'
922
+ )
923
+ ctx = InvocationContext (
924
+ invocation_id = 'test_invocation' ,
925
+ agent = agent ,
926
+ session = session ,
927
+ session_service = session_service ,
938
928
)
939
929
940
- assert is_resuming
941
- assert state == persisted_state
930
+ ctx .branch = 'test_branch'
931
+
932
+ # Test case 1: with state
933
+ state = _TestAgentState (test_field = 'checkpoint' )
934
+ event = agent ._create_agent_state_event (ctx , agent_state = state )
935
+ assert event is not None
936
+ assert event .invocation_id == ctx .invocation_id
937
+ assert event .author == agent .name
938
+ assert event .branch == 'test_branch'
939
+ assert event .actions is not None
940
+ assert event .actions .agent_state is not None
941
+ assert event .actions .agent_state == state .model_dump (mode = 'json' )
942
+ assert not event .actions .end_of_agent
943
+
944
+ # Test case 2: with end_of_agent
945
+ event = agent ._create_agent_state_event (ctx , end_of_agent = True )
946
+ assert event is not None
947
+ assert event .invocation_id == ctx .invocation_id
948
+ assert event .author == agent .name
949
+ assert event .branch == 'test_branch'
950
+ assert event .actions is not None
951
+ assert event .actions .end_of_agent
952
+ assert event .actions .agent_state is None
953
+
954
+ # Test case 3: with both state and end_of_agent
955
+ state = _TestAgentState (test_field = 'checkpoint' )
956
+ event = agent ._create_agent_state_event (
957
+ ctx , agent_state = state , end_of_agent = True
958
+ )
959
+ assert event is not None
960
+ assert event .actions .agent_state == state .model_dump (mode = 'json' )
961
+ assert event .actions .end_of_agent
962
+
963
+ # Test case 4: with neither
964
+ event = agent ._create_agent_state_event (ctx )
965
+ assert event is not None
966
+ assert event .actions .agent_state is None
967
+ assert not event .actions .end_of_agent
0 commit comments