from pathlib import Path

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


def test_invoice_send_guard_does_not_require_pending_task():
    rules = (ROOT / 'app/domain/opportunity_flow/rules.py').read_text(encoding='utf-8')
    assert 'if e.has_invoice and e.payment_confirmed and e.invoice_sent is not True:' in rules
    assert 'e.invoice_sent is not True and (e.invoice_sent is False or e.pending_task_action_code == ACTION_SEND_INVOICE)' not in rules
    assert 'envio sempre que não exista evidência local de envio do PDF ao cliente' in rules


def test_after_delivery_invoice_send_guard_does_not_require_pending_task():
    rules = (ROOT / 'app/domain/opportunity_flow/rules.py').read_text(encoding='utf-8')
    assert 'if e.invoice_sent is not True:' in rules
    assert 'if e.invoice_sent is False or e.pending_task_action_code == ACTION_SEND_INVOICE:' not in rules


def test_operator_summary_does_not_fallback_to_stale_last_action_first():
    page = (ROOT / 'app/admin_ui/pages/opportunities.py').read_text(encoding='utf-8')
    assert 'display_next_action_code' in page
    assert "display_next_action_code = 'SHIP_ORDER'" in page
    assert "display_next_action_code = 'SEND_INVOICE'" in page
    assert "esc(display_next_action_code)" in page
