Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Test script to validate MCP integration in TTS service. | |
| This script tests the MCP functionality without requiring GPU or full model loading. | |
| """ | |
| import asyncio | |
| import json | |
| import sys | |
| import os | |
| # Add the current directory to the path so we can import from app.py | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| async def test_mcp_integration(): | |
| """Test the MCP integration functionality""" | |
| print("π§ͺ Testing TTS MCP Integration...") | |
| try: | |
| # Try to import the MCP components from our app | |
| from app import MCP_AVAILABLE, mcp_server | |
| print(f"β MCP Availability: {'Available' if MCP_AVAILABLE else 'Not Available'}") | |
| if not MCP_AVAILABLE: | |
| print("β οΈ MCP not available - this is expected if mcp package is not installed") | |
| print("π To install MCP: pip install mcp>=1.0.0") | |
| return True | |
| if mcp_server is None: | |
| print("β MCP server not initialized properly") | |
| return False | |
| print("β MCP server instance created successfully") | |
| # Test tool listing (this should work without GPU) | |
| try: | |
| # Import the handler function | |
| from app import handle_list_tools | |
| # Get the list of tools | |
| tools = await handle_list_tools() | |
| print(f"β Available MCP tools ({len(tools)}):") | |
| for tool in tools: | |
| print(f" - {tool.name}: {tool.description}") | |
| # Verify we have the expected tools | |
| expected_tools = {"tts_synthesize", "tts_batch_synthesize", "tts_get_info"} | |
| actual_tools = {tool.name for tool in tools} | |
| if expected_tools == actual_tools: | |
| print("β All expected tools are available") | |
| else: | |
| missing = expected_tools - actual_tools | |
| extra = actual_tools - expected_tools | |
| if missing: | |
| print(f"β Missing tools: {missing}") | |
| if extra: | |
| print(f"β οΈ Extra tools: {extra}") | |
| return False | |
| except Exception as e: | |
| print(f"β Error testing tool listing: {e}") | |
| return False | |
| # Test the get_info tool (this should work without GPU) | |
| try: | |
| from app import handle_call_tool | |
| print("\nπ Testing tts_get_info tool...") | |
| info_result = await handle_call_tool("tts_get_info", {}) | |
| if info_result and len(info_result) > 0: | |
| info_data = json.loads(info_result[0].text) | |
| print("β System info retrieved successfully") | |
| print(f" - MCP Status: {info_data.get('mcp_status', 'Unknown')}") | |
| print(f" - Available Tools: {len(info_data.get('available_tools', []))}") | |
| print(f" - Voice Presets: {len(info_data.get('voice_presets', []))}") | |
| else: | |
| print("β Failed to get system info") | |
| return False | |
| except Exception as e: | |
| print(f"β Error testing get_info tool: {e}") | |
| return False | |
| # Test error handling for invalid tool | |
| try: | |
| print("\nπ Testing error handling...") | |
| error_result = await handle_call_tool("invalid_tool", {}) | |
| if error_result and len(error_result) > 0: | |
| error_data = json.loads(error_result[0].text) | |
| if "error" in error_data and "Unknown tool" in error_data["error"]: | |
| print("β Error handling works correctly") | |
| else: | |
| print("β Unexpected error response format") | |
| return False | |
| else: | |
| print("β No error response received") | |
| return False | |
| except Exception as e: | |
| print(f"β Error testing error handling: {e}") | |
| return False | |
| print("\nπ All MCP integration tests passed!") | |
| return True | |
| except ImportError as e: | |
| print(f"β Import error: {e}") | |
| return False | |
| except Exception as e: | |
| print(f"β Unexpected error: {e}") | |
| return False | |
| def test_voice_presets(): | |
| """Test that voice presets are properly defined""" | |
| try: | |
| from app import VOICE_PRESETS | |
| print(f"\nπ Testing Voice Presets ({len(VOICE_PRESETS)} available):") | |
| if len(VOICE_PRESETS) == 0: | |
| print("β No voice presets defined") | |
| return False | |
| for code, description in VOICE_PRESETS[:3]: # Show first 3 | |
| print(f" - {code}: {description}") | |
| if len(VOICE_PRESETS) > 3: | |
| print(f" ... and {len(VOICE_PRESETS) - 3} more") | |
| print("β Voice presets loaded successfully") | |
| return True | |
| except Exception as e: | |
| print(f"β Error testing voice presets: {e}") | |
| return False | |
| def main(): | |
| """Main test function""" | |
| print("=" * 60) | |
| print("π TTS Service MCP Integration Test") | |
| print("=" * 60) | |
| # Test voice presets first (synchronous) | |
| presets_ok = test_voice_presets() | |
| # Test MCP integration (asynchronous) | |
| mcp_ok = asyncio.run(test_mcp_integration()) | |
| print("\n" + "=" * 60) | |
| if presets_ok and mcp_ok: | |
| print("β ALL TESTS PASSED - TTS MCP integration is ready!") | |
| print("\nπ Summary:") | |
| print(" β Voice presets configured") | |
| print(" β MCP server setup complete") | |
| print(" β MCP tools available") | |
| print(" β Error handling working") | |
| print("\nπ Ready to deploy TTS service with dual protocol support!") | |
| return 0 | |
| else: | |
| print("β SOME TESTS FAILED - Check the issues above") | |
| return 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) |