function test_session_detection
A comprehensive test function that validates session detection capabilities from multiple sources including filenames, PDF files, and text patterns.
/tf/active/vicechatdev/e-ink-llm/test_session_detection.py
10 - 83
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
pathlibsystempfilesession_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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class SessionDetector 74.4% similar
-
function test_session_manager 72.1% similar
-
function test_pdf_session_integration 71.1% similar
-
function detect_session_from_file 68.7% similar
-
function test_document_extractor 63.8% similar