"""Crisis numbers live in one table (config/texts/crisis.yaml); every other text refers to it by `{phone:}`.""" import re from pathlib import Path import pytest from app.core.safety import CRISIS_PATH, fill_phones from app.llm.agent.prompts import SYSTEM_PROMPT ROOT = Path(__file__).resolve().parents[1] # Any phone number except the universal short emergency numbers 112 / 101–103. NUMBER = re.compile(r"(\+7|\b8)[\s(-]*\d{3}[\s)-]*\d{3}[\s-]*\d{2}[\s-]*\d{2}") def test_no_number_outside_the_crisis_table(): texts = [p for p in (ROOT / "config" / "texts").glob("*.yaml") if p != CRISIS_PATH] code = [*(ROOT / "app").rglob("*.py"), *(ROOT / "web" / "src").rglob("*.ts*")] offenders = [ str(p.relative_to(ROOT)) for p in [*texts, *code] if p.name != "privacy.py" # the PII scrubber documents the phone format with an example and NUMBER.search("\n".join(line for line in p.read_text(encoding="utf-8").splitlines() if "#" not in line)) ] assert offenders == [] def test_references_resolve_and_unknown_keys_fail(): assert "989-50-50" in fill_phones("{phone:helpline}") and "989-50-50" in SYSTEM_PROMPT with pytest.raises(ValueError): fill_phones("{phone:no_such_line}")