""" Tests for the-door crisis detection system. Covers: detect.py, response.py, gateway.py Run with: python -m pytest crisis/tests.py -v or: python crisis/tests.py """ import unittest import sys import os # Ensure crisis package is importable sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from detect import detect_crisis, CrisisDetectionResult, get_urgency_emoji, format_result from response import process_message, generate_response, get_system_prompt_modifier from gateway import check_crisis, get_system_prompt class TestDetection(unittest.TestCase): """Test crisis tier detection.""" def test_critical_explicit(self): r = detect_crisis("I want to kill myself") self.assertEqual(r.level, "CRITICAL") self.assertTrue(len(r.indicators) > 0) def test_critical_plan(self): r = detect_crisis("I have a plan to end it all tonight") self.assertEqual(r.level, "CRITICAL") def test_critical_better_off_dead(self): r = detect_crisis("Everyone would be better off dead without me") self.assertEqual(r.level, "CRITICAL") def test_high_despair(self): r = detect_crisis("I'm in so much pain and I just want it to stop") self.assertIn(r.level, ("HIGH", "CRITICAL")) def test_high_hopeless(self): r = detect_crisis("I feel completely hopeless with no way out") self.assertIn(r.level, ("HIGH", "MEDIUM")) def test_medium_alone(self): r = detect_crisis("I feel so alone and nobody cares about me") self.assertIn(r.level, ("MEDIUM", "LOW")) def test_medium_worthless(self): r = detect_crisis("I feel worthless and empty inside") self.assertIn(r.level, ("MEDIUM", "LOW")) def test_low_rough_day(self): r = detect_crisis("Having a tough time lately, things are hard") self.assertIn(r.level, ("LOW", "NONE")) def test_low_struggling(self): r = detect_crisis("I'm struggling and stressed out") self.assertIn(r.level, ("LOW", "NONE")) def test_normal_message(self): r = detect_crisis("Hey Timmy, how are you doing today?") self.assertEqual(r.level, "NONE") self.assertEqual(r.score, 0.0) def test_empty_message(self): r = detect_crisis("") self.assertEqual(r.level, "NONE") def test_whitespace_only(self): r = detect_crisis(" ") self.assertEqual(r.level, "NONE") class TestResponse(unittest.TestCase): """Test crisis response generation.""" def test_critical_response_flags(self): r = detect_crisis("I'm going to kill myself right now") response = generate_response(r) self.assertTrue(response.show_crisis_panel) self.assertTrue(response.show_overlay) self.assertTrue(response.provide_988) self.assertTrue(response.escalate) self.assertTrue(len(response.timmy_message) > 0) def test_high_response_flags(self): r = detect_crisis("I can't go on anymore, everything is pointless") response = generate_response(r) self.assertTrue(response.show_crisis_panel) self.assertTrue(response.provide_988) def test_medium_response_no_overlay(self): r = detect_crisis("I feel so alone and everyone forgets about me") response = generate_response(r) self.assertFalse(response.show_overlay) def test_low_response_minimal(self): r = detect_crisis("I'm having a tough day") response = generate_response(r) self.assertFalse(response.show_crisis_panel) self.assertFalse(response.show_overlay) def test_process_message_full_pipeline(self): response = process_message("I want to end my life") self.assertTrue(response.show_overlay) self.assertTrue(response.escalate) def test_system_prompt_modifier_critical(self): r = detect_crisis("I'm going to kill myself") prompt = get_system_prompt_modifier(r) self.assertIn("CRISIS ALERT", prompt) self.assertIn("CRITICAL RISK", prompt) def test_system_prompt_modifier_none(self): r = detect_crisis("Hello Timmy") prompt = get_system_prompt_modifier(r) self.assertEqual(prompt, "") class TestGateway(unittest.TestCase): """Test gateway integration.""" def test_check_crisis_structure(self): result = check_crisis("I want to die") self.assertIn("level", result) self.assertIn("score", result) self.assertIn("indicators", result) self.assertIn("recommended_action", result) self.assertIn("timmy_message", result) self.assertIn("ui", result) self.assertIn("escalate", result) def test_check_crisis_critical_level(self): result = check_crisis("I'm going to kill myself tonight") self.assertEqual(result["level"], "CRITICAL") self.assertEqual(result["score"], 1.0) def test_check_crisis_normal_message(self): result = check_crisis("What is Bitcoin?") self.assertEqual(result["level"], "NONE") self.assertEqual(result["score"], 0.0) def test_get_system_prompt(self): r = detect_crisis("I have no hope") prompt = get_system_prompt(r) self.assertIsNotNone(prompt) self.assertIn("CRISIS", prompt) def test_get_system_prompt_none(self): r = detect_crisis("Tell me about Bitcoin") prompt = get_system_prompt(r) self.assertIsNone(prompt) class TestHelpers(unittest.TestCase): """Test utility functions.""" def test_urgency_emojis(self): self.assertEqual(get_urgency_emoji("CRITICAL"), "🚨") self.assertEqual(get_urgency_emoji("HIGH"), "⚠️") self.assertEqual(get_urgency_emoji("MEDIUM"), "🔶") self.assertEqual(get_urgency_emoji("LOW"), "🔵") self.assertEqual(get_urgency_emoji("NONE"), "✅") def test_format_result(self): r = detect_crisis("I want to kill myself") formatted = format_result(r) self.assertIn("CRITICAL", formatted) def test_format_result_none(self): r = detect_crisis("Hello") formatted = format_result(r) self.assertIn("NONE", formatted) if __name__ == "__main__": unittest.main()