"""Tests for chat MCP tools."""
import json
import pytest
from unittest.mock import AsyncMock, MagicMock, patch


@pytest.fixture
def client(mock_client):
    return mock_client


@pytest.mark.asyncio
async def test_send_message_creates_conversation_and_streams(client):
    from fable_mcp.tools.chat import send_message

    client.post = AsyncMock(return_value={"id": "conv-1"})

    async def fake_stream(path, **kwargs):
        for line in [
            'data: {"type": "token", "content": "Hello"}',
            'data: {"type": "token", "content": " world"}',
            'data: {"type": "done"}',
        ]:
            yield line

    client.stream_get = fake_stream

    result = await send_message(client, message="Hi", conversation_id=None)
    assert result["conversation_id"] == "conv-1"
    assert "Hello world" in result["response"]


@pytest.mark.asyncio
async def test_send_message_reuses_existing_conversation(client):
    from fable_mcp.tools.chat import send_message

    async def fake_stream(path, **kwargs):
        for line in [
            'data: {"type": "token", "content": "Reply"}',
            'data: {"type": "done"}',
        ]:
            yield line

    client.stream_get = fake_stream

    result = await send_message(client, message="Hi", conversation_id="existing-conv")
    # Should not call post to create a conversation
    client.post.assert_not_called()
    assert result["conversation_id"] == "existing-conv"


@pytest.mark.asyncio
async def test_list_conversations_calls_get(client):
    from fable_mcp.tools.chat import list_conversations
    client.get = AsyncMock(return_value={"conversations": []})
    await list_conversations(client)
    client.get.assert_called_once()
    assert "/api/chat/conversations" in client.get.call_args[0][0]
