#!/usr/bin/env python3 """ Comprehensive test of the world events auto-refresh fix """ import sys import os import time sys.path.append(os.path.dirname(os.path.abspath(__file__))) try: from src.core.game_engine import get_game_engine from src.facades.game_facade import GameFacade from src.ui.interface_manager import InterfaceManager from src.ui.huggingface_ui import HuggingFaceUI except ImportError as e: print(f"Import error: {e}") print("Make sure to run this from the project root directory") sys.exit(1) def test_complete_functionality(): """Test the complete world events functionality""" print("๐Ÿ”ง WORLD EVENTS AUTO-REFRESH FIX VERIFICATION") print("=" * 60) # Test 1: Verify GameFacade has get_world_events method print("\n1๏ธโƒฃ Testing GameFacade.get_world_events()...") try: facade = GameFacade() if hasattr(facade, 'get_world_events'): print("โœ… GameFacade.get_world_events() method exists") # Test calling the method events = facade.get_world_events() print(f"โœ… Method returns {len(events)} events") if events: print(" Sample events:") for i, event in enumerate(events[-3:], 1): print(f" {i}. {event}") else: print("โŒ GameFacade.get_world_events() method missing") return False except Exception as e: print(f"โŒ Error testing GameFacade: {e}") return False # Test 2: Verify world events can be added print("\n2๏ธโƒฃ Testing world event creation...") try: # Get the game world through the engine engine = get_game_engine() game_world = engine.get_world() initial_count = len(game_world.world_events) game_world.add_world_event("๐Ÿงช Test event - Auto-refresh fix verification") game_world.add_world_event("๐Ÿ”„ Testing auto-refresh functionality") game_world.add_world_event("โœจ World events should now display properly") final_count = len(game_world.world_events) added_count = final_count - initial_count print(f"โœ… Added {added_count} test events (total: {final_count})") # Verify through facade facade_events = facade.get_world_events() print(f"โœ… GameFacade retrieves {len(facade_events)} events") except Exception as e: print(f"โŒ Error adding world events: {e}") return False # Test 3: Test auto-refresh method print("\n3๏ธโƒฃ Testing _auto_refresh_game_state method...") try: # Create UI components ui = HuggingFaceUI(facade) interface_manager = InterfaceManager(facade, ui) # Test auto-refresh with mock state mock_state = {"joined": True, "player_id": "test-player"} # This should not throw an error about mismatched outputs result = interface_manager._auto_refresh_game_state(mock_state, True) if isinstance(result, tuple) and len(result) == 8: print(f"โœ… Auto-refresh returns correct number of outputs: {len(result)}") print("โœ… Timer configuration matches method return values") else: print(f"โŒ Auto-refresh returns {len(result) if isinstance(result, tuple) else 'non-tuple'} outputs, expected 8") return False except Exception as e: print(f"โŒ Error testing auto-refresh: {e}") import traceback traceback.print_exc() return False # Test 4: Create a real player to test full functionality print("\n4๏ธโƒฃ Testing with real player...") try: test_player_id = facade.join_game("AutoRefreshTester") if test_player_id: print(f"โœ… Created test player: {test_player_id}") # Test auto-refresh with real player real_state = {"joined": True, "player_id": test_player_id} result = interface_manager._auto_refresh_game_state(real_state, True) if len(result) == 8: print("โœ… Auto-refresh works with real player") # Check if world_events data is in the result world_events_update = result[7] # 8th element (0-indexed) if hasattr(world_events_update, 'value'): print("โœ… World events data included in auto-refresh result") if "Test event" in str(world_events_update.value): print("โœ… Test events are being retrieved correctly") else: print("โš ๏ธ World events update object has no value attribute") # Clean up facade.leave_game(test_player_id) print("โœ… Test player removed") else: print("โŒ Could not create test player") return False except Exception as e: print(f"โŒ Error testing with real player: {e}") return False print("\n๐ŸŽ‰ ALL TESTS PASSED!") print("\n๐Ÿ“‹ SUMMARY:") print("โœ… GameFacade.get_world_events() method exists and works") print("โœ… World events can be created and retrieved") print("โœ… Auto-refresh method returns 8 outputs (matching timer config)") print("โœ… Timer configuration includes world_events in outputs list") print("โœ… World events data is properly included in auto-refresh cycle") print("\n๐ŸŒ The fix is working! Check the browser at http://localhost:7866") print(" Join the game and watch the 'World Events' panel update automatically!") return True if __name__ == "__main__": success = test_complete_functionality() if success: print("\n๐Ÿš€ Fix verification complete - world events auto-refresh is working!") else: print("\nโŒ Fix verification failed - there may be remaining issues") sys.exit(1)