from pathlib import Path

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


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


def test_pdf_format_guard_helper_exists():
    src = read("app/pdf_format_guard.py")
    assert "def analyze_pdf_format" in src
    assert "non_a4_or_unexpected_mediabox" in src
    assert "invoice_pdf_block_message" in src


def test_customer_send_uses_validated_pdf_download():
    src = read("app/reply_assistant_service.py")
    assert "validate_customer_send=True" in src
    assert "clientflow_pdf_send_blocked" in src
    assert "PDF de fatura bloqueado" in src


def test_jasmin_service_records_and_blocks_non_a4_invoice_pdf_for_send():
    src = read("app/jasmin_service.py")
    assert "analyze_pdf_format" in src
    assert "_record_pdf_format_check" in src
    assert "CLIENTFLOW_ALLOW_NON_A4_JASMIN_INVOICE_PDF" in src
    assert "validate_customer_send" in src


def test_audit_days_param_and_mark_local_are_safe():
    src = read("scripts/audit_jasmin_converted_invoice_format.py")
    assert ":days::text" not in src
    assert ":days * INTERVAL '1 day'" in src
    assert "--mark-local" in src
    assert "clientflow_pdf_format_checked_by" in src
