Spaces:
Sleeping
Sleeping
| # test_enhanced_results_display.py | |
| """ | |
| Unit tests for Enhanced Results Display Manager. | |
| Tests the core functionality of the enhanced results display system | |
| including section formatting, visual separation, and provider summary formatting. | |
| """ | |
| import pytest | |
| from unittest.mock import Mock | |
| from datetime import datetime | |
| from src.interface.enhanced_results_display_manager import ( | |
| EnhancedResultsDisplayManager, | |
| EnhancedDisplayConfig, | |
| ContentSection, | |
| SectionType | |
| ) | |
| from src.interface.visual_separation_manager import ( | |
| VisualSeparationManager, | |
| ContentType, | |
| VisualStyle | |
| ) | |
| from src.interface.provider_summary_formatter import ( | |
| ProviderSummaryFormatter, | |
| PatientData, | |
| ClassificationData | |
| ) | |
| from src.core.provider_summary_generator import ProviderSummary | |
| class TestEnhancedResultsDisplayManager: | |
| """Test cases for EnhancedResultsDisplayManager.""" | |
| def setup_method(self): | |
| """Set up test fixtures.""" | |
| self.display_manager = EnhancedResultsDisplayManager() | |
| self.config = EnhancedDisplayConfig() | |
| def test_initialization_with_default_config(self): | |
| """Test initialization with default configuration.""" | |
| manager = EnhancedResultsDisplayManager() | |
| assert manager.config is not None | |
| assert manager.config.ai_analysis_icon == "π€" | |
| assert manager.config.patient_message_icon == "π¬" | |
| assert manager.config.provider_summary_icon == "π" | |
| def test_initialization_with_custom_config(self): | |
| """Test initialization with custom configuration.""" | |
| custom_config = EnhancedDisplayConfig( | |
| ai_analysis_icon="π", | |
| patient_message_icon="π", | |
| use_color_coding=False | |
| ) | |
| manager = EnhancedResultsDisplayManager(custom_config) | |
| assert manager.config.ai_analysis_icon == "π" | |
| assert manager.config.patient_message_icon == "π" | |
| assert manager.config.use_color_coding is False | |
| def test_format_ai_analysis_section_basic(self): | |
| """Test basic AI analysis section formatting.""" | |
| result = self.display_manager.format_ai_analysis_section( | |
| classification="RED", | |
| indicators=["Loss of meaning", "Spiritual distress"], | |
| reasoning="Patient expressed existential concerns" | |
| ) | |
| assert "AI Analysis - RED FLAG" in result | |
| assert "Loss of meaning" in result | |
| assert "Spiritual distress" in result | |
| assert "Patient expressed existential concerns" in result | |
| assert self.config.ai_analysis_icon in result | |
| def test_format_ai_analysis_section_with_confidence(self): | |
| """Test AI analysis section formatting with confidence score.""" | |
| result = self.display_manager.format_ai_analysis_section( | |
| classification="YELLOW", | |
| indicators=["Mild distress"], | |
| reasoning="Unclear indicators", | |
| confidence=0.75 | |
| ) | |
| assert "AI Analysis - YELLOW FLAG" in result | |
| assert "Confidence: 75%" in result | |
| assert "Mild distress" in result | |
| def test_format_ai_analysis_section_no_indicators(self): | |
| """Test AI analysis section formatting with no indicators.""" | |
| result = self.display_manager.format_ai_analysis_section( | |
| classification="GREEN", | |
| indicators=[], | |
| reasoning="Normal conversation" | |
| ) | |
| assert "AI Analysis - GREEN FLAG" in result | |
| assert "Normal conversation" in result | |
| # Should not contain indicators section when empty | |
| assert "Indicators:" not in result | |
| def test_format_patient_message_section(self): | |
| """Test patient message section formatting.""" | |
| message = "I'm feeling lost and don't know what to do." | |
| result = self.display_manager.format_patient_message_section(message) | |
| assert "Patient Message" in result | |
| # Check for HTML-escaped version of the message | |
| assert "I'm feeling lost and don't know what to do." in result | |
| assert self.config.patient_message_icon in result | |
| def test_format_patient_message_section_html_escaping(self): | |
| """Test patient message section with HTML characters.""" | |
| message = "I feel <confused> & don't know what's \"right\"" | |
| result = self.display_manager.format_patient_message_section(message) | |
| assert "<confused>" in result | |
| assert "&" in result | |
| assert ""right"" in result | |
| def test_format_provider_summary_section(self): | |
| """Test provider summary section formatting.""" | |
| # Create mock provider summary | |
| summary = Mock(spec=ProviderSummary) | |
| summary.patient_name = "John Doe" | |
| summary.patient_phone = "(555) 123-4567" | |
| summary.urgency_level = "URGENT" | |
| summary.follow_up_timeline = "Within 24 hours" | |
| summary.situation_description = "Patient experiencing spiritual crisis" | |
| summary.indicators = ["Loss of faith", "Existential questioning"] | |
| result = self.display_manager.format_provider_summary_section(summary) | |
| assert "Provider Summary" in result | |
| assert "John Doe" in result | |
| assert "(555) 123-4567" in result | |
| assert "URGENT" in result | |
| assert "Loss of faith" in result | |
| assert "Existential questioning" in result | |
| def test_create_visual_separators(self): | |
| """Test visual separator creation.""" | |
| separators = self.display_manager.create_visual_separators() | |
| assert "section_break" in separators | |
| assert "content_divider" in separators | |
| assert "major_break" in separators | |
| # Check that separators contain HTML | |
| for separator in separators.values(): | |
| assert "<div" in separator | |
| def test_apply_section_styling(self): | |
| """Test section styling application.""" | |
| content = "Test content" | |
| # Test AI analysis styling | |
| result = self.display_manager.apply_section_styling(content, SectionType.AI_ANALYSIS) | |
| assert content in result | |
| assert "background-color: #f8f9fa" in result | |
| # Test patient message styling | |
| result = self.display_manager.apply_section_styling(content, SectionType.PATIENT_MESSAGE) | |
| assert content in result | |
| assert "background-color: #f0f7ff" in result | |
| def test_format_combined_results_all_sections(self): | |
| """Test combined results formatting with all sections.""" | |
| ai_analysis = { | |
| "classification": "RED", | |
| "indicators": ["Spiritual crisis"], | |
| "reasoning": "Patient in distress", | |
| "confidence": 0.9 | |
| } | |
| patient_message = "I don't see the point anymore" | |
| provider_summary = Mock(spec=ProviderSummary) | |
| provider_summary.patient_name = "Jane Smith" | |
| provider_summary.patient_phone = "(555) 987-6543" | |
| provider_summary.urgency_level = "IMMEDIATE" | |
| provider_summary.follow_up_timeline = "Immediately" | |
| provider_summary.situation_description = "Crisis situation" | |
| provider_summary.indicators = ["Hopelessness"] | |
| result = self.display_manager.format_combined_results( | |
| ai_analysis=ai_analysis, | |
| patient_message=patient_message, | |
| provider_summary=provider_summary | |
| ) | |
| assert "AI Analysis - RED FLAG" in result | |
| assert "Patient Message" in result | |
| assert "Provider Summary" in result | |
| assert "Jane Smith" in result | |
| # Check for HTML-escaped version of the patient message | |
| assert "I don't see the point anymore" in result | |
| def test_format_combined_results_empty(self): | |
| """Test combined results formatting with no content.""" | |
| result = self.display_manager.format_combined_results() | |
| assert "No content to display" in result | |
| class TestVisualSeparationManager: | |
| """Test cases for VisualSeparationManager.""" | |
| def setup_method(self): | |
| """Set up test fixtures.""" | |
| self.visual_manager = VisualSeparationManager() | |
| def test_initialization(self): | |
| """Test visual separation manager initialization.""" | |
| assert self.visual_manager.styles is not None | |
| assert ContentType.AI_ANALYSIS in self.visual_manager.styles | |
| assert ContentType.PATIENT_MESSAGE in self.visual_manager.styles | |
| assert ContentType.PROVIDER_SUMMARY in self.visual_manager.styles | |
| def test_create_ai_analysis_styling(self): | |
| """Test AI analysis styling creation.""" | |
| styling = self.visual_manager.create_ai_analysis_styling() | |
| assert "container" in styling | |
| assert "header" in styling | |
| assert "icon" in styling | |
| assert "content" in styling | |
| # Check that styling contains CSS properties | |
| assert "border:" in styling["container"] | |
| assert "background-color:" in styling["container"] | |
| def test_create_patient_message_styling(self): | |
| """Test patient message styling creation.""" | |
| styling = self.visual_manager.create_patient_message_styling() | |
| assert "container" in styling | |
| assert "header" in styling | |
| assert "message_box" in styling | |
| # Check for patient message specific colors | |
| assert "#4a90e2" in styling["container"] | |
| def test_create_provider_summary_styling(self): | |
| """Test provider summary styling creation.""" | |
| styling = self.visual_manager.create_provider_summary_styling() | |
| assert "container" in styling | |
| assert "header" in styling | |
| assert "info_box" in styling | |
| assert "urgency_box" in styling | |
| def test_generate_section_separators(self): | |
| """Test section separator generation.""" | |
| separators = self.visual_manager.generate_section_separators() | |
| assert "light" in separators | |
| assert "medium" in separators | |
| assert "heavy" in separators | |
| assert "section_break" in separators | |
| # Check that all separators are HTML | |
| for separator in separators.values(): | |
| assert "<div" in separator | |
| def test_get_classification_styling(self): | |
| """Test classification-specific styling.""" | |
| red_styling = self.visual_manager.get_classification_styling("RED") | |
| yellow_styling = self.visual_manager.get_classification_styling("YELLOW") | |
| green_styling = self.visual_manager.get_classification_styling("GREEN") | |
| assert "#dc3545" in red_styling["badge"] | |
| assert "#ffc107" in yellow_styling["badge"] | |
| assert "#28a745" in green_styling["badge"] | |
| def test_get_urgency_styling(self): | |
| """Test urgency-specific styling.""" | |
| immediate_styling = self.visual_manager.get_urgency_styling("IMMEDIATE") | |
| urgent_styling = self.visual_manager.get_urgency_styling("URGENT") | |
| standard_styling = self.visual_manager.get_urgency_styling("STANDARD") | |
| assert "#dc3545" in immediate_styling["badge"] | |
| assert "#fd7e14" in urgent_styling["badge"] | |
| assert "#28a745" in standard_styling["badge"] | |
| def test_apply_consistent_formatting_multiple_sections(self): | |
| """Test consistent formatting with multiple sections.""" | |
| sections = [ | |
| {"type": "ai_analysis", "content": "AI analysis content"}, | |
| {"type": "patient_message", "content": "Patient message content"}, | |
| {"type": "provider_summary", "content": "Provider summary content"} | |
| ] | |
| result = self.visual_manager.apply_consistent_formatting(sections) | |
| assert "AI analysis content" in result | |
| assert "Patient message content" in result | |
| assert "Provider summary content" in result | |
| # Should contain section separators between sections | |
| assert "---" in result | |
| def test_apply_consistent_formatting_empty_sections(self): | |
| """Test consistent formatting with empty sections list.""" | |
| result = self.visual_manager.apply_consistent_formatting([]) | |
| assert "No content to display" in result | |
| def test_apply_consistent_formatting_single_section(self): | |
| """Test consistent formatting with single section.""" | |
| sections = [ | |
| {"type": "ai_analysis", "content": "Single section content"} | |
| ] | |
| result = self.visual_manager.apply_consistent_formatting(sections) | |
| assert "Single section content" in result | |
| # Should not contain separators for single section | |
| assert result.count("---") == 0 | |
| def test_create_icon_styling(self): | |
| """Test icon styling creation for different content types.""" | |
| ai_styling = self.visual_manager.create_icon_styling(ContentType.AI_ANALYSIS) | |
| patient_styling = self.visual_manager.create_icon_styling(ContentType.PATIENT_MESSAGE) | |
| provider_styling = self.visual_manager.create_icon_styling(ContentType.PROVIDER_SUMMARY) | |
| # All should contain base styling | |
| assert "font-size: 1.2em" in ai_styling | |
| assert "margin-right: 8px" in patient_styling | |
| assert "color:" in provider_styling | |
| class TestProviderSummaryFormatter: | |
| """Test cases for ProviderSummaryFormatter.""" | |
| def setup_method(self): | |
| """Set up test fixtures.""" | |
| self.formatter = ProviderSummaryFormatter() | |
| self.sample_patient_data = PatientData( | |
| name="John Doe", | |
| age=45, | |
| gender="male", | |
| phone="(555) 123-4567", | |
| medical_history=["Diabetes", "Hypertension"], | |
| expressed_concerns=["Loss of faith", "Feeling hopeless"], | |
| patient_input="I don't know what to believe anymore" | |
| ) | |
| self.sample_classification_data = ClassificationData( | |
| classification="RED", | |
| spiritual_concern_type="existential crisis", | |
| consent_given=True | |
| ) | |
| def test_build_demographic_section(self): | |
| """Test demographic section building.""" | |
| result = self.formatter.build_demographic_section(self.sample_patient_data) | |
| assert "John Doe is a 45-year-old male" == result | |
| def test_build_demographic_section_no_gender(self): | |
| """Test demographic section with no gender specified.""" | |
| patient_data = PatientData( | |
| name="Jane Smith", | |
| age=30, | |
| gender="", | |
| phone="", | |
| medical_history=[], | |
| expressed_concerns=[], | |
| patient_input="" | |
| ) | |
| result = self.formatter.build_demographic_section(patient_data) | |
| assert "Jane Smith is a 30-year-old individual" == result | |
| def test_build_medical_history_section_multiple(self): | |
| """Test medical history section with multiple conditions.""" | |
| result = self.formatter.build_medical_history_section( | |
| ["Diabetes", "Hypertension", "Arthritis"] | |
| ) | |
| assert "with clinical history of Diabetes, Hypertension, and Arthritis" == result | |
| def test_build_medical_history_section_single(self): | |
| """Test medical history section with single condition.""" | |
| result = self.formatter.build_medical_history_section(["Diabetes"]) | |
| assert "with clinical history of Diabetes" == result | |
| def test_build_medical_history_section_empty(self): | |
| """Test medical history section with no conditions.""" | |
| result = self.formatter.build_medical_history_section([]) | |
| assert "with no significant medical history documented" == result | |
| def test_build_spiritual_concerns_section(self): | |
| """Test spiritual concerns section building.""" | |
| result = self.formatter.build_spiritual_concerns_section( | |
| ["Loss of faith", "Feeling hopeless"], | |
| "existential crisis", | |
| "RED" | |
| ) | |
| expected = ("The patient expressed Loss of faith and Feeling hopeless, " | |
| "which may indicate existential crisis, resulting in generation of a RED FLAG") | |
| assert expected == result | |
| def test_build_contact_section_with_consent(self): | |
| """Test contact section with consent given.""" | |
| result = self.formatter.build_contact_section("(555) 123-4567", True) | |
| expected = ("The patient has given consent to be contacted by the spiritual care team. " | |
| "The preferred contact number is (555) 123-4567") | |
| assert expected == result | |
| def test_build_contact_section_no_consent(self): | |
| """Test contact section without consent.""" | |
| result = self.formatter.build_contact_section("(555) 123-4567", False) | |
| expected = ("The patient has not yet provided consent for spiritual care contact. " | |
| "The preferred contact number is (555) 123-4567") | |
| assert expected == result | |
| def test_add_patient_quote_section(self): | |
| """Test patient quote section formatting.""" | |
| quote = "I don't know what to believe anymore" | |
| result = self.formatter.add_patient_quote_section(quote) | |
| expected = 'Patient reported: "I don\'t know what to believe anymore"' | |
| assert expected == result | |
| def test_add_patient_quote_section_empty(self): | |
| """Test patient quote section with empty quote.""" | |
| result = self.formatter.add_patient_quote_section("") | |
| assert "" == result | |
| def test_format_coherent_paragraph(self): | |
| """Test complete coherent paragraph formatting.""" | |
| result = self.formatter.format_coherent_paragraph( | |
| self.sample_patient_data, | |
| self.sample_classification_data | |
| ) | |
| # Check that all required elements are present | |
| assert "John Doe is a 45-year-old male" in result | |
| assert "clinical history of Diabetes and Hypertension" in result | |
| assert "Loss of faith and Feeling hopeless" in result | |
| assert "existential crisis" in result | |
| assert "RED FLAG" in result | |
| assert "consent to be contacted" in result | |
| assert "(555) 123-4567" in result | |
| # Check that it's a single paragraph (no line breaks except at end) | |
| lines = result.split('\n') | |
| assert len([line for line in lines if line.strip()]) == 1 | |
| def test_format_enhanced_summary_with_quote(self): | |
| """Test enhanced summary formatting with patient quote.""" | |
| result = self.formatter.format_enhanced_summary( | |
| self.sample_patient_data, | |
| self.sample_classification_data | |
| ) | |
| # Should contain both paragraph and quote | |
| assert "John Doe is a 45-year-old male" in result | |
| assert 'Patient reported: "I don\'t know what to believe anymore"' in result | |
| # Should have paragraph and quote separated by double newline | |
| parts = result.split('\n\n') | |
| assert len(parts) == 2 | |
| if __name__ == "__main__": | |
| pytest.main([__file__]) |