"""Tests for ComputerTool20251124."""

import pytest
from unittest.mock import AsyncMock, patch, MagicMock
from nanobot.agent.tools.anthropic.computer import ComputerTool20251124
from nanobot.agent.tools.anthropic.base import ToolResult


@pytest.mark.asyncio
async def test_computer_tool_screenshot():
    """Test computer tool can take screenshot."""
    tool = ComputerTool20251124(vnc_host="localhost", vnc_port=5900)

    # Mock VNC client
    with patch('nanobot.agent.tools.anthropic.computer.vnc_api.connect') as mock_connect:
        mock_client = MagicMock()
        # Mock captureScreen to write fake PNG data to file path
        def fake_capture(path):
            from pathlib import Path
            Path(path).write_bytes(b"fake_png_data")
        mock_client.captureScreen = MagicMock(side_effect=fake_capture)
        mock_client.mouseMove = MagicMock()
        mock_client.keyPress = MagicMock()
        mock_client.refreshScreen = MagicMock()
        mock_connect.return_value = mock_client

        result = await tool(action="screenshot")

        assert isinstance(result, ToolResult)
        assert result.base64_image is not None
        assert len(result.base64_image) > 0


@pytest.mark.asyncio
async def test_computer_tool_mouse_move():
    """Test computer tool can move mouse."""
    tool = ComputerTool20251124(vnc_host="localhost", vnc_port=5900)

    with patch('nanobot.agent.tools.anthropic.computer.vnc_api.connect') as mock_connect:
        mock_client = MagicMock()
        mock_client.mouseMove = MagicMock()
        mock_connect.return_value = mock_client

        result = await tool(action="mouse_move", coordinate=[100, 200])

        assert isinstance(result, ToolResult)
        assert result.error is None
        mock_client.mouseMove.assert_called_once_with(100, 200)


@pytest.mark.asyncio
async def test_computer_tool_key():
    """Test computer tool can press keys."""
    tool = ComputerTool20251124(vnc_host="localhost", vnc_port=5900)

    with patch('nanobot.agent.tools.anthropic.computer.vnc_api.connect') as mock_connect:
        mock_client = MagicMock()
        mock_client.keyPress = MagicMock()
        mock_connect.return_value = mock_client

        result = await tool(action="key", text="Return")

        assert isinstance(result, ToolResult)
        assert result.error is None
        # Implementation converts keys to lowercase
        mock_client.keyPress.assert_called_once_with("return")


def test_computer_tool_to_params():
    """Test computer tool returns correct params."""
    tool = ComputerTool20251124(vnc_host="localhost", vnc_port=5900)
    params = tool.to_params()

    assert params["type"] == "computer_20251124"
    assert params["name"] == "computer"
