@@ -316,6 +316,7 @@ def test_cloudsql_host_logged_correctly(self, caplog):
316316 "/agent_sessions?host=/cloudsql/project:region:instance"
317317 )
318318 mock_settings = MagicMock ()
319+ mock_settings .session_backend = "database"
319320 mock_settings .session_database_url = cloudsql_url
320321
321322 mock_db_session = MagicMock ()
@@ -341,6 +342,7 @@ def test_standard_host_logged_correctly(self, caplog):
341342 """Test that a standard PostgreSQL host is logged correctly."""
342343 standard_url = "postgresql+asyncpg://user:pass@db.example.com:5432/mydb"
343344 mock_settings = MagicMock ()
345+ mock_settings .session_backend = "database"
344346 mock_settings .session_database_url = standard_url
345347
346348 mock_db_session = MagicMock ()
@@ -360,13 +362,14 @@ def test_standard_host_logged_correctly(self, caplog):
360362
361363 assert "host=db.example.com" in caplog .text
362364
363- def test_credentials_not_leaked_on_init_failure (self , caplog ):
364- """Test that database credentials are sanitized in error logs ."""
365+ def test_credentials_not_leaked_on_init_failure (self ):
366+ """Test that database credentials are sanitized in error messages ."""
365367 db_url = (
366368 "postgresql+asyncpg://sessions:8dnL1i3eo4GtqwUpKKhNVA@"
367369 "/agent_sessions?host=/cloudsql/project:region:instance"
368370 )
369371 mock_settings = MagicMock ()
372+ mock_settings .session_backend = "database"
370373 mock_settings .session_database_url = db_url
371374
372375 error_msg = (
@@ -376,6 +379,10 @@ def test_credentials_not_leaked_on_init_failure(self, caplog):
376379 )
377380
378381 with (
382+ pytest .raises (
383+ RuntimeError ,
384+ match = r"Failed to initialize DatabaseSessionService" ,
385+ ) as exc_info ,
379386 patch (
380387 "lightspeed_agent.api.a2a.a2a_setup.get_settings" ,
381388 return_value = mock_settings ,
@@ -384,25 +391,41 @@ def test_credentials_not_leaked_on_init_failure(self, caplog):
384391 "google.adk.sessions.DatabaseSessionService" ,
385392 side_effect = RuntimeError (error_msg ),
386393 ),
387- caplog .at_level (logging .WARNING ),
394+ ):
395+ _get_session_service ()
396+
397+ # Password must not appear in the raised error
398+ assert "8dnL1i3eo4GtqwUpKKhNVA" not in str (exc_info .value )
399+
400+ # But the sanitized URL structure should still be present for debugging
401+ assert "://***@" in str (exc_info .value )
402+
403+ def test_memory_backend_used_when_configured (self , caplog ):
404+ """Test that InMemorySessionService is used when SESSION_BACKEND=memory."""
405+ mock_settings = MagicMock ()
406+ mock_settings .session_backend = "memory"
407+
408+ with (
409+ patch (
410+ "lightspeed_agent.api.a2a.a2a_setup.get_settings" ,
411+ return_value = mock_settings ,
412+ ),
413+ caplog .at_level (logging .INFO ),
388414 ):
389415 service = _get_session_service ()
390416
391- # Should fall back to InMemorySessionService
392417 from google .adk .sessions import InMemorySessionService
393418
394419 assert isinstance (service , InMemorySessionService )
420+ assert "InMemorySessionService" in caplog .text
395421
396- # Password must not appear in logs
397- assert "8dnL1i3eo4GtqwUpKKhNVA" not in caplog .text
398-
399- # But the sanitized URL structure should still be present for debugging
400- assert "://***@" in caplog .text
401-
402- def test_fallback_to_inmemory_when_no_url (self , caplog ):
403- """Test that InMemorySessionService is used when no session URL is set."""
422+ def test_memory_backend_ignores_database_url (self , caplog ):
423+ """Test that SESSION_BACKEND=memory ignores SESSION_DATABASE_URL."""
404424 mock_settings = MagicMock ()
405- mock_settings .session_database_url = ""
425+ mock_settings .session_backend = "memory"
426+ mock_settings .session_database_url = (
427+ "postgresql+asyncpg://user:pass@host:5432/sessions"
428+ )
406429
407430 with (
408431 patch (
@@ -417,3 +440,29 @@ def test_fallback_to_inmemory_when_no_url(self, caplog):
417440
418441 assert isinstance (service , InMemorySessionService )
419442 assert "InMemorySessionService" in caplog .text
443+
444+ def test_database_backend_returns_database_service (self ):
445+ """Test that SESSION_BACKEND=database returns DatabaseSessionService."""
446+ mock_settings = MagicMock ()
447+ mock_settings .session_backend = "database"
448+ mock_settings .session_database_url = (
449+ "postgresql+asyncpg://user:pass@host:5432/sessions"
450+ )
451+
452+ mock_db_session = MagicMock ()
453+
454+ with (
455+ patch (
456+ "lightspeed_agent.api.a2a.a2a_setup.get_settings" ,
457+ return_value = mock_settings ,
458+ ),
459+ patch (
460+ "google.adk.sessions.DatabaseSessionService" ,
461+ mock_db_session ,
462+ ),
463+ ):
464+ _get_session_service ()
465+
466+ mock_db_session .assert_called_once_with (
467+ db_url = "postgresql+asyncpg://user:pass@host:5432/sessions"
468+ )
0 commit comments