Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python3 | |
| """ | |
| Test script for MarioGPT MCP Server | |
| """ | |
| import asyncio | |
| import json | |
| from mcp_server import mcp_server, GenerateLevelParams | |
| async def test_mcp_server(): | |
| """Test MCP server functionality""" | |
| print("=" * 60) | |
| print("MarioGPT MCP Server Test Suite") | |
| print("=" * 60) | |
| # Test 1: List tools | |
| print("\n[Test 1] Listing available tools...") | |
| try: | |
| tools = await mcp_server.list_tools() | |
| print(f"β Found {len(tools)} tools:") | |
| for tool in tools: | |
| print(f" - {tool.name}: {tool.description[:80]}...") | |
| except Exception as e: | |
| print(f"β Failed to list tools: {e}") | |
| return | |
| # Test 2: Get level suggestions | |
| print("\n[Test 2] Getting level suggestions...") | |
| try: | |
| result = await mcp_server.call_tool( | |
| name="get_level_suggestions", | |
| arguments={} | |
| ) | |
| print(f"β Retrieved suggestions:") | |
| print(result[0].text[:200] + "...") | |
| except Exception as e: | |
| print(f"β Failed to get suggestions: {e}") | |
| # Test 3: Validate parameters schema | |
| print("\n[Test 3] Validating parameter schema...") | |
| try: | |
| schema = GenerateLevelParams.model_json_schema() | |
| print(f"β Schema validation passed") | |
| print(f" Properties: {', '.join(schema['properties'].keys())}") | |
| except Exception as e: | |
| print(f"β Schema validation failed: {e}") | |
| # Test 4: Parameter validation | |
| print("\n[Test 4] Testing parameter validation...") | |
| # Valid parameters | |
| try: | |
| params = GenerateLevelParams( | |
| prompt="many pipes, some enemies, high elevation", | |
| temperature=1.5, | |
| level_size=1000 | |
| ) | |
| print(f"β Valid parameters accepted") | |
| except Exception as e: | |
| print(f"β Valid parameters rejected: {e}") | |
| # Invalid temperature (too high) | |
| try: | |
| params = GenerateLevelParams( | |
| prompt="test", | |
| temperature=5.0 | |
| ) | |
| print(f"β Invalid temperature accepted (should fail)") | |
| except Exception as e: | |
| print(f"β Invalid temperature correctly rejected") | |
| # Invalid level size (too small) | |
| try: | |
| params = GenerateLevelParams( | |
| prompt="test", | |
| level_size=50 | |
| ) | |
| print(f"β Invalid level_size accepted (should fail)") | |
| except Exception as e: | |
| print(f"β Invalid level_size correctly rejected") | |
| # Test 5: Generate level (mock test - requires model) | |
| print("\n[Test 5] Testing level generation (requires model)...") | |
| print("β Skipping actual generation test (requires full model setup)") | |
| print(" To test generation, run:") | |
| print(" python mcp_server.py") | |
| print(" Then use MCP client to call generate_mario_level") | |
| print("\n" + "=" * 60) | |
| print("Test suite completed!") | |
| print("=" * 60) | |
| async def test_mcp_protocol(): | |
| """Test MCP protocol compatibility""" | |
| print("\n[Protocol Test] MCP Protocol Compatibility") | |
| print("-" * 60) | |
| # Check server initialization | |
| print("β Server initialized:", mcp_server.name) | |
| # Check tool registration | |
| tools = await mcp_server.list_tools() | |
| print(f"β Tools registered: {len(tools)}") | |
| # Check tool schemas | |
| for tool in tools: | |
| schema = tool.inputSchema | |
| if schema: | |
| print(f"β {tool.name} has valid input schema") | |
| else: | |
| print(f"β {tool.name} has no input schema") | |
| print("-" * 60) | |
| if __name__ == "__main__": | |
| print("\nStarting MCP Server Tests...\n") | |
| try: | |
| # Run async tests | |
| asyncio.run(test_mcp_server()) | |
| asyncio.run(test_mcp_protocol()) | |
| print("\nβ All tests completed successfully!") | |
| except KeyboardInterrupt: | |
| print("\n\nβ Tests interrupted by user") | |
| except Exception as e: | |
| print(f"\nβ Test suite failed: {e}") | |
| import traceback | |
| traceback.print_exc() | |