-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-javascript-generator.ts
More file actions
276 lines (243 loc) · 8.59 KB
/
test-javascript-generator.ts
File metadata and controls
276 lines (243 loc) · 8.59 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
/**
* Test Script for New JavaScript SDK Generator
* Tests the modular TypeScript JavaScript generator
*/
import { JavaScriptGenerator } from './src/generators/languages/javascript/javascript.generator';
import type { SDKInput, Algorithm } from './src/generators/types';
import * as fs from 'fs';
import * as path from 'path';
/**
* Test the JavaScript SDK generator
*/
async function testJavaScriptGenerator() {
console.log('🧪 Testing New JavaScript SDK Generator');
console.log('='.repeat(80));
// Create test SDK input
const testInput: SDKInput = {
sdkId: 'test-sdk-' + Date.now(),
name: 'TestJavaScriptSDK',
version: '2.0.0',
languages: ['javascript'],
algorithms: ['aes-256-gcm', 'chacha20-poly1305'],
tenantId: 'test-tenant-123',
environment: 'test',
telemetry: {
enabled: true,
endpoint: 'http://localhost:3000/api/telemetry',
},
vault: {
enabled: true,
kekName: 'kek-test-tenant-123',
apiEndpoint: 'http://localhost:3000/api/vault',
kekAlgorithm: 'aes256-gcm96',
},
envelopeEncryptionEnabled: true,
};
const testAlgorithms: readonly Algorithm[] = ['aes-256-gcm', 'chacha20-poly1305'];
console.log('\n📋 Test Configuration:');
console.log(` SDK ID: ${testInput.sdkId}`);
console.log(` SDK Name: ${testInput.name}`);
console.log(` Version: ${testInput.version}`);
console.log(` Algorithms: ${testAlgorithms.join(', ')}`);
console.log(` Tenant: ${testInput.tenantId}`);
try {
// Create generator instance
console.log('\n🔧 Creating JavaScriptGenerator instance...');
const generator = new JavaScriptGenerator();
// Validate
console.log('✅ Validating input...');
generator.validate(testInput, testAlgorithms);
console.log('✅ Validation passed');
// Generate SDK
console.log('\n🏗️ Generating SDK files...');
const startTime = Date.now();
const files = generator.generate(testInput, testAlgorithms);
const generationTime = Date.now() - startTime;
console.log(`✅ Generation completed in ${generationTime}ms`);
console.log(`📦 Generated ${files.size} files`);
// List generated files
console.log('\n📁 Generated Files:');
const fileList = Array.from(files.keys()).sort();
for (const filePath of fileList) {
const content = files.get(filePath);
const size = content ? Buffer.byteLength(content, 'utf8') : 0;
console.log(` ${filePath} (${(size / 1024).toFixed(2)} KB)`);
}
// Verify critical files exist
console.log('\n🔍 Verifying critical files...');
const criticalFiles = [
'src/index.ts',
'src/core.ts',
'src/metadata.ts',
'src/index.d.ts',
'package.json',
'tsconfig.json',
'jest.config.js',
'README.md',
'SECURITY.md',
'API.md',
'LICENSE',
];
let allFilesPresent = true;
for (const file of criticalFiles) {
if (files.has(file)) {
console.log(` ✅ ${file}`);
} else {
console.log(` ❌ ${file} - MISSING`);
allFilesPresent = false;
}
}
// Verify test files
console.log('\n🧪 Verifying test files...');
const testFiles = [
'test/nist.test.ts',
'test/audit.test.ts',
'test/wycheproof.test.ts',
'test/basic.test.ts',
'test/edge-cases.test.ts',
'test/error-handling.test.ts',
'test/memory-safety.test.ts',
'test/comprehensive.test.ts',
];
for (const file of testFiles) {
if (files.has(file)) {
console.log(` ✅ ${file}`);
} else {
console.log(` ❌ ${file} - MISSING`);
allFilesPresent = false;
}
}
// Verify documentation files
console.log('\n📚 Verifying documentation files...');
const docFiles = [
'README.md',
'SECURITY.md',
'API.md',
'CHANGELOG.md',
'INSTALLATION.md',
'TROUBLESHOOTING.md',
'THREAT-MODEL.md',
];
for (const file of docFiles) {
if (files.has(file)) {
console.log(` ✅ ${file}`);
} else {
console.log(` ❌ ${file} - MISSING`);
allFilesPresent = false;
}
}
// Verify file content quality
console.log('\n🔎 Verifying file content quality...');
let contentIssues = 0;
// Check package.json
const packageJson = files.get('package.json');
if (packageJson) {
try {
const pkg = JSON.parse(packageJson);
if (pkg.name && pkg.version) {
console.log(` ✅ package.json is valid JSON`);
} else {
console.log(` ⚠️ package.json missing required fields`);
contentIssues++;
}
} catch (e) {
console.log(` ❌ package.json is invalid JSON: ${e}`);
contentIssues++;
}
}
// Check tsconfig.json
const tsconfig = files.get('tsconfig.json');
if (tsconfig) {
try {
const config = JSON.parse(tsconfig);
if (config.compilerOptions) {
console.log(` ✅ tsconfig.json is valid JSON`);
} else {
console.log(` ⚠️ tsconfig.json missing compilerOptions`);
contentIssues++;
}
} catch (e) {
console.log(` ❌ tsconfig.json is invalid JSON: ${e}`);
contentIssues++;
}
}
// Check core.ts contains expected exports
const coreTs = files.get('src/core.ts');
if (coreTs) {
const hasAveroxCrypto = coreTs.includes('class AveroxCrypto');
const hasChaCha20 = coreTs.includes('class ChaCha20Poly1305');
const hasEncrypt = coreTs.includes('encrypt(');
const hasDecrypt = coreTs.includes('decrypt(');
console.log(` ${hasAveroxCrypto ? '✅' : '❌'} Core.ts contains AveroxCrypto class`);
console.log(` ${hasChaCha20 ? '✅' : '❌'} Core.ts contains ChaCha20Poly1305 class`);
console.log(` ${hasEncrypt ? '✅' : '❌'} Core.ts contains encrypt method`);
console.log(` ${hasDecrypt ? '✅' : '❌'} Core.ts contains decrypt method`);
if (!hasAveroxCrypto || !hasChaCha20 || !hasEncrypt || !hasDecrypt) {
contentIssues++;
}
}
// Check metadata.ts
const metadataTs = files.get('src/metadata.ts');
if (metadataTs) {
const hasGetSDKMetadata = metadataTs.includes('getSDKMetadata');
const hasSDKMetadata = metadataTs.includes('SDK_METADATA');
console.log(` ${hasGetSDKMetadata ? '✅' : '❌'} metadata.ts contains getSDKMetadata function`);
console.log(` ${hasSDKMetadata ? '✅' : '❌'} metadata.ts contains SDK_METADATA constant`);
if (!hasGetSDKMetadata || !hasSDKMetadata) {
contentIssues++;
}
}
// Optional: Write files to disk for manual inspection
const outputDir = path.join(process.cwd(), 'test-output', testInput.sdkId);
const writeToDisk = process.env.WRITE_TEST_OUTPUT === 'true';
if (writeToDisk) {
console.log(`\n💾 Writing files to disk: ${outputDir}`);
fs.mkdirSync(outputDir, { recursive: true });
for (const [filePath, content] of files.entries()) {
const fullPath = path.join(outputDir, filePath);
const dir = path.dirname(fullPath);
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(fullPath, content, 'utf8');
}
console.log(`✅ Files written to ${outputDir}`);
console.log(`\n💡 To inspect the generated SDK:`);
console.log(` cd ${outputDir}`);
console.log(` npm install`);
console.log(` npm test`);
}
// Summary
console.log('\n' + '='.repeat(80));
console.log('📊 Test Summary:');
console.log(` ✅ Files Generated: ${files.size}`);
console.log(` ✅ Generation Time: ${generationTime}ms`);
console.log(` ${allFilesPresent ? '✅' : '❌'} All Critical Files Present: ${allFilesPresent}`);
console.log(` ${contentIssues === 0 ? '✅' : '⚠️ '} Content Quality Issues: ${contentIssues}`);
if (allFilesPresent && contentIssues === 0) {
console.log('\n🎉 SUCCESS! JavaScript SDK Generator is working correctly!');
console.log('\n✅ All tests passed!');
return 0;
} else {
console.log('\n⚠️ Some issues detected. Please review the output above.');
return 1;
}
} catch (error: any) {
console.error('\n❌ Test Failed:');
console.error(` Error: ${error.message}`);
if (error.stack) {
console.error(` Stack: ${error.stack}`);
}
return 1;
}
}
// Run the test
if (require.main === module) {
testJavaScriptGenerator()
.then((exitCode) => {
process.exit(exitCode);
})
.catch((error) => {
console.error('Fatal error:', error);
process.exit(1);
});
}
export { testJavaScriptGenerator };