#!/usr/bin/env python3 """ Simple test script to verify the Sundew demo functionality Run this before deploying to Hugging Face to ensure everything works """ import numpy as np import random import sys def test_demo_imports(): """Test that all required modules can be imported""" try: import numpy import plotly.graph_objects as go import plotly.express as px from plotly.subplots import make_subplots import random import time from typing import List, Tuple, Dict, Optional print("✅ All imports successful") return True except ImportError as e: print(f"❌ Import error: {e}") return False def test_data_generation(): """Test the data generation function""" try: # Import the function from app.py sys.path.append('.') from app import generate_domain_stream, PRODUCTION_PRESETS # Test each preset for preset_name in PRODUCTION_PRESETS.keys(): samples = generate_domain_stream(preset_name, 10) if len(samples) != 10: print(f"❌ Data generation failed for {preset_name}: expected 10 samples, got {len(samples)}") return False # Check sample structure sample = samples[0] required_keys = ['magnitude', 'anomaly', 'context', 'urgency', 'ground_truth'] if not all(key in sample for key in required_keys): print(f"❌ Sample structure invalid for {preset_name}: missing keys") return False print("✅ Data generation working correctly") return True except Exception as e: print(f"❌ Data generation error: {e}") return False def test_algorithm(): """Test the core algorithm functionality""" try: from app import SundewAlgorithmV2, generate_domain_stream # Create algorithm instance algo = SundewAlgorithmV2("auto_tuned") # Generate test data samples = generate_domain_stream("auto_tuned", 20) # Process samples for sample in samples: ground_truth = sample.pop('ground_truth') result = algo.process_sample(sample, ground_truth) # Check result structure required_keys = ['activated', 'significance', 'threshold', 'energy_level'] if not all(key in result for key in required_keys): print("❌ Algorithm result structure invalid") return False # Check that data was stored if len(algo.significances) != 20: print(f"❌ Algorithm data storage failed: expected 20 significances, got {len(algo.significances)}") return False print("✅ Algorithm functionality working correctly") return True except Exception as e: print(f"❌ Algorithm error: {e}") return False def test_visualization(): """Test the visualization creation""" try: from app import SundewAlgorithmV2, generate_domain_stream, create_comprehensive_visualization # Create algorithm and process some data algo = SundewAlgorithmV2("custom_breast_probe") samples = generate_domain_stream("custom_breast_probe", 15) for sample in samples: ground_truth = sample.pop('ground_truth') algo.process_sample(sample, ground_truth) # Create visualization fig = create_comprehensive_visualization(algo, "custom_breast_probe") if fig is None: print("❌ Visualization creation failed: returned None") return False print("✅ Visualization creation working correctly") return True except Exception as e: print(f"❌ Visualization error: {e}") return False def main(): """Run all tests""" print("🧪 Testing Sundew Demo Functionality\n") tests = [ ("Module Imports", test_demo_imports), ("Data Generation", test_data_generation), ("Algorithm Processing", test_algorithm), ("Visualization Creation", test_visualization) ] all_passed = True for test_name, test_func in tests: print(f"Testing {test_name}...") if not test_func(): all_passed = False print() if all_passed: print("🎉 All tests passed! Demo is ready for deployment.") print("\nTo run the demo locally:") print("1. Install requirements: pip install -r requirements.txt") print("2. Run demo: python app.py") print("3. Open browser to displayed URL") else: print("❌ Some tests failed. Please fix issues before deployment.") return 1 return 0 if __name__ == "__main__": exit(main())