2626from google .adk .agents .base_agent import BaseAgentState
2727from google .adk .agents .callback_context import CallbackContext
2828from google .adk .agents .invocation_context import InvocationContext
29+ from google .adk .apps .app import ResumabilityConfig
2930from google .adk .events .event import Event
3031from google .adk .plugins .base_plugin import BasePlugin
3132from google .adk .plugins .plugin_manager import PluginManager
@@ -733,39 +734,6 @@ async def test_run_live_incomplete_agent(request: pytest.FixtureRequest):
733734 [e async for e in agent .run_live (parent_ctx )]
734735
735736
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-
769737def test_set_parent_agent_for_sub_agents (request : pytest .FixtureRequest ):
770738 sub_agents : list [BaseAgent ] = [
771739 _TestingAgent (name = f'{ request .function .__name__ } _sub_agent_1' ),
@@ -895,7 +863,7 @@ class _TestAgentState(BaseAgentState):
895863
896864
897865@pytest .mark .asyncio
898- async def test_load_agent_state_no_resume ():
866+ async def test_load_agent_state_not_resumable ():
899867 agent = BaseAgent (name = 'test_agent' )
900868 session_service = InMemorySessionService ()
901869 session = await session_service .create_session (
@@ -907,14 +875,15 @@ async def test_load_agent_state_no_resume():
907875 session = session ,
908876 session_service = session_service ,
909877 )
910- default_state = _TestAgentState (test_field = 'default' )
911878
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
915882
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
918887
919888
920889@pytest .mark .asyncio
@@ -929,13 +898,70 @@ async def test_load_agent_state_with_resume():
929898 agent = agent ,
930899 session = session ,
931900 session_service = session_service ,
901+ resumability_config = ResumabilityConfig (is_resumable = True ),
932902 )
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
933909 persisted_state = _TestAgentState (test_field = 'resumed' )
934910 ctx .agent_states [agent .name ] = persisted_state .model_dump (mode = 'json' )
935911
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 ,
938928 )
939929
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