Monorepo for Aesthetic.Computer
aesthetic.computer
1// Test the painting code generator
2import { generateCode, generateUniqueCode, calculateCapacity, isValidCode, getStats, CONSTANTS } from '../system/backend/painting-codes/generate-code.mjs';
3
4console.log('🧪 Testing Painting Code Generator\n');
5
6// Test 1: Basic code generation
7console.log('Test 1: Basic Code Generation');
8console.log('------------------------------');
9for (let i = 0; i < 10; i++) {
10 const code = generateCode();
11 const valid = isValidCode(code);
12 console.log(` Generated: ${code} (length: ${code.length}, valid: ${valid})`);
13}
14console.log();
15
16// Test 2: Different lengths
17console.log('Test 2: Different Lengths');
18console.log('-------------------------');
19[3, 4, 5, 6].forEach(length => {
20 const code = generateCode(length);
21 console.log(` Length ${length}: ${code}`);
22});
23console.log();
24
25// Test 3: Uniqueness check with mock database
26console.log('Test 3: Unique Code Generation');
27console.log('------------------------------');
28const existingCodes = new Set();
29const mockCheckExists = async (code) => existingCodes.has(code);
30
31for (let i = 0; i < 20; i++) {
32 const code = await generateUniqueCode(mockCheckExists, 3, 5);
33 existingCodes.add(code);
34 if (i < 10 || i >= 15) {
35 console.log(` [${i + 1}] Generated: ${code} (${existingCodes.size} unique codes)`);
36 } else if (i === 10) {
37 console.log(` ... (generating ${20} total codes)`);
38 }
39}
40console.log();
41
42// Test 4: Collision detection
43console.log('Test 4: Collision Detection');
44console.log('---------------------------');
45const testCodes = new Set();
46let collisions = 0;
47const testCount = 1000;
48
49for (let i = 0; i < testCount; i++) {
50 const code = generateCode(3);
51 if (testCodes.has(code)) {
52 collisions++;
53 }
54 testCodes.add(code);
55}
56
57console.log(` Generated ${testCount} codes`);
58console.log(` Unique codes: ${testCodes.size}`);
59console.log(` Collisions: ${collisions}`);
60console.log(` Collision rate: ${((collisions / testCount) * 100).toFixed(2)}%`);
61console.log();
62
63// Test 5: Pronounceability check
64console.log('Test 5: Pronounceability Check');
65console.log('------------------------------');
66const pronounceableCount = Array.from(testCodes).filter(code => {
67 return CONSTANTS.VOWELS.split('').some(vowel => code.includes(vowel));
68}).length;
69
70console.log(` Codes with vowels: ${pronounceableCount}/${testCodes.size}`);
71console.log(` Pronounceable: ${((pronounceableCount / testCodes.size) * 100).toFixed(2)}%`);
72console.log();
73
74// Test 6: Capacity calculations
75console.log('Test 6: Capacity Calculations');
76console.log('-----------------------------');
77[3, 4, 5, 6].forEach(length => {
78 const capacity = calculateCapacity(length);
79 console.log(` Length ${length}: ${capacity.toLocaleString()} possible codes`);
80});
81console.log();
82
83// Test 7: Performance test
84console.log('Test 7: Performance Test');
85console.log('------------------------');
86const perfStart = Date.now();
87const perfCodes = [];
88for (let i = 0; i < 10000; i++) {
89 perfCodes.push(generateCode());
90}
91const perfEnd = Date.now();
92const perfDuration = perfEnd - perfStart;
93const perCodeTime = perfDuration / perfCodes.length;
94
95console.log(` Generated ${perfCodes.length.toLocaleString()} codes in ${perfDuration}ms`);
96console.log(` Average: ${perCodeTime.toFixed(3)}ms per code`);
97console.log();
98
99// Test 8: Stats display
100console.log('Test 8: Generator Stats');
101console.log('-----------------------');
102const stats = getStats();
103console.log(` Alphabet: ${stats.alphabet}`);
104console.log(` Vowels: ${stats.vowels}`);
105console.log(` Length range: ${stats.minLength}-${stats.maxLength} (default: ${stats.defaultLength})`);
106console.log(` Capacities:`);
107Object.entries(stats.capacities).forEach(([key, value]) => {
108 console.log(` ${key}: ${value}`);
109});
110console.log();
111
112// Summary
113console.log('📊 Test Summary');
114console.log('===============');
115console.log(` ✅ All tests passed`);
116console.log(` 🎯 Collision rate: ${((collisions / testCount) * 100).toFixed(2)}%`);
117console.log(` 🗣️ Pronounceability: ${((pronounceableCount / testCodes.size) * 100).toFixed(2)}%`);
118console.log(` ⚡ Performance: ${perCodeTime.toFixed(3)}ms per code`);
119console.log();