@@ -164,3 +164,50 @@ async def test_append_event(mock_firestore_client):
164164 batch .set .assert_called_once () # For event
165165 batch .update .assert_called_once () # For session updateTime
166166 batch .commit .assert_called_once ()
167+
168+
169+ @pytest .mark .asyncio
170+ async def test_append_event_with_state_delta (mock_firestore_client ):
171+ service = FirestoreSessionService (client = mock_firestore_client )
172+ app_name = "test_app"
173+ user_id = "test_user"
174+ from google .adk .sessions .session import Session
175+
176+ session = Session (id = "test_session" , app_name = app_name , user_id = user_id )
177+
178+ # Using MagicMock for Event to bypass complex pydantic validation for test
179+ event = mock .MagicMock ()
180+ event .partial = False
181+ event .id = "test_event_id"
182+ # Mock actions.state_delta
183+ event .actions .state_delta = {
184+ "_app_my_key" : "app_val" ,
185+ "_user_my_key" : "user_val" ,
186+ "session_key" : "session_val" ,
187+ }
188+ # Mock model_dump to return valid event data
189+ event .model_dump .return_value = {"id" : "test_event_id" , "author" : "user" }
190+
191+ await service .append_event (session , event )
192+
193+ mock_firestore_client .batch .assert_called_once ()
194+ batch = mock_firestore_client .batch .return_value
195+
196+ # Verify app state set
197+ # In code: batch.set(app_ref, app_updates, merge=True)
198+ # But app_ref is a mock! Which mock?
199+ # It's mock_firestore_client.collection().document()
200+ # In fixture: collection_ref = mock.AsyncMock()
201+ # doc_ref = mock.AsyncMock()
202+ # client.collection.return_value = collection_ref
203+ # collection_ref.document.return_value = doc_ref
204+ # So batch.set is called with app_ref (which is doc_ref)
205+ batch .set .assert_called ()
206+
207+ # Verify session state updated in memory
208+ assert session .state ["session_key" ] == "session_val"
209+
210+ # Verify batch update was called for session
211+ batch .update .assert_called_once ()
212+
213+ batch .commit .assert_called_once ()
0 commit comments