๐Ÿ” Code Extractor

function test_session_detection

Maturity: 46

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

File:
/tf/active/vicechatdev/e-ink-llm/test_session_detection.py
Lines:
10 - 83
Complexity:
moderate

Purpose

This test function validates the SessionDetector class by testing three key scenarios: (1) detecting session information from standardized filenames with conversation IDs and exchange numbers, (2) extracting session metadata from actual PDF files, and (3) recognizing session patterns in text strings. It provides detailed console output with emojis for visual feedback on test results, making it useful for debugging and verifying the session detection system's functionality.

Source Code

def test_session_detection():
    """Test session detection from various sources"""
    print("๐Ÿงช Testing Automatic Session Detection")
    print("=" * 60)
    
    detector = SessionDetector()
    
    # Test 1: Filename detection
    print("\n๐ŸŽฏ Test 1: Filename Detection")
    test_filenames = [
        "RESPONSE_conv_20250731_224420_6a63a783_ex001_test_file.pdf",
        "ERROR_conv_20250731_143022_a8f9c2d1_ex005_another_file.pdf",
        "random_document.pdf",
        "conv_20250730_120000_12345678_notes.pdf"
    ]
    
    for filename in test_filenames:
        result = detector._detect_from_filename(Path(filename))
        if result:
            print(f"   โœ… {filename}")
            print(f"      ๐Ÿ†” {result.conversation_id} | #๏ธโƒฃ {result.exchange_number} | ๐ŸŽฏ {result.confidence:.2f}")
        else:
            print(f"   โŒ {filename} - No session detected")
    
    # Test 2: Check if we can detect from the actual PDF we generated earlier
    print(f"\n๐ŸŽฏ Test 2: Real PDF Detection")
    
    # Look for the PDF we generated in our previous test
    pdf_candidates = [
        "../RESPONSE_conv_20250731_224420_6a63a783_ex001_test_session_sample.pdf",
        "ERROR_conv_20250731_224420_6a63a783_ex002_test_followup.pdf",
        "ERROR_conv_20250731_224420_6a63a783_ex003_test_compact_notes.pdf"
    ]
    
    for pdf_path in pdf_candidates:
        if Path(pdf_path).exists():
            print(f"\n   ๐Ÿ“„ Testing: {Path(pdf_path).name}")
            result = detect_session_from_file(pdf_path)
            if result:
                print(f"   โœ… Detected Session:")
                print(f"      ๐Ÿ†” Conversation: {result.conversation_id}")
                print(f"      #๏ธโƒฃ  Exchange: {result.exchange_number}")
                print(f"      ๐ŸŽฏ Confidence: {result.confidence:.2f}")
                print(f"      ๐Ÿ“ Source: {result.source}")
                print(f"      โžก๏ธ  Next exchange: {detector.get_next_exchange_number(result.conversation_id, result.exchange_number)}")
            else:
                print(f"   โŒ No session information detected")
        else:
            print(f"   โš ๏ธ  File not found: {pdf_path}")
    
    # Test 3: Pattern matching
    print(f"\n๐ŸŽฏ Test 3: Pattern Recognition")
    test_texts = [
        "Session: conv_20250731_224420_6a63a783 | Exchange #1",
        "conversation conv_20250730_120000_abcd1234 exchange 5",
        "Random text with no session info",
        "conv_20250731_143022_a8f9c2d1 ex3 test"
    ]
    
    for text in test_texts:
        # Test footer pattern
        footer_match = detector.session_footer_pattern.search(text)
        conv_match = detector.conv_id_pattern.search(text)
        ex_match = detector.exchange_pattern.search(text)
        
        print(f"   ๐Ÿ“ Text: '{text}'")
        if footer_match:
            print(f"      โœ… Footer pattern: {footer_match.group(1)} | Exchange #{footer_match.group(2)}")
        elif conv_match and ex_match:
            print(f"      โœ… Partial match: {conv_match.group(0)} | Exchange #{ex_match.group(1)}")
        else:
            print(f"      โŒ No patterns detected")
    
    print(f"\n๐ŸŽ‰ Session Detection Tests Complete!")

Return Value

This function does not return any value (implicitly returns None). It performs tests and outputs results directly to the console using print statements, displaying success/failure indicators and extracted session information.

Dependencies

  • pathlib
  • sys
  • tempfile
  • session_detector

Required Imports

import sys
import tempfile
from pathlib import Path
from session_detector import SessionDetector
from session_detector import detect_session_from_file

Usage Example

# Run the test function directly
test_session_detection()

# Expected output includes:
# - Test results for filename pattern detection
# - Session information extracted from PDF files
# - Pattern matching results for various text formats
# - Visual indicators (โœ…/โŒ) showing success/failure of each test

# Example output:
# ๐Ÿงช Testing Automatic Session Detection
# ============================================================
# 
# ๐ŸŽฏ Test 1: Filename Detection
#    โœ… RESPONSE_conv_20250731_224420_6a63a783_ex001_test_file.pdf
#       ๐Ÿ†” conv_20250731_224420_6a63a783 | #๏ธโƒฃ 1 | ๐ŸŽฏ 0.95

Best Practices

  • This is a test function meant to be run standalone, not imported for reuse in production code
  • Ensure the session_detector module is properly installed and accessible before running
  • The function expects specific PDF files to exist for Test 2; missing files will be reported but won't cause the test to fail
  • Test filenames follow a specific pattern: PREFIX_conv_YYYYMMDD_HHMMSS_HASH_exNNN_description.pdf
  • The function uses console output for feedback; redirect stdout if you need to capture results programmatically
  • Consider running this in an environment where the relative paths to test PDFs are valid
  • The SessionDetector class must implement specific methods and attributes for this test to work correctly
  • This test function is primarily for development and debugging purposes, not for automated CI/CD pipelines without modification

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class SessionDetector 74.4% similar

    Detects session information (conversation ID and exchange number) from PDF files using multiple detection methods including metadata, filename, footer, and content analysis.

    From: /tf/active/vicechatdev/e-ink-llm/session_detector.py
  • function test_session_manager 72.1% similar

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

    From: /tf/active/vicechatdev/e-ink-llm/test_improvements.py
  • function test_pdf_session_integration 71.1% 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 detect_session_from_file 68.7% similar

    Detects session information from a file by analyzing its content (for PDFs) or filename, returning structured session metadata if found.

    From: /tf/active/vicechatdev/e-ink-llm/session_detector.py
  • function test_document_extractor 63.8% similar

    A test function that validates the DocumentExtractor class by testing file type support detection, text extraction from various document formats, and error handling.

    From: /tf/active/vicechatdev/leexi/test_document_extractor.py
โ† Back to Browse