-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathverify-implementation.js
More file actions
359 lines (298 loc) · 11 KB
/
verify-implementation.js
File metadata and controls
359 lines (298 loc) · 11 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
#!/usr/bin/env node
/**
* @title Implementation Verification Script
* @description Verifies all requirements for Redis caching implementation are met
*/
const fs = require('fs');
const path = require('path');
const serverDir = path.join(__dirname, 'server');
// Color codes for terminal output
const colors = {
reset: '\x1b[0m',
green: '\x1b[32m',
red: '\x1b[31m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
};
const log = {
success: (msg) => console.log(`${colors.green}✅ ${msg}${colors.reset}`),
error: (msg) => console.log(`${colors.red}❌ ${msg}${colors.reset}`),
warn: (msg) => console.log(`${colors.yellow}⚠️ ${msg}${colors.reset}`),
info: (msg) => console.log(`${colors.blue}ℹ️ ${msg}${colors.reset}`),
section: (msg) => console.log(`\n${colors.blue}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${colors.reset}\n${msg}\n${colors.blue}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${colors.reset}\n`),
};
let totalChecks = 0;
let passedChecks = 0;
function checkFile(filePath, description) {
totalChecks++;
const fullPath = path.join(serverDir, filePath);
if (fs.existsSync(fullPath)) {
log.success(`File exists: ${filePath}`);
passedChecks++;
return true;
} else {
log.error(`File missing: ${filePath}`);
return false;
}
}
function checkFileContent(filePath, searchString, description) {
totalChecks++;
const fullPath = path.join(serverDir, filePath);
if (!fs.existsSync(fullPath)) {
log.error(`${description} - File not found: ${filePath}`);
return false;
}
const content = fs.readFileSync(fullPath, 'utf8');
if (content.includes(searchString)) {
log.success(description);
passedChecks++;
return true;
} else {
log.error(`${description} - Pattern not found in ${filePath}`);
return false;
}
}
function main() {
console.clear();
log.section('🔍 REDIS CACHING IMPLEMENTATION VERIFICATION');
// =====================================================
log.section('1️⃣ REQUIREMENT: Cache-Aside Pattern Implementation');
// =====================================================
checkFile(
'services/cache-service.js',
'Cache service module exists'
);
checkFileContent(
'services/cache-service.js',
'async getOrSet(key, fetchFunction, options = {})',
'Cache-aside pattern method exists'
);
checkFileContent(
'services/cache-service.js',
'const cachedData = await this.get(key);',
'Cache-aside checks cache first'
);
checkFileContent(
'services/cache-service.js',
'const data = await fetchFunction();',
'Cache-aside fetches from source on miss'
);
checkFileContent(
'services/cache-service.js',
'await this.set(key, data, ttl);',
'Cache-aside stores result with TTL'
);
checkFileContent(
'routes/token-routes.js',
'const cachedResult = await cacheService.get(cacheKey);',
'Token routes check cache before DB'
);
checkFileContent(
'routes/token-routes.js',
'await cacheService.set(cacheKey, result);',
'Token routes cache results'
);
// =====================================================
log.section('2️⃣ REQUIREMENT: Cache Invalidation on Metadata Update');
// =====================================================
checkFileContent(
'routes/token-routes.js',
'await cacheService.deleteByPattern(`tokens:owner:${ownerPublicKey}:*`);',
'Cache invalidation on token creation'
);
checkFileContent(
'services/cache-service.js',
'async deleteByPattern(pattern)',
'Pattern-based cache deletion method exists'
);
checkFileContent(
'services/cache-service.js',
'const keys = await this.client.keys(pat);',
'Pattern-based deletion uses Redis KEYS command'
);
// =====================================================
log.section('3️⃣ REQUIREMENT: TTL Configuration');
// =====================================================
checkFileContent(
'config/env-config.js',
'CACHE_TTL_METADATA',
'CACHE_TTL_METADATA environment variable defined'
);
checkFileContent(
'config/env-config.js',
'REDIS_URL',
'REDIS_URL environment variable defined'
);
checkFileContent(
'config/env-config.js',
'REDIS_PASSWORD',
'REDIS_PASSWORD environment variable defined'
);
checkFileContent(
'config/env-config.js',
'REDIS_DB',
'REDIS_DB environment variable defined'
);
checkFileContent(
'config/env-config.js',
'default: 3600',
'Default TTL is 3600 seconds (1 hour)'
);
// =====================================================
log.section('4️⃣ ADDITIONAL: Dependency Management');
// =====================================================
checkFileContent(
'package.json',
'"redis": "^4.7.0"',
'Redis dependency added to package.json'
);
// =====================================================
log.section('5️⃣ ADDITIONAL: Server Initialization');
// =====================================================
checkFileContent(
'index.js',
'const { getCacheService } = require("./services/cache-service");',
'Server imports cache service'
);
checkFileContent(
'index.js',
'await cacheService.initialize();',
'Server initializes cache on startup'
);
// =====================================================
log.section('6️⃣ ADDITIONAL: Error Handling & Graceful Degradation');
// =====================================================
checkFileContent(
'services/cache-service.js',
'this.isConnected = false;',
'Cache tracks connection state'
);
checkFileContent(
'services/cache-service.js',
'catch (error)',
'Cache operations handle errors'
);
checkFileContent(
'index.js',
'continuing without cache',
'Server continues gracefully without cache'
);
// =====================================================
log.section('7️⃣ ADDITIONAL: Health Checks');
// =====================================================
checkFileContent(
'services/cache-service.js',
'isHealthy()',
'Cache health check method exists'
);
checkFileContent(
'services/cache-service.js',
'async getHealth()',
'Cache detailed health method exists'
);
// =====================================================
log.section('8️⃣ ADDITIONAL: Logging');
// =====================================================
checkFileContent(
'services/cache-service.js',
'logger.info',
'Cache service logs INFO level'
);
checkFileContent(
'services/cache-service.js',
'logger.debug',
'Cache service logs DEBUG level'
);
checkFileContent(
'services/cache-service.js',
'logger.warn',
'Cache service logs WARN level'
);
checkFileContent(
'services/cache-service.js',
'logger.error',
'Cache service logs ERROR level'
);
// =====================================================
log.section('9️⃣ ADDITIONAL: Testing');
// =====================================================
checkFile(
'tests/services/cache-service.test.js',
'Cache service unit tests'
);
checkFile(
'tests/routes/token-routes-cache.test.js',
'Token routes integration tests'
);
checkFileContent(
'tests/services/cache-service.test.js',
'describe(\'CacheService\'',
'Cache service test suite exists'
);
checkFileContent(
'tests/routes/token-routes-cache.test.js',
'describe(\'Token Routes with Cache Integration\'',
'Integration test suite exists'
);
// =====================================================
log.section('🔟 ADDITIONAL: Documentation');
// =====================================================
// Check docs at root level, not server level
const docsDir = path.join(__dirname, 'docs');
totalChecks++;
if (fs.existsSync(path.join(docsDir, 'redis-caching.md'))) {
log.success(`File exists: docs/redis-caching.md`);
passedChecks++;
} else {
log.error(`File missing: docs/redis-caching.md`);
}
checkFile(
'.env.example.redis',
'Environment configuration example'
);
// Check documentation content from root docs directory
const redisDocPath = path.join(docsDir, 'redis-caching.md');
totalChecks++;
if (fs.existsSync(redisDocPath)) {
const docContent = fs.readFileSync(redisDocPath, 'utf8');
if (docContent.includes('Cache-Aside Pattern')) {
log.success('Documentation includes cache-aside pattern');
passedChecks++;
} else {
log.error('Documentation includes cache-aside pattern - Pattern not found');
}
} else {
log.error('Documentation includes cache-aside pattern - File not found: docs/redis-caching.md');
}
totalChecks++;
if (fs.existsSync(redisDocPath)) {
const docContent = fs.readFileSync(redisDocPath, 'utf8');
if (docContent.includes('Cache Invalidation')) {
log.success('Documentation includes cache invalidation');
passedChecks++;
} else {
log.error('Documentation includes cache invalidation - Pattern not found');
}
} else {
log.error('Documentation includes cache invalidation - File not found: docs/redis-caching.md');
}
// =====================================================
log.section('📊 VERIFICATION RESULTS');
// =====================================================
const percentage = Math.round((passedChecks / totalChecks) * 100);
console.log(`\nTotal Checks: ${totalChecks}`);
console.log(`Passed: ${colors.green}${passedChecks}${colors.reset}`);
console.log(`Failed: ${passedChecks === totalChecks ? colors.green + '0' : colors.red + (totalChecks - passedChecks)}${colors.reset}`);
console.log(`\nSuccess Rate: ${percentage}%\n`);
if (passedChecks === totalChecks) {
log.success('✨ ALL REQUIREMENTS MET! Implementation is complete. ✨');
console.log(`\n${colors.green}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${colors.reset}`);
console.log(`${colors.green}Ready to test: npm install && npm test${colors.reset}`);
console.log(`${colors.green}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${colors.reset}\n`);
process.exit(0);
} else {
log.error(`Some checks failed. Please review above.`);
process.exit(1);
}
}
main();