"""Tests for the RemnicClient HTTP methods."""

import pytest
from unittest.mock import AsyncMock, MagicMock

from remnic_hermes import EngramClient
from remnic_hermes.client import RemnicClient


@pytest.fixture
def client():
    """Create a client with test config."""
    return RemnicClient(host="127.0.0.1", port=4318, token="test-token", client_id="hermes")


class TestClientInit:
    def test_base_url(self, client):
        # HTTP path still uses the legacy /engram/v1 prefix during the compat window.
        assert client.base_url == "http://127.0.0.1:4318/engram/v1"

    def test_token_set(self, client):
        assert client.token == "test-token"

    def test_client_id(self, client):
        assert client.client_id == "hermes"


class TestClientClose:
    @pytest.mark.asyncio
    async def test_close_calls_aclose(self, client):
        client._http = MagicMock()
        client._http.aclose = AsyncMock()
        await client.close()
        client._http.aclose.assert_awaited_once()


class TestClientRecall:
    @pytest.mark.asyncio
    async def test_recall_omits_mode_by_default(self, client):
        response = MagicMock()
        response.json.return_value = {"context": "memory", "count": 1}
        client._http = MagicMock()
        client._http.post = AsyncMock(return_value=response)

        await client.recall("what did we decide", session_key="hermes-session")

        client._http.post.assert_awaited_once_with(
            "/recall",
            json={
                "query": "what did we decide",
                "sessionKey": "hermes-session",
                "topK": 8,
            },
        )


class TestClientLcmSearch:
    @pytest.mark.asyncio
    async def test_lcm_search_posts_to_lcm_endpoint(self, client):
        response = MagicMock()
        response.json.return_value = {"query": "archive", "results": [], "count": 0}
        client._http = MagicMock()
        client._http.post = AsyncMock(return_value=response)

        await client.lcm_search(
            "archive",
            session_key="hermes-session",
            namespace="research",
            limit=5,
        )

        client._http.post.assert_awaited_once_with(
            "/lcm/search",
            json={
                "query": "archive",
                "sessionKey": "hermes-session",
                "namespace": "research",
                "limit": 5,
            },
        )


class TestLegacyAlias:
    def test_engram_client_is_alias(self):
        """The legacy EngramClient name resolves to RemnicClient."""
        assert EngramClient is RemnicClient
