File size: 6,316 Bytes
623e14e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/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)