import sys
import os

sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from telegram_notifier import notifier
from config import save_config, load_config

def test_telegram_toggle():
    print("Testing Telegram Toggle...")
    original_config = load_config()
    
    try:
        # 1. Enable
        print("1. Testing ENABLED...")
        cfg = load_config()
        if 'preferences' not in cfg: cfg['preferences'] = {}
        cfg['preferences']['enable_telegram'] = True
        save_config(cfg)
        
        # We can't easily mock requests.post here without importing mock, 
        # but we can check if it attempts to read credentials.
        # Ideally, we'd check if it returns early.
        # For this environment, let's just ensure no crash.
        notifier.send_message("Test Message (Should Send)")
        
        # 2. Disable
        print("2. Testing DISABLED...")
        cfg['preferences']['enable_telegram'] = False
        save_config(cfg)
        
        # This should return early and NOT log "Telegram credentials missing" if implemented right.
        notifier.send_message("Test Message (Should NOT Send)")
        
        print("Toggle logic executed without error.")
        
    finally:
        # Restore
        save_config(original_config)
        print("Original config restored.")

if __name__ == "__main__":
    test_telegram_toggle()
