"""Tests for refget client."""

from __future__ import annotations

import pytest
from pytest_httpx import HTTPXMock

from ai4gh.specs.refget.client import RefgetClient


@pytest.fixture
def refget_client() -> RefgetClient:
    return RefgetClient(base_url="https://refget.example.com")


class TestRefgetClient:
    def test_spec_attrs(self) -> None:
        client = RefgetClient(base_url="https://example.com")
        assert client.spec_name == "refget"
        assert client.spec_version == "2.0.0"
        assert client.base_path == "/sequence"

    def test_url(self) -> None:
        client = RefgetClient(base_url="https://example.com")
        assert client.url == "https://example.com/sequence"

    async def test_get_sequence(self, refget_client: RefgetClient, httpx_mock: HTTPXMock) -> None:
        httpx_mock.add_response(
            url="https://refget.example.com/sequence/abc123",
            text="ACGTACGTACGT",
        )
        seq = await refget_client.get_sequence("abc123")
        assert seq == "ACGTACGTACGT"

    async def test_get_sequence_with_range(
        self, refget_client: RefgetClient, httpx_mock: HTTPXMock
    ) -> None:
        httpx_mock.add_response(text="ACGT")
        seq = await refget_client.get_sequence("abc123", start=0, end=4)
        assert seq == "ACGT"
        request = httpx_mock.get_requests()[0]
        url_str = str(request.url)
        assert "start=0" in url_str
        assert "end=4" in url_str

    async def test_get_metadata(self, refget_client: RefgetClient, httpx_mock: HTTPXMock) -> None:
        httpx_mock.add_response(
            json={
                "metadata": {
                    "id": "abc123",
                    "length": 248956422,
                    "md5": "6aef897c3d6ff0c78aff06ac189178dd",
                    "aliases": [
                        {"alias": "chr1", "naming_authority": "UCSC"},
                    ],
                }
            }
        )
        metadata = await refget_client.get_metadata("abc123")
        assert metadata.id == "abc123"
        assert metadata.length == 248956422
        assert metadata.aliases is not None
        assert len(metadata.aliases) == 1

    async def test_get_metadata_flat(
        self, refget_client: RefgetClient, httpx_mock: HTTPXMock
    ) -> None:
        httpx_mock.add_response(
            json={
                "id": "abc123",
                "length": 1000,
            }
        )
        metadata = await refget_client.get_metadata("abc123")
        assert metadata.id == "abc123"
        assert metadata.length == 1000
