/ laxyfile / test_media_viewer_fix.py
swadhin biswas Update on working process Β· 7/28/2025
f1cf876
145 lines Β· 4.7 KB
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
#!/usr/bin/env python3
"""
Test script to verify the media viewer NoneType error fix
"""

import sys
from pathlib import Path

# Add the laxyfile package to the path
sys.path.insert(0, str(Path(__file__).parent))

def test_media_viewer_error_handling():
    """Test that media viewer handles initialization errors gracefully"""
    print("πŸ§ͺ Testing Media Viewer Error Handling...")

    try:
        from laxyfile.main import LaxyFileApp

        # Create LaxyFile app
        app = LaxyFileApp()
        print("  βœ… LaxyFile app created successfully")

        # Test create_preview_panel method - this should not crash even if media viewer is None
        print("  πŸ” Testing create_preview_panel method...")

        try:
            preview_panel = app.create_preview_panel()
            print("  βœ… Preview panel created successfully without crashing")

            # Check if the panel has content (even if it's an error message)
            if hasattr(preview_panel, 'renderable'):
                print("  βœ… Preview panel has renderable content")
            else:
                print("  βœ… Preview panel created (may be fallback content)")

        except Exception as e:
            print(f"  ❌ Preview panel creation failed: {e}")
            return False

        # Test that media viewer lazy initialization works
        print("  πŸ” Testing media viewer lazy initialization...")

        try:
            media_viewer = app._get_media_viewer()
            if media_viewer is not None:
                print("  βœ… Media viewer initialized successfully")
            else:
                print("  ⚠️  Media viewer is None (expected if dependencies missing)")

        except Exception as e:
            print(f"  ❌ Media viewer initialization failed: {e}")
            return False

        print("  βœ… Media viewer error handling test completed successfully")
        return True

    except Exception as e:
        print(f"  ❌ Media viewer test failed: {e}")
        import traceback
        traceback.print_exc()
        return False

def test_file_manager_service_integration():
    """Test that file manager service integration works correctly"""
    print("\nπŸ§ͺ Testing File Manager Service Integration...")

    try:
        from laxyfile.main import LaxyFileApp
        from laxyfile.core.file_manager_service import FileManagerService

        # Reset file manager service to test initialization
        FileManagerService.reset()

        # Create LaxyFile app
        app = LaxyFileApp()
        print("  βœ… LaxyFile app created successfully")

        # Test file manager property access
        print("  πŸ” Testing file manager property access...")

        try:
            file_manager = app.file_manager
            if file_manager is not None:
                print("  βœ… File manager accessed successfully through property")
            else:
                print("  ⚠️  File manager is None (may need initialization)")

        except Exception as e:
            print(f"  ❌ File manager property access failed: {e}")
            return False

        # Test safe file operations
        print("  πŸ” Testing safe file operations...")

        try:
            # This should not crash even if file manager is not available
            test_result = app.safe_file_operation(
                operation=lambda: [],
                fallback_value=[],
                cache_key="test_operation"
            )
            print("  βœ… Safe file operation completed successfully")

        except Exception as e:
            print(f"  ❌ Safe file operation failed: {e}")
            return False

        print("  βœ… File manager service integration test completed successfully")
        return True

    except Exception as e:
        print(f"  ❌ File manager service integration test failed: {e}")
        import traceback
        traceback.print_exc()
        return False

def main():
    """Run all tests"""
    print("πŸš€ Starting LaxyFile Media Viewer Error Fix Tests\n")

    test_results = []

    # Test media viewer error handling
    result1 = test_media_viewer_error_handling()
    test_results.append(result1)

    # Test file manager service integration
    result2 = test_file_manager_service_integration()
    test_results.append(result2)

    # Summary
    print(f"\nπŸ“Š Test Results:")
    print(f"  βœ… Passed: {sum(test_results)}")
    print(f"  ❌ Failed: {len(test_results) - sum(test_results)}")

    if all(test_results):
        print("\nπŸŽ‰ All tests passed! Media viewer and file manager error handling is working correctly.")
        return 0
    else:
        print("\nπŸ’₯ Some tests failed. Please check the error messages above.")
        return 1

if __name__ == "__main__":
    exit_code = main()
    sys.exit(exit_code)