"""
Smoke tests for the MCP server stdio transport.

Spawns the shaped-mcp server as a subprocess and communicates via stdin/stdout
using the MCP protocol. These tests only verify that the stdio transport works
end-to-end — that tools are advertised and callable via the protocol. Tool
correctness (CRUD, error handling, all operations) is covered by test_server_e2e.py.

No deployed infrastructure needed — the server process inherits SHAPED_API_KEY
from the environment. All tests are @pytest.mark.integration and skip without
SHAPED_API_KEY.
"""

import json
import os

import pytest

from mcp.client.session import ClientSession
from mcp.client.stdio import StdioServerParameters, stdio_client

pytestmark = pytest.mark.skipif(
    not os.environ.get("SHAPED_API_KEY"), reason="SHAPED_API_KEY not set"
)


@pytest.mark.integration
async def test_stdio_list_tools():
    """Spawn server via stdio and verify expected tools are advertised."""
    async with stdio_client(
        StdioServerParameters(
            command="python", args=["-m", "shaped_mcp"], env=dict(os.environ)
        )
    ) as (r, w):
        async with ClientSession(r, w) as session:
            await session.initialize()
            result = await session.list_tools()
            tool_names = {t.name for t in result.tools}
            assert "shaped_list_engines" in tool_names
            assert "shaped_query" in tool_names
            assert "shaped_fetch_docs" in tool_names


@pytest.mark.integration
async def test_stdio_list_resources():
    """Spawn server via stdio and verify expected resources are advertised."""
    async with stdio_client(
        StdioServerParameters(
            command="python", args=["-m", "shaped_mcp"], env=dict(os.environ)
        )
    ) as (r, w):
        async with ClientSession(r, w) as session:
            await session.initialize()
            result = await session.list_resources()
            uris = {str(r.uri) for r in result.resources}
            assert "shaped://account" in uris
            assert "shaped://docs/shapedql" in uris


@pytest.mark.integration
async def test_stdio_list_prompts():
    """Spawn server via stdio and verify expected prompts are advertised."""
    async with stdio_client(
        StdioServerParameters(
            command="python", args=["-m", "shaped_mcp"], env=dict(os.environ)
        )
    ) as (r, w):
        async with ClientSession(r, w) as session:
            await session.initialize()
            result = await session.list_prompts()
            prompt_names = {p.name for p in result.prompts}
            assert "shaped-build-query" in prompt_names
            assert "shaped-engine-data-summary" in prompt_names


@pytest.mark.integration
async def test_stdio_call_list_engines():
    """Call shaped_list_engines via stdio transport end-to-end."""
    async with stdio_client(
        StdioServerParameters(
            command="python", args=["-m", "shaped_mcp"], env=dict(os.environ)
        )
    ) as (r, w):
        async with ClientSession(r, w) as session:
            await session.initialize()
            result = await session.call_tool("shaped_list_engines", {})
            data = json.loads(result.content[0].text)
            assert "engines" in data
            assert isinstance(data["engines"], list)
