Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Unit tests for ImprovedClassificationPromptManager. | |
| Tests the implementation of updated classification prompts with new red flag definitions | |
| and explicit indicators based on medical professional feedback. | |
| Requirements: 4.1, 4.2, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6 | |
| """ | |
| import pytest | |
| from src.core.improved_classification_prompt_manager import ( | |
| ImprovedClassificationPromptManager, | |
| ClassificationResult, | |
| create_improved_classification_prompt_manager | |
| ) | |
| class TestImprovedClassificationPromptManager: | |
| """Test suite for ImprovedClassificationPromptManager.""" | |
| def setup_method(self): | |
| """Set up test fixtures.""" | |
| self.manager = create_improved_classification_prompt_manager() | |
| def test_updated_red_flag_definition(self): | |
| """ | |
| Test updated red flag definition - Requirements 4.1, 4.2. | |
| Verifies that the new definition is broader and includes active distress, | |
| not just crisis situations. | |
| """ | |
| definition = self.manager.get_updated_red_flag_definition() | |
| # Check that definition contains key phrases from requirements | |
| assert "caused by or actively causing" in definition | |
| assert "emotional or spiritual distress" in definition | |
| # Ensure it's not empty | |
| assert len(definition.strip()) > 0 | |
| def test_explicit_red_indicators_completeness(self): | |
| """ | |
| Test explicit red indicators - Requirements 5.2, 5.3, 5.4, 5.5, 5.6. | |
| Verifies that all five required explicit indicators are present. | |
| """ | |
| indicators = self.manager.get_explicit_red_indicators() | |
| # Check that all 5 required indicators are present | |
| expected_indicators = [ | |
| "Complex grief", | |
| "Loss of a loved one", | |
| "Doubt about meaning of life", | |
| "Doubt about meaning of suffering", | |
| "Doubt about personal dignity" | |
| ] | |
| assert len(indicators) == 5 | |
| for expected in expected_indicators: | |
| assert expected in indicators | |
| def test_yellow_flag_criteria(self): | |
| """ | |
| Test yellow flag criteria - Requirements 6.1, 6.2, 6.3, 6.4, 6.5. | |
| Verifies that yellow flag criteria include appropriate situations | |
| requiring further investigation. | |
| """ | |
| criteria = self.manager.get_yellow_flag_criteria() | |
| # Check that criteria are not empty | |
| assert len(criteria) > 0 | |
| # Check for key criteria from requirements | |
| criteria_text = " ".join(criteria).lower() | |
| assert "sadness without additional context" in criteria_text | |
| assert "further clarification" in criteria_text or "further investigation" in criteria_text | |
| def test_enhanced_prompt_structure(self): | |
| """ | |
| Test enhanced prompt generation - Requirements 5.1, 5.7. | |
| Verifies that the prompt contains all required sections and indicators. | |
| """ | |
| prompt = self.manager.build_enhanced_classification_prompt() | |
| # Check that prompt contains all required sections | |
| required_sections = [ | |
| "<system_role>", | |
| "<updated_red_flag_definition>", | |
| "<explicit_red_indicators>", | |
| "<yellow_flag_criteria>", | |
| "<classification_logic>", | |
| "<output_format>" | |
| ] | |
| for section in required_sections: | |
| assert section in prompt | |
| # Check that all explicit indicators are in the prompt | |
| red_indicators = self.manager.get_explicit_red_indicators() | |
| for indicator in red_indicators: | |
| assert indicator in prompt | |
| def test_classification_validation_valid_red(self): | |
| """ | |
| Test classification validation for valid red classification. | |
| Requirements: 5.7, 4.1, 4.2 | |
| """ | |
| # Test valid red classification with explicit indicator | |
| valid_red = ClassificationResult( | |
| classification="red", | |
| confidence=0.9, | |
| indicators=["complex grief", "loss"], | |
| reasoning="Complex grief detected", | |
| red_flag_indicators=["Complex grief"], | |
| yellow_flag_indicators=[], | |
| is_valid=False | |
| ) | |
| assert self.manager.validate_classification_consistency(valid_red) == True | |
| def test_classification_validation_invalid_simple_sadness(self): | |
| """ | |
| Test classification validation rejects simple sadness as red. | |
| Requirements: 3.1, 6.1 | |
| """ | |
| # Test invalid classification (simple sadness as red without context) | |
| invalid_red = ClassificationResult( | |
| classification="red", | |
| confidence=0.8, | |
| indicators=["feeling sad"], | |
| reasoning="Sadness", | |
| red_flag_indicators=[], | |
| yellow_flag_indicators=[], | |
| is_valid=False | |
| ) | |
| assert self.manager.validate_classification_consistency(invalid_red) == False | |
| def test_classification_validation_meaning_loss_should_be_red(self): | |
| """ | |
| Test that loss of meaning/purpose should be classified as red. | |
| Requirements: 3.2 | |
| """ | |
| # Test loss of meaning should be red | |
| meaning_loss = ClassificationResult( | |
| classification="yellow", | |
| confidence=0.7, | |
| indicators=["loss of meaning in life"], | |
| reasoning="Loss of meaning", | |
| red_flag_indicators=[], | |
| yellow_flag_indicators=["loss of meaning"], | |
| is_valid=False | |
| ) | |
| assert self.manager.validate_classification_consistency(meaning_loss) == False | |
| def test_classification_validation_invalid_classification_type(self): | |
| """Test validation rejects invalid classification types.""" | |
| invalid_classification = ClassificationResult( | |
| classification="blue", # Invalid | |
| confidence=0.8, | |
| indicators=["test"], | |
| reasoning="Test", | |
| red_flag_indicators=[], | |
| yellow_flag_indicators=[], | |
| is_valid=False | |
| ) | |
| assert self.manager.validate_classification_consistency(invalid_classification) == False | |
| def test_classification_validation_invalid_confidence(self): | |
| """Test validation rejects invalid confidence values.""" | |
| invalid_confidence = ClassificationResult( | |
| classification="red", | |
| confidence=1.5, # Invalid - should be 0.0-1.0 | |
| indicators=["test"], | |
| reasoning="Test", | |
| red_flag_indicators=[], | |
| yellow_flag_indicators=[], | |
| is_valid=False | |
| ) | |
| assert self.manager.validate_classification_consistency(invalid_confidence) == False | |
| def test_classification_guidelines(self): | |
| """Test classification guidelines content.""" | |
| guidelines = self.manager.get_classification_guidelines() | |
| # Check that all classification types are present | |
| assert "red" in guidelines | |
| assert "yellow" in guidelines | |
| assert "green" in guidelines | |
| # Check content quality | |
| assert len(guidelines["red"]) > 0 | |
| assert len(guidelines["yellow"]) > 0 | |
| assert len(guidelines["green"]) > 0 | |
| # Check for key terms | |
| assert "spiritual" in guidelines["red"] | |
| assert "further investigation" in guidelines["yellow"] | |
| assert "No signs" in guidelines["green"] | |
| def test_create_classification_result(self): | |
| """Test classification result creation with validation.""" | |
| result = self.manager.create_classification_result( | |
| classification="red", | |
| confidence=0.9, | |
| indicators=["complex grief"], | |
| reasoning="Complex grief detected", | |
| red_flag_indicators=["Complex grief"] | |
| ) | |
| assert result.classification == "red" | |
| assert result.confidence == 0.9 | |
| assert result.indicators == ["complex grief"] | |
| assert result.reasoning == "Complex grief detected" | |
| assert result.red_flag_indicators == ["Complex grief"] | |
| assert result.yellow_flag_indicators == [] | |
| assert result.is_valid == True | |
| def test_create_classification_result_with_validation_failure(self): | |
| """Test classification result creation that fails validation.""" | |
| result = self.manager.create_classification_result( | |
| classification="red", | |
| confidence=0.8, | |
| indicators=["почуття смутку"], # Simple sadness without context | |
| reasoning="Смуток", | |
| red_flag_indicators=[], | |
| yellow_flag_indicators=[] | |
| ) | |
| assert result.classification == "red" | |
| assert result.is_valid == False # Should fail validation | |
| def test_factory_function(self): | |
| """Test factory function creates proper instance.""" | |
| manager = create_improved_classification_prompt_manager() | |
| assert isinstance(manager, ImprovedClassificationPromptManager) | |
| # Test that it works properly | |
| definition = manager.get_updated_red_flag_definition() | |
| assert len(definition) > 0 | |
| indicators = manager.get_explicit_red_indicators() | |
| assert len(indicators) == 5 | |
| class TestClassificationResult: | |
| """Test suite for ClassificationResult data class.""" | |
| def test_classification_result_creation(self): | |
| """Test ClassificationResult creation.""" | |
| result = ClassificationResult( | |
| classification="red", | |
| confidence=0.9, | |
| indicators=["test indicator"], | |
| reasoning="test reasoning", | |
| red_flag_indicators=["red indicator"], | |
| yellow_flag_indicators=["yellow indicator"], | |
| is_valid=True | |
| ) | |
| assert result.classification == "red" | |
| assert result.confidence == 0.9 | |
| assert result.indicators == ["test indicator"] | |
| assert result.reasoning == "test reasoning" | |
| assert result.red_flag_indicators == ["red indicator"] | |
| assert result.yellow_flag_indicators == ["yellow indicator"] | |
| assert result.is_valid == True | |
| def test_classification_result_defaults(self): | |
| """Test ClassificationResult with default values.""" | |
| result = ClassificationResult( | |
| classification="green", | |
| confidence=0.7, | |
| indicators=[], | |
| reasoning="No indicators found", | |
| red_flag_indicators=[], | |
| yellow_flag_indicators=[], | |
| is_valid=True | |
| ) | |
| assert result.red_flag_indicators == [] | |
| assert result.yellow_flag_indicators == [] | |
| if __name__ == "__main__": | |
| pytest.main([__file__]) |