#!/usr/bin/env python3 """ Test script for template.docx conversion with specific font sizes Tests the new Arial font integration and font size preservation """ import os import sys from pathlib import Path import tempfile import shutil # Add current directory to path to import app module sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from app import ( setup_libreoffice, validate_docx_structure, preprocess_docx_for_perfect_conversion, analyze_template_font_sizes, setup_local_arial_font ) def test_arial_font_setup(): """Test local Arial font setup""" print("๐Ÿ”ค Testing local Arial font setup...") # Check if Arial font exists in fonts directory arial_path = Path("fonts/arial.ttf") if not arial_path.exists(): print(f"โŒ Arial font not found at {arial_path}") return False print(f"โœ… Arial font found at {arial_path}") # Test font setup function result = setup_local_arial_font() if result: print("โœ… Local Arial font setup successful") else: print("โŒ Local Arial font setup failed") return result def test_template_analysis(): """Test template.docx analysis for font sizes""" print("\n๐Ÿ“ Testing template.docx font size analysis...") template_path = Path("template.docx") if not template_path.exists(): print(f"โŒ Template file not found at {template_path}") return False print(f"โœ… Template file found at {template_path}") # Test font size analysis font_mapping = analyze_template_font_sizes(str(template_path)) if font_mapping: print(f"โœ… Font size analysis successful - {len(font_mapping)} patterns found:") for text, size in list(font_mapping.items())[:10]: # Show first 10 print(f" โ€ข '{text[:30]}...' โ†’ {size}pt") if len(font_mapping) > 10: print(f" โ€ข ... and {len(font_mapping) - 10} more patterns") else: print("โŒ Font size analysis failed") return bool(font_mapping) def test_docx_validation(): """Test DOCX structure validation with template""" print("\n๐Ÿ” Testing DOCX structure validation...") template_path = Path("template.docx") if not template_path.exists(): print(f"โŒ Template file not found at {template_path}") return False # Test validation function validation_info = validate_docx_structure(str(template_path)) print("โœ… DOCX validation completed:") print(f" โ€ข Has tables: {validation_info.get('has_tables', False)}") print(f" โ€ข Has images: {validation_info.get('has_images', False)}") print(f" โ€ข Text length: {validation_info.get('text_content_length', 0)} chars") print(f" โ€ข Font families: {len(validation_info.get('font_families', set()))}") print(f" โ€ข RTL content: {validation_info.get('rtl_content_detected', False)}") print(f" โ€ข Placeholders: {validation_info.get('placeholder_count', 0)}") print(f" โ€ข Font mapping: {len(validation_info.get('font_size_mapping', {}))}") return True def test_preprocessing(): """Test DOCX preprocessing with font settings""" print("\n๐Ÿ”ง Testing DOCX preprocessing...") template_path = Path("template.docx") if not template_path.exists(): print(f"โŒ Template file not found at {template_path}") return False # First validate the structure validation_info = validate_docx_structure(str(template_path)) # Test preprocessing try: processed_path = preprocess_docx_for_perfect_conversion(str(template_path), validation_info) if processed_path != str(template_path): print(f"โœ… Preprocessing applied - new file: {processed_path}") # Check if processed file exists if Path(processed_path).exists(): print(f"โœ… Processed file exists and is accessible") # Clean up temporary file try: os.unlink(processed_path) print("โœ… Temporary file cleaned up") except: pass else: print(f"โŒ Processed file not found") return False else: print("โ„น๏ธ No preprocessing needed - file structure is optimal") return True except Exception as e: print(f"โŒ Preprocessing failed: {e}") return False def test_libreoffice_setup(): """Test LibreOffice setup""" print("\nโš™๏ธ Testing LibreOffice setup...") result = setup_libreoffice() if result: print("โœ… LibreOffice setup successful") else: print("โŒ LibreOffice setup failed") return result def main(): """Run all tests""" print("๐Ÿงช Starting Template Conversion Tests") print("=" * 50) tests = [ ("Arial Font Setup", test_arial_font_setup), ("Template Analysis", test_template_analysis), ("DOCX Validation", test_docx_validation), ("DOCX Preprocessing", test_preprocessing), ("LibreOffice Setup", test_libreoffice_setup), ] results = {} for test_name, test_func in tests: try: results[test_name] = test_func() except Exception as e: print(f"โŒ {test_name} failed with exception: {e}") results[test_name] = False # Summary print("\n" + "=" * 50) print("๐Ÿ“Š Test Results Summary:") passed = 0 total = len(tests) for test_name, result in results.items(): status = "โœ… PASS" if result else "โŒ FAIL" print(f" {status} - {test_name}") if result: passed += 1 print(f"\n๐ŸŽฏ Overall: {passed}/{total} tests passed ({passed/total*100:.1f}%)") if passed == total: print("๐ŸŒŸ All tests passed! Template conversion system is ready.") elif passed >= total * 0.8: print("๐Ÿ‘ Most tests passed. System should work with minor issues.") else: print("โš ๏ธ Several tests failed. Please check the setup.") return passed == total if __name__ == "__main__": success = main() sys.exit(0 if success else 1)