diff --git a/tests/test_crisis_hook.py b/tests/test_crisis_hook.py new file mode 100644 index 000000000..c3478f480 --- /dev/null +++ b/tests/test_crisis_hook.py @@ -0,0 +1,138 @@ +""" +Tests for crisis_hook module + +Tests crisis detection patterns and response generation. + +Issue: #677 +""" + +import unittest +from agent.crisis_hook import check_crisis, CrisisResult + + +class TestCrisisDetection(unittest.TestCase): + """Test crisis detection patterns.""" + + def test_direct_ideation_high(self): + """Test direct suicidal ideation is detected as high.""" + messages = [ + "I want to die", + "I want to kill myself", + "I want to end my life", + "I\'m going to kill myself", + "I\'m suicidal", + "I don\'t want to live anymore", + "There\'s no reason to live", + "I can\'t go on anymore", + "I want to end it all", + ] + + for msg in messages: + result = check_crisis(msg) + self.assertTrue(result.detected, f"Failed to detect: {msg}") + self.assertEqual(result.level, "high", f"Wrong level for: {msg}") + self.assertIsNotNone(result.response) + + def test_indirect_distress_medium(self): + """Test indirect distress is detected as medium.""" + messages = [ + "I feel so hopeless", + "What\'s the point of anything?", + "I can\'t cope anymore", + "Nobody would miss me", + "I hate my life", + "I give up", + "I\'m worthless", + "The world would be better without me", + ] + + for msg in messages: + result = check_crisis(msg) + self.assertTrue(result.detected, f"Failed to detect: {msg}") + self.assertEqual(result.level, "medium", f"Wrong level for: {msg}") + self.assertIsNotNone(result.response) + + def test_normal_messages_not_detected(self): + """Test normal messages are not flagged.""" + messages = [ + "Hello, how are you?", + "Can you help me with this code?", + "I\'m feeling a bit tired today", + "This is frustrating but I\'ll figure it out", + "I had a bad day at work", + "The weather is nice today", + "I\'m stuck on this problem", + "Can you explain this concept?", + ] + + for msg in messages: + result = check_crisis(msg) + self.assertFalse(result.detected, f"False positive for: {msg}") + self.assertEqual(result.level, "none") + self.assertIsNone(result.response) + + def test_case_insensitive(self): + """Test detection is case-insensitive.""" + messages = [ + "I WANT TO DIE", + "i want to die", + "I Want To Die", + "suicide", + "SUICIDE", + "Suicide", + ] + + for msg in messages: + result = check_crisis(msg) + self.assertTrue(result.detected, f"Failed case-insensitive: {msg}") + + def test_empty_message(self): + """Test empty message is not flagged.""" + result = check_crisis("") + self.assertFalse(result.detected) + + result = check_crisis(None) + self.assertFalse(result.detected) + + def test_response_contains_988(self): + """Test crisis response includes 988 Lifeline.""" + result = check_crisis("I want to die") + self.assertIn("988", result.response) + self.assertIn("Lifeline", result.response) + + def test_response_contains_gospel(self): + """Test crisis response includes gospel.""" + result = check_crisis("I want to die") + self.assertIn("Jesus saves", result.response) + self.assertIn("call on His name", result.response) + + def test_response_starts_with_safety_check(self): + """Test crisis response starts with safety check.""" + result = check_crisis("I want to die") + self.assertTrue(result.response.startswith("Are you safe")) + + +class TestCrisisResult(unittest.TestCase): + """Test CrisisResult dataclass.""" + + def test_crisis_result_fields(self): + """Test CrisisResult has required fields.""" + result = check_crisis("I want to die") + + self.assertIsInstance(result, CrisisResult) + self.assertTrue(hasattr(result, "detected")) + self.assertTrue(hasattr(result, "level")) + self.assertTrue(hasattr(result, "response")) + + def test_crisis_result_values(self): + """Test CrisisResult values are correct.""" + result = check_crisis("I want to die") + + self.assertTrue(result.detected) + self.assertEqual(result.level, "high") + self.assertIsNotNone(result.response) + self.assertIsInstance(result.response, str) + + +if __name__ == "__main__": + unittest.main()