Repository navigation
Expand file tree
/
Copy pathtest-worker.js
More file actions
190 lines (163 loc) Β· 5.1 KB
/
Copy pathtest-worker.js
File metadata and controls
190 lines (163 loc) Β· 5.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
// Test script for Ada SnapFix Worker
const fetch = require('node-fetch');
const WORKER_URL = process.env.WORKER_URL || 'http://localhost:3000';
async function testHealth() {
try {
console.log('π Testing health endpoint...');
const response = await fetch(`${WORKER_URL}/health`);
const data = await response.json();
if (response.ok) {
console.log('β
Health check passed:', data);
return true;
} else {
console.log('β Health check failed:', data);
return false;
}
} catch (error) {
console.log('β Health check error:', error.message);
return false;
}
}
async function testBrowserService() {
try {
console.log('π Testing browser service...');
const response = await fetch(`${WORKER_URL}/browser`);
const data = await response.json();
if (response.ok && data.success) {
console.log('β
Browser service working:', data.message);
return true;
} else {
console.log('β Browser service failed:', data);
return false;
}
} catch (error) {
console.log('β Browser service error:', error.message);
return false;
}
}
async function testSyncScan() {
try {
console.log('π Testing sync scan...');
const response = await fetch(`${WORKER_URL}/api/scan-sync`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'https://example.com'
})
});
const data = await response.json();
if (response.ok) {
console.log('β
Sync scan completed:', {
url: data.url,
totalIssues: data.totalIssues,
scanType: data.metadata?.scanType
});
return true;
} else {
console.log('β Sync scan failed:', data);
return false;
}
} catch (error) {
console.log('β Sync scan error:', error.message);
return false;
}
}
async function testStreamingScan() {
try {
console.log('π Testing streaming scan...');
const response = await fetch(`${WORKER_URL}/api/scan`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'text/event-stream'
},
body: JSON.stringify({
url: 'https://example.com'
})
});
if (!response.ok) {
console.log('β Streaming scan failed:', response.status);
return false;
}
console.log('β
Streaming scan started, receiving logs...');
const reader = response.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) { // Show first 3 logs
console.log(`π Log: ${data.message}`);
}
} else if (data.type === 'error') {
console.log(`β Error: ${data.message}`);
}
} catch (parseError) {
console.log('β οΈ Parse error:', parseError.message);
}
}
}
}
return resultsReceived;
} catch (error) {
console.log('β Streaming scan error:', error.message);
return false;
}
}
async function runAllTests() {
console.log('π Starting Ada SnapFix Worker tests...\n');
const tests = [
{ name: 'Health Check', fn: testHealth },
{ name: 'Browser Service', fn: testBrowserService },
{ name: 'Sync Scan', fn: testSyncScan },
{ name: 'Streaming Scan', fn: testStreamingScan }
];
const results = [];
for (const test of tests) {
console.log(`\n--- ${test.name} ---`);
const result = await test.fn();
results.push({ name: test.name, passed: result });
console.log(`--- ${result ? 'PASSED' : 'FAILED'} ---\n`);
}
console.log('π Test Results:');
results.forEach(result => {
console.log(`${result.passed ? 'β
' : 'β'} ${result.name}`);
});
const passedCount = results.filter(r => r.passed).length;
const totalCount = results.length;
console.log(`\nπ― Overall: ${passedCount}/${totalCount} tests passed`);
if (passedCount === totalCount) {
console.log('π All tests passed! Worker is ready for production.');
} else {
console.log('β οΈ Some tests failed. Check the logs above for details.');
}
}
// Run tests if this file is executed directly
if (require.main === module) {
runAllTests().catch(console.error);
}
module.exports = {
testHealth,
testBrowserService,
testSyncScan,
testStreamingScan,
runAllTests
};