File size: 4,030 Bytes
49b8c43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/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()