@@ -507,6 +507,72 @@ async def test_streaming_agent_run_with_events(
507507 events .append (event )
508508 assert len (events ) == 1
509509
510+ @pytest .mark .asyncio
511+ async def test_streaming_agent_run_with_events_existing_session (
512+ self ,
513+ default_instrumentor_builder_mock : mock .Mock ,
514+ get_project_id_mock : mock .Mock ,
515+ ):
516+ app = agent_engines .AdkApp (agent = _TEST_AGENT )
517+ app .set_up ()
518+
519+ # Pre-create a session in the real in-memory session service
520+ await app .async_create_session (
521+ user_id = _TEST_USER_ID , session_id = "test_session_id"
522+ )
523+
524+ # Mock the main runner
525+ runner_mock = mock .Mock ()
526+
527+ # Define an async generator for run_async mock return value
528+ async def mock_run_async (* args , ** kwargs ):
529+ from google .adk .events import event
530+ yield event .Event (
531+ ** {
532+ "author" : "currency_exchange_agent" ,
533+ "content" : {
534+ "parts" : [{"text" : "Sweden" }],
535+ "role" : "model" ,
536+ },
537+ "id" : "9aaItGK9" ,
538+ "invocation_id" : "e-6543c213-6417-484b-9551-b67915d1d5f7" ,
539+ }
540+ )
541+
542+ spy = mock .MagicMock (side_effect = mock_run_async )
543+ runner_mock .run_async = spy
544+ app ._tmpl_attrs ["runner" ] = runner_mock
545+
546+ request_json = json .dumps (
547+ {
548+ "authorizations" : {
549+ "test_user_id1" : {"access_token" : "test_access_token" },
550+ },
551+ "user_id" : _TEST_USER_ID ,
552+ "session_id" : "test_session_id" ,
553+ "message" : {
554+ "parts" : [{"text" : "What is the exchange rate from USD to SEK?" }],
555+ "role" : "user" ,
556+ },
557+ }
558+ )
559+
560+ events = []
561+ async for event in app .streaming_agent_run_with_events (
562+ request_json = request_json ,
563+ ):
564+ events .append (event )
565+
566+ assert len (events ) == 1
567+
568+ # Assert that run_async was called with the expected state_delta!
569+ spy .assert_called_once_with (
570+ user_id = _TEST_USER_ID ,
571+ session_id = "test_session_id" ,
572+ new_message = mock .ANY ,
573+ state_delta = {"test_user_id1" : "test_access_token" },
574+ )
575+
510576 @pytest .mark .asyncio
511577 @mock .patch .dict (
512578 os .environ ,
0 commit comments