#!/usr/bin/env python3 """ Test script for Telegram Monitor functionality Validates JSON parsing and message formatting """ import sys import os # Add the tools directory to the path current_dir = os.path.dirname(os.path.abspath(__file__)) sys.path.append(current_dir) from telegram_monitor import TelegramNotifier, PositionMonitor def test_json_parsing(): """Test JSON parsing from hedge_status.json""" print("Testing JSON parsing...") monitor = PositionMonitor("../hedge_status.json", "test_state.json") data = monitor.safe_read_json() if not data: print("❌ Failed to read JSON data") return False print(f"✅ Successfully read {len(data)} positions") # Find open and closed positions last_closed, current_open = monitor.extract_notification_data(data) if current_open: print(f"✅ Found open position: Token ID {current_open.get('token_id')}") else: print("ℹ️ No open position found") if last_closed: print(f"✅ Found last closed: Token ID {last_closed.get('token_id')}") else: print("ℹ️ No closed positions found") return True def test_message_formatting(): """Test message formatting""" print("\n🧪 Testing message formatting...") # Create test data last_closed = { "token_id": 1234567, "entry_price": 3000.0, "target_value": 2000.0, "hedge_pnl_realized": -15.50, "hedge_fees_paid": 2.25, "timestamp_open": 1766328197, "timestamp_close": 1766331797 } current_open = { "token_id": 1234568, "entry_price": 2980.0, "target_value": 1950.0, "range_lower": 2900.0, "range_upper": 3060.0, "amount0_initial": 0.3, "amount1_initial": 900.0, "timestamp_open": 1766335400 } notifier = TelegramNotifier("test_token", "test_chat") message = notifier.format_position_message(last_closed, current_open) print("📱 Sample message:") print("-" * 40) print(message) print("-" * 40) return True def main(): print("Telegram Monitor Test Suite") print("=" * 50) success = True try: if not test_json_parsing(): success = False if not test_message_formatting(): success = False except Exception as e: print(f"❌ Test failed with error: {e}") success = False if success: print("\n✅ All tests passed! Telegram monitor is ready.") else: print("\n❌ Some tests failed. Check the errors above.") return 0 if success else 1 if __name__ == "__main__": sys.exit(main())