File size: 14,949 Bytes
24214fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/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)