customeragent-api / server /scripts /test_emergency_detector.py
anasraza526's picture
Clean deploy to Hugging Face
ac90985
import sys
import os
import asyncio
from datetime import datetime
# Add parent directory to path
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_dir)
sys.path.append(parent_dir)
from app.services.emergency_detector import EmergencyService
# Mock DB Session
class MockDB:
def add(self, obj): pass
async def commit(self): pass
async def refresh(self, obj): obj.id = 1
async def get(self, model, id): return None
async def test_emergency_detector():
service = EmergencyService(MockDB())
test_cases = [
("I feel like I want to end my life", "suicide_self_harm", "crisis_hotline_immediate"),
("My throat is closing up after eating peanuts", "anaphylaxis", "call_911_immediately"),
("My husband hit me and I am scared", "abuse_violence", "domestic_violence_resources"),
("He is having a seizure and shaking", "seizure", "call_911_if_sustained"),
("I have a mild headache", None, None)
]
print("πŸš‘ Testing Emergency Detector...")
for query, expected_type, expected_action in test_cases:
result = await service.check_and_escalate(query, 1, "test-session")
is_match = False
if expected_type is None:
is_match = not result.is_emergency
else:
if result.is_emergency:
# We can't access detection type directly from result object easily without modifying it
# But we can check the action message content implicitly
action_msg = result.actions[0].message if result.actions else ""
if expected_action == "crisis_hotline_immediate" and "988" in action_msg: is_match = True
elif expected_action == "call_911_immediately" and "CALL 911 NOW" in action_msg: is_match = True
elif expected_action == "domestic_violence_resources" and "Domestic Violence Hotline" in action_msg: is_match = True
elif expected_action == "call_911_if_sustained" and "seizure" in action_msg: is_match = True
status = "βœ… PASS" if is_match else f"❌ FAIL (Got emergency={result.is_emergency})"
print(f"\nQuery: {query}")
print(f"Expected: {expected_type}, Status: {status}")
if result.is_emergency:
print(f"Action: {result.actions[0].message}")
if __name__ == "__main__":
asyncio.run(test_emergency_detector())