-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-implementation.js
More file actions
595 lines (512 loc) · 22.2 KB
/
Copy pathtest-implementation.js
File metadata and controls
595 lines (512 loc) · 22.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/usr/bin/env node
/**
* LPS Crawler Implementation Test Suite
* Validates all production-grade components are working correctly
*
* @author Production-Grade Implementer
* @version 1.0.0
*/
const fs = require('fs').promises;
const path = require('path');
const http = require('http');
const mathUtils = require('./src/utils/mathUtils');
const {
BrowserInterface,
LPSCrawler,
TextExtractor,
MFTExtractor,
TBRExtractor,
AudioExtractor,
PDFExtractor,
DataSynthesizer
} = require('./src');
const { createTestServer, closeServer } = require('./tests/test-utils');
class ImplementationTester {
constructor() {
this.testResults = [];
this.startTime = Date.now();
this.testContext = {};
}
async runAllTests() {
console.log('🔍 LPS Crawler Implementation Test Suite');
console.log('==========================================');
console.log('');
const { server, baseUrl } = await createTestServer();
this.testContext.server = server;
this.testContext.baseUrl = baseUrl;
try {
await this.testFileStructure();
await this.testCodeQuality();
await this.testServerFunctionality();
await this.testMathUtils();
await this.testBrowserInterfaceIntegration();
await this.testCrawlerIntegration();
await this.testExtractorIntegration();
await this.testDataSynthesizerIntegration();
await this.testDesktopAppStructure();
await this.testGUIImplementation();
} finally {
await closeServer(this.testContext.server);
}
this.generateTestReport();
}
async testFileStructure() {
console.log('📁 Testing File Structure...');
const requiredFiles = [
'serve-gui.js',
'index.html',
'package.json',
'start-desktop.js',
'desktop/main.js',
'desktop/preload.js'
];
for (const file of requiredFiles) {
try {
await fs.access(path.join(__dirname, file));
this.testResults.push({
test: `File exists: ${file}`,
status: 'PASS',
details: 'File found and accessible'
});
console.log(` ✅ ${file}`);
} catch (error) {
this.testResults.push({
test: `File exists: ${file}`,
status: 'FAIL',
details: `File not found: ${error.message}`
});
console.log(` ❌ ${file} - MISSING`);
}
}
console.log('');
}
async testCodeQuality() {
console.log('🔍 Testing Code Quality...');
const filesToCheck = [
'start-desktop.js',
'serve-gui.js',
'desktop/main.js',
'index.html'
];
for (const file of filesToCheck) {
try {
const content = await fs.readFile(path.join(__dirname, file), 'utf8');
const lines = content.split('\n');
const hasComments = lines.some(line => line.includes('/**') || line.includes('//'));
const hasErrorHandling = content.includes('try') || content.includes('catch') || content.includes('error');
const hasProductionFeatures = content.includes('production') || content.includes('PRODUCTION');
this.testResults.push({
test: `Code quality: ${file}`,
status: hasComments && hasErrorHandling ? 'PASS' : 'WARN',
details: `Lines: ${lines.length}, Comments: ${hasComments}, Error handling: ${hasErrorHandling}, Production features: ${hasProductionFeatures}`
});
const status = hasComments && hasErrorHandling ? '✅' : '⚠️';
console.log(` ${status} ${file} - ${lines.length} lines`);
} catch (error) {
this.testResults.push({
test: `Code quality: ${file}`,
status: 'FAIL',
details: `Could not read file: ${error.message}`
});
console.log(` ❌ ${file} - ERROR`);
}
}
console.log('');
}
async testServerFunctionality() {
console.log('🌐 Testing Server Functionality...');
// Start the server in a child process
const { spawn } = require('child_process');
const serverProcess = spawn('node', ['serve-gui.js'], {
stdio: 'pipe',
env: { ...process.env, PORT: '3002' }
});
let serverStarted = false;
let serverOutput = '';
serverProcess.stdout.on('data', (data) => {
serverOutput += data.toString();
if (data.toString().includes('READY TO USE!')) {
serverStarted = true;
}
});
// Wait for server to start
await this.delay(3000);
if (serverStarted) {
console.log(' ✅ Server started successfully');
// Test HTTP requests
await this.testHTTPRequest('http://localhost:3002', 'Main page');
await this.testHTTPRequest('http://localhost:3002/index.html', 'GUI file');
await this.testHTTPRequest('http://localhost:3002/api/health', 'Health endpoint');
this.testResults.push({
test: 'Server functionality',
status: 'PASS',
details: 'Server starts and responds to HTTP requests'
});
} else {
console.log(' ❌ Server failed to start');
this.testResults.push({
test: 'Server functionality',
status: 'FAIL',
details: 'Server did not start within timeout'
});
}
// Kill server process
serverProcess.kill('SIGTERM');
await this.delay(1000);
console.log('');
}
recordResult(test, passed, details) {
const status = passed ? 'PASS' : 'FAIL';
this.testResults.push({ test, status, details });
const icon = passed ? '✅' : '❌';
console.log(` ${icon} ${test}`);
}
async testMathUtils() {
console.log('📐 Testing Mathematical Utilities...');
const range = Array.from({ length: 14 }, (_, idx) => idx - 1);
const clampResults = range.map(value => mathUtils.clamp(value, 0, 10));
const clampWithinBounds = clampResults.every(value => value >= 0 && value <= 10);
this.recordResult(
'clamp enforces bounds for -1..12',
clampWithinBounds,
`Clamp results: ${clampResults.join(', ')}`
);
const readingTimes = range.map(value => mathUtils.calculateReadingTime(value, 200));
const readingTimeValid = readingTimes.filter(value => value !== null).every(value => value >= 1);
this.recordResult(
'calculateReadingTime handles -1..12 range',
readingTimeValid,
`Reading time outputs: ${readingTimes.join(', ')}`
);
const rateLimitWaits = range.map(value => mathUtils.calculateRateLimitWait(1000, value * 100, 100));
const waitValid = rateLimitWaits.every(value => value >= 100);
this.recordResult(
'calculateRateLimitWait enforces minWait across -1..12',
waitValid,
`Waits: ${rateLimitWaits.join(', ')}`
);
const successRates = range.map(value => mathUtils.calculateSuccessRate(Math.max(0, value), 12));
const successRateValid = successRates.every(result => typeof result.rate === 'number' && typeof result.percentage === 'string');
this.recordResult(
'calculateSuccessRate outputs stable types for -1..12',
successRateValid,
`Rates: ${successRates.map(r => r.percentage).join(', ')}`
);
console.log('');
}
async testBrowserInterfaceIntegration() {
console.log('🧭 Testing BrowserInterface Integration...');
const browser = new BrowserInterface({
headless: true,
respectRobots: false,
defaultTimeout: 5000
});
try {
await browser.initialize(this.testContext.baseUrl);
const scrollLinks = await browser.interact('SCROLL');
const hasPage2 = scrollLinks.some(link => link.url.includes('/page2'));
this.recordResult(
'BrowserInterface scroll link extraction',
hasPage2,
`Scroll returned ${scrollLinks.length} links`
);
const nextLinks = await browser.interact('PAGE_NEXT');
const hasPage3 = nextLinks.some(link => link.url.includes('/page3'));
this.recordResult(
'BrowserInterface pagination link extraction',
hasPage3,
`Pagination returned ${nextLinks.length} links`
);
} catch (error) {
this.recordResult('BrowserInterface integration', false, error.message);
} finally {
await browser.close();
}
console.log('');
}
async testCrawlerIntegration() {
console.log('🕸️ Testing LPSCrawler Integration...');
const browser = new BrowserInterface({
headless: true,
respectRobots: false,
defaultTimeout: 5000
});
const outputDir = path.join(__dirname, 'test-output');
const crawler = new LPSCrawler(browser, {
maxPhases: 2,
saveInterval: 99,
outputDir
});
try {
const report = await crawler.run(this.testContext.baseUrl);
this.testContext.crawlerLog = crawler.extractionLog;
this.recordResult(
'LPSCrawler report links found',
report.linksFound > 0,
`Links found: ${report.linksFound}`
);
this.recordResult(
'LPSCrawler report phases executed',
report.phases >= 2,
`Phases: ${report.phases}`
);
} catch (error) {
this.recordResult('LPSCrawler integration', false, error.message);
}
console.log('');
}
async testExtractorIntegration() {
console.log('🧪 Testing Extractor Integrations...');
const baseUrl = this.testContext.baseUrl;
const browserOptions = { headless: true, respectRobots: false, defaultTimeout: 5000 };
try {
const textExtractor = new TextExtractor(new BrowserInterface(browserOptions), {
waitForDynamicContent: 200
});
const textResults = await textExtractor.run(baseUrl);
this.recordResult(
'TextExtractor summary generated',
textResults?.summary?.wordCount > 0,
`Word count: ${textResults?.summary?.wordCount}`
);
const imageExtractor = new MFTExtractor(new BrowserInterface(browserOptions), {
maxScrolls: 1,
scrollDelay: 150,
stabilizationDelay: 300
});
const imageResults = await imageExtractor.run(baseUrl);
const heroImage = `${baseUrl}/media/hero.jpg`;
this.recordResult(
'MFTExtractor hero image detected',
imageResults.items.includes(heroImage),
`Images found: ${imageResults.items.length}`
);
const videoExtractor = new TBRExtractor(new BrowserInterface(browserOptions), {
observationWindow: 500,
scanShadowDOM: false
});
const videoResults = await videoExtractor.run(baseUrl);
const hasHls = videoResults.grouped.hls.some(url => url.includes('stream.m3u8'));
const hasMp4 = videoResults.grouped.direct.some(url => url.includes('video.mp4'));
this.recordResult(
'TBRExtractor streaming and direct video detected',
hasHls && hasMp4,
`HLS: ${videoResults.grouped.hls.length}, Direct: ${videoResults.grouped.direct.length}`
);
const audioExtractor = new AudioExtractor(new BrowserInterface(browserOptions), {
observationWindow: 500
});
const audioResults = await audioExtractor.run(baseUrl);
const hasMp3 = audioResults.grouped.mp3.some(url => url.includes('audio.mp3'));
this.recordResult(
'AudioExtractor mp3 detected',
hasMp3,
`Audio found: ${audioResults.items.length}`
);
const pdfExtractor = new PDFExtractor(new BrowserInterface(browserOptions));
const pdfResults = await pdfExtractor.run(baseUrl);
const hasPdf = pdfResults.grouped.pdf.some(url => url.includes('sample.pdf'));
this.recordResult(
'PDFExtractor pdf detected',
hasPdf,
`Documents found: ${pdfResults.items.length}`
);
} catch (error) {
this.recordResult('Extractor integration', false, error.message);
}
console.log('');
}
async testDataSynthesizerIntegration() {
console.log('🧾 Testing DataSynthesizer Integration...');
const logs = this.testContext.crawlerLog || [];
if (logs.length === 0) {
this.recordResult('DataSynthesizer input logs', false, 'No crawl log entries captured');
console.log('');
return;
}
const synthesizer = new DataSynthesizer(logs);
const jsonl = synthesizer.toJSONL();
const markdown = synthesizer.toMarkdown();
const csv = synthesizer.toCSV();
const raw = synthesizer.toRaw();
this.recordResult(
'DataSynthesizer JSONL entries',
jsonl.split('\n').length === logs.length,
`JSONL lines: ${jsonl.split('\n').length}`
);
this.recordResult(
'DataSynthesizer Markdown report',
markdown.includes('# LPS Discovery Report') && markdown.includes('## Summary'),
'Markdown report generated'
);
this.recordResult(
'DataSynthesizer CSV output',
csv.startsWith('timestamp,phase,interaction,url,text,title'),
'CSV header validated'
);
this.recordResult(
'DataSynthesizer raw output',
raw.includes('SCROLL') || raw.includes('PAGE_NEXT'),
'Raw output contains interactions'
);
console.log('');
}
async testHTTPRequest(url, description) {
return new Promise((resolve) => {
const req = http.get(url, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
const success = res.statusCode === 200;
const status = success ? '✅' : '❌';
console.log(` ${status} ${description} - HTTP ${res.statusCode}`);
this.testResults.push({
test: `HTTP request: ${description}`,
status: success ? 'PASS' : 'FAIL',
details: `Status: ${res.statusCode}, Content length: ${data.length}`
});
resolve();
});
});
req.on('error', (error) => {
console.log(` ❌ ${description} - Error: ${error.message}`);
this.testResults.push({
test: `HTTP request: ${description}`,
status: 'FAIL',
details: `Error: ${error.message}`
});
resolve();
});
req.setTimeout(5000, () => {
req.abort();
console.log(` ❌ ${description} - Timeout`);
this.testResults.push({
test: `HTTP request: ${description}`,
status: 'FAIL',
details: 'Request timeout'
});
resolve();
});
});
}
async testDesktopAppStructure() {
console.log('🖥️ Testing Desktop App Structure...');
try {
const desktopAppContent = await fs.readFile(path.join(__dirname, 'desktop', 'main.js'), 'utf8');
const hasElectron = desktopAppContent.includes('electron');
const hasIPCHandlers = desktopAppContent.includes('ipcMain');
const hasBrowserWindow = desktopAppContent.includes('BrowserWindow');
const hasErrorHandling = desktopAppContent.includes('try') || desktopAppContent.includes('catch');
const hasGracefulShutdown = desktopAppContent.includes('SIGTERM') || desktopAppContent.includes('cleanup');
const checks = [
{ name: 'Electron integration', passed: hasElectron },
{ name: 'IPC handlers', passed: hasIPCHandlers },
{ name: 'BrowserWindow usage', passed: hasBrowserWindow },
{ name: 'Error handling', passed: hasErrorHandling },
{ name: 'Graceful shutdown', passed: hasGracefulShutdown }
];
checks.forEach(check => {
const status = check.passed ? '✅' : '❌';
console.log(` ${status} ${check.name}`);
this.testResults.push({
test: `Desktop app: ${check.name}`,
status: check.passed ? 'PASS' : 'FAIL',
details: check.passed ? 'Feature implemented' : 'Feature missing'
});
});
} catch (error) {
console.log(` ❌ Could not read desktop app file: ${error.message}`);
this.testResults.push({
test: 'Desktop app structure',
status: 'FAIL',
details: `File read error: ${error.message}`
});
}
console.log('');
}
async testGUIImplementation() {
console.log('🎨 Testing GUI Implementation...');
try {
const guiContent = await fs.readFile(path.join(__dirname, 'index.html'), 'utf8');
const hasProductionClasses = guiContent.includes('WebCrawler') || guiContent.includes('ImageExtractor') || guiContent.includes('VideoExtractor');
const hasErrorHandling = guiContent.includes('try') || guiContent.includes('catch') || guiContent.includes('error');
const hasStateManagement = guiContent.includes('AppState');
const hasAdvancedUI = guiContent.includes('status-indicator') || guiContent.includes('log-output');
const hasExportFunction = guiContent.includes('exportResults');
const hasSettings = guiContent.includes('saveSettings');
const checks = [
{ name: 'Production-grade classes', passed: hasProductionClasses },
{ name: 'Error handling', passed: hasErrorHandling },
{ name: 'State management', passed: hasStateManagement },
{ name: 'Advanced UI features', passed: hasAdvancedUI },
{ name: 'Export functionality', passed: hasExportFunction },
{ name: 'Settings management', passed: hasSettings }
];
checks.forEach(check => {
const status = check.passed ? '✅' : '❌';
console.log(` ${status} ${check.name}`);
this.testResults.push({
test: `GUI: ${check.name}`,
status: check.passed ? 'PASS' : 'FAIL',
details: check.passed ? 'Feature implemented' : 'Feature missing'
});
});
} catch (error) {
console.log(` ❌ Could not read GUI file: ${error.message}`);
this.testResults.push({
test: 'GUI implementation',
status: 'FAIL',
details: `File read error: ${error.message}`
});
}
console.log('');
}
generateTestReport() {
const duration = ((Date.now() - this.startTime) / 1000).toFixed(1);
const passed = this.testResults.filter(r => r.status === 'PASS').length;
const failed = this.testResults.filter(r => r.status === 'FAIL').length;
const warnings = this.testResults.filter(r => r.status === 'WARN').length;
const total = this.testResults.length;
console.log('');
console.log('📊 TEST SUMMARY');
console.log('===============');
console.log(`Total tests: ${total}`);
console.log(`Passed: ${passed}`);
console.log(`Failed: ${failed}`);
console.log(`Warnings: ${warnings}`);
console.log(`Duration: ${duration}s`);
console.log('');
if (failed === 0) {
console.log('🎉 ALL TESTS PASSED!');
console.log('✅ Implementation is production-ready');
} else {
console.log('❌ Some tests failed. Please review the implementation.');
}
// Generate detailed report
const report = {
timestamp: new Date().toISOString(),
summary: {
total,
passed,
failed,
warnings,
duration: duration + 's'
},
results: this.testResults
};
// Save report
const reportPath = path.join(__dirname, 'test-report.json');
fs.writeFile(reportPath, JSON.stringify(report, null, 2))
.then(() => console.log(`📋 Detailed report saved: ${reportPath}`))
.catch(err => console.error('Failed to save report:', err));
}
delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
// Run tests if this file is executed directly
if (require.main === module) {
const tester = new ImplementationTester();
tester.runAllTests().catch(console.error);
}
module.exports = ImplementationTester;