from pathlib import Path


def test_materializer_limits_to_send_invoice_and_is_idempotent():
    text = Path('app/opportunity_action_task_materializer.py').read_text()
    assert 'MATERIALIZED_ACTIONS = {"SEND_INVOICE"}' in text
    assert 'status = \'pending\'' in text
    assert 'task:next_action:' in text
    assert 'ON CONFLICT (idempotency_key) DO NOTHING' in text


def test_opportunity_page_materializes_next_action_task_and_refreshes_tasks():
    text = Path('app/admin_ui/pages/opportunities.py').read_text()
    assert 'ensure_pending_task_for_next_action' in text
    assert 'materialized_task.get("created")' in text
    assert 'tasks = list_opportunity_tasks(opportunity_id, limit=100)' in text


def test_repair_script_uses_central_next_action():
    text = Path('scripts/repair_missing_send_invoice_tasks.py').read_text()
    assert 'get_opportunity_next_action' in text
    assert 'ensure_pending_task_for_next_action' in text
    assert 'code == "SEND_INVOICE"' in text
