Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Test script for Enhanced Provider Summary Generator. | |
| This script validates the enhanced provider summary generation functionality including: | |
| - Complete provider summary generation with all required fields | |
| - Structured summary validation and completeness checking | |
| - Triage context inclusion and conversation background extraction | |
| - Integration with context-aware classification results | |
| """ | |
| import sys | |
| import os | |
| sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'src')) | |
| from datetime import datetime, timedelta | |
| from core.provider_summary_generator import ProviderSummaryGenerator, ProviderSummary | |
| def test_enhanced_provider_summary_generation(): | |
| """Test the enhanced provider summary generation.""" | |
| print("Testing Enhanced Provider Summary Generation...") | |
| generator = ProviderSummaryGenerator() | |
| # Test 1: Complete summary with all fields | |
| print("\n1. Testing complete summary generation...") | |
| summary = generator.generate_summary( | |
| indicators=['hopelessness', 'suicidal ideation', 'spiritual distress'], | |
| reasoning="Patient expressing severe spiritual distress with suicidal ideation. Immediate intervention required.", | |
| confidence=0.95, | |
| patient_name="John Doe", | |
| patient_phone="555-123-4567", | |
| patient_email="john.doe@email.com", | |
| emergency_contact="Jane Doe (spouse) - 555-987-6543", | |
| triage_questions=[ | |
| "Can you tell me more about these feelings?", | |
| "Do you have thoughts of harming yourself?", | |
| "What support do you have available?" | |
| ], | |
| triage_responses=[ | |
| "I feel like there's no point in going on", | |
| "Yes, I've been thinking about ending it all", | |
| "I don't really have anyone to talk to" | |
| ], | |
| conversation_context="Patient initially mentioned feeling overwhelmed, then revealed deeper spiritual crisis and suicidal thoughts during follow-up questioning.", | |
| conversation_history=[ | |
| {"message": "I'm feeling overwhelmed with my treatment", "classification": "YELLOW"}, | |
| {"message": "I don't think God cares about me anymore", "classification": "RED"}, | |
| {"message": "Sometimes I think about ending it all", "classification": "RED"} | |
| ], | |
| medical_context={ | |
| "conditions": ["terminal cancer", "depression"], | |
| "medications": ["morphine", "antidepressant"] | |
| }, | |
| context_factors=["escalating_distress", "medical_context_relevant"], | |
| defensive_patterns_detected=False | |
| ) | |
| print(f" Generated summary for: {summary.patient_name}") | |
| print(f" Classification: {summary.classification} ({summary.confidence:.0%})") | |
| print(f" Severity: {summary.severity_level}, Urgency: {summary.urgency_level}") | |
| print(f" Indicators: {len(summary.indicators)}") | |
| print(f" Triage exchanges: {len(summary.triage_context)}") | |
| print(f" Recommended actions: {len(summary.recommended_actions)}") | |
| # Validate completeness | |
| validation_issues = summary.validate_completeness() | |
| if validation_issues: | |
| print(f" β Validation issues: {validation_issues}") | |
| return False | |
| else: | |
| print(" β Summary validation passed") | |
| # Test 2: Summary with missing information | |
| print("\n2. Testing summary with missing information...") | |
| incomplete_summary = generator.generate_summary( | |
| indicators=[], # No indicators | |
| reasoning="", # No reasoning | |
| confidence=0.8, | |
| patient_name=None, # Missing name | |
| patient_phone=None # Missing phone | |
| ) | |
| validation_issues = incomplete_summary.validate_completeness() | |
| print(f" Validation issues found: {len(validation_issues)}") | |
| for issue in validation_issues: | |
| print(f" - {issue}") | |
| # Should still generate a summary with placeholders | |
| assert incomplete_summary.patient_name == "[Patient Name]", "Should use placeholder for missing name" | |
| assert incomplete_summary.patient_phone == "[Phone Number]", "Should use placeholder for missing phone" | |
| print(" β Graceful handling of missing information") | |
| # Test 3: Severity and urgency determination | |
| print("\n3. Testing severity and urgency determination...") | |
| test_cases = [ | |
| { | |
| "confidence": 0.95, | |
| "indicators": ["suicidal ideation", "hopelessness"], | |
| "expected_severity": "CRITICAL", | |
| "expected_urgency": "IMMEDIATE" | |
| }, | |
| { | |
| "confidence": 0.8, | |
| "indicators": ["anxiety", "spiritual distress"], | |
| "expected_severity": "HIGH", | |
| "expected_urgency": "URGENT" | |
| }, | |
| { | |
| "confidence": 0.6, | |
| "indicators": ["mild worry"], | |
| "expected_severity": "MODERATE", | |
| "expected_urgency": "STANDARD" | |
| } | |
| ] | |
| for i, case in enumerate(test_cases, 1): | |
| test_summary = generator.generate_summary( | |
| indicators=case["indicators"], | |
| reasoning="Test case reasoning", | |
| confidence=case["confidence"], | |
| patient_name="Test Patient", | |
| patient_phone="555-0000" | |
| ) | |
| print(f" Case {i}: Confidence {case['confidence']:.0%}") | |
| print(f" Expected: {case['expected_severity']}/{case['expected_urgency']}") | |
| print(f" Actual: {test_summary.severity_level}/{test_summary.urgency_level}") | |
| assert test_summary.severity_level == case["expected_severity"], \ | |
| f"Expected severity {case['expected_severity']}, got {test_summary.severity_level}" | |
| assert test_summary.urgency_level == case["expected_urgency"], \ | |
| f"Expected urgency {case['expected_urgency']}, got {test_summary.urgency_level}" | |
| print(" β Severity and urgency determination working correctly") | |
| return True | |
| def test_provider_summary_formatting(): | |
| """Test provider summary formatting for display and export.""" | |
| print("\nTesting Provider Summary Formatting...") | |
| generator = ProviderSummaryGenerator() | |
| # Create a comprehensive test summary | |
| summary = generator.generate_summary( | |
| indicators=['spiritual crisis', 'family conflict', 'loss of faith'], | |
| reasoning="Patient experiencing significant spiritual distress following family conflict and questioning of faith beliefs.", | |
| confidence=0.85, | |
| patient_name="Sarah Johnson", | |
| patient_phone="555-456-7890", | |
| patient_email="sarah.j@email.com", | |
| triage_questions=["How has this affected your relationship with your faith?"], | |
| triage_responses=["I don't know if I believe in God anymore"], | |
| conversation_context="Patient discussed ongoing family issues and spiritual questioning.", | |
| medical_context={"conditions": ["chronic illness"], "medications": ["pain medication"]}, | |
| context_factors=["family_conflict", "spiritual_questioning"], | |
| defensive_patterns_detected=True | |
| ) | |
| # Test display formatting | |
| print("\n1. Testing display formatting...") | |
| display_format = generator.format_for_display(summary) | |
| # Check for required sections | |
| required_sections = [ | |
| "PROVIDER SUMMARY", | |
| "PATIENT INFORMATION", | |
| "CLASSIFICATION & URGENCY", | |
| "SITUATION OVERVIEW", | |
| "DISTRESS INDICATORS", | |
| "CLINICAL REASONING", | |
| "RECOMMENDED ACTIONS" | |
| ] | |
| for section in required_sections: | |
| if section in display_format: | |
| print(f" β Found {section}") | |
| else: | |
| print(f" β Missing {section}") | |
| return False | |
| # Check for patient information | |
| assert "Sarah Johnson" in display_format, "Should show patient name" | |
| assert "555-456-7890" in display_format, "Should show patient phone" | |
| assert "sarah.j@email.com" in display_format, "Should show patient email" | |
| # Check for defensive patterns warning | |
| assert "Defensive response patterns detected" in display_format, "Should show defensive patterns warning" | |
| print(" β Display formatting includes all required information") | |
| # Test export formatting | |
| print("\n2. Testing export formatting...") | |
| export_format = generator.format_for_export(summary) | |
| # Should be single line | |
| assert '\n' not in export_format, "Export should be single line" | |
| # Should contain key information | |
| assert "Sarah Johnson" in export_format, "Export should include patient name" | |
| assert "555-456-7890" in export_format, "Export should include phone" | |
| assert "RED" in export_format, "Export should show classification" | |
| assert "HIGH" in export_format, "Export should show severity" | |
| # Should use pipe separators | |
| assert '|' in export_format, "Export should use pipe separators" | |
| print(f" Export format length: {len(export_format)} characters") | |
| print(" β Export formatting working correctly") | |
| return True | |
| def test_triage_context_inclusion(): | |
| """Test triage context inclusion and conversation background extraction.""" | |
| print("\nTesting Triage Context Inclusion...") | |
| generator = ProviderSummaryGenerator() | |
| # Test with comprehensive triage context | |
| triage_questions = [ | |
| "Can you tell me more about what you're experiencing?", | |
| "How long have you been feeling this way?", | |
| "What kind of support do you have available?", | |
| "Have you had thoughts of harming yourself?" | |
| ] | |
| triage_responses = [ | |
| "I feel completely lost and abandoned by God", | |
| "It's been getting worse over the past few weeks", | |
| "I don't really have anyone I can talk to about this", | |
| "Yes, I've been thinking it would be easier if I wasn't here" | |
| ] | |
| conversation_history = [ | |
| {"message": "I'm struggling with my faith", "classification": "YELLOW"}, | |
| {"message": "I feel like God has abandoned me", "classification": "RED"}, | |
| {"message": "I don't see the point in continuing", "classification": "RED"} | |
| ] | |
| summary = generator.generate_summary( | |
| indicators=['spiritual abandonment', 'suicidal ideation', 'social isolation'], | |
| reasoning="Progressive spiritual crisis with suicidal ideation requiring immediate intervention.", | |
| confidence=0.9, | |
| patient_name="Michael Chen", | |
| patient_phone="555-789-0123", | |
| triage_questions=triage_questions, | |
| triage_responses=triage_responses, | |
| conversation_context="Patient revealed escalating spiritual crisis through targeted questioning.", | |
| conversation_history=conversation_history, | |
| context_factors=["escalating_distress", "spiritual_crisis"] | |
| ) | |
| # Test triage context inclusion | |
| print(f"\n1. Triage context validation...") | |
| print(f" Questions asked: {len(summary.triage_context)}") | |
| assert len(summary.triage_context) == 4, "Should include all triage exchanges" | |
| for i, exchange in enumerate(summary.triage_context): | |
| assert 'question' in exchange, "Should include question" | |
| assert 'response' in exchange, "Should include response" | |
| assert 'timestamp' in exchange, "Should include timestamp" | |
| assert exchange['question'] == triage_questions[i], "Should preserve original question" | |
| assert exchange['response'] == triage_responses[i], "Should preserve original response" | |
| print(" β All triage exchanges properly included") | |
| # Test conversation history summary | |
| print(f"\n2. Conversation history analysis...") | |
| print(f" History summary: {summary.conversation_history_summary}") | |
| assert len(summary.conversation_history_summary) > 0, "Should generate conversation summary" | |
| assert "3 exchanges" in summary.conversation_history_summary, "Should mention conversation length" | |
| # Should reflect escalating pattern | |
| if 'escalating_distress' in summary.context_factors: | |
| assert "escalating" in summary.conversation_history_summary.lower(), \ | |
| "Should mention escalating pattern" | |
| print(" β Conversation history properly analyzed") | |
| # Test display includes triage information | |
| display_format = generator.format_for_display(summary) | |
| assert "TRIAGE EXCHANGES" in display_format, "Display should include triage section" | |
| for question in triage_questions: | |
| assert question in display_format, f"Should show triage question: {question}" | |
| for response in triage_responses: | |
| assert response in display_format, f"Should show triage response: {response}" | |
| print(" β Triage context properly displayed") | |
| return True | |
| def test_integration_with_context_aware_classification(): | |
| """Test integration with context-aware classification results.""" | |
| print("\nTesting Integration with Context-Aware Classification...") | |
| try: | |
| from config.prompt_management.context_aware_classifier import ContextAwareClassifier | |
| from config.prompt_management.data_models import ConversationHistory, Message | |
| classifier = ContextAwareClassifier() | |
| generator = ProviderSummaryGenerator() | |
| # Create conversation scenario | |
| history = ConversationHistory( | |
| messages=[ | |
| Message("I'm worried about my treatment", "YELLOW", datetime.now() - timedelta(hours=2)), | |
| Message("I feel like God doesn't care about me", "RED", datetime.now() - timedelta(hours=1)), | |
| Message("I'm fine, don't worry about me", "YELLOW", datetime.now() - timedelta(minutes=30)) | |
| ], | |
| distress_indicators_found=['worry', 'spiritual_abandonment', 'defensive'], | |
| context_flags=['defensive_response_pattern'], | |
| medical_context={'conditions': ['cancer'], 'medications': ['chemotherapy']} | |
| ) | |
| # Classify current message | |
| current_message = "Everything is okay, I'm handling it well" | |
| classification_result = classifier.classify_with_context(current_message, history) | |
| print(f" Classification: {classification_result.category} ({classification_result.confidence:.2f})") | |
| print(f" Context factors: {classification_result.context_factors}") | |
| # Generate provider summary using classification results | |
| summary = generator.generate_summary( | |
| indicators=classification_result.indicators_found, | |
| reasoning=classification_result.reasoning, | |
| confidence=classification_result.confidence, | |
| patient_name="Integration Test Patient", | |
| patient_phone="555-TEST-123", | |
| conversation_context=f"Current message: {current_message}", | |
| conversation_history=[msg.to_dict() for msg in history.messages], | |
| medical_context=history.medical_context, | |
| context_factors=classification_result.context_factors, | |
| defensive_patterns_detected='defensive_response_pattern' in classification_result.context_factors | |
| ) | |
| print(f" Summary severity: {summary.severity_level}") | |
| print(f" Defensive patterns detected: {summary.defensive_patterns_detected}") | |
| # Validate integration | |
| assert summary.classification == "RED", "Should maintain RED classification" | |
| assert summary.confidence == classification_result.confidence, "Should preserve confidence" | |
| assert summary.reasoning == classification_result.reasoning, "Should use classification reasoning" | |
| if 'defensive_response_pattern' in classification_result.context_factors: | |
| assert summary.defensive_patterns_detected == True, "Should detect defensive patterns" | |
| print(" β Integration with context-aware classification working") | |
| except ImportError: | |
| print(" β οΈ Context-aware classifier not available, skipping integration test") | |
| return True | |
| def main(): | |
| """Run all enhanced provider summary tests.""" | |
| print("=" * 70) | |
| print("ENHANCED PROVIDER SUMMARY GENERATOR TEST SUITE") | |
| print("=" * 70) | |
| try: | |
| # Run tests | |
| if not test_enhanced_provider_summary_generation(): | |
| return False | |
| if not test_provider_summary_formatting(): | |
| return False | |
| if not test_triage_context_inclusion(): | |
| return False | |
| if not test_integration_with_context_aware_classification(): | |
| return False | |
| print("\n" + "=" * 70) | |
| print("β ALL ENHANCED PROVIDER SUMMARY TESTS PASSED!") | |
| print("=" * 70) | |
| print("IMPLEMENTED FEATURES:") | |
| print("β Complete provider summary generation with all required fields") | |
| print("β Enhanced data model with contact validation and completeness checking") | |
| print("β Comprehensive triage context inclusion and conversation analysis") | |
| print("β Severity and urgency level determination") | |
| print("β Defensive pattern detection and handling") | |
| print("β Medical context integration") | |
| print("β Recommended actions generation based on assessment") | |
| print("β Enhanced display and export formatting") | |
| print("β Integration with context-aware classification") | |
| print("β Validation and completeness checking") | |
| print("=" * 70) | |
| return True | |
| except Exception as e: | |
| print(f"\nβ ENHANCED PROVIDER SUMMARY TEST FAILED: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| if __name__ == "__main__": | |
| success = main() | |
| sys.exit(0 if success else 1) |