"""MCP server tests, in memory with the SDK's Client. Skipped unless `pip install "mcp[cli]"` (v2)."""

import pytest

pytest.importorskip("mcp.server", reason="the MCP Python SDK v2 is an optional dependency")

from mcp import Client  # noqa: E402

from copilot.mcp_server import mcp  # noqa: E402


@pytest.fixture
def anyio_backend():
    return "asyncio"


@pytest.mark.anyio
async def test_tools_are_listed_with_schemas():
    async with Client(mcp, raise_exceptions=True) as client:
        names = [t.name for t in (await client.list_tools()).tools]
        assert names == ["search_docs", "ask"]


@pytest.mark.anyio
async def test_search_and_unknown_course():
    async with Client(mcp, raise_exceptions=True) as client:
        ok = await client.call_tool("search_docs", {"query": "keyset pagination", "course": "sql"})
        assert not ok.is_error and ok.structured_content["result"][0]["url"].startswith("sql/")
        bad = await client.call_tool("search_docs", {"query": "x", "course": "cobol"})
        assert bad.is_error and "Valid values" in bad.content[0].text
