import sys import os import traceback import importlib.util def compile_file(filepath): try: with open(filepath, 'r', encoding='utf-8') as f: code = f.read() compile(code, filepath, 'exec') return True, None except SyntaxError as e: return False, f"Syntax Error: {e.msg} at line {e.lineno}" except Exception as e: return False, f"Compilation Error: {str(e)}" def test_imports(): results = {} modules = [ ('queue_monitor', 'queue_monitor.py'), ('llm_analyzer', 'llm_analyzer.py'), ('utils', 'utils.py'), ('app', 'app.py'), ] for module_name, filepath in modules: if not os.path.exists(filepath): results[module_name] = (False, f"File not found: {filepath}") continue try: spec = importlib.util.spec_from_file_location(module_name, filepath) if spec is None or spec.loader is None: results[module_name] = (False, "Could not create module spec") continue module = importlib.util.module_from_spec(spec) sys.modules[module_name] = module spec.loader.exec_module(module) results[module_name] = (True, "Import successful") except ImportError as e: results[module_name] = (False, f"Import Error: {str(e)}") except Exception as e: results[module_name] = (False, f"Error: {str(e)}\n{traceback.format_exc()}") return results def test_queue_monitor(): try: import supervision except ImportError: return True, "QueueMonitor test skipped (supervision not installed)" try: from queue_monitor import QueueMonitor import numpy as np monitor = QueueMonitor(confidence=0.5, fps=30.0) dummy_frame = np.zeros((720, 1280, 3), dtype=np.uint8) polygon = np.array([[100, 100], [600, 100], [600, 600], [100, 600]]) monitor.setup_zones([polygon]) processed, stats = monitor.process_frame(dummy_frame) assert processed is not None, "Processed frame is None" assert isinstance(stats, list), "Stats should be a list" assert len(stats) > 0, "Stats should contain zone data" return True, "QueueMonitor test passed" except ImportError as e: return True, f"QueueMonitor test skipped (missing dependency: {str(e)})" except Exception as e: return False, f"QueueMonitor test failed: {str(e)}\n{traceback.format_exc()}" def test_utils(): try: from utils import ( is_valid_youtube_url, extract_video_id, YT_DOWNLOADER_AVAILABLE ) test_url = "https://youtu.be/5rkwqp6nnr4?si=itvwJ-oSR0S8xSZQ" assert is_valid_youtube_url(test_url), "Should validate YouTube URL" video_id = extract_video_id(test_url) assert video_id == "5rkwqp6nnr4", f"Expected video ID '5rkwqp6nnr4', got '{video_id}'" return True, "Utils test passed" except Exception as e: return False, f"Utils test failed: {str(e)}\n{traceback.format_exc()}" def test_app_components(): try: import app assert hasattr(app, 'EXAMPLE_VIDEO_URL'), "app should have EXAMPLE_VIDEO_URL" assert app.EXAMPLE_VIDEO_URL == "https://youtu.be/5rkwqp6nnr4?si=itvwJ-oSR0S8xSZQ", "Example URL should match" assert hasattr(app, 'QUEUE_MONITOR_AVAILABLE'), "app should have QUEUE_MONITOR_AVAILABLE" assert hasattr(app, 'LLM_ANALYZER_AVAILABLE'), "app should have LLM_ANALYZER_AVAILABLE" assert hasattr(app, 'UTILS_AVAILABLE'), "app should have UTILS_AVAILABLE" return True, "App components test passed" except Exception as e: return False, f"App components test failed: {str(e)}\n{traceback.format_exc()}" def main(): print("=" * 60) print("COMPILATION AND TEST SUITE") print("=" * 60) all_passed = True print("\n1. COMPILING FILES...") print("-" * 60) files_to_check = [ 'queue_monitor.py', 'llm_analyzer.py', 'utils.py', 'app.py', 'test_backend.py' ] for filepath in files_to_check: if not os.path.exists(filepath): print(f"❌ {filepath}: File not found") all_passed = False continue success, error = compile_file(filepath) if success: print(f"✅ {filepath}: Compilation successful") else: print(f"❌ {filepath}: {error}") all_passed = False print("\n2. TESTING IMPORTS...") print("-" * 60) import_results = test_imports() for module_name, (success, message) in import_results.items(): if success: print(f"✅ {module_name}: {message}") else: if "No module named" in message or "Import Error" in message: print(f"⚠️ {module_name}: {message} (expected if dependencies not installed)") else: print(f"❌ {module_name}: {message}") all_passed = False print("\n3. TESTING FUNCTIONALITY...") print("-" * 60) success, message = test_queue_monitor() if success: print(f"✅ QueueMonitor: {message}") else: print(f"❌ QueueMonitor: {message}") all_passed = False success, message = test_utils() if success: print(f"✅ Utils: {message}") else: print(f"❌ Utils: {message}") all_passed = False success, message = test_app_components() if success: print(f"✅ App Components: {message}") else: print(f"❌ App Components: {message}") all_passed = False print("\n" + "=" * 60) if all_passed: print("✅ ALL TESTS PASSED") print("=" * 60) return 0 else: print("❌ SOME TESTS FAILED") print("=" * 60) return 1 if __name__ == "__main__": sys.exit(main())