SWCanvas

SWCanvas Test Build Utilities

This directory contains utility scripts for managing the SWCanvas test system.

Scripts Overview

concat-tests.js

Purpose: Concatenates individual modular test files into unified test suites for optimal performance.

What it does:

Usage: Called automatically by the build system, not typically run directly.

update-test-counts.js

Purpose: Automatically updates test count references throughout all documentation files to match actual filesystem counts.

What it does:

Usage:

# Update test count references to match current file counts
node tests/build/update-test-counts.js

# Or use as npm script (if configured)
npm run update-test-counts

When to use:

Output: The script provides detailed logging of changes made and suggests appropriate commit messages.

renumber-tests.js

Purpose: Renumbers test files to make space for inserting new tests at specific positions.

What it does:

Usage:

# Make space for a new visual test at position 25
node tests/build/renumber-tests.js --type visual --position 25 --shift forward

# Close a gap after removing core test 15
node tests/build/renumber-tests.js --type core --position 15 --shift backward

# Preview changes without executing
node tests/build/renumber-tests.js --type visual --position 30 --shift forward --dry-run

Arguments:

Workflow for Adding Tests in the Middle

When you want to add a new test at a specific position (e.g., position 25) instead of at the end:

1. Make space for the new test

# Shift all tests from position 25 onwards forward by 1
node tests/build/renumber-tests.js --type visual --position 25 --shift forward

This renames:

2. Create your new test file

# Now position 025 is free
echo "// Test: My New Test" > tests/visual/025-my-new-test.js
# Add your test implementation...

3. Rebuild and test

npm run build  # Regenerate built test files
npm test       # Run all tests including your new one

4. Undo if needed

The renumbering script generates an undo-renumber.sh script:

./undo-renumber.sh  # Revert the renumbering operation

File Naming Convention

All test files follow this naming pattern:

Internal Structure:

// Test {number}: {description}
// This file will be concatenated into the main test suite

// Test {number}
registerVisualTest('test-id', {
    name: 'Human-readable test name',
    width: 200, height: 150,
    draw: function(canvas) {
        // Test implementation
    }
});

Directory Structure

tests/build/
├── README.md              # This documentation
├── concat-tests.js        # Test concatenation utility
├── update-test-counts.js  # Automated test count updater
└── renumber-tests.js      # Test renumbering utility

Error Handling and Safety

Renumbering Safety Features:

Common Error Scenarios:

  1. Conflict: Target file already exists
    • Solution: Use --dry-run to preview, resolve conflicts manually
  2. Git not available: Script falls back to regular mv
    • Solution: Manual git history preservation if needed
  3. Invalid position: Position outside valid range (1-999)
    • Solution: Check existing test numbers, use valid range
  4. Missing directory: Test directory doesn’t exist
    • Solution: Ensure you’re in the correct project directory

Integration with Build System

The build utilities integrate seamlessly with the main SWCanvas build process:

# Full development cycle
npm run build              # Runs concat-tests.js automatically
npm test                   # Uses built test files from /tests/dist/

# Test development cycle
node tests/build/renumber-tests.js --type visual --position 30 --shift forward
# Create your new test at position 030
npm run build              # Rebuild with new test
npm test                   # Verify all tests pass

Tips and Best Practices

When to Use Renumbering:

When NOT to Use Renumbering:

  1. Plan first: Use --dry-run to see the impact
  2. Small batches: Renumber small ranges to minimize disruption
  3. Test thoroughly: Run full test suite after renumbering
  4. Commit atomically: Commit renumbering and new test separately for clear history

Git Integration:

This comprehensive utility system ensures that test organization remains maintainable as the SWCanvas test suite continues to grow and evolve.