#!/usr/bin/env python3 """ Test WordPerfect processor implementation without requiring actual WPD files. This test verifies: 1. WordPerfect processor initialization 2. Processing chain detection 3. File structure analysis capabilities 4. Error handling and fallback systems """ import sys import os import tempfile from pathlib import Path # Add src to path sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), 'src')) def create_mock_wpd_file(version: str = "wp6") -> str: """Create a mock WordPerfect file for testing.""" # WordPerfect magic signatures signatures = { "wp42": b"\xFF\x57\x50\x42", "wp50": b"\xFF\x57\x50\x44", "wp6": b"\xFF\x57\x50\x43", "wpd": b"\xFF\x57\x50\x43\x4D\x42" } # Create temporary file with WP signature temp_file = tempfile.NamedTemporaryFile(mode='wb', suffix='.wpd', delete=False) # Write WordPerfect header signature = signatures.get(version, signatures["wp6"]) temp_file.write(signature) # Add some mock header data temp_file.write(b'\x00' * 10) # Padding temp_file.write(b'\x80\x01\x00\x00') # Mock document pointer temp_file.write(b'\x00' * 100) # More header space # Add some mock document content that looks like text mock_content = ( "This is a test WordPerfect document created for testing purposes. " "It contains multiple paragraphs and demonstrates the ability to " "extract text content from WordPerfect files. " "The text should be readable after processing through various methods." ) # Embed text in typical WP format (simplified) for char in mock_content: temp_file.write(char.encode('cp1252')) if char == ' ': temp_file.write(b'\x00') # Add some formatting codes temp_file.close() return temp_file.name async def test_wordperfect_processor(): """Test WordPerfect processor functionality.""" print("šŸ›ļø WordPerfect Processor Test") print("=" * 60) success_count = 0 total_tests = 0 try: from mcp_legacy_files.processors.wordperfect import WordPerfectProcessor, WordPerfectFileInfo # Test 1: Processor initialization total_tests += 1 print(f"\nšŸ“‹ Test 1: Processor Initialization") try: processor = WordPerfectProcessor() processing_chain = processor.get_processing_chain() print(f"āœ… WordPerfect processor initialized") print(f" Processing chain: {processing_chain}") print(f" Available methods: {len(processing_chain)}") # Verify fallback chain includes binary parser if "binary_parser" in processing_chain: print(f" āœ… Emergency binary parser available") success_count += 1 else: print(f" āŒ Missing emergency fallback") except Exception as e: print(f"āŒ Processor initialization failed: {e}") # Test 2: File structure analysis total_tests += 1 print(f"\nšŸ“‹ Test 2: File Structure Analysis") # Test with different WordPerfect versions test_versions = ["wp42", "wp50", "wp6", "wpd"] for version in test_versions: try: mock_file = create_mock_wpd_file(version) # Test structure analysis file_info = await processor._analyze_wp_structure(mock_file) if file_info: print(f" āœ… {version.upper()}: {file_info.version}") print(f" Product: {file_info.product_type}") print(f" Size: {file_info.file_size} bytes") print(f" Encoding: {file_info.encoding}") print(f" Password: {'Yes' if file_info.has_password else 'No'}") if file_info.document_area_pointer: print(f" Document pointer: 0x{file_info.document_area_pointer:X}") else: print(f" āŒ {version.upper()}: Structure analysis failed") # Clean up os.unlink(mock_file) except Exception as e: print(f" āŒ {version.upper()}: Error - {e}") if 'mock_file' in locals(): try: os.unlink(mock_file) except: pass success_count += 1 # Test 3: Processing method selection total_tests += 1 print(f"\nšŸ“‹ Test 3: Processing Method Selection") try: mock_file = create_mock_wpd_file("wp6") file_info = await processor._analyze_wp_structure(mock_file) if file_info: # Test each available processing method for method in processing_chain: try: print(f" Testing method: {method}") # Test method availability check result = await processor._process_with_method( mock_file, method, file_info, preserve_formatting=True ) if result: print(f" āœ… {method}: {'Success' if result.success else 'Expected failure'}") if result.success: print(f" Text length: {len(result.text_content or '')}") print(f" Method used: {result.method_used}") else: print(f" Error: {result.error_message}") else: print(f" āš ļø {method}: Method not available") except Exception as e: print(f" āŒ {method}: Exception - {e}") success_count += 1 else: print(f" āŒ Could not analyze mock file structure") os.unlink(mock_file) except Exception as e: print(f"āŒ Processing method test failed: {e}") # Test 4: Error handling total_tests += 1 print(f"\nšŸ“‹ Test 4: Error Handling") try: # Test with non-existent file result = await processor.process("nonexistent_file.wpd") if not result.success and "structure" in result.error_message.lower(): print(f" āœ… Non-existent file: Proper error handling") success_count += 1 else: print(f" āŒ Non-existent file: Unexpected result") except Exception as e: print(f"āŒ Error handling test failed: {e}") # Test 5: Encoding detection total_tests += 1 print(f"\nšŸ“‹ Test 5: Encoding Detection") try: # Test encoding detection for different versions version_encodings = { "WordPerfect 4.2": "cp437", "WordPerfect 5.0-5.1": "cp850", "WordPerfect 6.0+": "cp1252" } encoding_tests_passed = 0 for version, expected_encoding in version_encodings.items(): detected_encoding = processor._detect_wp_encoding(version, b"test_header") if detected_encoding == expected_encoding: print(f" āœ… {version}: {detected_encoding}") encoding_tests_passed += 1 else: print(f" āŒ {version}: Expected {expected_encoding}, got {detected_encoding}") if encoding_tests_passed == len(version_encodings): success_count += 1 except Exception as e: print(f"āŒ Encoding detection test failed: {e}") except ImportError as e: print(f"āŒ Could not import WordPerfect processor: {e}") return False # Summary print("\n" + "=" * 60) print("šŸ† WordPerfect Processor Test Results:") print(f" Tests passed: {success_count}/{total_tests}") print(f" Success rate: {(success_count/total_tests)*100:.1f}%") if success_count == total_tests: print(" šŸŽ‰ All tests passed! WordPerfect processor ready for use.") elif success_count >= total_tests * 0.8: print(" āœ… Most tests passed. WordPerfect processor functional with some limitations.") else: print(" āš ļø Several tests failed. WordPerfect processor needs attention.") print("\nšŸ’” Next Steps:") print(" • Install libwpd-tools for full WordPerfect support:") print(" sudo apt-get install libwpd-dev libwpd-tools") print(" • Test with real WordPerfect files from your archives") print(" • Verify processing chain works with actual documents") return success_count >= total_tests * 0.8 if __name__ == "__main__": import asyncio success = asyncio.run(test_wordperfect_processor()) sys.exit(0 if success else 1)