"""Tests for MCP resources."""

from __future__ import annotations

import json

from ai4gh.mcp.resources import SPEC_DESCRIPTIONS, get_resource, list_resources


class TestListResources:
    def test_returns_list(self) -> None:
        resources = list_resources()
        assert isinstance(resources, list)

    def test_includes_specs_index(self) -> None:
        resources = list_resources()
        uris = [str(r.uri) for r in resources]
        assert "ga4gh://specs" in uris

    def test_includes_individual_specs(self) -> None:
        resources = list_resources()
        uris = [str(r.uri) for r in resources]
        for spec_id in SPEC_DESCRIPTIONS:
            assert f"ga4gh://specs/{spec_id}" in uris

    def test_total_count(self) -> None:
        resources = list_resources()
        # 1 index + N individual specs
        assert len(resources) == 1 + len(SPEC_DESCRIPTIONS)

    def test_resource_has_name_and_description(self) -> None:
        resources = list_resources()
        for r in resources:
            assert r.name
            assert r.description
            assert r.mimeType == "application/json"


class TestGetResource:
    def test_specs_index(self) -> None:
        result = json.loads(get_resource("ga4gh://specs"))
        assert "specifications" in result
        assert result["total"] == len(SPEC_DESCRIPTIONS)

    def test_individual_spec(self) -> None:
        result = json.loads(get_resource("ga4gh://specs/beacon"))
        assert result["name"] == "Beacon v2"
        assert result["version"] == "2.0.0"

    def test_individual_spec_drs(self) -> None:
        result = json.loads(get_resource("ga4gh://specs/drs"))
        assert result["name"] == "Data Repository Service (DRS)"

    def test_unknown_spec(self) -> None:
        result = json.loads(get_resource("ga4gh://specs/nonexistent"))
        assert "error" in result

    def test_unknown_uri(self) -> None:
        result = json.loads(get_resource("ga4gh://unknown"))
        assert "error" in result

    def test_all_specs_retrievable(self) -> None:
        for spec_id, spec_info in SPEC_DESCRIPTIONS.items():
            result = json.loads(get_resource(f"ga4gh://specs/{spec_id}"))
            assert result["name"] == spec_info["name"]
            assert result["version"] == spec_info["version"]
