-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-cache-endpoints.js
More file actions
354 lines (293 loc) · 13.6 KB
/
test-cache-endpoints.js
File metadata and controls
354 lines (293 loc) · 13.6 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
/**
* Cache Endpoints Test Script
*
* This script tests all cached endpoints to verify CACHE HIT/MISS behavior.
* It requires the server to be running on localhost:4000.
*
* Usage: node test-cache-endpoints.js
*
* Prerequisites:
* - Server must be running (npm run dev)
* - Database must have at least one device
*/
const BASE_URL = process.env.TEST_URL || 'http://localhost:4000';
// Helper function to make HTTP requests
async function fetchJSON(url) {
const response = await fetch(url);
const data = await response.json();
return { status: response.status, data };
}
// Helper to wait for specified milliseconds
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Color codes for terminal output
const colors = {
green: '\x1b[32m',
red: '\x1b[31m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
reset: '\x1b[0m',
bold: '\x1b[1m'
};
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
function logTestResult(testName, passed, details = '') {
const status = passed ? `${colors.green}✓ PASS${colors.reset}` : `${colors.red}✗ FAIL${colors.reset}`;
console.log(` ${status} - ${testName}${details ? ` (${details})` : ''}`);
}
// Test results tracking
const testResults = {
passed: 0,
failed: 0,
skipped: 0,
tests: []
};
function recordTest(name, passed, details = '') {
testResults.tests.push({ name, passed, details });
if (passed) testResults.passed++;
else testResults.failed++;
logTestResult(name, passed, details);
}
// Main test function
async function runTests() {
log('\n========================================', 'bold');
log(' CACHE ENDPOINTS TEST SUITE', 'bold');
log('========================================\n', 'bold');
// Check if server is running
try {
await fetchJSON(`${BASE_URL}/api/health`);
log('Server is running on port 4000\n', 'green');
} catch (error) {
log('ERROR: Server is not running. Please start the server first with: npm run dev\n', 'red');
process.exit(1);
}
// Get a sample device IP for testing device-specific endpoints
let sampleDeviceIP = null;
try {
const { data } = await fetchJSON(`${BASE_URL}/api/devices`);
if (data.devices && data.devices.length > 0) {
sampleDeviceIP = data.devices[0].ip || data.devices[0].ip_address;
log(`Using sample device IP: ${sampleDeviceIP}\n`, 'blue');
}
} catch (error) {
log('WARNING: Could not fetch devices list. Device-specific tests will be skipped.\n', 'yellow');
}
// ========================================
// Test 1: Cache Stats Endpoint
// ========================================
log('--- Test 1: Cache Stats Endpoint ---', 'yellow');
try {
const { status, data } = await fetchJSON(`${BASE_URL}/api/cache/stats`);
const hasMapCache = data.mapCache !== undefined;
const hasDeviceCache = data.deviceCache !== undefined;
const hasTimestamp = data.timestamp !== undefined;
recordTest('Cache stats returns mapCache', hasMapCache);
recordTest('Cache stats returns deviceCache', hasDeviceCache);
recordTest('Cache stats returns timestamp', hasTimestamp);
if (hasMapCache) {
recordTest('mapCache has size property', data.mapCache.size !== undefined);
recordTest('mapCache has ttlMs property', data.mapCache.ttlMs !== undefined);
}
if (hasDeviceCache) {
recordTest('deviceCache has size property', data.deviceCache.size !== undefined);
recordTest('deviceCache has ttlMs property', data.deviceCache.ttlMs !== undefined);
}
log(` Initial cache stats: mapCache.size=${data.mapCache?.size}, deviceCache.size=${data.deviceCache?.size}`, 'blue');
} catch (error) {
recordTest('Cache stats endpoint accessible', false, error.message);
}
console.log('');
// ========================================
// Test 2: GET /api/devices - Cache HIT/MISS
// ========================================
log('--- Test 2: GET /api/devices Caching ---', 'yellow');
try {
// Get initial cache stats
const { data: statsBefore } = await fetchJSON(`${BASE_URL}/api/cache/stats`);
const sizeBefore = statsBefore.deviceCache?.size || 0;
// First request - should be CACHE MISS
const { status: status1, data: data1 } = await fetchJSON(`${BASE_URL}/api/devices`);
recordTest('/api/devices - First request succeeds', status1 === 200);
// Check cache size increased
const { data: statsAfter1 } = await fetchJSON(`${BASE_URL}/api/cache/stats`);
const sizeAfter1 = statsAfter1.deviceCache?.size || 0;
recordTest('/api/devices - Cache populated after first request', sizeAfter1 > sizeBefore);
// Second request - should be CACHE HIT
const { status: status2, data: data2 } = await fetchJSON(`${BASE_URL}/api/devices`);
recordTest('/api/devices - Second request succeeds', status2 === 200);
recordTest('/api/devices - Same data returned on cache hit', JSON.stringify(data1) === JSON.stringify(data2));
} catch (error) {
recordTest('/api/devices endpoint test', false, error.message);
}
console.log('');
// ========================================
// Test 3: Device-specific endpoints (if sample device available)
// ========================================
if (sampleDeviceIP) {
// Test GET /api/devices/:ip
log('--- Test 3: GET /api/devices/:ip Caching ---', 'yellow');
try {
const { data: statsBefore } = await fetchJSON(`${BASE_URL}/api/cache/stats`);
const sizeBefore = statsBefore.deviceCache?.size || 0;
const { status: status1, data: data1 } = await fetchJSON(`${BASE_URL}/api/devices/${sampleDeviceIP}`);
if (status1 === 200) {
recordTest('/api/devices/:ip - First request succeeds', true);
const { data: statsAfter } = await fetchJSON(`${BASE_URL}/api/cache/stats`);
const sizeAfter = statsAfter.deviceCache?.size || 0;
recordTest('/api/devices/:ip - Cache entry created', sizeAfter > sizeBefore);
const { status: status2, data: data2 } = await fetchJSON(`${BASE_URL}/api/devices/${sampleDeviceIP}`);
recordTest('/api/devices/:ip - Second request succeeds', status2 === 200);
recordTest('/api/devices/:ip - Same data on cache hit', JSON.stringify(data1) === JSON.stringify(data2));
} else if (status1 === 404) {
log(' Device not found (404) - test skipped', 'yellow');
testResults.skipped++;
}
} catch (error) {
recordTest('/api/devices/:ip endpoint test', false, error.message);
}
console.log('');
// Test GET /api/devices/:ip/summary
log('--- Test 4: GET /api/devices/:ip/summary Caching ---', 'yellow');
try {
const { status: status1, data: data1 } = await fetchJSON(`${BASE_URL}/api/devices/${sampleDeviceIP}/summary`);
if (status1 === 200) {
recordTest('/api/devices/:ip/summary - First request succeeds', true);
const { status: status2, data: data2 } = await fetchJSON(`${BASE_URL}/api/devices/${sampleDeviceIP}/summary`);
recordTest('/api/devices/:ip/summary - Second request succeeds', status2 === 200);
recordTest('/api/devices/:ip/summary - Same data on cache hit', JSON.stringify(data1) === JSON.stringify(data2));
} else if (status1 === 404) {
log(' Device not found (404) - test skipped', 'yellow');
testResults.skipped++;
}
} catch (error) {
recordTest('/api/devices/:ip/summary endpoint test', false, error.message);
}
console.log('');
// Test GET /api/devices/:ip/port-stats
log('--- Test 5: GET /api/devices/:ip/port-stats Caching ---', 'yellow');
try {
const { status: status1, data: data1 } = await fetchJSON(`${BASE_URL}/api/devices/${sampleDeviceIP}/port-stats`);
if (status1 === 200) {
recordTest('/api/devices/:ip/port-stats - First request succeeds', true);
const { status: status2, data: data2 } = await fetchJSON(`${BASE_URL}/api/devices/${sampleDeviceIP}/port-stats`);
recordTest('/api/devices/:ip/port-stats - Second request succeeds', status2 === 200);
recordTest('/api/devices/:ip/port-stats - Same data on cache hit', JSON.stringify(data1) === JSON.stringify(data2));
} else if (status1 === 404) {
log(' Device not found (404) - test skipped', 'yellow');
testResults.skipped++;
}
} catch (error) {
recordTest('/api/devices/:ip/port-stats endpoint test', false, error.message);
}
console.log('');
// Test GET /api/devices/:ip/addresses
log('--- Test 6: GET /api/devices/:ip/addresses Caching ---', 'yellow');
try {
const { status: status1, data: data1 } = await fetchJSON(`${BASE_URL}/api/devices/${sampleDeviceIP}/addresses`);
if (status1 === 200) {
recordTest('/api/devices/:ip/addresses - First request succeeds', true);
const { status: status2, data: data2 } = await fetchJSON(`${BASE_URL}/api/devices/${sampleDeviceIP}/addresses`);
recordTest('/api/devices/:ip/addresses - Second request succeeds', status2 === 200);
recordTest('/api/devices/:ip/addresses - Same data on cache hit', JSON.stringify(data1) === JSON.stringify(data2));
} else if (status1 === 404) {
log(' Device not found (404) - test skipped', 'yellow');
testResults.skipped++;
}
} catch (error) {
recordTest('/api/devices/:ip/addresses endpoint test', false, error.message);
}
console.log('');
// Test GET /api/devices/:ip/vlans
log('--- Test 7: GET /api/devices/:ip/vlans Caching ---', 'yellow');
try {
const { status: status1, data: data1 } = await fetchJSON(`${BASE_URL}/api/devices/${sampleDeviceIP}/vlans`);
if (status1 === 200) {
recordTest('/api/devices/:ip/vlans - First request succeeds', true);
const { status: status2, data: data2 } = await fetchJSON(`${BASE_URL}/api/devices/${sampleDeviceIP}/vlans`);
recordTest('/api/devices/:ip/vlans - Second request succeeds', status2 === 200);
recordTest('/api/devices/:ip/vlans - Same data on cache hit', JSON.stringify(data1) === JSON.stringify(data2));
} else if (status1 === 404) {
log(' Device not found (404) - test skipped', 'yellow');
testResults.skipped++;
}
} catch (error) {
recordTest('/api/devices/:ip/vlans endpoint test', false, error.message);
}
console.log('');
// Test GET /api/devices/:ip/lags
log('--- Test 8: GET /api/devices/:ip/lags Caching ---', 'yellow');
try {
const { status: status1, data: data1 } = await fetchJSON(`${BASE_URL}/api/devices/${sampleDeviceIP}/lags`);
if (status1 === 200) {
recordTest('/api/devices/:ip/lags - First request succeeds', true);
const { status: status2, data: data2 } = await fetchJSON(`${BASE_URL}/api/devices/${sampleDeviceIP}/lags`);
recordTest('/api/devices/:ip/lags - Second request succeeds', status2 === 200);
recordTest('/api/devices/:ip/lags - Same data on cache hit', JSON.stringify(data1) === JSON.stringify(data2));
} else if (status1 === 404) {
log(' Device not found (404) - test skipped', 'yellow');
testResults.skipped++;
}
} catch (error) {
recordTest('/api/devices/:ip/lags endpoint test', false, error.message);
}
console.log('');
} else {
log('--- Device-specific endpoint tests skipped (no devices in DB) ---', 'yellow');
testResults.skipped += 6; // 6 device-specific endpoint groups
}
// ========================================
// Test 9: 404 responses are NOT cached
// ========================================
log('--- Test 9: 404 Responses Not Cached ---', 'yellow');
try {
const fakeIP = '192.0.2.1'; // Reserved IP for documentation, won't exist
// Get cache size before
const { data: statsBefore } = await fetchJSON(`${BASE_URL}/api/cache/stats`);
const sizeBefore = statsBefore.deviceCache?.size || 0;
// Request non-existent device
const { status } = await fetchJSON(`${BASE_URL}/api/devices/${fakeIP}`);
recordTest('Non-existent device returns 404', status === 404);
// Check cache size didn't increase for 404
const { data: statsAfter } = await fetchJSON(`${BASE_URL}/api/cache/stats`);
const sizeAfter = statsAfter.deviceCache?.size || 0;
recordTest('404 response not cached (size unchanged)', sizeAfter === sizeBefore);
} catch (error) {
recordTest('404 not cached test', false, error.message);
}
console.log('');
// ========================================
// Test 10: Final Cache Stats Summary
// ========================================
log('--- Test 10: Final Cache Stats ---', 'yellow');
try {
const { data } = await fetchJSON(`${BASE_URL}/api/cache/stats`);
log(` mapCache: size=${data.mapCache?.size}, ttlMs=${data.mapCache?.ttlMs}`, 'blue');
log(` deviceCache: size=${data.deviceCache?.size}, ttlMs=${data.deviceCache?.ttlMs}`, 'blue');
recordTest('Final cache stats accessible', true);
} catch (error) {
recordTest('Final cache stats', false, error.message);
}
// ========================================
// Summary
// ========================================
console.log('\n========================================');
log(' TEST SUMMARY', 'bold');
console.log('========================================');
log(` Passed: ${testResults.passed}`, 'green');
if (testResults.failed > 0) {
log(` Failed: ${testResults.failed}`, 'red');
}
if (testResults.skipped > 0) {
log(` Skipped: ${testResults.skipped}`, 'yellow');
}
console.log('========================================\n');
// Exit with appropriate code
process.exit(testResults.failed > 0 ? 1 : 0);
}
// Run tests
runTests().catch(error => {
console.error('Test suite error:', error);
process.exit(1);
});