-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest-complete-flow.js
More file actions
309 lines (258 loc) · 10.7 KB
/
test-complete-flow.js
File metadata and controls
309 lines (258 loc) · 10.7 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
const fs = require('fs').promises;
const path = require('path');
const crypto = require('crypto');
// Test configuration
const TEST_USERNAME = 'testuser123';
const EXPECTED_HASH = crypto.createHash('sha256').update(TEST_USERNAME.toLowerCase()).digest('hex').substring(0, 12);
console.log('🧪 OpenReadme Complete Flow Test');
console.log('================================');
async function testUserMappingLogic() {
console.log('\n📋 Testing User Mapping Logic...');
try {
// Import the userMapping functions (handle TypeScript module)
const userMappingPath = require.resolve('./utils/userMapping.ts');
delete require.cache[userMappingPath];
// For TypeScript files, we need to use a different approach
const fs = require('fs').promises;
const path = require('path');
// Test the functions by reading the actual implementation
async function testGetHashedUsername(username) {
const data = await fs.readFile(path.join(__dirname, 'data/user-mapping.json'), 'utf-8');
const mapping = JSON.parse(data);
return mapping[username.toLowerCase()] || null;
}
async function testStoreUserMapping(username, hashed) {
let mapping = {};
try {
const data = await fs.readFile(path.join(__dirname, 'data/user-mapping.json'), 'utf-8');
mapping = JSON.parse(data);
} catch (e) {
// File doesn't exist yet
}
mapping[username.toLowerCase()] = hashed;
await fs.writeFile(path.join(__dirname, 'data/user-mapping.json'), JSON.stringify(mapping, null, 2), 'utf-8');
// Verify
const verification = await testGetHashedUsername(username);
if (verification !== hashed) {
throw new Error(`Failed to verify user mapping for ${username}`);
}
}
// Test 1: Check existing user
console.log('1. Testing existing user lookup...');
const existingHash = await testGetHashedUsername('ravixalgorithm');
if (existingHash === 'fad62070c0e0') {
console.log('✅ Existing user mapping works correctly');
} else {
console.log('❌ Existing user mapping failed:', existingHash);
}
// Test 2: Store new user mapping
console.log('2. Testing new user mapping storage...');
await testStoreUserMapping(TEST_USERNAME, EXPECTED_HASH);
// Test 3: Verify new user was stored
console.log('3. Verifying new user mapping...');
const newHash = await testGetHashedUsername(TEST_USERNAME);
if (newHash === EXPECTED_HASH) {
console.log('✅ New user mapping stored and retrieved correctly');
} else {
console.log('❌ New user mapping verification failed:', newHash);
}
// Clean up test user
console.log('4. Cleaning up test data...');
let mapping = {};
try {
const data = await fs.readFile(path.join(__dirname, 'data/user-mapping.json'), 'utf-8');
mapping = JSON.parse(data);
delete mapping[TEST_USERNAME.toLowerCase()];
await fs.writeFile(path.join(__dirname, 'data/user-mapping.json'), JSON.stringify(mapping, null, 2), 'utf-8');
console.log('✅ Test data cleaned up');
} catch (e) {
console.log('⚠️ Could not clean up test data');
}
return true;
} catch (error) {
console.log('❌ User mapping test failed:', error.message);
return false;
}
}
async function testHashingLogic() {
console.log('\n🔐 Testing Hashing Logic...');
// Test the hashing function from route.ts
function hashUsername(username) {
return crypto.createHash('sha256').update(username.toLowerCase()).digest('hex').substring(0, 12);
}
const testCases = [
{ input: 'ravixalgorithm', expected: 'fad62070c0e0' },
{ input: 'TestUser', expected: hashUsername('testuser') },
{ input: 'UPPERCASE', expected: hashUsername('uppercase') }
];
let passed = 0;
for (const testCase of testCases) {
const result = hashUsername(testCase.input);
if (result === testCase.expected) {
console.log(`✅ Hash test passed for "${testCase.input}"`);
passed++;
} else {
console.log(`❌ Hash test failed for "${testCase.input}": expected ${testCase.expected}, got ${result}`);
}
}
return passed === testCases.length;
}
async function testFileStructure() {
console.log('\n📁 Testing File Structure...');
const requiredFiles = [
'package.json',
'next.config.ts',
'app/api/openreadme/route.ts',
'app/api/lookup/route.ts',
'utils/userMapping.ts',
'data/user-mapping.json',
'stats/usage-log.json'
];
let allExist = true;
for (const file of requiredFiles) {
try {
await fs.access(path.join(__dirname, file));
console.log(`✅ ${file} exists`);
} catch (error) {
console.log(`❌ ${file} missing`);
allExist = false;
}
}
return allExist;
}
async function testDataIntegrity() {
console.log('\n🔍 Testing Data Integrity...');
try {
// Test user-mapping.json
const mappingData = await fs.readFile(path.join(__dirname, 'data/user-mapping.json'), 'utf-8');
const mapping = JSON.parse(mappingData);
if (typeof mapping === 'object' && mapping.ravixalgorithm === 'fad62070c0e0') {
console.log('✅ user-mapping.json has correct structure and data');
} else {
console.log('❌ user-mapping.json structure or data is incorrect');
return false;
}
// Test usage-log.json
const logData = await fs.readFile(path.join(__dirname, 'stats/usage-log.json'), 'utf-8');
const log = JSON.parse(logData);
if (Array.isArray(log)) {
console.log(`✅ usage-log.json has correct structure (${log.length} entries)`);
} else {
console.log('❌ usage-log.json should be an array');
return false;
}
return true;
} catch (error) {
console.log('❌ Data integrity test failed:', error.message);
return false;
}
}
async function testAPIStructure() {
console.log('\n🔌 Testing API Structure...');
try {
// Check if route.ts has required functions
const routeContent = await fs.readFile(path.join(__dirname, 'app/api/openreadme/route.ts'), 'utf-8');
const requiredFunctions = [
'hashUsername',
'generateSecureFileName',
'logUserGeneration',
'uploadToGitHubSafely',
'POST',
'GET'
];
let allFound = true;
for (const func of requiredFunctions) {
if (routeContent.includes(func)) {
console.log(`✅ Function ${func} found`);
} else {
console.log(`❌ Function ${func} missing`);
allFound = false;
}
}
// Check for our recent fixes
const fixes = [
'throw error', // Error re-throwing
'uploadToken', // Token consistency fix
'Successfully logged usage', // Improved logging
'Failed to create user mapping' // Better error messages
];
for (const fix of fixes) {
if (routeContent.includes(fix)) {
console.log(`✅ Fix implemented: ${fix}`);
} else {
console.log(`⚠️ Fix may be missing: ${fix}`);
}
}
return allFound;
} catch (error) {
console.log('❌ API structure test failed:', error.message);
return false;
}
}
async function testEnvironmentSetup() {
console.log('\n🌍 Testing Environment Setup...');
// Check if .env.example exists and has required variables
try {
const envExample = await fs.readFile(path.join(__dirname, '.env.example'), 'utf-8');
const requiredVars = [
'GITHUB_TOKEN',
'GITHUB_TOKEN_IMAGES',
'NODE_ENV'
];
let allFound = true;
for (const variable of requiredVars) {
if (envExample.includes(variable)) {
console.log(`✅ Environment variable ${variable} documented`);
} else {
console.log(`❌ Environment variable ${variable} not documented`);
allFound = false;
}
}
return allFound;
} catch (error) {
console.log('❌ Environment setup test failed:', error.message);
return false;
}
}
async function generateTestReport() {
console.log('\n📊 Running Complete Test Suite...');
console.log('==================================');
const tests = [
{ name: 'File Structure', test: testFileStructure },
{ name: 'Data Integrity', test: testDataIntegrity },
{ name: 'Hashing Logic', test: testHashingLogic },
{ name: 'User Mapping Logic', test: testUserMappingLogic },
{ name: 'API Structure', test: testAPIStructure },
{ name: 'Environment Setup', test: testEnvironmentSetup }
];
const results = [];
for (const { name, test } of tests) {
try {
const result = await test();
results.push({ name, passed: result });
} catch (error) {
console.log(`❌ ${name} test crashed:`, error.message);
results.push({ name, passed: false, error: error.message });
}
}
console.log('\n📋 Test Results Summary');
console.log('=======================');
let totalPassed = 0;
for (const result of results) {
const status = result.passed ? '✅ PASS' : '❌ FAIL';
console.log(`${status} - ${result.name}`);
if (result.error) {
console.log(` Error: ${result.error}`);
}
if (result.passed) totalPassed++;
}
console.log(`\n🎯 Overall: ${totalPassed}/${results.length} tests passed`);
if (totalPassed === results.length) {
console.log('🎉 All tests passed! Your OpenReadme application is ready for deployment.');
} else {
console.log('⚠️ Some tests failed. Please review the issues above before deployment.');
}
return totalPassed === results.length;
}
// Run the complete test suite
generateTestReport().catch(console.error);