feat(web): two-stage questionnaires, screening result and diary by result

- Sign-in: centred ЕМИАС card without the sidebar; anonymous entry asks «Депрессия / ПТСР».
- Stage 1 is the same for everyone and follows the fixed order of the MVP questionnaire:
  PHQ-2 → trauma filter/TSQ → RUS-AUDIT-S → C-SSRS → context; stage 2 only where stage 1
  is positive. Starts without a profile.
- After stage 1 the patient sees a preliminary result (screening, not a diagnosis), stored
  with the record; it switches on the diary modules (depression / PTSD).
- Stage 2 diary: morning (sleep, sleep quality, alcohol) and evening (mood, medication,
  3 PHQ-9 items in rotation, short PCL-5); C-SSRS since last contact only under risk;
  flags on weekly means against the personal norm; scheduled full scales with change
  (remission, response, worsening).
- Questionnaire log: GET /me/{token}/results and the «Мои результаты» screen.
- Questionnaires centred without the sidebar and without scale names; «Продолжить позже»
  goes home; confirmations are toasts; free-text comments are optional.
- Demo policy config/questionnaire_demo.yaml (INTAKE_DEMO only, not a clinical approval).

ADR-012.

Refs #77

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-04 15:14:09 +03:00
co-authored by Claude Opus 5.5
parent 6e7c89900b
commit 290b0d6151
36 changed files with 1900 additions and 691 deletions
+97 -29
View File
@@ -56,28 +56,51 @@ def test_medication_schedule_overlapping_and_delayed_completion():
)
def test_diary_consecutive_days_and_baseline_do_not_include_bad_window():
today = date(2026, 10, 3)
history = {today - timedelta(days=i): {"mood": 8} for i in range(3, 10)}
for i in range(3):
history[today - timedelta(days=i)] = {"mood": 5, "sleep_hours": 3.9, "medication": "none"}
assert set(diary_flags(history, today)) == {"phq9_due_mood", "sleep_review", "medication_contact"}
history[today - timedelta(days=1)] = {}
assert diary_flags(history, today) == ()
history[today]["death_thoughts"] = 1
assert diary_flags(history, today) == ("suicide_assessment_now",)
def norm_and_week(norm: dict, week: dict, today=date(2026, 10, 30)):
"""14 days of personal norm starting 27 days ago, then the last 7 days with other values."""
start = today - timedelta(days=27)
history = {start + timedelta(days=i): dict(norm) for i in range(14)}
history.update({today - timedelta(days=i): dict(week) for i in range(7)})
return history, today
def test_sleep_alternation_counts_but_threshold_is_strict():
def test_weekly_mood_against_personal_norm():
history, today = norm_and_week({"mood": 8}, {"mood": 5})
assert "phq9_due_mood" in diary_flags(history, today)
history, today = norm_and_week({"mood": 8}, {"mood": 6})
assert "phq9_due_mood" not in diary_flags(history, today)
# Inside the norm window there is no trend yet.
early = {date(2026, 10, 1) + timedelta(days=i): {"mood": 9 if i < 3 else 1} for i in range(10)}
assert "phq9_due_mood" not in diary_flags(early, date(2026, 10, 10))
def test_module_trends_bring_full_scales_forward():
ptsd = {"dpcl_1": 1, "dpcl_7": 1, "dpcl_10": 1, "dpcl_18": 1}
history, today = norm_and_week(ptsd, {k: 2 for k in ptsd})
assert "pcl5_due_ptsd" in diary_flags(history, today)
history, today = norm_and_week({"dphq_4": 0}, {"dphq_4": 1})
assert "phq9_due_depression" in diary_flags(history, today)
def test_sleep_alcohol_and_medication_flags():
today = date(2026, 10, 3)
h = {
today: {"nightmares": 3},
today - timedelta(days=1): {"sleep_hours": 3.5},
today - timedelta(days=2): {"sleep_hours": 3.5},
}
assert "sleep_review" in diary_flags(h, today)
h[today - timedelta(days=1)]["sleep_hours"] = 4
assert "sleep_review" not in diary_flags(h, today)
nights = {today - timedelta(days=i): {"sleep_hours": 7, "sleep_quality": 0} for i in range(3)}
nights[today - timedelta(days=1)] = {"sleep_hours": 3.5, "sleep_quality": 2}
assert "sleep_review" in diary_flags(nights, today)
nights[today - timedelta(days=1)]["sleep_hours"] = 4
assert "sleep_review" not in diary_flags(nights, today)
drinks = {today: {"alcohol_portions": 6}, today - timedelta(days=2): {"alcohol_portions": 7}}
assert set(diary_flags(drinks, today, medication_active=True)) >= {"alcohol_on_medication", "rus_audit_due_alcohol"}
assert "alcohol_on_medication" not in diary_flags(drinks, today)
missed = {today - timedelta(days=i): {"medication": "no"} for i in range(3)}
assert "medication_contact" in diary_flags(missed, today)
week = {today - timedelta(days=i): {"medication": "yes" if i % 3 else "no"} for i in range(7)}
assert "adherence_review" in diary_flags(week, today)
assert diary_flags({today: {"medication": "no", "medication_reason": "side_effects"}}, today) == (
"side_effects_review",
)
def test_total_threshold_and_zone_are_separate_decisions():
@@ -96,15 +119,60 @@ def test_total_threshold_and_zone_are_separate_decisions():
IntakePolicy.model_validate({**data, "rus_audit_s_expand_from_zone": 2})
def test_module_rotation_never_drops_suicide_question():
def test_diary_parts_and_module_rotation():
from app.core.intake_questions import diary_questions
first = {
q.id for q in diary_questions(["veteran", "depression"], suicide_monitoring=False, weekday=0, day_index=100)
}
second = {
q.id for q in diary_questions(["veteran", "depression"], suicide_monitoring=False, weekday=1, day_index=101)
}
assert "death_thoughts" in first & second
assert "nightmares" in first and "nightmares" not in second
assert "burden" in first and "burden" not in second
assert [q.id for q in diary_questions("morning", ["veteran", "depression"])] == [
"sleep_hours",
"sleep_quality",
"alcohol_portions",
]
evening = [q.id for q in diary_questions("evening", ["veteran", "depression"], day_index=0)]
assert evening == ["mood", "medication", "medication_reason", "dphq_1", "dphq_2", "dphq_3"] + [
"dpcl_1",
"dpcl_7",
"dpcl_10",
"dpcl_18",
]
assert [q.id for q in diary_questions("evening", ["depression"], day_index=5)][-3:] == [
"dphq_7",
"dphq_8",
"dphq_9",
]
assert [q.id for q in diary_questions("evening", [], day_index=1)] == ["mood", "medication", "medication_reason"]
def test_demo_policy_is_separate_and_marked(monkeypatch):
from app.config import get_settings
monkeypatch.setenv("INTAKE_DEMO", "1")
monkeypatch.setattr(get_settings(), "demo_clock", True)
demo = load_policy()
assert demo.approval and "DEMO" in demo.approval.reviewer and demo.cssrs_positive_policy == "graded"
monkeypatch.delenv("INTAKE_DEMO")
assert load_policy().approval is None
def test_full_scale_change_against_the_start_of_observation():
from app.core.interpretation import scale_interpretation
def phq(total, baseline, previous=None):
totals = {"phq9": {"complete": True, "total": total}}
return scale_interpretation(
"phq9", totals, baseline=baseline, previous=previous, policy=load_policy(), retriage_delta=5
)["codes"]
assert phq(4, 16) == ["minimal", "remission"]
assert phq(8, 16) == ["mild", "response"]
assert phq(11, 16) == ["moderate", "improved"]
assert phq(15, 16) == ["moderately_severe", "stable"]
assert phq(21, 10, previous=16) == ["severe", "worse"]
pcl = scale_interpretation(
"pcl5",
{"pcl5": {"complete": True, "total": 27}},
baseline=45,
previous=40,
policy=load_policy(),
retriage_delta=10,
)
assert pcl["codes"] == ["below", "response"]