| name | error-scenario-testing |
| description | Techniques for forcing error conditions in code to test error handling paths, including mock failure injection. Use when testing error handling, maze generation failures, or when error conditions are hard to trigger naturally. Create test scenarios to force error conditions and verify error handling works correctly. |
Error Scenario Testing
Overview
Test error handling by forcing error conditions. Don't commit error handling code without testing error paths.
Forcing Error Conditions
Methods:
- Add test mode flags to enable error injection
- Create test seam commands to trigger errors
- Temporarily modify code to force failures (revert after testing)
- Use environment variables to enable error testing
See references/error-injection.md for detailed error injection patterns.
Maze Generation Error Testing
For maze generation failures:
- Add
forceMazeFailure() test seam command
- Modify maze generation to fail after N attempts
- Test fallback maze creation
- Verify console warnings appear
See references/maze-generation-errors.md for specific patterns.
Error Testing Checklist
Test Mode Pattern
See references/error-injection.md for test mode implementation pattern.
Test Seam Error Injection
Add test seam commands to trigger errors:
window.__TEST__.commands.forceMazeFailure = () => {
this.mazeGenerator.forceFailure = true;
};
window.__TEST__.commands.restoreMazeGeneration = () => {
this.mazeGenerator.forceFailure = false;
};
Error scenario test commands:
window.__TEST__.commands = {
forceMazeFailure: () => {
this.mazeGenerator.forceFailure = true;
},
restoreMazeGeneration: () => {
this.mazeGenerator.forceFailure = false;
},
triggerAssetLoadError: () => {
this.assetLoader.simulateError = true;
},
restoreAssetLoading: () => {
this.assetLoader.simulateError = false;
}
};
Test error handling paths:
agent-browser eval "window.__TEST__.commands.forceMazeFailure()"
agent-browser eval "window.__TEST__.commands.generateMaze()"
agent-browser eval "window.__TEST__.commands.restoreMazeGeneration()"
Error Recovery Patterns
Retry logic with configurable attempts:
async function generateWithRetry(
generator: () => Promise<Result>,
maxAttempts: number = 3
): Promise<Result> {
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await generator();
} catch (error) {
console.warn(`Attempt ${attempt} failed:`, error);
if (attempt === maxAttempts) {
throw new Error(`Failed after ${maxAttempts} attempts`);
}
await sleep(1000 * attempt);
}
}
throw new Error('Max attempts reached');
}
Fallback mechanisms for critical operations:
async function loadAssetWithFallback(
assetKey: string,
primaryUrl: string,
fallbackUrl: string
): Promise<void> {
try {
this.load.image(assetKey, primaryUrl);
} catch (error) {
console.warn(`Failed to load ${assetKey} from primary URL, using fallback`);
this.load.image(assetKey, fallbackUrl);
}
}
Console warning patterns:
if (!this.textures.exists('asset-key')) {
console.warn('Asset not found:', 'asset-key');
console.warn('Using fallback placeholder');
}
Graceful degradation strategies:
create() {
try {
this.initPrimaryFeature();
} catch (error) {
console.warn('Primary feature failed, using fallback:', error);
this.initFallbackFeature();
}
}
Verification Checklists
Verify error handling works:
## Error Handling Verification
- [ ] Error condition can be triggered
- [ ] Error handling code executes
- [ ] Fallback behavior works correctly
- [ ] User experience is acceptable
- [ ] Console warnings/logs appear
- [ ] Game doesn't crash
Test fallback mechanisms:
agent-browser eval "window.__TEST__.commands.forceMazeFailure()"
agent-browser eval "window.__TEST__.commands.generateMaze()"
agent-browser eval "window.__TEST__.gameState().maze"
Check console warnings:
agent-browser console | grep -i "warn\|error"
agent-browser eval "window.__TEST__.commands.forceMazeFailure()"
agent-browser console
Validate recovery behavior:
agent-browser eval "window.__TEST__.commands.forceMazeFailure()"
agent-browser eval "window.__TEST__.commands.generateMaze()"
agent-browser eval "window.__TEST__.commands.restoreMazeGeneration()"
agent-browser eval "window.__TEST__.commands.generateMaze()"
Resources
references/error-injection.md - Test mode flags, test seam commands, environment variables
references/maze-generation-errors.md - Specific patterns for maze generation failures