/ laxyfile / test_error_fixes.py
swadhin biswas Update on working process Β· 7/28/2025
f1cf876
290 lines Β· 9.1 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python3
"""
Test script to verify the LaxyFile error fixes are working correctly.

This script tests the FileManagerService, ComponentInitializer, and ErrorHandlingMixin
to ensure they properly handle the NoneType errors that were occurring.
"""

import sys
import os
from pathlib import Path
import time
import threading

# Add the project root to Python path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

def test_file_manager_service():
    """Test FileManagerService singleton and initialization"""
    print("πŸ§ͺ Testing FileManagerService...")

    try:
        from laxyfile.core.file_manager_service import FileManagerService

        # Test singleton behavior
        service1 = FileManagerService.get_instance()
        service2 = FileManagerService.get_instance()

        assert service1 is service2, "FileManagerService should be a singleton"
        print("βœ… Singleton behavior working correctly")

        # Test initialization
        success = service1.ensure_initialized()
        print(f"βœ… Initialization: {'Success' if success else 'Failed'}")

        # Test health check
        is_healthy = service1.is_healthy()
        print(f"βœ… Health check: {'Healthy' if is_healthy else 'Unhealthy'}")

        # Test directory listing
        current_dir = Path.cwd()
        files = service1.list_directory(current_dir)
        print(f"βœ… Directory listing: Found {len(files)} items in {current_dir}")

        # Test status
        status = service1.get_status()
        print(f"βœ… Service status: {status}")

        return True

    except Exception as e:
        print(f"❌ FileManagerService test failed: {e}")
        return False

def test_component_initializer():
    """Test ComponentInitializer proper initialization order"""
    print("\nπŸ§ͺ Testing ComponentInitializer...")

    try:
        from laxyfile.core.component_initializer import ComponentInitializer
        from laxyfile.core.config import Config

        config = Config()
        initializer = ComponentInitializer(config)

        # Test core services initialization
        core_success = initializer.initialize_core_services()
        print(f"βœ… Core services initization: {'Success' if core_success else 'Failed'}")

        # Test UI components initialization
        ui_success = initializer.initialize_ui_components()
        print(f"βœ… UI components initialization: {'Success' if ui_success else 'Failed'}")

        # Test dependency verification
        missing_deps = initializer.verify_dependencies()
        print(f"βœ… Dependency verification: {len(missing_deps)} missing dependencies")
        if missing_deps:
            print(f"   Missing: {missing_deps}")

        # Test initialization status
        status = initializer.get_initialization_status()
        print(f"βœ… Initialization status: {status}")

        # Test health check
        health = initializer.health_check()
        print(f"βœ… Health check: {'Healthy' if health else 'Unhealthy'}")

        return True

    except Exception as e:
        print(f"❌ ComponentInitializer test failed: {e}")
        return False

def test_error_handling_mixin():
    """Test ErrorHandlingMixin error handling and recovery"""
    print("\nπŸ§ͺ Testing ErrorHandlingMixin...")

    try:
        from laxyfile.core.error_handling_mixin import ErrorHandlingMixin

        class TestComponent(ErrorHandlingMixin):
            def __init__(self):
                super().__init__()
                self.call_count = 0

            def test_operation(self):
                self.call_count += 1
                if self.call_count < 3:
                    raise Exception("Simulated error")
                return "Success"

            def failing_operation(self):
                raise Exception("Always fails")

        component = TestComponent()

        # Test safe operation with retry
        result = component.safe_file_operation(
            operation=component.test_operation,
            fallback_value="Fallback",
            max_retries=3
        )
        print(f"βœ… Safe operation with retry: {result}")

        # Test safe operation with fallback
        result = component.safe_file_operation(
            operation=component.failing_operation,
            fallback_value="Fallback used",
            max_retries=1
        )
        print(f"βœ… Safe operation with fallback: {result}")

        # Test error statistics
        stats = component.get_error_statistics()
        print(f"βœ… Error statistics: {stats}")

        # Test file manager availability check
        available = component.is_file_manager_available()
        print(f"βœ… File manager availability: {'Available' if available else 'Unavailable'}")

        return True

    except Exception as e:
        print(f"❌ ErrorHandlingMixin test failed: {e}")
        return False

def test_panel_manager_integration():
    """Test PanelManager integration with FileManagerService"""
    print("\nπŸ§ͺ Testing PanelManager integration...")

    try:
        from laxyfile.ui.panels import PanelManager
        from laxyfile.ui.theme import ThemeManager
        from laxyfile.core.config import Config

        config = Config()
        theme_manager = ThemeManager(config)
        panel_manager = PanelManager(theme_manager)

        # Test panel rendering
        current_dir = Path.cwd()
        panel = panel_manager.render_panel(current_dir, "test", is_active=True)

        print(f"βœ… Panel rendering: Successfully created panel for {current_dir}")
        print(f"   Panel type: {type(panel)}")

        # Test error handling in panel manager
        non_existent_path = Path("/non/existent/path")
        error_panel = panel_manager.render_panel(non_existent_path, "error_test", is_active=False)

        print(f"βœ… Error handling: Successfully handled non-existent path")
        print(f"   Error panel type: {type(error_panel)}")

        return True

    except Exception as e:
        print(f"❌ PanelManager integration test failed: {e}")
        return False

def test_thread_safety():
    """Test thread safety of FileManagerService"""
    print("\nπŸ§ͺ Testing thread safety...")

    try:
        from laxyfile.core.file_manager_service import FileManagerService

        results = []
        errors = []

        def worker_thread(thread_id):
            try:
                service = FileManagerService.get_instance()
                current_dir = Path.cwd()
                files = service.list_directory(current_dir)
                results.append((thread_id, len(files)))
            except Exception as e:
                errors.append((thread_id, str(e)))

        # Create multiple threads
        threads = []
        for i in range(5):
            thread = threading.Thread(target=worker_thread, args=(i,))
            threads.append(thread)
            thread.start()

        # Wait for all threads to complete
        for thread in threads:
            thread.join()

        print(f"βœ… Thread safety: {len(results)} successful operations, {len(errors)} errors")
        if errors:
            print(f"   Errors: {errors}")

        return len(errors) == 0

    except Exception as e:
        print(f"❌ Thread safety test failed: {e}")
        return False

def test_recovery_mechanisms():
    """Test automatic recovery mechanisms"""
    print("\nπŸ§ͺ Testing recovery mechanisms...")

    try:
        from laxyfile.core.file_manager_service import FileManagerService

        service = FileManagerService.get_instance()

        # Test reinitialization
        reinit_success = service.reinitialize(force=True)
        print(f"βœ… Reinitialization: {'Success' if reinit_success else 'Failed'}")

        # Test health after reinitialization
        is_healthy = service.is_healthy()
        print(f"βœ… Health after reinit: {'Healthy' if is_healthy else 'Unhealthy'}")

        # Test service reset and recovery
        FileManagerService.reset()
        new_service = FileManagerService.get_instance()
        recovery_success = new_service.ensure_initialized()
        print(f"βœ… Service recovery: {'Success' if recovery_success else 'Failed'}")

        return True

    except Exception as e:
        print(f"❌ Recovery mechanisms test failed: {e}")
        return False

def main():
    """Run all tests"""
    print("πŸš€ Starting LaxyFile Error Fixes Test Suite")
    print("=" * 50)

    tests = [
        test_file_manager_service,
        test_component_initializer,
        test_error_handling_mixin,
        test_panel_manager_integration,
        test_thread_safety,
        test_recovery_mechanisms
    ]

    passed = 0
    failed = 0

    for test in tests:
        try:
            if test():
                passed += 1
            else:
                failed += 1
        except Exception as e:
            print(f"❌ Test {test.__name__} crashed: {e}")
            failed += 1

        time.sleep(0.1)  # Brief pause between tests

    print("\n" + "=" * 50)
    print(f"πŸ“Š Test Results: {passed} passed, {failed} failed")

    if failed == 0:
        print("πŸŽ‰ All tests passed! Error fixes are working correctly.")
        return 0
    else:
        print("⚠️  Some tests failed. Please check the error messages above.")
        return 1

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