Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python3 | |
| """ | |
| Final Test Verification Script | |
| Tests both keyboard controls and private chat functionality after fixes | |
| """ | |
| import requests | |
| import json | |
| import time | |
| from typing import Dict, List, Any | |
| class FinalTestSuite: | |
| def __init__(self, base_url: str = "http://localhost:7865"): | |
| self.base_url = base_url | |
| self.test_results = {} | |
| def test_server_connection(self) -> bool: | |
| """Test if server is running and accessible.""" | |
| try: | |
| response = requests.get(self.base_url, timeout=5) | |
| success = response.status_code == 200 | |
| self.test_results['server_connection'] = { | |
| 'status': 'PASS' if success else 'FAIL', | |
| 'details': f"Status code: {response.status_code}" | |
| } | |
| return success | |
| except Exception as e: | |
| self.test_results['server_connection'] = { | |
| 'status': 'FAIL', | |
| 'details': f"Connection error: {str(e)}" | |
| } | |
| return False | |
| def test_keyboard_movement_backend(self) -> bool: | |
| """Test movement commands (WASD/Arrow keys backend functionality).""" | |
| test_data = { | |
| "W": {"direction": "north", "expected": "move"}, | |
| "A": {"direction": "west", "expected": "move"}, | |
| "S": {"direction": "south", "expected": "move"}, | |
| "D": {"direction": "east", "expected": "move"}, | |
| "ArrowUp": {"direction": "north", "expected": "move"}, | |
| "ArrowLeft": {"direction": "west", "expected": "move"}, | |
| "ArrowDown": {"direction": "south", "expected": "move"}, | |
| "ArrowRight": {"direction": "east", "expected": "move"} | |
| } | |
| results = {} try: | |
| # Test movement commands via API | |
| from src.facades.game_facade import GameFacade | |
| facade = GameFacade() | |
| # Create test player | |
| player_id = "test_keyboard_player" | |
| facade.add_player(player_id, "TestKeyboardPlayer") | |
| for key, test_info in test_data.items(): | |
| try: | |
| # Test movement | |
| success, position, messages = facade.move_player(player_id, test_info["direction"]) | |
| results[key] = { | |
| 'success': success, | |
| 'position': position, | |
| 'messages': messages, | |
| 'status': 'PASS' if success else 'FAIL' | |
| } | |
| except Exception as e: | |
| results[key] = { | |
| 'success': False, | |
| 'error': str(e), | |
| 'status': 'FAIL' | |
| } | |
| # Clean up | |
| facade.remove_player(player_id) | |
| all_passed = all(r.get('success', False) for r in results.values()) | |
| self.test_results['keyboard_movement'] = { | |
| 'status': 'PASS' if all_passed else 'FAIL', | |
| 'details': results, | |
| 'summary': f"Passed {sum(1 for r in results.values() if r.get('success', False))}/{len(results)} movement tests" | |
| } | |
| return all_passed | |
| except Exception as e: | |
| self.test_results['keyboard_movement'] = { | |
| 'status': 'FAIL', | |
| 'details': f"Backend test error: {str(e)}" | |
| } | |
| return False | |
| def test_private_chat_backend(self) -> bool: | |
| """Test private chat functionality.""" try: | |
| from src.facades.game_facade import GameFacade | |
| facade = GameFacade() | |
| # Create test players | |
| player1_id = "test_sender" | |
| player2_id = "test_receiver" | |
| facade.add_player(player1_id, "TestSender") | |
| facade.add_player(player2_id, "TestReceiver") | |
| # Test sending private message | |
| test_message = "Hello, this is a test private message!" | |
| send_success = facade.send_private_message( | |
| player1_id, "TestSender", player2_id, test_message | |
| ) | |
| # Test receiving private messages | |
| messages = facade.get_private_messages(player2_id, player1_id) | |
| # Clean up | |
| facade.remove_player(player1_id) | |
| facade.remove_player(player2_id) | |
| self.test_results['private_chat'] = { | |
| 'status': 'PASS' if send_success else 'FAIL', | |
| 'details': { | |
| 'send_success': send_success, | |
| 'messages_received': len(messages), | |
| 'message_content': messages[-1] if messages else None | |
| }, | |
| 'summary': f"Send: {'✓' if send_success else '✗'}, Receive: {'✓' if messages else '✗'}" | |
| } | |
| return send_success and len(messages) > 0 | |
| except Exception as e: | |
| self.test_results['private_chat'] = { | |
| 'status': 'FAIL', | |
| 'details': f"Backend test error: {str(e)}" | |
| } | |
| return False | |
| def test_method_signatures(self) -> bool: | |
| """Verify that all method signatures are correct.""" try: | |
| from src.facades.game_facade import GameFacade | |
| import inspect | |
| facade = GameFacade() | |
| # Check send_private_message signature | |
| send_sig = inspect.signature(facade.send_private_message) | |
| expected_params = ['sender_id', 'sender_name', 'recipient_id', 'message'] | |
| actual_params = list(send_sig.parameters.keys()) | |
| # Check get_private_messages signature | |
| get_sig = inspect.signature(facade.get_private_messages) | |
| expected_get_params = ['player_id', 'entity_id'] | |
| actual_get_params = list(get_sig.parameters.keys()) | |
| # Check move_player signature | |
| move_sig = inspect.signature(facade.move_player) | |
| expected_move_params = ['player_id', 'direction'] | |
| actual_move_params = list(move_sig.parameters.keys()) | |
| signatures_correct = ( | |
| actual_params == expected_params and | |
| actual_get_params == expected_get_params and | |
| actual_move_params == expected_move_params | |
| ) | |
| self.test_results['method_signatures'] = { | |
| 'status': 'PASS' if signatures_correct else 'FAIL', | |
| 'details': { | |
| 'send_private_message': { | |
| 'expected': expected_params, | |
| 'actual': actual_params, | |
| 'match': actual_params == expected_params | |
| }, | |
| 'get_private_messages': { | |
| 'expected': expected_get_params, | |
| 'actual': actual_get_params, | |
| 'match': actual_get_params == expected_get_params | |
| }, | |
| 'move_player': { | |
| 'expected': expected_move_params, | |
| 'actual': actual_move_params, | |
| 'match': actual_move_params == expected_move_params | |
| } | |
| } | |
| } | |
| return signatures_correct | |
| except Exception as e: | |
| self.test_results['method_signatures'] = { | |
| 'status': 'FAIL', | |
| 'details': f"Signature test error: {str(e)}" | |
| } | |
| return False | |
| def run_all_tests(self) -> Dict[str, Any]: | |
| """Run all tests and return comprehensive results.""" | |
| print("🧪 Starting Final Test Verification...") | |
| print("=" * 50) | |
| # Test 1: Server Connection | |
| print("1. Testing server connection...") | |
| server_ok = self.test_server_connection() | |
| print(f" Result: {'✅ PASS' if server_ok else '❌ FAIL'}") | |
| # Test 2: Method Signatures | |
| print("2. Testing method signatures...") | |
| signatures_ok = self.test_method_signatures() | |
| print(f" Result: {'✅ PASS' if signatures_ok else '❌ FAIL'}") | |
| # Test 3: Keyboard Movement Backend | |
| print("3. Testing keyboard movement backend...") | |
| keyboard_ok = self.test_keyboard_movement_backend() | |
| print(f" Result: {'✅ PASS' if keyboard_ok else '❌ FAIL'}") | |
| # Test 4: Private Chat Backend | |
| print("4. Testing private chat backend...") | |
| chat_ok = self.test_private_chat_backend() | |
| print(f" Result: {'✅ PASS' if chat_ok else '❌ FAIL'}") | |
| # Summary | |
| total_tests = 4 | |
| passed_tests = sum([server_ok, signatures_ok, keyboard_ok, chat_ok]) | |
| print("\n" + "=" * 50) | |
| print(f"📊 TEST SUMMARY: {passed_tests}/{total_tests} tests passed") | |
| print("=" * 50) | |
| if passed_tests == total_tests: | |
| print("🎉 ALL TESTS PASSED! The fixes have been successful.") | |
| print("\n📋 Next Steps:") | |
| print(" 1. Test keyboard controls in browser (WASD/Arrow keys)") | |
| print(" 2. Test private chat functionality in browser") | |
| print(" 3. Check browser console for any JavaScript errors") | |
| else: | |
| print("⚠️ Some tests failed. Please check the details below.") | |
| return { | |
| 'total_tests': total_tests, | |
| 'passed_tests': passed_tests, | |
| 'success_rate': f"{(passed_tests/total_tests)*100:.1f}%", | |
| 'all_passed': passed_tests == total_tests, | |
| 'test_results': self.test_results, | |
| 'server_url': self.base_url | |
| } | |
| if __name__ == "__main__": | |
| tester = FinalTestSuite() | |
| results = tester.run_all_tests() | |
| # Print detailed results | |
| print("\n🔍 DETAILED RESULTS:") | |
| for test_name, result in results['test_results'].items(): | |
| print(f"\n{test_name.upper()}:") | |
| print(f" Status: {result['status']}") | |
| if 'summary' in result: | |
| print(f" Summary: {result['summary']}") | |
| if isinstance(result.get('details'), dict): | |
| for key, value in result['details'].items(): | |
| print(f" {key}: {value}") | |
| elif result.get('details'): | |
| print(f" Details: {result['details']}") | |