from pathlib import Path


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


def test_reply_panel_exposes_save_and_revise_actions():
    tasks = read("app/admin_ui/pages/tasks.py")
    assert "v4928.1.5.16" in tasks
    assert '/reply-draft/save' in tasks
    assert '/reply-draft/revise' in tasks
    assert 'Aplicar correção com IA' in tasks
    assert 'Guardar rascunho' in tasks
    assert 'syncReplyDraftBody' in tasks


def test_reply_service_persists_edits_and_revises_with_recent_context():
    service = read("app/reply_assistant_service.py")
    assert "def save_reply_draft" in service
    assert "def revise_reply_draft" in service
    assert "def recent_conversation_context_for_task" in service
    assert "recent_conversation_context" in service
    assert "final_body_saved_before_send" in service


def test_openai_revision_agent_has_guardrails():
    agent = read("app/email_reply_agent_service.py")
    assert "def revise_email_reply_agent" in agent
    assert "clientflow_blif_email_reply_revision" in agent
    assert "Não deve inventar preços nem URLs" in agent or "Não inventar preços" in agent
    assert "historico_recente_conversa" in agent
