test_bot_fixes.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env python3
  2. """
  3. Test script to verify that the bot configuration and format string fixes work correctly.
  4. """
  5. import sys
  6. import os
  7. sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
  8. from config import Config
  9. def test_config_fixes():
  10. """Test that the configuration changes work correctly."""
  11. print("🔧 Configuration Fixes Test")
  12. print("=" * 50)
  13. # Test that the new configuration variables exist
  14. try:
  15. token = Config.DEFAULT_TRADING_TOKEN
  16. risk_enabled = Config.RISK_MANAGEMENT_ENABLED
  17. stop_loss = Config.STOP_LOSS_PERCENTAGE
  18. heartbeat = Config.BOT_HEARTBEAT_SECONDS
  19. print(f"✅ DEFAULT_TRADING_TOKEN: {token}")
  20. print(f"✅ RISK_MANAGEMENT_ENABLED: {risk_enabled}")
  21. print(f"✅ STOP_LOSS_PERCENTAGE: {stop_loss}%")
  22. print(f"✅ BOT_HEARTBEAT_SECONDS: {heartbeat}")
  23. # Test that old variables are gone
  24. try:
  25. amount = Config.DEFAULT_TRADE_AMOUNT
  26. print(f"❌ DEFAULT_TRADE_AMOUNT still exists: {amount}")
  27. except AttributeError:
  28. print("✅ DEFAULT_TRADE_AMOUNT properly removed")
  29. try:
  30. symbol = Config.DEFAULT_TRADING_SYMBOL
  31. print(f"❌ DEFAULT_TRADING_SYMBOL still exists: {symbol}")
  32. except AttributeError:
  33. print("✅ DEFAULT_TRADING_SYMBOL properly removed")
  34. except AttributeError as e:
  35. print(f"❌ Configuration error: {e}")
  36. return False
  37. return True
  38. def test_format_strings():
  39. """Test that format strings work correctly."""
  40. print("\n📝 Format String Test")
  41. print("=" * 50)
  42. try:
  43. # Test the format parameters that would be used in telegram bot
  44. symbol = Config.DEFAULT_TRADING_TOKEN
  45. network = "Testnet" if Config.HYPERLIQUID_TESTNET else "Mainnet"
  46. risk_enabled = Config.RISK_MANAGEMENT_ENABLED
  47. stop_loss = Config.STOP_LOSS_PERCENTAGE
  48. heartbeat = Config.BOT_HEARTBEAT_SECONDS
  49. # Test format string similar to what's used in the bot
  50. test_format = """
  51. ⚙️ Configuration:
  52. • Default Token: {symbol}
  53. • Network: {network}
  54. • Risk Management: {risk_enabled}
  55. • Stop Loss: {stop_loss}%
  56. • Monitoring: Every {heartbeat} seconds
  57. """.format(
  58. symbol=symbol,
  59. network=network,
  60. risk_enabled=risk_enabled,
  61. stop_loss=stop_loss,
  62. heartbeat=heartbeat
  63. )
  64. print("✅ Format string test successful:")
  65. print(test_format.strip())
  66. except KeyError as e:
  67. print(f"❌ Format string error: {e}")
  68. return False
  69. except Exception as e:
  70. print(f"❌ Unexpected error: {e}")
  71. return False
  72. return True
  73. def test_timestamp_handling():
  74. """Test timestamp handling for external trades."""
  75. print("\n⏰ Timestamp Handling Test")
  76. print("=" * 50)
  77. from datetime import datetime, timedelta
  78. try:
  79. # Test different timestamp formats
  80. test_timestamps = [
  81. 1733155660, # Unix timestamp (seconds)
  82. 1733155660000, # Unix timestamp (milliseconds)
  83. "2024-12-02T15:47:40", # ISO format
  84. "2024-12-02T15:47:40.123Z", # ISO with Z
  85. ]
  86. base_time = (datetime.now() - timedelta(hours=1)).isoformat()
  87. for ts in test_timestamps:
  88. try:
  89. # Test the conversion logic from the bot
  90. if isinstance(ts, (int, float)):
  91. # Assume it's a unix timestamp
  92. ts_str = datetime.fromtimestamp(ts / 1000 if ts > 1e10 else ts).isoformat()
  93. else:
  94. ts_str = str(ts)
  95. # Test comparison
  96. comparison_result = ts_str > base_time
  97. print(f"✅ Timestamp {ts} -> {ts_str} (comparison: {comparison_result})")
  98. except Exception as e:
  99. print(f"❌ Error processing timestamp {ts}: {e}")
  100. return False
  101. print("✅ All timestamp formats handled correctly")
  102. except Exception as e:
  103. print(f"❌ Timestamp handling error: {e}")
  104. return False
  105. return True
  106. if __name__ == "__main__":
  107. print("🚀 Bot Fixes Verification Test")
  108. print("=" * 60)
  109. # Run all tests
  110. tests = [
  111. test_config_fixes,
  112. test_format_strings,
  113. test_timestamp_handling
  114. ]
  115. results = []
  116. for test in tests:
  117. try:
  118. result = test()
  119. results.append(result)
  120. except Exception as e:
  121. print(f"❌ Test failed with exception: {e}")
  122. results.append(False)
  123. print("\n" + "=" * 60)
  124. if all(results):
  125. print("🎉 All tests passed! Bot fixes are working correctly.")
  126. print("✅ Configuration cleanup successful")
  127. print("✅ Format string errors fixed")
  128. print("✅ Timestamp comparison issues resolved")
  129. else:
  130. print("⚠️ Some tests failed. Please check the issues above.")
  131. failed_count = len([r for r in results if not r])
  132. print(f"❌ {failed_count}/{len(results)} tests failed")