# #region Test.AgentChat.Middleware [C:3] [TYPE Module] [SEMANTICS test,agent,middleware,audit]
# @BRIEF Tests for agent/middleware.py — log_tool_event.
# @RELATION BINDS_TO -> [AgentChat.Middleware]

from pathlib import Path
import sys

sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src"))

from unittest.mock import MagicMock, patch
import pytest


# #region test_log_tool_event [C:2] [TYPE Function]
# @BRIEF Test log_tool_event for various event types.
class TestLogToolEvent:
    @pytest.mark.asyncio
    async def test_logs_tool_start(self):
        from src.agent.middleware import log_tool_event
        event = {
            "event": "on_tool_start",
            "name": "migrate",
            "data": {"input": {"dashboard_id": "42"}},
        }
        with patch("src.agent.middleware.get_user_jwt", return_value="user-token"):
            await log_tool_event(event, "conv-1")
            # No exception = success

    @pytest.mark.asyncio
    async def test_logs_tool_end(self):
        from src.agent.middleware import log_tool_event
        event = {
            "event": "on_tool_end",
            "name": "migrate",
            "data": {"output": "success"},
        }
        with patch("src.agent.middleware.get_user_jwt", return_value="user-token"):
            await log_tool_event(event, "conv-1")

    @pytest.mark.asyncio
    async def test_logs_tool_error(self):
        from src.agent.middleware import log_tool_event
        event = {
            "event": "on_tool_error",
            "name": "migrate",
            "data": {"error": "Connection failed"},
        }
        with patch("src.agent.middleware.get_user_jwt", return_value="user-token"):
            await log_tool_event(event, "conv-1")

    @pytest.mark.asyncio
    async def test_logs_without_user_jwt(self):
        from src.agent.middleware import log_tool_event
        event = {
            "event": "on_tool_start",
            "name": "test_tool",
            "data": {"input": {}},
        }
        with patch("src.agent.middleware.get_user_jwt", return_value=None):
            await log_tool_event(event, "conv-1")

    @pytest.mark.asyncio
    async def test_handles_missing_data_key(self):
        from src.agent.middleware import log_tool_event
        event = {"event": "on_tool_start", "name": "test_tool"}
        with patch("src.agent.middleware.get_user_jwt", return_value=None):
            await log_tool_event(event, "conv-1")

    @pytest.mark.asyncio
    async def test_handles_unknown_event_kind(self):
        from src.agent.middleware import log_tool_event
        event = {"event": "on_custom_event", "name": "custom"}
        with patch("src.agent.middleware.get_user_jwt", return_value=None):
            await log_tool_event(event, "conv-1")

    @pytest.mark.asyncio
    async def test_truncates_long_input(self):
        from src.agent.middleware import log_tool_event
        long_input = "x" * 1000
        event = {
            "event": "on_tool_start",
            "name": "big_tool",
            "data": {"input": long_input},
        }
        with patch("src.agent.middleware.get_user_jwt", return_value="token"):
            await log_tool_event(event, "conv-1")
# #endregion test_log_tool_event
# #endregion Test.AgentChat.Middleware
