from types import SimpleNamespace

from app.workflows.agent_graph import AgentWorkflow


def decision(code, allowed=False, reason=""):
    return SimpleNamespace(code=code, allowed=allowed, reason=reason)


def test_coer_block_is_clarification_not_fake_security_message():
    msg = AgentWorkflow._input_guardrail_user_message(
        [decision("COER", reason="fala incompreensível ou negação ambígua na transcrição")],
        {},
        "está cobrando um",
    )
    assert "incompleta ou ambígua" in msg
    assert "regra de segurança" not in msg.lower()
    assert "COER" not in msg


def test_other_input_block_has_safe_non_internal_fallback():
    msg = AgentWorkflow._input_guardrail_user_message(
        [decision("UNKNOWN_RAIL", reason="internal implementation detail")],
        {},
        "texto",
    )
    assert "reformular" in msg.lower()
    assert "internal implementation detail" not in msg


def test_blocked_input_is_routed_through_output_guardrails():
    src = open("app/workflows/agent_graph.py", encoding="utf-8").read()
    assert '{"blocked": "output_guardrails", "continue": "load_long_term_memory"}' in src
    assert '{"blocked": "persist", "continue": "judge"}' in src


def test_blocked_input_clears_stale_tool_state():
    src = open("app/workflows/agent_graph.py", encoding="utf-8").read()
    assert '"mcp_tools": []' in src
    assert '"mcp_results": []' in src
    assert '"intent": "input_guardrail_blocked"' in src
