from unittest.mock import MagicMock, patch

import pytest

from acp.decorators import governed_tool
from acp.exceptions import PolicyDenied


@patch("acp.client.requests.post")
def test_governed_tool_allows_execution(mock_post: MagicMock) -> None:
    mock_post.return_value = MagicMock()
    mock_post.return_value.raise_for_status = MagicMock()
    mock_post.return_value.json.return_value = {
        "decision": "allow",
        "trace_id": "t1",
    }

    @governed_tool(agent_id="a1", tool="refund_api", action="issue_refund")
    def refund_api(amount: int) -> int:
        return amount + 1

    assert refund_api(amount=5) == 6
    assert mock_post.called


@patch("acp.client.requests.post")
def test_governed_tool_blocks_on_deny(mock_post: MagicMock) -> None:
    mock_post.return_value = MagicMock()
    mock_post.return_value.raise_for_status = MagicMock()
    mock_post.return_value.json.return_value = {
        "decision": "deny",
        "trace_id": "t2",
        "reason": "nope",
    }

    @governed_tool(agent_id="a1", tool="refund_api", action="issue_refund")
    def refund_api(amount: int) -> int:
        return amount + 1

    with pytest.raises(PolicyDenied):
        refund_api(amount=5)
