forked from angrytidy/ada-snapfix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-worker-integration.js
More file actions
107 lines (91 loc) · 3.25 KB
/
test-worker-integration.js
File metadata and controls
107 lines (91 loc) · 3.25 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
// Test script for worker integration
const fetch = require('node-fetch');
const NEXTJS_URL = process.env.NEXTJS_URL || 'http://localhost:3000';
const WORKER_URL = process.env.WORKER_URL || 'http://localhost:9999';
async function testWorkerIntegration() {
console.log('🧪 Testing Worker Integration with Next.js App...\n');
try {
// Test 1: Health check
console.log('1️⃣ Testing health check...');
const healthResponse = await fetch(`${NEXTJS_URL}/api/scan`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ url: 'https://example.com' })
});
if (healthResponse.ok) {
const healthData = await healthResponse.json();
console.log('✅ Health check passed:', {
status: healthResponse.status,
hasResults: !!healthData.issues,
totalIssues: healthData.totalIssues,
scanType: healthData.metadata?.scanType
});
} else {
console.log('❌ Health check failed:', healthResponse.status);
}
// Test 2: Streaming scan
console.log('\n2️⃣ Testing streaming scan...');
const streamResponse = await fetch(`${NEXTJS_URL}/api/scan`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'text/event-stream'
},
body: JSON.stringify({ url: 'https://example.com' })
});
if (streamResponse.ok) {
console.log('✅ Streaming scan started successfully');
const reader = streamResponse.body.getReader();
const decoder = new TextDecoder();
let logCount = 0;
let resultsReceived = false;
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
try {
const data = JSON.parse(line.slice(6));
if (data.type === 'results') {
console.log('✅ Streaming scan completed:', {
url: data.data.url,
totalIssues: data.data.totalIssues,
scanType: data.data.metadata?.scanType
});
resultsReceived = true;
} else if (data.type === 'log') {
logCount++;
if (logCount <= 3) {
console.log(`📝 Log: ${data.message}`);
}
} else if (data.type === 'error') {
console.log(`❌ Error: ${data.message}`);
}
} catch (parseError) {
// Ignore parse errors for non-JSON lines
}
}
}
}
if (resultsReceived) {
console.log('✅ Streaming scan test passed');
} else {
console.log('❌ Streaming scan test failed - no results received');
}
} else {
console.log('❌ Streaming scan failed:', streamResponse.status);
}
console.log('\n🎉 Worker integration test completed!');
} catch (error) {
console.error('❌ Test failed:', error.message);
}
}
// Run test if this file is executed directly
if (require.main === module) {
testWorkerIntegration();
}
module.exports = { testWorkerIntegration };