Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Test script for the consent message generator. | |
| Tests Task 5.2 implementation. | |
| """ | |
| import sys | |
| import os | |
| sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'src')) | |
| from config.prompt_management.consent_message_generator import ConsentMessageGenerator | |
| from config.prompt_management.consent_manager import ConsentMessageType | |
| from config.prompt_management.data_models import Template | |
| def test_consent_message_generator_initialization(): | |
| """Test that the consent message generator initializes correctly.""" | |
| print("Testing consent message generator initialization...") | |
| generator = ConsentMessageGenerator() | |
| # Verify components are initialized | |
| assert hasattr(generator, 'consent_manager') | |
| assert hasattr(generator, 'consent_templates') | |
| assert hasattr(generator, 'validation_rules') | |
| # Verify validation rules structure | |
| assert 'required_elements' in generator.validation_rules | |
| assert 'forbidden_elements' in generator.validation_rules | |
| # Verify required elements for each message type | |
| required_elements = generator.validation_rules['required_elements'] | |
| assert 'initial_request' in required_elements | |
| assert 'clarification' in required_elements | |
| assert 'confirmation' in required_elements | |
| assert 'decline_acknowledgment' in required_elements | |
| print("β Consent message generator initializes correctly") | |
| return True | |
| def test_consent_request_generation(): | |
| """Test consent request generation with validation.""" | |
| print("Testing consent request generation...") | |
| generator = ConsentMessageGenerator() | |
| # Test basic consent request generation | |
| result = generator.generate_consent_request() | |
| # Verify result structure | |
| required_fields = ['message', 'is_compliant', 'violations', 'validation_score', | |
| 'message_type', 'generated_at', 'context_used'] | |
| for field in required_fields: | |
| assert field in result, f"Result missing required field: {field}" | |
| # Verify message properties | |
| assert len(result['message']) > 0, "Generated message should not be empty" | |
| assert result['message_type'] == 'initial_request', "Should be initial request type" | |
| assert isinstance(result['is_compliant'], bool), "Compliance should be boolean" | |
| assert isinstance(result['violations'], list), "Violations should be list" | |
| assert 0.0 <= result['validation_score'] <= 1.0, "Validation score should be between 0 and 1" | |
| # Test with context | |
| context = { | |
| 'distress_level': 'high', | |
| 'previous_spiritual_mention': True, | |
| 'patient_name': 'John' | |
| } | |
| contextual_result = generator.generate_consent_request(context=context) | |
| assert contextual_result['context_used'] == context, "Should record context used" | |
| assert len(contextual_result['message']) > 0, "Contextual message should not be empty" | |
| print("β Consent request generation works correctly") | |
| return True | |
| def test_response_message_generation(): | |
| """Test response message generation based on patient responses.""" | |
| print("Testing response message generation...") | |
| generator = ConsentMessageGenerator() | |
| # Test acceptance response | |
| accept_response = "Yes, I would like to speak with someone" | |
| result = generator.generate_response_message(accept_response, "test_session_1") | |
| # Verify result structure | |
| assert 'action' in result, "Result should contain action" | |
| assert 'message' in result, "Result should contain response message" | |
| assert 'is_compliant' in result, "Result should contain compliance check" | |
| assert 'validation_score' in result, "Result should contain validation score" | |
| assert 'patient_response' in result, "Result should record patient response" | |
| # Verify acceptance handling | |
| assert result['action'] == 'proceed_with_referral', "Accept should proceed with referral" | |
| assert result['generate_provider_summary'] == True, "Accept should generate summary" | |
| assert result['patient_response'] == accept_response, "Should record original response" | |
| # Test decline response | |
| decline_response = "No, I'm fine" | |
| decline_result = generator.generate_response_message(decline_response, "test_session_2") | |
| assert decline_result['action'] == 'return_to_medical_dialogue', "Decline should return to medical" | |
| assert decline_result['generate_provider_summary'] == False, "Decline should not generate summary" | |
| # Test ambiguous response | |
| ambiguous_response = "I don't know, what would that involve?" | |
| ambiguous_result = generator.generate_response_message(ambiguous_response, "test_session_3") | |
| assert ambiguous_result['action'] == 'request_clarification', "Ambiguous should request clarification" | |
| assert ambiguous_result.get('requires_follow_up') == True, "Ambiguous should require follow-up" | |
| print("β Response message generation works correctly") | |
| return True | |
| def test_consent_template_creation(): | |
| """Test consent template creation and management.""" | |
| print("Testing consent template creation...") | |
| generator = ConsentMessageGenerator() | |
| # Create a test template | |
| template_id = "test_template_1" | |
| template_name = "Test Initial Request" | |
| message_type = ConsentMessageType.INITIAL_REQUEST | |
| content = "Would you be interested in speaking with {team_type} if that would be helpful?" | |
| variables = ["team_type"] | |
| # Create template | |
| success = generator.create_consent_template( | |
| template_id, template_name, message_type, content, variables | |
| ) | |
| assert success, "Template creation should succeed" | |
| assert template_id in generator.consent_templates, "Template should be stored" | |
| # Verify template properties | |
| template = generator.consent_templates[template_id] | |
| assert template.template_id == template_id, "Template ID should match" | |
| assert template.name == template_name, "Template name should match" | |
| assert template.content == content, "Template content should match" | |
| assert template.variables == variables, "Template variables should match" | |
| # Test template usage | |
| context = {"team_type": "our spiritual care team"} | |
| result = generator.generate_consent_request(context=context, template_id=template_id) | |
| assert "our spiritual care team" in result['message'], "Template should substitute variables" | |
| assert result['template_id'] == template_id, "Should record template used" | |
| # Test invalid template content | |
| try: | |
| generator.create_consent_template( | |
| "invalid_template", | |
| "Invalid Template", | |
| ConsentMessageType.INITIAL_REQUEST, | |
| "You need to speak with someone from spiritual care.", # Non-compliant | |
| [] | |
| ) | |
| assert False, "Should raise ValueError for non-compliant template" | |
| except ValueError as e: | |
| assert "violates language compliance" in str(e), "Should indicate compliance violation" | |
| print("β Consent template creation works correctly") | |
| return True | |
| def test_message_batch_validation(): | |
| """Test batch validation of consent messages.""" | |
| print("Testing message batch validation...") | |
| generator = ConsentMessageGenerator() | |
| # Create test messages (mix of compliant and non-compliant) | |
| test_messages = [ | |
| "Would you be interested in speaking with someone from our spiritual care team?", # Compliant | |
| "Our spiritual care team is available if you'd like to connect with them.", # Compliant | |
| "You need to speak with someone from spiritual care.", # Non-compliant | |
| "This will help you feel better.", # Non-compliant | |
| "I understand and respect your decision.", # Compliant | |
| ] | |
| # Validate batch | |
| results = generator.validate_message_batch(test_messages) | |
| # Verify results structure | |
| required_fields = ['total_messages', 'compliant_messages', 'non_compliant_messages', | |
| 'average_validation_score', 'common_violations', 'detailed_results'] | |
| for field in required_fields: | |
| assert field in results, f"Results missing required field: {field}" | |
| # Verify counts | |
| assert results['total_messages'] == 5, "Should count all messages" | |
| assert results['compliant_messages'] == 3, "Should identify compliant messages" | |
| assert results['non_compliant_messages'] == 2, "Should identify non-compliant messages" | |
| # Verify detailed results | |
| assert len(results['detailed_results']) == 5, "Should have detailed results for all messages" | |
| for i, detail in enumerate(results['detailed_results']): | |
| assert detail['message_index'] == i, "Should have correct message index" | |
| assert 'message' in detail, "Should include original message" | |
| assert 'is_compliant' in detail, "Should include compliance status" | |
| assert 'validation_score' in detail, "Should include validation score" | |
| # Verify common violations tracking | |
| assert isinstance(results['common_violations'], dict), "Common violations should be dict" | |
| print("β Message batch validation works correctly") | |
| return True | |
| def test_approved_patterns_access(): | |
| """Test access to approved language patterns.""" | |
| print("Testing approved patterns access...") | |
| generator = ConsentMessageGenerator() | |
| # Get approved patterns | |
| patterns = generator.get_approved_patterns() | |
| # Verify structure | |
| assert isinstance(patterns, dict), "Patterns should be dictionary" | |
| # Verify required pattern types | |
| required_types = ['initial_request', 'clarification', 'confirmation', 'decline_acknowledgment'] | |
| for pattern_type in required_types: | |
| assert pattern_type in patterns, f"Missing pattern type: {pattern_type}" | |
| assert len(patterns[pattern_type]) > 0, f"Pattern type {pattern_type} should have examples" | |
| # Verify all patterns are strings | |
| for pattern_type, pattern_list in patterns.items(): | |
| for pattern in pattern_list: | |
| assert isinstance(pattern, str), f"Pattern should be string: {pattern}" | |
| assert len(pattern) > 0, f"Pattern should not be empty: {pattern}" | |
| print("β Approved patterns access works correctly") | |
| return True | |
| def test_validation_guidelines(): | |
| """Test validation guidelines access.""" | |
| print("Testing validation guidelines...") | |
| generator = ConsentMessageGenerator() | |
| # Get validation guidelines | |
| guidelines = generator.get_validation_guidelines() | |
| # Verify structure | |
| required_fields = ['non_assumptive_requirements', 'validation_rules', | |
| 'respectful_language_indicators', 'message_types', 'response_types'] | |
| for field in required_fields: | |
| assert field in guidelines, f"Guidelines missing required field: {field}" | |
| # Verify non-assumptive requirements | |
| requirements = guidelines['non_assumptive_requirements'] | |
| assert 'avoid_assumptions' in requirements, "Should have assumption avoidance rules" | |
| assert 'avoid_pressure' in requirements, "Should have pressure avoidance rules" | |
| assert 'avoid_religious_assumptions' in requirements, "Should have religious assumption rules" | |
| # Verify message types | |
| message_types = guidelines['message_types'] | |
| expected_types = ['initial_request', 'clarification', 'confirmation', 'decline_acknowledgment'] | |
| for expected_type in expected_types: | |
| assert expected_type in message_types, f"Missing message type: {expected_type}" | |
| # Verify response types | |
| response_types = guidelines['response_types'] | |
| expected_responses = ['accept', 'decline', 'ambiguous', 'unclear'] | |
| for expected_response in expected_responses: | |
| assert expected_response in response_types, f"Missing response type: {expected_response}" | |
| print("β Validation guidelines work correctly") | |
| return True | |
| def test_validation_score_calculation(): | |
| """Test validation score calculation.""" | |
| print("Testing validation score calculation...") | |
| generator = ConsentMessageGenerator() | |
| # Test high-scoring message | |
| good_message = "Would you be interested in speaking with someone if that would be helpful?" | |
| good_score = generator._calculate_validation_score(good_message) | |
| assert 0.0 <= good_score <= 1.0, "Score should be between 0 and 1" | |
| assert good_score > 0.5, "Good message should have high score" | |
| # Test low-scoring message | |
| bad_message = "You need to speak with God about your problems." | |
| bad_score = generator._calculate_validation_score(bad_message) | |
| assert 0.0 <= bad_score <= 1.0, "Score should be between 0 and 1" | |
| assert bad_score < good_score, "Bad message should have lower score than good message" | |
| # Test neutral message | |
| neutral_message = "I will contact someone for you." | |
| neutral_score = generator._calculate_validation_score(neutral_message) | |
| assert 0.0 <= neutral_score <= 1.0, "Score should be between 0 and 1" | |
| print("β Validation score calculation works correctly") | |
| return True | |
| def main(): | |
| """Run all consent message generator tests.""" | |
| print("=" * 60) | |
| print("CONSENT MESSAGE GENERATOR TESTS") | |
| print("=" * 60) | |
| tests = [ | |
| test_consent_message_generator_initialization, | |
| test_consent_request_generation, | |
| test_response_message_generation, | |
| test_consent_template_creation, | |
| test_message_batch_validation, | |
| test_approved_patterns_access, | |
| test_validation_guidelines, | |
| test_validation_score_calculation | |
| ] | |
| passed = 0 | |
| failed = 0 | |
| for test in tests: | |
| try: | |
| print(f"\n{test.__name__.replace('_', ' ').title()}:") | |
| print("-" * 40) | |
| result = test() | |
| if result: | |
| passed += 1 | |
| print("β PASSED") | |
| else: | |
| failed += 1 | |
| print("β FAILED") | |
| except Exception as e: | |
| failed += 1 | |
| print(f"β FAILED: {str(e)}") | |
| print("\n" + "=" * 60) | |
| print(f"RESULTS: {passed} passed, {failed} failed") | |
| print("=" * 60) | |
| if failed == 0: | |
| print("π All consent message generator tests passed!") | |
| print("\n**Task 5.2: Consent Message Generation Logic**") | |
| print("β COMPLETED: Approved language pattern validation") | |
| print("β COMPLETED: Non-assumptive language checking") | |
| print("β COMPLETED: Consent message template system") | |
| print("β COMPLETED: Batch validation capabilities") | |
| print("β VALIDATED: Requirements 4.1, 4.5") | |
| return True | |
| else: | |
| print("β Some tests failed. Please check the implementation.") | |
| return False | |
| if __name__ == "__main__": | |
| success = main() | |
| sys.exit(0 if success else 1) |