#!/usr/bin/env python3 """ Test script to validate Streamlit app imports and basic functionality """ import sys import os from pathlib import Path def test_imports(): """Test that all required modules can be imported""" print("šŸ” Testing Streamlit app imports...") # Add current directory to Python path current_dir = os.path.dirname(os.path.abspath(__file__)) if current_dir not in sys.path: sys.path.insert(0, current_dir) # Test core modules modules_to_test = [ 'streamlit', 'pandas', 'plotly', 'psutil', 'numpy', 'streamlit_app.core.cache_manager', 'streamlit_app.core.text_processor', 'streamlit_app.core.llm_analyzer', 'streamlit_app.core.dataset_builder', 'streamlit_app.utils.config', 'streamlit_app.utils.performance', 'streamlit_app.utils.ui_helpers' ] failed_imports = [] for module in modules_to_test: try: __import__(module) print(f"āœ… {module}") except ImportError as e: print(f"āŒ {module}: {e}") failed_imports.append(module) except Exception as e: print(f"āš ļø {module}: Unexpected error - {e}") if failed_imports: print(f"\nāŒ Failed to import {len(failed_imports)} modules:") for module in failed_imports: print(f" - {module}") return False print(f"\nāœ… All {len(modules_to_test)} modules imported successfully!") return True def test_core_functionality(): """Test basic functionality of core modules""" print("\nšŸ”§ Testing core functionality...") try: # Test cache manager from streamlit_app.core.cache_manager import CacheManager, get_cache_manager cache = get_cache_manager(max_memory_mb=10, persistent=False) # Small cache for testing cache_stats = cache.get_stats() print(f"āœ… Cache Manager: {cache_stats}") # Test text processor from streamlit_app.core.text_processor import TextProcessor processor = TextProcessor() test_text = "This is a test of the New Zealand legislation analysis system." cleaned = processor.clean_text(test_text) chunks = processor.chunk_text(cleaned, chunk_size=50, overlap=10) print(f"āœ… Text Processor: {len(chunks)} chunks created") # Test configuration manager from streamlit_app.utils.config import ConfigManager config = ConfigManager() config_dict = config.get_config() print(f"āœ… Config Manager: {len(config_dict)} configuration sections") # Test performance monitor from streamlit_app.utils.performance import PerformanceMonitor perf = PerformanceMonitor(max_history=10) stats = perf.get_stats() print(f"āœ… Performance Monitor: Memory usage {stats['memory_usage_mb']:.1f} MB") # Test UI helpers (basic instantiation) from streamlit_app.utils.ui_helpers import UIHelpers helper = UIHelpers() print("āœ… UI Helpers: Module loaded") print("\nšŸŽ‰ All core functionality tests passed!") return True except Exception as e: print(f"\nāŒ Core functionality test failed: {e}") import traceback traceback.print_exc() return False def test_file_structure(): """Test that all required files exist""" print("\nšŸ“ Testing file structure...") required_files = [ 'streamlit_app/app.py', 'streamlit_app/core/cache_manager.py', 'streamlit_app/core/text_processor.py', 'streamlit_app/core/llm_analyzer.py', 'streamlit_app/core/dataset_builder.py', 'streamlit_app/utils/config.py', 'streamlit_app/utils/performance.py', 'streamlit_app/utils/ui_helpers.py', 'requirements.txt', 'run_streamlit_app.py', 'README_Streamlit_App.md' ] missing_files = [] for file_path in required_files: if not Path(file_path).exists(): missing_files.append(file_path) print(f"āŒ Missing: {file_path}") else: print(f"āœ… Found: {file_path}") if missing_files: print(f"\nāŒ Missing {len(missing_files)} files:") for file_path in missing_files: print(f" - {file_path}") return False print(f"\nāœ… All {len(required_files)} files present!") return True def main(): """Main test function""" print("šŸ›ļø NZ Legislation Loophole Analysis - App Validation") print("=" * 60) all_passed = True # Test file structure if not test_file_structure(): all_passed = False # Test imports if not test_imports(): all_passed = False # Test core functionality if not test_core_functionality(): all_passed = False print("\n" + "=" * 60) if all_passed: print("šŸŽ‰ VALIDATION COMPLETE - App is ready to run!") print("\nšŸš€ To start the application:") print(" python run_streamlit_app.py") print("\nšŸ“± Then visit: http://localhost:8501") else: print("āŒ VALIDATION FAILED - Please check the errors above") print("\nšŸ”§ Troubleshooting:") print(" - Ensure all dependencies are installed: pip install -r requirements.txt") print(" - Check Python version (3.8+ required)") print(" - Verify file permissions") return all_passed if __name__ == "__main__": success = main() sys.exit(0 if success else 1)