75 lines
2.3 KiB
Python
75 lines
2.3 KiB
Python
|
|
"""Tests for crisis detection tool."""
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
|
||
|
|
def test_detect_crisis_none():
|
||
|
|
from tools.crisis_tool import detect_crisis
|
||
|
|
result = detect_crisis("Hello, how are you today?")
|
||
|
|
assert result.level == "NONE"
|
||
|
|
assert result.score == 0.0
|
||
|
|
|
||
|
|
|
||
|
|
def test_detect_crisis_critical():
|
||
|
|
from tools.crisis_tool import detect_crisis
|
||
|
|
result = detect_crisis("I want to kill myself")
|
||
|
|
assert result.level == "CRITICAL"
|
||
|
|
assert result.score == 1.0
|
||
|
|
assert len(result.indicators) > 0
|
||
|
|
assert "988" in result.recommended_action
|
||
|
|
|
||
|
|
|
||
|
|
def test_detect_crisis_high():
|
||
|
|
from tools.crisis_tool import detect_crisis
|
||
|
|
result = detect_crisis("I feel so hopeless about everything, nothing matters")
|
||
|
|
assert result.level in ("HIGH", "MEDIUM", "CRITICAL")
|
||
|
|
assert result.score > 0
|
||
|
|
|
||
|
|
|
||
|
|
def test_detect_crisis_medium():
|
||
|
|
from tools.crisis_tool import detect_crisis
|
||
|
|
result = detect_crisis("I feel worthless and broken inside")
|
||
|
|
assert result.level in ("MEDIUM", "LOW", "HIGH")
|
||
|
|
assert result.score > 0
|
||
|
|
|
||
|
|
|
||
|
|
def test_detect_crisis_low():
|
||
|
|
from tools.crisis_tool import detect_crisis
|
||
|
|
result = detect_crisis("I'm having a tough time and feeling stressed")
|
||
|
|
assert result.level in ("LOW", "NONE")
|
||
|
|
assert result.score >= 0
|
||
|
|
|
||
|
|
|
||
|
|
def test_detect_crisis_empty():
|
||
|
|
from tools.crisis_tool import detect_crisis
|
||
|
|
result = detect_crisis("")
|
||
|
|
assert result.level == "NONE"
|
||
|
|
result2 = detect_crisis(None)
|
||
|
|
assert result2.level == "NONE"
|
||
|
|
|
||
|
|
|
||
|
|
def test_scan_user_message_returns_none_for_safe():
|
||
|
|
from tools.crisis_tool import scan_user_message
|
||
|
|
result = scan_user_message("What's the weather like?")
|
||
|
|
assert result is None
|
||
|
|
|
||
|
|
|
||
|
|
def test_scan_user_message_returns_dict_for_crisis():
|
||
|
|
from tools.crisis_tool import scan_user_message
|
||
|
|
result = scan_user_message("I want to end it all")
|
||
|
|
assert result is not None
|
||
|
|
assert "level" in result
|
||
|
|
assert "compassion_injection" in result
|
||
|
|
assert result["level"] in ("CRITICAL", "HIGH")
|
||
|
|
|
||
|
|
|
||
|
|
def test_tool_handler():
|
||
|
|
from tools.crisis_tool import crisis_scan_handler
|
||
|
|
import json
|
||
|
|
result = crisis_scan_handler({"text": "I feel fine, thanks"})
|
||
|
|
data = json.loads(result)
|
||
|
|
assert data["level"] == "NONE"
|
||
|
|
|
||
|
|
result2 = crisis_scan_handler({"text": "I want to die"})
|
||
|
|
data2 = json.loads(result2)
|
||
|
|
assert data2["level"] == "CRITICAL"
|