"""Tests for MCP server.

Generated by GRIMLOCK MCP Factory
"""

import pytest

from loom_mcp.server import list_tools, call_tool


class TestServerListTools:
    """Tests for server list_tools."""

    @pytest.mark.asyncio
    async def test_list_tools_returns_all_tools(self) -> None:
        """Test that all expected tools are listed."""
        tools = await list_tools()

        assert len(tools) == 4

        tool_names = {tool.name for tool in tools}
        expected_tools = {
            "list_recorded_videos",
            "get_video",
            "edit_video",
            "merge_videos",
        }
        assert tool_names == expected_tools

    @pytest.mark.asyncio
    async def test_list_tools_has_descriptions(self) -> None:
        """Test that all tools have descriptions."""
        tools = await list_tools()

        for tool in tools:
            assert tool.description
            assert len(tool.description) > 10

    @pytest.mark.asyncio
    async def test_list_tools_has_input_schemas(self) -> None:
        """Test that all tools have input schemas."""
        tools = await list_tools()

        for tool in tools:
            assert tool.inputSchema is not None
            assert "type" in tool.inputSchema


class TestServerCallTool:
    """Tests for server call_tool."""

    @pytest.mark.asyncio
    async def test_call_unknown_tool(self) -> None:
        """Test error for unknown tool name."""
        with pytest.raises(ValueError, match="Unknown tool"):
            await call_tool("nonexistent_tool", {})

    @pytest.mark.asyncio
    async def test_call_tool_routes_correctly(
        self,
        mock_env: None,
        mock_httpx_client,
        sample_videos_response: dict,
    ) -> None:
        """Test that call_tool routes to correct handler."""
        import httpx

        mock_httpx_client.get("https://api.loom.com/v1/videos").mock(
            return_value=httpx.Response(200, json=sample_videos_response)
        )

        result = await call_tool("list_recorded_videos", {})
        assert len(result) == 1
        assert result[0].type == "text"


class TestToolInputSchemas:
    """Tests for tool input schemas."""

    @pytest.mark.asyncio
    async def test_list_recorded_videos_schema(self) -> None:
        """Test list_recorded_videos schema structure."""
        tools = await list_tools()
        tool = next(t for t in tools if t.name == "list_recorded_videos")

        schema = tool.inputSchema
        assert schema["type"] == "object"
        assert "properties" in schema
        assert "limit" in schema["properties"]
        assert "offset" in schema["properties"]
        assert "folder_id" in schema["properties"]

    @pytest.mark.asyncio
    async def test_get_video_schema(self) -> None:
        """Test get_video schema structure."""
        tools = await list_tools()
        tool = next(t for t in tools if t.name == "get_video")

        schema = tool.inputSchema
        assert schema["type"] == "object"
        assert "properties" in schema
        assert "video_id" in schema["properties"]
        assert "video_id" in schema.get("required", [])

    @pytest.mark.asyncio
    async def test_edit_video_schema(self) -> None:
        """Test edit_video schema structure."""
        tools = await list_tools()
        tool = next(t for t in tools if t.name == "edit_video")

        schema = tool.inputSchema
        assert schema["type"] == "object"
        assert "properties" in schema
        assert "video_id" in schema["properties"]
        assert "editing_details" in schema["properties"]
        assert "video_id" in schema.get("required", [])
        assert "editing_details" in schema.get("required", [])

    @pytest.mark.asyncio
    async def test_merge_videos_schema(self) -> None:
        """Test merge_videos schema structure."""
        tools = await list_tools()
        tool = next(t for t in tools if t.name == "merge_videos")

        schema = tool.inputSchema
        assert schema["type"] == "object"
        assert "properties" in schema
        assert "video_ids" in schema["properties"]
        assert "title" in schema["properties"]
        assert "video_ids" in schema.get("required", [])
