#!/usr/bin/env python3
import sys
import os
import json
from unittest.mock import patch, MagicMock
# Add parent directory to path to import server
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from server import SleeperMCP
def run_smoke_test():
"""
Run a smoke test to verify that all tools can be called successfully.
This test mocks all API calls to avoid actual network requests.
"""
print("Running smoke test for Sleeper API MCP...")
# Create MCP instance
mcp = SleeperMCP()
# Get list of all tools
list_tools_message = {
"jsonrpc": "2.0",
"id": 1,
"method": "listTools"
}
list_tools_response = mcp.handle_message(list_tools_message)
if "error" in list_tools_response:
print(f"ERROR: Failed to list tools: {list_tools_response['error']}")
return False
tools = list_tools_response["result"]
print(f"Found {len(tools)} tools")
# Mock the requests.get function for all tests
with patch('requests.get') as mock_get:
# Set up mock response
mock_response = MagicMock()
mock_response.json.return_value = {"test": "data"}
mock_response.raise_for_status.return_value = None
mock_get.return_value = mock_response
# Test each tool
success_count = 0
for tool in tools:
tool_name = tool["name"]
print(f"Testing tool: {tool_name}")
# Create test parameters based on tool name
params = create_test_params(tool_name)
# Create message to invoke tool
message = {
"jsonrpc": "2.0",
"id": 2,
"method": tool_name,
"params": params
}
# Call the tool
try:
response = mcp.handle_message(message)
if "error" in response:
print(f" ERROR: Tool returned error: {response['error']}")
else:
print(f" SUCCESS: Tool returned response")
success_count += 1
except Exception as e:
print(f" ERROR: Exception while calling tool: {str(e)}")
# Print summary
print(f"\nSmoke test completed: {success_count}/{len(tools)} tools succeeded")
return success_count == len(tools)
def create_test_params(tool_name):
"""
Create test parameters based on the tool name.
"""
if tool_name == "getUserInfo":
return {"username_or_user_id": "test_user"}
elif tool_name == "getUserLeagues" or tool_name == "getUserDrafts":
return {"user_id": "12345", "sport": "nfl", "season": "2023"}
elif tool_name == "getLeagueInfo" or tool_name == "getLeagueRosters" or tool_name == "getLeagueUsers" or \
tool_name == "getLeagueWinnersBracket" or tool_name == "getLeagueLosersBracket" or \
tool_name == "getLeagueTradedPicks" or tool_name == "getLeagueDrafts":
return {"league_id": "67890"}
elif tool_name == "getLeagueMatchups" or tool_name == "getLeagueTransactions":
return {"league_id": "67890", "week": 1}
elif tool_name == "getDraftInfo" or tool_name == "getDraftPicks" or tool_name == "getDraftTradedPicks":
return {"draft_id": "12345"}
elif tool_name == "getAllPlayers":
return {"sport": "nfl"}
elif tool_name == "getTrendingPlayers":
return {"sport": "nfl", "type": "add", "lookback_hours": 24, "limit": 10}
elif tool_name == "getNFLState":
return {}
else:
return {}
if __name__ == "__main__":
success = run_smoke_test()
sys.exit(0 if success else 1)