Monorepo for Aesthetic.Computer aesthetic.computer
at main 113 lines 3.7 kB view raw
1// Simple test for KidLisp embed functionality 2// This is not a formal test suite, just a basic verification 3 4import { KidLisp, fetchCachedCode } from '../system/public/aesthetic.computer/lib/kidlisp.mjs'; 5 6// Mock API object for testing 7const mockApi = { 8 screen: { width: 256, height: 256, pixels: new Uint8ClampedArray(256 * 256 * 4) }, 9 wipe: (color) => console.log(`Mock wipe: ${color}`), 10 ink: (...args) => console.log(`Mock ink: ${args.join(', ')}`), 11 box: (...args) => console.log(`Mock box: ${args.join(', ')}`), 12 paste: (...args) => console.log(`Mock paste: ${args.join(', ')}`), 13 painting: (width, height, callback) => { 14 console.log(`Mock painting: ${width}x${height}`); 15 const buffer = { 16 width, 17 height, 18 pixels: new Uint8ClampedArray(width * height * 4) 19 }; 20 if (callback) callback(mockApi); 21 return buffer; 22 } 23}; 24 25// Test the embed command 26async function testEmbedCommand() { 27 console.log('Testing KidLisp embed command...'); 28 29 const kidlisp = new KidLisp(); 30 const globalEnv = kidlisp.getGlobalEnv(); 31 32 // Test that embed command exists 33 if (globalEnv.embed) { 34 console.log('✅ embed command found in global environment'); 35 36 // Test embed with invalid cache code 37 const result1 = globalEnv.embed(mockApi, ['invalid']); 38 console.log('embed with invalid code:', result1); 39 40 // Test embed with $-prefixed code format 41 const result2 = globalEnv.embed(mockApi, ['$abc123XY']); 42 console.log('embed with $-prefixed code:', result2); 43 44 } else { 45 console.log('❌ embed command not found in global environment'); 46 } 47} 48 49// Test the $-prefixed function resolution 50async function testDollarPrefixedFunctions() { 51 console.log('\nTesting $-prefixed function resolution...'); 52 53 const kidlisp = new KidLisp(); 54 55 // Test resolving a $-prefixed function 56 const resolved = kidlisp.resolveFunction('$abc123XY', mockApi, {}); 57 58 if (resolved && resolved.type === 'cache') { 59 console.log('✅ $-prefixed function correctly resolved as cache type'); 60 61 // Test calling the resolved function 62 try { 63 const result = resolved.value(mockApi, []); 64 console.log('Cache function call result:', result); 65 } catch (error) { 66 console.log('Cache function call error (expected):', error.message); 67 } 68 } else { 69 console.log('❌ $-prefixed function not resolved correctly'); 70 } 71} 72 73// Test parsing and evaluating $-prefixed function calls 74async function testParsing() { 75 console.log('\nTesting parsing of $-prefixed function calls...'); 76 77 const kidlisp = new KidLisp(); 78 79 // Test parsing a simple $-prefixed call 80 try { 81 const parsed = kidlisp.parse('($abc123XY)'); 82 console.log('Parsed ($abc123XY):', JSON.stringify(parsed, null, 2)); 83 84 // Test parsing with arguments 85 const parsed2 = kidlisp.parse('($abc123XY 64 64)'); 86 console.log('Parsed ($abc123XY 64 64):', JSON.stringify(parsed2, null, 2)); 87 88 console.log('✅ Parsing works correctly'); 89 } catch (error) { 90 console.log('❌ Parsing error:', error.message); 91 } 92} 93 94// Run all tests 95async function runTests() { 96 console.log('🚀 Running KidLisp embed functionality tests...\n'); 97 98 await testEmbedCommand(); 99 await testDollarPrefixedFunctions(); 100 await testParsing(); 101 102 console.log('\n✨ Tests completed!'); 103 console.log('\nNote: Network-dependent tests will show expected errors'); 104 console.log('since we don\'t have actual cached codes to test with.'); 105} 106 107// Export for potential use in other test files 108export { testEmbedCommand, testDollarPrefixedFunctions, testParsing, runTests }; 109 110// Run tests if this file is executed directly 111if (import.meta.url === `file://${process.argv[1]}`) { 112 runTests().catch(console.error); 113}