"""Tests for nav_query tool."""

import pytest
from unittest.mock import AsyncMock, patch

from godotiq.tools.bridge.nav_query import nav_query


class TestNavQueryTool:
    @pytest.mark.asyncio
    async def test_missing_from(self):
        result = await nav_query(to_node="PrinterSlot_P1")
        assert "error" in result

    @pytest.mark.asyncio
    async def test_missing_to(self):
        result = await nav_query(from_node="Worker_1")
        assert "error" in result

    @pytest.mark.asyncio
    async def test_node_to_node(self):
        mock_bridge = AsyncMock()
        mock_bridge.request = AsyncMock(return_value={
            "reachable": True,
            "distance": 5.8,
            "direct_distance": 3.1,
            "efficiency_ratio": 0.53,
            "path_points": [[0, 0, 0], [1, 0, -1], [3, 0, -2]],
            "waypoint_count": 12,
            "from_on_navmesh": True,
            "to_on_navmesh": True,
        })
        with patch("godotiq.tools.bridge.nav_query.get_bridge", return_value=mock_bridge):
            result = await nav_query(from_node="Worker_1", to_node="PrinterSlot_P4")
            params = mock_bridge.request.call_args[1]["params"]
            assert params["from_node"] == "Worker_1"
            assert params["to_node"] == "PrinterSlot_P4"
            assert result["reachable"] is True

    @pytest.mark.asyncio
    async def test_position_to_position(self):
        mock_bridge = AsyncMock()
        mock_bridge.request = AsyncMock(return_value={"reachable": True, "distance": 4.0})
        with patch("godotiq.tools.bridge.nav_query.get_bridge", return_value=mock_bridge):
            result = await nav_query(from_position=[0, 0, 0], to_position=[3, 0, -2])
            params = mock_bridge.request.call_args[1]["params"]
            assert params["from_position"] == [0, 0, 0]
            assert params["to_position"] == [3, 0, -2]

    @pytest.mark.asyncio
    async def test_optimize_default_true(self):
        mock_bridge = AsyncMock()
        mock_bridge.request = AsyncMock(return_value={"reachable": True})
        with patch("godotiq.tools.bridge.nav_query.get_bridge", return_value=mock_bridge):
            await nav_query(from_node="A", to_node="B")
            params = mock_bridge.request.call_args[1]["params"]
            assert params["optimize"] is True
