from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def read(path: str) -> str:
    return (ROOT / path).read_text(encoding="utf-8")


def test_remote_chatwoot_gap_audit_script_exists_and_compares_remote_messages():
    script = read("scripts/audit_chatwoot_remote_missing_messages.py")
    assert "fetch_messages" in script
    assert "MISSING_IN_CLIENTFLOW" in script
    assert "LEFT JOIN raw_events" in script
    assert "LEFT JOIN messages" in script
    assert "LEFT JOIN communications" in script
    assert "CHATWOOT_BASE_URL" in script


def test_remote_chatwoot_gap_audit_can_repost_missing_messages_safely():
    script = read("scripts/audit_chatwoot_remote_missing_messages.py")
    assert "--post-missing" in script
    assert "CLIENTFLOW_WEBHOOK_SECRET" in script
    assert "X-Chatwoot-Signature" in script
    assert "post_to_clientflow(build_webhook_payload" in script
    assert "status == \"MISSING_IN_CLIENTFLOW\"" in script
