#!/usr/bin/env python3 """ Test script for stock manager toggle_chart functionality. This script tests that the toggle_chart setting properly adds/removes charts from the scrolling ticker. """ import sys import os import json import time # Add the src directory to the path so we can import our modules sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src')) from stock_manager import StockManager from display_manager import DisplayManager def test_toggle_chart_functionality(): """Test that toggle_chart properly controls chart display in scrolling ticker.""" # Load test configuration config = { 'stocks': { 'enabled': True, 'symbols': ['AAPL', 'MSFT', 'GOOGL'], 'scroll_speed': 1, 'scroll_delay': 0.01, 'toggle_chart': False # Start with charts disabled }, 'crypto': { 'enabled': False, 'symbols': [] } } # Create a mock display manager for testing class MockDisplayManager: def __init__(self): self.matrix = type('Matrix', (), {'width': 64, 'height': 32})() self.image = None self.regular_font = type('Font', (), {'path': 'assets/fonts/5x7.bdf', 'size': 7})() self.small_font = type('Font', (), {'path': 'assets/fonts/4x6.bdf', 'size': 6})() def clear(self): pass def update_display(self): pass display_manager = MockDisplayManager() # Create stock manager stock_manager = StockManager(config, display_manager) print("Testing Stock Manager toggle_chart functionality...") print("=" * 50) # Test 1: Verify initial state (charts disabled) print(f"1. Initial toggle_chart setting: {stock_manager.toggle_chart}") assert stock_manager.toggle_chart == False, "Initial toggle_chart should be False" print("✓ Initial state correct") # Test 2: Enable charts print("\n2. Enabling charts...") stock_manager.set_toggle_chart(True) assert stock_manager.toggle_chart == True, "toggle_chart should be True after enabling" print("✓ Charts enabled successfully") # Test 3: Disable charts print("\n3. Disabling charts...") stock_manager.set_toggle_chart(False) assert stock_manager.toggle_chart == False, "toggle_chart should be False after disabling" print("✓ Charts disabled successfully") # Test 4: Verify cache clearing print("\n4. Testing cache clearing...") stock_manager.cached_text_image = "test_cache" stock_manager.set_toggle_chart(True) assert stock_manager.cached_text_image is None, "Cache should be cleared when toggle_chart changes" print("✓ Cache clearing works correctly") # Test 5: Test configuration reload print("\n5. Testing configuration reload...") config['stocks']['toggle_chart'] = True stock_manager.config = config stock_manager.stocks_config = config['stocks'] stock_manager._reload_config() assert stock_manager.toggle_chart == True, "toggle_chart should be updated from config" print("✓ Configuration reload works correctly") print("\n" + "=" * 50) print("All tests passed! ✓") print("\nSummary:") print("- toggle_chart setting properly controls chart display in scrolling ticker") print("- Charts are only shown when toggle_chart is True") print("- Cache is properly cleared when setting changes") print("- Configuration reload works correctly") print("- No sleep delays are used in the scrolling ticker") if __name__ == "__main__": test_toggle_chart_functionality()