#!/usr/bin/env python3 """ šŸ”§ Test Unicode Fix for Broker Migration Logging Verifies that the broker change detection no longer causes Unicode encoding errors """ import sys import os sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import logging from io import StringIO def test_unicode_fix(): """Test that the new ASCII arrow works without encoding issues""" print("šŸ”§ Testing Unicode Fix for Broker Migration...") print("=" * 50) # Test the old problematic Unicode arrow try: old_message = "Broker changed detected: 'MetaQuotes-Demo' → 'XMGlobal-MT5 7'" old_message.encode('cp1252') # This should fail print("āŒ Old Unicode arrow should have failed but didn't!") return False except UnicodeEncodeError: print("āœ… Confirmed: Old Unicode arrow causes encoding error (as expected)") # Test the new ASCII arrow try: new_message = "Broker changed detected: 'MetaQuotes-Demo' -> 'XMGlobal-MT5 7'" new_message.encode('cp1252') # This should work print("āœ… New ASCII arrow encodes properly in Windows cp1252") except UnicodeEncodeError as e: print(f"āŒ ERROR: New ASCII arrow still causes issues: {e}") return False # Test actual logging print("\nšŸ“ Testing Actual Logging...") try: # Create a logger similar to what's used in the application logger = logging.getLogger('test_broker_migration') # Create a string buffer to capture log output log_capture = StringIO() handler = logging.StreamHandler(log_capture) handler.setFormatter(logging.Formatter('%(levelname)s:%(name)s:%(message)s')) logger.addHandler(handler) logger.setLevel(logging.INFO) # Test the new format (this is what's now in the code) last_broker = "MetaQuotes-Demo" current_broker = "XMGlobal-MT5 7" logger.info(f"Broker changed detected: '{last_broker}' -> '{current_broker}'") # Get the logged output log_output = log_capture.getvalue() print(f"āœ… Successfully logged: {log_output.strip()}") # Verify it contains the ASCII arrow if " -> " in log_output: print("āœ… ASCII arrow (-->) found in log output") else: print("āŒ ASCII arrow not found in log output") return False return True except Exception as e: print(f"āŒ Logging test failed: {e}") return False def main(): """Main test function""" print("šŸ”§ Unicode Fix Validation") print("Testing broker migration logging fix\n") success = test_unicode_fix() if success: print("\nšŸŽ‰ SUCCESS!") print("✨ The Unicode encoding issue has been fixed!") print("\nšŸ”§ What was fixed:") print(" āŒ OLD: Unicode arrow '→' (U+2192)") print(" āœ… NEW: ASCII arrow '->' (safe for Windows)") print("\nšŸš€ Broker switching will now work smoothly on Windows!") else: print("\nāŒ FAILED - Unicode issue may still exist") return success if __name__ == "__main__": success = main() sys.exit(0 if success else 1)