"""An acked crisis is not raised again for the same signal; a newer PHQ-9 with item 9 raises a new one.""" from datetime import datetime, timedelta from app.core.rules import Action from app.core.types import AlertLevel from app.services import monitoring class _A: def __init__(self, code, ts, scale=None): self.code, self.ts, self.scale = code, ts, scale async def test_drop_handled_crises(monkeypatch): t0 = datetime(2026, 10, 4, 16, 32) alerts = [_A("phq9_item9", t0 + timedelta(seconds=1))] results = [_A(None, t0, "phq9"), _A(None, t0 + timedelta(hours=2), "auditc")] async def list_alerts(s, token): return alerts async def list_scale_results(s, token): return results monkeypatch.setattr(monitoring.repo, "list_alerts", list_alerts) monkeypatch.setattr(monitoring.repo, "list_scale_results", list_scale_results) crisis = Action(type="crisis", level=AlertLevel.ACUTE, code="phq9_item9") other = Action(type="alert", level=AlertLevel.HIGH, code="phq9_severe") assert await monitoring._drop_handled_crises(None, "t", [crisis, other]) == [other] # AUDIT-C is not a trigger results.append(_A(None, t0 + timedelta(hours=3), "phq9")) # a new PHQ-9 after the alert assert await monitoring._drop_handled_crises(None, "t", [crisis]) == [crisis]