Repository navigation
Expand file tree
/
Copy pathtest_core_system.py
More file actions
331 lines (247 loc) · 10.2 KB
/
Copy pathtest_core_system.py
File metadata and controls
331 lines (247 loc) · 10.2 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python3
"""
Test script for MangaForge Phase 1 core system.
This script tests all the core components to ensure they work correctly
with the mock provider before moving to Phase 2.
"""
import logging
import sys
from pathlib import Path
# Add the current directory to Python path for imports
sys.path.insert(0, str(Path(__file__).parent))
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def test_provider_auto_discovery():
"""Test that ProviderManager can auto-discover the mock provider."""
logger.info("Testing provider auto-discovery...")
try:
from core.provider_manager import ProviderManager
manager = ProviderManager()
# Check if mock provider was loaded
if 'mock' not in manager.providers:
logger.error("Mock provider not auto-discovered!")
return False
logger.info(f"✓ Auto-discovered providers: {list(manager.providers.keys())}")
# Test getting provider by ID
mock_provider = manager.get_provider('mock')
logger.info(f"✓ Got mock provider: {mock_provider}")
# Test provider info
info = manager.get_provider_info('mock')
logger.info(f"✓ Provider info: {info}")
return True
except Exception as e:
logger.error(f"Provider auto-discovery test failed: {e}")
return False
def test_mock_provider_functionality():
"""Test that the mock provider works correctly."""
logger.info("Testing mock provider functionality...")
try:
from core.provider_manager import ProviderManager
manager = ProviderManager()
mock_provider = manager.get_provider('mock')
# Test search
logger.info("Testing search...")
results, has_next = mock_provider.search("test manga", page=1)
if not results:
logger.error("Search returned no results!")
return False
logger.info(f"✓ Search returned {len(results)} results")
logger.info(f" First result: {results[0]}")
# Test get_manga_info
logger.info("Testing get_manga_info...")
manga_info = mock_provider.get_manga_info(manga_id="test_id")
logger.info(f"✓ Got manga info: {manga_info.title}")
logger.info(f" Genres: {manga_info.genres}")
logger.info(f" Status: {manga_info.status}")
# Test get_chapters
logger.info("Testing get_chapters...")
chapters = mock_provider.get_chapters("test_id")
if not chapters:
logger.error("get_chapters returned no chapters!")
return False
logger.info(f"✓ Got {len(chapters)} chapters")
logger.info(f" First chapter: {chapters[0]}")
logger.info(f" Last chapter: {chapters[-1]}")
# Test get_chapter_images
logger.info("Testing get_chapter_images...")
image_urls = mock_provider.get_chapter_images(chapters[0].chapter_id)
if not image_urls:
logger.error("get_chapter_images returned no image URLs!")
return False
logger.info(f"✓ Got {len(image_urls)} image URLs")
logger.info(f" First image URL: {image_urls[0]}")
# Test download_image
logger.info("Testing download_image...")
image_data = mock_provider.download_image(image_urls[0])
if not image_data:
logger.error("download_image returned no data!")
return False
logger.info(f"✓ Downloaded image data: {len(image_data)} bytes")
return True
except Exception as e:
logger.error(f"Mock provider functionality test failed: {e}")
import traceback
traceback.print_exc()
return False
def test_config_system():
"""Test that the configuration system works correctly."""
logger.info("Testing configuration system...")
try:
from core.config import Config
config = Config()
# Test basic config access
download_dir = config.download_dir
max_workers = config.max_chapter_workers
default_format = config.default_format
logger.info(f"✓ Download directory: {download_dir}")
logger.info(f"✓ Max chapter workers: {max_workers}")
logger.info(f"✓ Default format: {default_format}")
# Test provider rate limits
mock_rate_limit = config.get_rate_limit('mock')
logger.info(f"✓ Mock provider rate limit: {mock_rate_limit}s")
return True
except Exception as e:
logger.error(f"Configuration system test failed: {e}")
return False
def test_downloader():
"""Test the downloader with mock data."""
logger.info("Testing downloader...")
try:
from core.provider_manager import ProviderManager
from core.downloader import Downloader
from core.config import Config
from pathlib import Path
import tempfile
# Create temporary directory for downloads
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
manager = ProviderManager()
mock_provider = manager.get_provider('mock')
downloader = Downloader()
config = Config()
# Get test data
manga_info = mock_provider.get_manga_info(manga_id="test_manga")
chapters = mock_provider.get_chapters("test_manga")[:2] # Just first 2 chapters
logger.info(f"Testing download of {len(chapters)} chapters...")
# Test downloading chapters
downloaded_paths = downloader.download_chapters(
mock_provider,
manga_info,
chapters,
temp_path
)
logger.info(f"✓ Downloaded {len(downloaded_paths)} chapters")
# Check that files were created
for path in downloaded_paths:
if path.exists():
logger.info(f"✓ Chapter directory exists: {path}")
# Check for image files
image_files = list(path.glob("*.jpg"))
logger.info(f" Contains {len(image_files)} images")
else:
logger.error(f"✗ Chapter directory not found: {path}")
return False
return True
except Exception as e:
logger.error(f"Downloader test failed: {e}")
import traceback
traceback.print_exc()
return False
def test_converter():
"""Test the converter functionality."""
logger.info("Testing converter...")
try:
from core.converter import Converter
from pathlib import Path
import tempfile
# Create temporary directory with fake images
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
fake_images_dir = temp_path / "fake_images"
fake_images_dir.mkdir()
# Create some fake image files
for i in range(5):
fake_image = fake_images_dir / f"page_{i:03d}.jpg"
fake_image.write_bytes(b"fake image data " * 100) # 1500 bytes
converter = Converter()
# Test CBZ conversion
cbz_path = temp_path / "test.cbz"
result_path = converter.to_cbz(fake_images_dir, cbz_path, delete_images=False)
if result_path.exists():
logger.info(f"✓ CBZ created successfully: {result_path}")
logger.info(f" File size: {result_path.stat().st_size} bytes")
else:
logger.error("✗ CBZ file not created!")
return False
# Test PDF conversion (if dependencies available)
try:
pdf_path = temp_path / "test.pdf"
result_path = converter.to_pdf(fake_images_dir, pdf_path, delete_images=False)
if result_path.exists():
logger.info(f"✓ PDF created successfully: {result_path}")
logger.info(f" File size: {result_path.stat().st_size} bytes")
else:
logger.warning("⚠ PDF file not created (missing dependencies?)")
except Exception as e:
logger.warning(f"⚠ PDF conversion failed (expected if dependencies missing): {e}")
return True
except Exception as e:
logger.error(f"Converter test failed: {e}")
import traceback
traceback.print_exc()
return False
def main():
"""Run all tests."""
logger.info("Starting MangaForge Phase 1 core system tests...")
tests = [
("Provider Auto-Discovery", test_provider_auto_discovery),
("Mock Provider Functionality", test_mock_provider_functionality),
("Configuration System", test_config_system),
("Downloader", test_downloader),
("Converter", test_converter),
]
results = []
for test_name, test_func in tests:
logger.info(f"\n{'='*50}")
logger.info(f"Running test: {test_name}")
logger.info(f"{'='*50}")
try:
success = test_func()
results.append((test_name, success))
if success:
logger.info(f"✓ {test_name} PASSED")
else:
logger.error(f"✗ {test_name} FAILED")
except Exception as e:
logger.error(f"✗ {test_name} FAILED with exception: {e}")
results.append((test_name, False))
# Summary
logger.info(f"\n{'='*60}")
logger.info("TEST SUMMARY")
logger.info(f"{'='*60}")
passed = 0
failed = 0
for test_name, success in results:
status = "PASSED" if success else "FAILED"
logger.info(f"{test_name:30} : {status}")
if success:
passed += 1
else:
failed += 1
logger.info(f"{'='*60}")
logger.info(f"Total: {len(results)} tests")
logger.info(f"Passed: {passed}")
logger.info(f"Failed: {failed}")
if failed == 0:
logger.info("🎉 All tests passed! Phase 1 core system is ready!")
return 0
else:
logger.error("❌ Some tests failed. Please fix the issues before proceeding.")
return 1
if __name__ == "__main__":
exit_code = main()
sys.exit(exit_code)