Files
soroban-abacus-flashcards/apps/web/scripts/validateTypstRefactoring.ts
Thomas Hallock b36df3a40c fix(worksheets): ten-frames not rendering in mastery mode
Fixed two critical bugs preventing ten-frames from rendering:

1. **Mastery mode not handled** (typstGenerator.ts:61)
   - Code only checked for 'smart' | 'manual' modes
   - Mastery mode fell into manual path, tried to use boolean flags that don't exist
   - Resulted in all display options being `undefined`
   - Fix: Check for both 'smart' OR 'mastery' modes (both use displayRules)

2. **Typst array membership syntax** (already fixed in previous commit)
   - Used `(i in array)` which doesn't work in Typst
   - Changed to `array.contains(i)`

Added comprehensive unit tests (tenFrames.test.ts):
- Problem analysis tests (regrouping detection)
- Display rule evaluation tests
- Full Typst template generation tests
- Mastery mode specific tests
- All 14 tests now passing

Added debug logging to trace display rules resolution:
- displayRules.ts: Shows rule evaluation per problem
- typstGenerator.ts: Shows enriched problems and Typst data
- Helps diagnose future issues

The issue was that mastery mode (which uses displayRules like smart mode)
was being treated as manual mode (which uses boolean flags), resulting in
undefined display options.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-10 10:06:27 -06:00

99 lines
3.4 KiB
TypeScript

#!/usr/bin/env tsx
/**
* Validation script for typstHelpers refactoring
*
* Generates sample worksheets and verifies that the refactored code
* produces identical Typst output to ensure no regressions.
*/
import { generateSubtractionProblemStackFunction } from '../src/app/create/worksheets/addition/typstHelpers'
import { generateTypstHelpers } from '../src/app/create/worksheets/addition/typstHelpers'
import { generatePlaceValueColors } from '../src/app/create/worksheets/addition/typstHelpers'
console.log('🔍 Validating typstHelpers refactoring...\n')
// Test 1: Check that functions are exported and callable
console.log('✓ Test 1: Functions are exported')
console.log(
` - generateSubtractionProblemStackFunction: ${typeof generateSubtractionProblemStackFunction}`
)
console.log(` - generateTypstHelpers: ${typeof generateTypstHelpers}`)
console.log(` - generatePlaceValueColors: ${typeof generatePlaceValueColors}`)
if (typeof generateSubtractionProblemStackFunction !== 'function') {
console.error('❌ generateSubtractionProblemStackFunction is not a function!')
process.exit(1)
}
// Test 2: Generate sample Typst code
console.log('\n✓ Test 2: Generate sample Typst code')
const cellSize = 0.55
const maxDigits = 3
const helpers = generateTypstHelpers(cellSize)
console.log(` - Helper functions: ${helpers.length} characters`)
const colors = generatePlaceValueColors()
console.log(` - Color definitions: ${colors.length} characters`)
const problemStack = generateSubtractionProblemStackFunction(cellSize, maxDigits)
console.log(` - Problem stack function: ${problemStack.length} characters`)
// Test 3: Verify key features are present
console.log('\n✓ Test 3: Verify key features in generated Typst')
const checks = [
{ name: 'Borrow boxes row', pattern: /Borrow boxes row/ },
{ name: 'Minuend row', pattern: /Minuend row/ },
{ name: 'Subtrahend row', pattern: /Subtrahend row/ },
{ name: 'Answer boxes', pattern: /Answer boxes/ },
{ name: 'Ten-frames', pattern: /Ten-frames row/ },
{ name: 'Borrowing hints', pattern: /show-borrowing-hints/ },
{ name: 'Arrow rendering', pattern: /path\(/ },
{ name: 'Place value colors', pattern: /place-colors/ },
{ name: 'Scratch work boxes', pattern: /dotted.*paint: gray/ },
]
let allPassed = true
for (const check of checks) {
const found = check.pattern.test(problemStack)
if (found) {
console.log(`${check.name}`)
} else {
console.log(`${check.name} - NOT FOUND`)
allPassed = false
}
}
// Test 4: Verify structure
console.log('\n✓ Test 4: Verify Typst structure')
const structureChecks = [
{ name: 'Function definition', pattern: /#let subtraction-problem-stack\(/ },
{ name: 'Grid structure', pattern: /grid\(/ },
{ name: 'Stack structure', pattern: /stack\(/ },
{ name: 'Problem number display', pattern: /problem-number-display/ },
]
for (const check of structureChecks) {
const found = check.pattern.test(problemStack)
if (found) {
console.log(`${check.name}`)
} else {
console.log(`${check.name} - NOT FOUND`)
allPassed = false
}
}
// Summary
console.log('\n' + '='.repeat(60))
if (allPassed) {
console.log('✅ All validation checks passed!')
console.log('\nThe refactored code generates valid Typst output with all')
console.log('expected features present.')
process.exit(0)
} else {
console.log('❌ Some validation checks failed!')
console.log('\nPlease review the output above for details.')
process.exit(1)
}