"""Tests for MCP server.
Note: FastMCP decorators wrap functions, so we test the underlying function logic
by importing the functions directly. The decorator preserves the function as callable.
"""
import pytest
from unittest.mock import Mock, patch
from src.server import list_collections, get_collection_apis, get_api_details
class TestMCPServer:
"""Test cases for MCP server tools."""
@pytest.fixture
def mock_client(self):
"""Create a mock API client."""
client = Mock()
client.list_collections.return_value = {"collections": []}
client.get_collection_apis.return_value = {"apis": []}
client.get_api_details.return_value = {"api": {}}
return client
def test_list_collections_success(self, mock_client):
"""Test successful collection listing."""
with patch("src.server.get_client", return_value=mock_client):
# FastMCP wraps functions in FunctionTool, access underlying function
func = list_collections.fn if hasattr(list_collections, 'fn') else list_collections
result = func(page=1, per_page=10)
assert result["success"] is True
assert "data" in result
mock_client.list_collections.assert_called_once()
def test_get_collection_apis_success(self, mock_client):
"""Test successful API retrieval from collection."""
with patch("src.server.get_client", return_value=mock_client):
func = get_collection_apis.fn if hasattr(get_collection_apis, 'fn') else get_collection_apis
result = func(collection_id="test-id")
assert result["success"] is True
assert "data" in result
mock_client.get_collection_apis.assert_called_once()
def test_get_collection_apis_missing_id(self):
"""Test error handling for missing collection_id."""
func = get_collection_apis.fn if hasattr(get_collection_apis, 'fn') else get_collection_apis
result = func(collection_id="")
assert result["success"] is False
assert "error" in result
def test_get_api_details_success(self, mock_client):
"""Test successful API details retrieval."""
with patch("src.server.get_client", return_value=mock_client):
func = get_api_details.fn if hasattr(get_api_details, 'fn') else get_api_details
result = func(api_id="test-id")
assert result["success"] is True
assert "data" in result
mock_client.get_api_details.assert_called_once()
def test_get_api_details_missing_id(self):
"""Test error handling for missing api_id."""
func = get_api_details.fn if hasattr(get_api_details, 'fn') else get_api_details
result = func(api_id="")
assert result["success"] is False
assert "error" in result