#!/usr/bin/env python3 """ Test script to verify profile session restoration fix. Tests the ensure_profile_session and get_or_restore_profile_session functions. """ import sys import os # Add webapp src to path sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src")) from services.session_helper import ensure_profile_session, get_or_restore_profile_session def test_cache_hit(): """Test that cached session ID is returned without API call""" print("Test 1: Cache hit (should return cached ID without API call)") user_id = "test-user" cached_id = "cached-session-123" result = get_or_restore_profile_session(user_id, cached_id) assert result == cached_id, f"Expected {cached_id}, got {result}" print("✓ Cache hit test passed") def test_cache_miss_explanation(): """Explain what happens on cache miss (requires API to be running)""" print("\nTest 2: Cache miss (would query backend API)") print("When cached_session_id is None, the function will:") print(" 1. Call backend_api.list_sessions(user_id=user_id)") print(" 2. Search for session with contact_id='user-profile'") print(" 3. If found, return existing session_id") print(" 4. If not found, create new session via backend_api.create_session()") print("✓ Cache miss behavior documented") if __name__ == "__main__": print("Profile Session Restoration Fix - Unit Tests") print("=" * 60) test_cache_hit() test_cache_miss_explanation() print("\n" + "=" * 60) print("All tests passed! ✓") print("\nTo test with live backend API:") print(" 1. Ensure API is running: docker compose up -d api") print(" 2. Ensure backend has profile session for test user") print(" 3. Run webapp and trigger message/fact without cached profile_session_id")