-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-enhanced-verification.js
More file actions
383 lines (326 loc) Β· 11.9 KB
/
Copy pathtest-enhanced-verification.js
File metadata and controls
383 lines (326 loc) Β· 11.9 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
/**
* Comprehensive Test Suite for Enhanced Background Verification System v2.0.0
*
* This script tests all aspects of the enhanced verification system:
* - Health check endpoints
* - Service statistics
* - Database performance
* - System monitoring
* - Error handling
*/
const BASE_URL = process.env.API_BASE_URL || 'http://localhost:3000/api/v1';
/**
* Test health check endpoints
*/
async function testHealthEndpoints() {
console.log('π₯ Testing Health Check Endpoints...\n');
const healthEndpoints = [
{
name: 'Basic Health Check',
url: `${BASE_URL}/health`,
expectedStatus: 200
},
{
name: 'Verification Service Health',
url: `${BASE_URL}/health/verification`,
expectedStatus: 200
},
{
name: 'Database Health Check',
url: `${BASE_URL}/health/database`,
expectedStatus: 200
},
{
name: 'System Health Check',
url: `${BASE_URL}/health/system`,
expectedStatus: 200
},
{
name: 'Transaction Statistics',
url: `${BASE_URL}/health/transactions`,
expectedStatus: 200
}
];
for (const endpoint of healthEndpoints) {
console.log(`π Testing: ${endpoint.name}`);
console.log(`π URL: ${endpoint.url}`);
try {
const startTime = Date.now();
const response = await fetch(endpoint.url);
const responseTime = Date.now() - startTime;
const data = await response.json();
if (response.status === endpoint.expectedStatus) {
console.log(`β
Status: ${response.status} (${responseTime}ms)`);
// Log key information based on endpoint
if (endpoint.url.includes('/verification')) {
console.log(`π Service Status: ${data.status}`);
console.log(`β±οΈ Uptime: ${data.uptime}`);
console.log(`π Total Runs: ${data.statistics?.totalRuns || 0}`);
console.log(`π― Success Rate: ${data.statistics?.successRate || 'N/A'}`);
} else if (endpoint.url.includes('/database')) {
console.log(`πΎ Database: ${data.database} (${data.connectivity})`);
console.log(`β‘ Query Time: ${data.queryTime}`);
console.log(`π Total Transactions: ${data.statistics?.totalTransactions || 0}`);
} else if (endpoint.url.includes('/system')) {
console.log(`π₯οΈ Overall Status: ${data.status}`);
console.log(`β±οΈ Uptime: ${data.uptime}`);
console.log(`πΎ Memory: ${data.components?.memory?.heapUsed || 'N/A'}`);
} else if (endpoint.url.includes('/transactions')) {
console.log(`π Total Transactions: ${data.totals?.all || 0}`);
console.log(`β³ Pending: ${data.totals?.pending || 0}`);
console.log(`β
Completed: ${data.totals?.completed || 0}`);
console.log(`π Completion Rate: ${data.percentages?.completionRate || 'N/A'}`);
} else {
console.log(`π Message: ${data.message}`);
console.log(`π’ Version: ${data.version}`);
}
} else {
console.log(`β Status: ${response.status} (Expected: ${endpoint.expectedStatus})`);
console.log(`π¬ Error: ${data.error || data.message}`);
}
} catch (error) {
console.log(`π₯ Request failed: ${error.message}`);
}
console.log(''); // Empty line for readability
}
}
/**
* Test verification service statistics and monitoring
*/
async function testVerificationMonitoring() {
console.log('π Testing Verification Service Monitoring...\n');
try {
const response = await fetch(`${BASE_URL}/health/verification`);
const data = await response.json();
if (response.ok) {
console.log('β
Verification Service Monitoring Data:');
console.log('=====================================');
console.log(`Service Status: ${data.status}`);
console.log(`Is Running: ${data.isRunning}`);
console.log(`Uptime: ${data.uptime}`);
console.log(`Last Run: ${data.lastRun || 'Never'}`);
console.log(`Last Run Duration: ${data.lastRunDuration || 0}ms`);
console.log('');
console.log('π Statistics:');
console.log(` Total Runs: ${data.statistics?.totalRuns || 0}`);
console.log(` Transactions Processed: ${data.statistics?.totalTransactionsProcessed || 0}`);
console.log(` Total Errors: ${data.statistics?.totalErrors || 0}`);
console.log(` Error Rate: ${data.statistics?.errorRate || '0%'}`);
console.log(` Success Rate: ${data.statistics?.successRate || '100%'}`);
console.log('');
console.log('β‘ Performance:');
console.log(` Memory Usage: ${data.performance?.memoryUsage || 'N/A'}`);
console.log(` Avg Processing Time: ${data.performance?.avgProcessingTime || 0}ms`);
console.log('');
console.log('βοΈ Configuration:');
console.log(` Batch Size: ${data.configuration?.batchSize || 'N/A'}`);
console.log(` Cron Interval: ${data.configuration?.cronInterval || 'N/A'}`);
console.log(` API Delay: ${data.configuration?.apiDelay || 'N/A'}`);
console.log(` Max Retries: ${data.configuration?.maxRetries || 'N/A'}`);
} else {
console.log(`β Failed to get monitoring data: ${data.error || data.message}`);
}
} catch (error) {
console.log(`π₯ Monitoring test failed: ${error.message}`);
}
console.log('');
}
/**
* Test system performance and resource usage
*/
async function testSystemPerformance() {
console.log('π₯οΈ Testing System Performance...\n');
const performanceTests = [
{
name: 'Database Query Performance',
test: async () => {
const startTime = Date.now();
const response = await fetch(`${BASE_URL}/health/database`);
const data = await response.json();
const totalTime = Date.now() - startTime;
return {
success: response.ok,
queryTime: data.queryTime,
totalTime: totalTime + 'ms',
transactions: data.statistics?.totalTransactions || 0
};
}
},
{
name: 'Memory Usage Check',
test: async () => {
const response = await fetch(`${BASE_URL}/health/system`);
const data = await response.json();
const memoryStatus = data.components?.memory?.status;
const heapUsed = data.components?.memory?.heapUsed;
return {
success: response.ok && memoryStatus === 'healthy',
status: memoryStatus,
heapUsed: heapUsed,
healthy: memoryStatus === 'healthy'
};
}
},
{
name: 'Service Response Time',
test: async () => {
const tests = [];
for (let i = 0; i < 5; i++) {
const startTime = Date.now();
const response = await fetch(`${BASE_URL}/health`);
const responseTime = Date.now() - startTime;
tests.push(responseTime);
}
const avgResponseTime = tests.reduce((a, b) => a + b, 0) / tests.length;
const maxResponseTime = Math.max(...tests);
const minResponseTime = Math.min(...tests);
return {
success: avgResponseTime < 1000, // Less than 1 second average
avgResponseTime: Math.round(avgResponseTime) + 'ms',
maxResponseTime: maxResponseTime + 'ms',
minResponseTime: minResponseTime + 'ms',
tests: tests.length
};
}
}
];
for (const perfTest of performanceTests) {
console.log(`β‘ Testing: ${perfTest.name}`);
try {
const result = await perfTest.test();
if (result.success) {
console.log(`β
Test passed`);
} else {
console.log(`β οΈ Test completed with warnings`);
}
// Log specific results
Object.keys(result).forEach(key => {
if (key !== 'success') {
console.log(` ${key}: ${result[key]}`);
}
});
} catch (error) {
console.log(`β Test failed: ${error.message}`);
}
console.log('');
}
}
/**
* Test error handling and edge cases
*/
async function testErrorHandling() {
console.log('π¨ Testing Error Handling...\n');
const errorTests = [
{
name: 'Invalid Health Endpoint',
url: `${BASE_URL}/health/invalid`,
expectedStatus: 404
},
{
name: 'Malformed Request',
url: `${BASE_URL}/health/verification?invalid=param`,
expectedStatus: 200 // Should still work with extra params
}
];
for (const errorTest of errorTests) {
console.log(`π§ͺ Testing: ${errorTest.name}`);
console.log(`π URL: ${errorTest.url}`);
try {
const response = await fetch(errorTest.url);
const data = await response.json();
if (response.status === errorTest.expectedStatus) {
console.log(`β
Expected status: ${response.status}`);
} else {
console.log(`β οΈ Unexpected status: ${response.status} (Expected: ${errorTest.expectedStatus})`);
}
if (data.error) {
console.log(`π Error message: ${data.error}`);
}
} catch (error) {
console.log(`π₯ Request failed: ${error.message}`);
}
console.log('');
}
}
/**
* Generate load test for verification system
*/
async function testLoadHandling() {
console.log('π₯ Testing Load Handling...\n');
const concurrentRequests = 10;
const requests = [];
console.log(`π Sending ${concurrentRequests} concurrent requests to health endpoint...`);
const startTime = Date.now();
for (let i = 0; i < concurrentRequests; i++) {
requests.push(
fetch(`${BASE_URL}/health/verification`)
.then(response => ({
status: response.status,
ok: response.ok,
time: Date.now() - startTime
}))
.catch(error => ({
status: 'error',
ok: false,
error: error.message,
time: Date.now() - startTime
}))
);
}
try {
const results = await Promise.all(requests);
const totalTime = Date.now() - startTime;
const successful = results.filter(r => r.ok).length;
const failed = results.filter(r => !r.ok).length;
const avgTime = results.reduce((sum, r) => sum + (r.time || 0), 0) / results.length;
console.log(`π Load Test Results:`);
console.log(` Total Requests: ${concurrentRequests}`);
console.log(` Successful: ${successful}`);
console.log(` Failed: ${failed}`);
console.log(` Success Rate: ${Math.round((successful / concurrentRequests) * 100)}%`);
console.log(` Total Time: ${totalTime}ms`);
console.log(` Average Response Time: ${Math.round(avgTime)}ms`);
if (successful === concurrentRequests) {
console.log(`β
Load test passed - all requests successful`);
} else {
console.log(`β οΈ Load test completed with ${failed} failures`);
}
} catch (error) {
console.log(`β Load test failed: ${error.message}`);
}
console.log('');
}
/**
* Main test runner
*/
async function runAllTests() {
console.log('π§ͺ Enhanced Background Verification System Test Suite v2.0.0');
console.log('================================================================\n');
const startTime = Date.now();
try {
await testHealthEndpoints();
await testVerificationMonitoring();
await testSystemPerformance();
await testErrorHandling();
await testLoadHandling();
const totalTime = Date.now() - startTime;
console.log('π Test Suite Completed Successfully!');
console.log(`β±οΈ Total execution time: ${totalTime}ms`);
console.log(`π
Completed at: ${new Date().toISOString()}`);
} catch (error) {
console.error('π₯ Test suite failed:', error);
}
}
// Export functions for individual testing
module.exports = {
testHealthEndpoints,
testVerificationMonitoring,
testSystemPerformance,
testErrorHandling,
testLoadHandling,
runAllTests
};
// Run all tests if this script is executed directly
if (require.main === module) {
runAllTests().catch(console.error);
}