"""Tests for utility tools."""


import pytest
import respx
from httpx import Response

from crustdata_mcp.client import CrustdataClient


class TestCreditsCheckTool:
    """Tests for crustdata_credits_check tool."""

    @pytest.mark.asyncio
    @respx.mock
    async def test_credits_check_success(self):
        """Test successful credits check."""
        # Mock the API response
        expected_response = {
            "credits": 1000,
            "tier": "professional",
            "monthly_limit": 5000,
        }
        respx.get("https://api.crustdata.com/user/credits").mock(
            return_value=Response(200, json=expected_response)
        )

        # Create client and call the endpoint directly
        async with CrustdataClient("test-token") as client:
            result = await client.get("/user/credits")

        assert result == expected_response
        assert result["credits"] == 1000
        assert result["tier"] == "professional"

    @pytest.mark.asyncio
    @respx.mock
    async def test_credits_check_auth_error(self):
        """Test credits check with invalid token."""
        from crustdata_mcp.client import CrustdataAuthError

        respx.get("https://api.crustdata.com/user/credits").mock(
            return_value=Response(401, json={"error": "Invalid token"})
        )

        async with CrustdataClient("invalid-token") as client:
            with pytest.raises(CrustdataAuthError):
                await client.get("/user/credits")

    def test_tool_is_registered(self):
        """Test that credits check tool is registered with server."""
        from crustdata_mcp.server import mcp

        tools = list(mcp._tool_manager._tools.keys())
        assert "crustdata_credits_check" in tools

    def test_tool_has_description(self):
        """Test that tool has proper description."""
        from crustdata_mcp.server import mcp

        tool = mcp._tool_manager._tools.get("crustdata_credits_check")
        assert tool is not None
        assert "credit" in tool.description.lower()
