|
| 1 | +/** |
| 2 | + * Test: Decompiler Terminal Condition Index |
| 3 | + * |
| 4 | + * Verifies that the decompiler shows the correct (terminal) condition index |
| 5 | + * in the "// Condition can be read by logicCondition[N]" comment. |
| 6 | + * |
| 7 | + * Run with: node js/transpiler/tests/decompiler_terminal_index_test.js |
| 8 | + */ |
| 9 | + |
| 10 | +import { Decompiler } from '../transpiler/decompiler.js'; |
| 11 | + |
| 12 | +// Test case: Chained logic conditions (from original INAV config) |
| 13 | +// First chain: 0 -> 1 -> 2 (terminal is 2) |
| 14 | +// Second chain: 19 -> 20 -> 21 -> 22 (terminal is 22) |
| 15 | +const testLogicConditions = [ |
| 16 | + // First chain: mixerTransitionActive && isArmed && groundSpeed > 1111 |
| 17 | + { index: 0, enabled: 1, activatorId: -1, operation: 1, operandAType: 2, operandAValue: 39, operandBType: 0, operandBValue: 1, flags: 0 }, |
| 18 | + { index: 1, enabled: 1, activatorId: 0, operation: 1, operandAType: 2, operandAValue: 17, operandBType: 0, operandBValue: 1, flags: 0 }, |
| 19 | + { index: 2, enabled: 1, activatorId: 1, operation: 2, operandAType: 2, operandAValue: 9, operandBType: 0, operandBValue: 1111, flags: 0 }, |
| 20 | + // Gap (disabled conditions 3-18) |
| 21 | + ...Array.from({ length: 16 }, (_, i) => ({ index: i + 3, enabled: 0, activatorId: -1, operation: 0, operandAType: 0, operandAValue: 0, operandBType: 0, operandBValue: 0, flags: 0 })), |
| 22 | + // Second chain: activeMixerProfile && isArmed && isAutoLaunch === 0 && airSpeed < 1111 |
| 23 | + { index: 19, enabled: 1, activatorId: -1, operation: 1, operandAType: 2, operandAValue: 38, operandBType: 0, operandBValue: 1, flags: 0 }, |
| 24 | + { index: 20, enabled: 1, activatorId: 19, operation: 1, operandAType: 2, operandAValue: 17, operandBType: 0, operandBValue: 1, flags: 0 }, |
| 25 | + { index: 21, enabled: 1, activatorId: 20, operation: 1, operandAType: 2, operandAValue: 18, operandBType: 0, operandBValue: 0, flags: 0 }, |
| 26 | + { index: 22, enabled: 1, activatorId: 21, operation: 3, operandAType: 2, operandAValue: 11, operandBType: 0, operandBValue: 1111, flags: 0 }, |
| 27 | +]; |
| 28 | + |
| 29 | +function runTest() { |
| 30 | + console.log('=== Decompiler Terminal Index Test ===\n'); |
| 31 | + |
| 32 | + const decompiler = new Decompiler(); |
| 33 | + const result = decompiler.decompile(testLogicConditions); |
| 34 | + |
| 35 | + console.log('Test: Decompile chained conditions and verify terminal indices\n'); |
| 36 | + |
| 37 | + let passed = true; |
| 38 | + |
| 39 | + // Test 1: Decompilation should succeed |
| 40 | + if (!result.success) { |
| 41 | + console.log('FAIL: Decompilation failed:', result.error); |
| 42 | + passed = false; |
| 43 | + } else { |
| 44 | + console.log('PASS: Decompilation succeeded'); |
| 45 | + } |
| 46 | + |
| 47 | + // Test 2: First chain should reference logicCondition[2] (not 0) |
| 48 | + if (!result.code.includes('logicCondition[2]')) { |
| 49 | + console.log('FAIL: First chain should reference logicCondition[2]'); |
| 50 | + passed = false; |
| 51 | + } else { |
| 52 | + console.log('PASS: First chain references logicCondition[2]'); |
| 53 | + } |
| 54 | + |
| 55 | + // Test 3: Second chain should reference logicCondition[22] (not 19) |
| 56 | + if (!result.code.includes('logicCondition[22]')) { |
| 57 | + console.log('FAIL: Second chain should reference logicCondition[22]'); |
| 58 | + passed = false; |
| 59 | + } else { |
| 60 | + console.log('PASS: Second chain references logicCondition[22]'); |
| 61 | + } |
| 62 | + |
| 63 | + // Test 4: Should NOT reference the first condition indices (0 or 19) for readable conditions |
| 64 | + const wrongRefs = result.code.match(/logicCondition\[(0|19)\]/g); |
| 65 | + if (wrongRefs && wrongRefs.length > 0) { |
| 66 | + console.log('FAIL: Should not reference first condition indices:', wrongRefs); |
| 67 | + passed = false; |
| 68 | + } else { |
| 69 | + console.log('PASS: Does not reference first condition indices incorrectly'); |
| 70 | + } |
| 71 | + |
| 72 | + console.log('\n--- Generated Code ---'); |
| 73 | + console.log(result.code); |
| 74 | + |
| 75 | + console.log('\n=== Test Result: ' + (passed ? 'PASSED' : 'FAILED') + ' ===\n'); |
| 76 | + |
| 77 | + if (!passed) { |
| 78 | + process.exit(1); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +runTest(); |
0 commit comments