šŸ” Code Extractor

function test_session_manager

Maturity: 44

A comprehensive test function that validates the SessionManager class functionality including conversation creation, exchange tracking, filename generation, and context retrieval.

File:
/tf/active/vicechatdev/e-ink-llm/test_improvements.py
Lines:
63 - 134
Complexity:
moderate

Purpose

This test function serves as both a validation suite and usage example for the SessionManager class. It tests core functionality including: creating conversations, adding exchanges with metadata, generating session filenames, retrieving conversation data, generating conversation context, and listing active conversations. The function creates a temporary test database, performs all operations, and cleans up afterward.

Source Code

def test_session_manager():
    """Test the session manager"""
    print("\nšŸ—‚ļø  Testing Session Manager")
    print("=" * 50)
    
    # Clean up any existing test database
    test_db = Path("test_sessions.db")
    if test_db.exists():
        test_db.unlink()
    
    session_manager = SessionManager(str(test_db))
    
    # Create a new conversation
    conv_id = session_manager.create_conversation(user_id="test_user")
    print(f"Created conversation: {conv_id}")
    
    # Add some exchanges
    exchange1_id = session_manager.add_exchange(
        conversation_id=conv_id,
        input_file="math_problem.pdf",
        input_type=".pdf",
        response_text="šŸŽÆ Quadratic equation\nšŸ“ x² + 5x + 6 = 0 → (x+3)(x+2) = 0\nāœ… x = -3, x = -2",
        processing_time=2.5,
        tokens_used=150,
        metadata={"compact_mode": True}
    )
    
    exchange2_id = session_manager.add_exchange(
        conversation_id=conv_id,
        input_file="followup_question.pdf", 
        input_type=".pdf",
        response_text="šŸŽÆ Graph analysis\nšŸ“Š Parabola opens upward\nšŸ“ Vertex: (-2.5, -0.25)",
        processing_time=1.8,
        tokens_used=120,
        metadata={"compact_mode": True}
    )
    
    print(f"Added exchange 1: {exchange1_id}")
    print(f"Added exchange 2: {exchange2_id}")
    
    # Test filename generation
    filename1 = session_manager.generate_session_filename(conv_id, 1, "math_problem.pdf")
    filename2 = session_manager.generate_session_filename(conv_id, 2, "followup_question.pdf")
    filename_error = session_manager.generate_session_filename(conv_id, 3, "error_test.pdf", is_error=True)
    
    print(f"\nGenerated filenames:")
    print(f"  Exchange 1: {filename1}")
    print(f"  Exchange 2: {filename2}")
    print(f"  Error file: {filename_error}")
    
    # Test conversation retrieval
    conversation = session_manager.get_conversation(conv_id)
    print(f"\nConversation summary:")
    print(f"  ID: {conversation.conversation_id}")
    print(f"  User: {conversation.user_id}")
    print(f"  Exchanges: {conversation.total_exchanges}")
    print(f"  Status: {conversation.status}")
    
    # Test context generation
    context = session_manager.get_conversation_context(conv_id)
    print(f"\nConversation context:")
    print(context)
    
    # List conversations
    conversations = session_manager.list_active_conversations()
    print(f"\nActive conversations: {len(conversations)}")
    for conv in conversations:
        print(f"  {conv['conversation_id']} - {conv['total_exchanges']} exchanges")
    
    # Clean up
    test_db.unlink()
    print("\nāœ… Session manager tests completed")

Return Value

This function does not return any value (implicitly returns None). It performs side effects by printing test results to stdout and creating/deleting a test database file.

Dependencies

  • pathlib
  • asyncio
  • compact_formatter
  • session_manager

Required Imports

from pathlib import Path
from session_manager import SessionManager

Usage Example

# Direct execution
from pathlib import Path
from session_manager import SessionManager

def test_session_manager():
    # Function implementation here
    pass

# Run the test
test_session_manager()

# Or as part of a test suite
if __name__ == '__main__':
    test_session_manager()

Best Practices

  • Always clean up test database files before and after running tests to avoid state pollution
  • Use descriptive test data that clearly demonstrates the functionality being tested
  • Print clear progress indicators and results for manual test verification
  • Test both normal operations and edge cases (like error filename generation)
  • Ensure test database uses a distinct name (test_sessions.db) to avoid conflicts with production data
  • Verify cleanup occurs even if tests fail by considering try-finally blocks in production test code
  • This function is designed for manual testing and demonstration; for automated testing, consider using pytest or unittest framework with assertions instead of print statements

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_session_detection 72.1% similar

    A comprehensive test function that validates session detection capabilities from multiple sources including filenames, PDF files, and text patterns.

    From: /tf/active/vicechatdev/e-ink-llm/test_session_detection.py
  • class SessionManager_v1 71.9% similar

    SessionManager is a class that manages conversation sessions and tracking using SQLite database, storing conversations and their exchanges with metadata.

    From: /tf/active/vicechatdev/e-ink-llm/session_manager.py
  • function test_pdf_session_integration 68.2% similar

    Integration test function that verifies PDF generation includes session tracking information by creating both response and error PDFs with conversation IDs and exchange numbers.

    From: /tf/active/vicechatdev/e-ink-llm/test_pdf_session.py
  • function test_data_analysis_service 63.6% similar

    A test function that validates the functionality of the DataAnalysisService by creating, initializing, and retrieving analysis sessions.

    From: /tf/active/vicechatdev/vice_ai/test_integration.py
  • function test_auto_continuation_workflow 59.7% similar

    Comprehensive test function that validates the auto-continuation workflow for multi-turn conversations, including session detection from PDFs, conversation context preservation, and automatic conversation switching.

    From: /tf/active/vicechatdev/e-ink-llm/test_auto_continuation.py
← Back to Browse