#!/usr/bin/env python3 """ Test JavaScript fixes for const redeclaration and MarkitectMain issues """ import sys from pathlib import Path import re # Add project root to path for imports project_root = Path(__file__).parent.parent.parent sys.path.insert(0, str(project_root)) def test_javascript_fixes(): """Test that JavaScript const redeclaration and MarkitectMain issues are resolved.""" print("šŸ”§ Testing JavaScript Fixes") print("=" * 50) try: # Test 1: Check for const declarations in loaded files print("1ļøāƒ£ Checking for const declaration conflicts...") from markitect.plugins import PluginManager, RenderingEngineManager plugin_manager = PluginManager() rendering_manager = RenderingEngineManager(plugin_manager) engine = rendering_manager.get_engine('testdrive-jsui') required_assets = engine.get_required_assets() js_files = required_assets.get('js', []) print(f" šŸ“„ JavaScript files to be loaded: {len(js_files)}") const_declarations = {} capability_root = Path(__file__).parent.parent for js_file in js_files: # Check if file exists in capability directory first file_path = capability_root / js_file if file_path.exists(): content = file_path.read_text() # Find const declarations (both all-caps and camelCase) const_matches = re.findall(r'^const\s+([A-Za-z_][A-Za-z0-9_]*)\s*=', content, re.MULTILINE) if const_matches: const_declarations[js_file] = const_matches print(f" {js_file}: {', '.join(const_matches)}") else: print(f" {js_file}: File not found in capability directory") # Check for duplicates all_consts = [] for file, consts in const_declarations.items(): all_consts.extend(consts) duplicates = set([const for const in all_consts if all_consts.count(const) > 1]) if duplicates: print(f" āŒ Found duplicate const declarations: {', '.join(duplicates)}") return False else: print(f" āœ… No duplicate const declarations found") # Test 2: Verify key components are in the loaded files print(f"\n2ļøāƒ£ Checking key component availability...") # Look for important components instead of MarkitectMain key_components = ['EditState', 'SectionType', 'DocumentControls', 'SectionManager', 'DOMRenderer'] found_components = {} for file, consts in const_declarations.items(): for component in key_components: if component in consts: if component in found_components: found_components[component].append(file) else: found_components[component] = [file] missing_components = [comp for comp in key_components if comp not in found_components] if missing_components: print(f" āš ļø Some components not found: {', '.join(missing_components)}") duplicate_components = {comp: files for comp, files in found_components.items() if len(files) > 1} if duplicate_components: print(f" āŒ Duplicate components found: {duplicate_components}") return False if found_components: print(f" āœ… Found key components: {', '.join(found_components.keys())}") else: print(f" ā„¹ļø No key components found (might use different patterns)") # Test 3: Verify file structure and loading order print(f"\n3ļøāƒ£ Checking file structure and loading order...") main_files = [f for f in js_files if 'main' in f.lower()] if main_files: print(f" āœ… Main files found: {', '.join(main_files)}") else: print(f" ā„¹ļø No explicit main files found in asset list") # Check for core components in the expected order core_files = [f for f in js_files if 'core' in f or 'components' in f or 'controls' in f] if core_files: print(f" āœ… Core component files found: {len(core_files)} files") else: print(f" āš ļø No core component files found") # Test 4: Generate and verify HTML output print(f"\n4ļøāƒ£ Testing HTML generation...") from markitect.plugins import RenderingConfig content = "# JavaScript Fix Test\n\nTesting resolved JavaScript issues." output_dir = Path('/tmp/test_js_fixes_verification') output_dir.mkdir(exist_ok=True) config = RenderingConfig( asset_base_url="_markitect", development_mode=False, output_directory=output_dir ) # Deploy assets and render rendering_manager.deploy_engine_assets('testdrive-jsui', config) html_content = engine.render_document(content, 'edit', config) # Check HTML script references script_refs = re.findall(r'