from pathlib import Path

AUDIT = Path('scripts/audit_operator_page_consistency.py')
REPAIR = Path('scripts/repair_operator_page_consistency_findings.py')


def test_audit_has_non_billable_delivery_guard():
    text = AUDIT.read_text(encoding='utf-8')
    assert 'DEFAULT_NON_BILLABLE_DELIVERY_PATTERNS' in text
    assert 'delivery_007' in text
    assert 'CLIENTFLOW_NON_BILLABLE_ODOO_LINES' in text
    assert 'odoo_non_billable_delivery_line_not_in_jasmin_invoice' in text
    assert 'suppress_non_billable_delivery_warnings' in text


def test_audit_ignores_acknowledged_low_noise():
    text = AUDIT.read_text(encoding='utf-8')
    assert 'clientflow_invoice_sent_evidence' in text
    assert 'invoice_sent_evidence' in text
    assert 'future_done_acknowledged_by' in text
    assert 'completed_before_due_acknowledged_at' in text


def test_repair_can_normalize_low_findings_safely():
    text = REPAIR.read_text(encoding='utf-8')
    assert 'def _select_future_due_done_tasks' in text
    assert 'def _acknowledge_future_done_tasks' in text
    assert 'def _select_invoice_sent_evidence_missing' in text
    assert 'def _mark_invoice_sent_evidence' in text
    assert '--fix-future-done-task-notes' in text
    assert '--fix-invoice-sent-evidence' in text
    assert 'clientflow_invoice_sent_evidence' in text
    assert 'status = \'ignored\'' in text  # existing safe repairs remain conservative


def test_repair_still_never_auto_fixes_critical_payment_exception():
    text = REPAIR.read_text(encoding='utf-8')
    assert 'CRITICAL_DELIVERED_WITHOUT_PAYMENT_CONFIRMED' in text
    assert 'Never auto-fixes' in text
    assert 'manual_action=Confirmar se o pagamento existe' in text
