"""Tests for MCP resources (config, position, firmware, motor-dynamics, el-limits)."""

import json

import pytest
from fastmcp import Client


@pytest.mark.anyio
async def test_config_resource(mcp_client: Client):
    contents = await mcp_client.read_resource("birdcage://config")
    data = json.loads(contents[0].text)

    assert data["demo_mode"] is True
    assert data["connected"] is True
    assert data["firmware"] == "g2"
    assert data["serial_port"] == "/dev/demo"


@pytest.mark.anyio
async def test_position_resource(mcp_client: Client):
    contents = await mcp_client.read_resource("birdcage://position")
    data = json.loads(contents[0].text)

    assert "azimuth" in data
    assert "elevation" in data
    assert isinstance(data["azimuth"], float)
    assert isinstance(data["elevation"], float)
    # DemoDevice starts at AZ=180, EL=45 (approximately, with settling noise).
    assert 170.0 < data["azimuth"] < 190.0
    assert 35.0 < data["elevation"] < 55.0


@pytest.mark.anyio
async def test_firmware_resource(mcp_client: Client):
    contents = await mcp_client.read_resource("birdcage://firmware")
    text = contents[0].text

    assert "02.02.48" in text
    assert "TWELINCH" in text
    assert "K60-144pin" in text


@pytest.mark.anyio
async def test_motor_dynamics_resource(mcp_client: Client):
    contents = await mcp_client.read_resource("birdcage://motor-dynamics")
    data = json.loads(contents[0].text)

    assert data["az_max_vel"] == 65.0
    assert data["el_max_vel"] == 45.0
    assert data["az_accel"] == 400.0
    assert data["el_accel"] == 400.0


@pytest.mark.anyio
async def test_el_limits_resource(mcp_client: Client):
    contents = await mcp_client.read_resource("birdcage://el-limits")
    data = json.loads(contents[0].text)

    assert data["min"] == 18.0
    assert data["max"] == 65.0
    assert data["home"] == 65.0
