JSDoc comments explain what code does. Heist.exe examples:
From Guard.js:
Code Runner Challenge
Unit Testing β Gem.collect()
View IPYNB Source
/**
* Reverse velocity when guard hits canvas edge to create bounce effect
*/
stayWithinCanvas() {
if (this.position.y < 0) {
this.velocity.y *= -1; // Bounce at top
}
}
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
From Gem.js:
Code Runner Challenge
API Error Handling β Fetch with try/catch and response.ok
View IPYNB Source
/**
* Marks gem as permanently collected to prevent double-collection
*/
collect() {
if (this.permanentlyCollected) return;
this.permanentlyCollected = true;
gameEnv.stats.coinsCollected += this.value;
}
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Target: > 10% comment density β 1 line of comments per ~10 lines of code.
%%js
//CODE_RUNNER: Unit Testing β Gem.collect()
const gameEnv = { stats: { coinsCollected: 0 } };
const gem = {
value: 5,
permanentlyCollected: false,
collect() {
if (this.permanentlyCollected) return; // Block double-collection
this.permanentlyCollected = true;
gameEnv.stats.coinsCollected += this.value;
}
};
// Test: collect once, then try again
gem.collect();
console.assert(gameEnv.stats.coinsCollected === 5, "FAIL: score should be 5");
gem.collect();
console.assert(gameEnv.stats.coinsCollected === 5, "FAIL: score should stay 5, not 10");
console.log("β
Unit tests passed! Score:", gameEnv.stats.coinsCollected);
2. Integration Testing β Guard + Player Collision
Integration tests verify two systems work together. In Heist, test that Guard collision: detects player β runs handleCollisionEvent β destroys player β blocks re-collision.
From Guard.js:
if (!this.playerDestroyed && this.collisionChecks()) {
this.handleCollisionEvent(); // Only fires once due to playerDestroyed flag
player.destroy();
this.playerDestroyed = true;
}
3. State Verification β Heist Game Stats
After collecting gems, verify the game state updated:
// After collecting 2 gems (5 points each):
console.assert(gameEnv.stats.coinsCollected === 10, "Should have 10 coins");
console.assert(gem1.permanentlyCollected === true, "gem1 should be locked");
console.assert(gem2.permanentlyCollected === true, "gem2 should be locked");
// Double-collection should fail:
gem1.collect();
console.assert(gameEnv.stats.coinsCollected === 10, "Still 10, not 15");
4. Boundary Testing β Guard Canvas Edges
Test Guard behavior at canvas edges:
// Bottom edge: should bounce
guard.position.y = gameEnv.innerHeight;
guard.stayWithinCanvas();
console.assert(guard.velocity.y < 0, "Should reverse at bottom");
// Top edge: should bounce
guard.position.y = 0;
guard.stayWithinCanvas();
console.assert(guard.velocity.y > 0, "Should reverse at top");
%%js
//CODE_RUNNER: API Error Handling β Fetch with try/catch and response.ok
async function testFetch(url, label) {
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
console.log(`[${label}] β Success`);
} catch (error) {
console.error(`[${label}] β Error:`, error.message);
}
}
// Test bad URL
await testFetch('https://official-joke-api.appspot.com/jokes/invalid', 'Bad endpoint');
// Test good URL
await testFetch('https://official-joke-api.appspot.com/random_joke', 'Good endpoint');
console.log('β
Both paths tested');
Summary β Testing Checklist for Heist.exe
Based on CS111 Testing & Verification Documentation:
| Test Type |
What to Verify |
Heist.exe Example |
Tool |
| Unit Testing |
Single method with known inputs |
Gem.collect() β collects once, blocks double-collection |
Code runner |
| Integration Testing |
Two systems work together |
Guard + Player β collision triggers destroy sequence |
Code runner |
| State Verification |
Runtime values match expected |
gameEnv.stats.coinsCollected === 10 after 2 gems |
DevTools Console |
| Boundary Testing |
Edge cases at limits |
Guard at top/bottom/left/right canvas edges |
Code runner |
| API Error Handling |
Both success and failure paths |
iTunes music fetch with try/catch + !response.ok |
Code runner |
The goal: Test the parts that break in interesting ways. In Heist.exe, thatβs one-time events (gem collection, collision) and external dependencies (iTunes API for music).