feat(js): m05 game and m06 view with fixtures and tests

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
Lucas Winther
2026-10-28 23:45:10 +01:00
co-authored by Claude Sonnet 5.5
parent 08b462d5cc
commit b83f94894e
12 changed files with 1398 additions and 0 deletions
+1
View File
@@ -13,6 +13,7 @@ const fixtureTests = (name, f) => {
fixtureTests('m04-free-cells', freeCells)
fixtureTests('m04-spawn', spawnApple)
fixtureTests('m04-new-game', newGame)
fixtureTests('m04-tick', tick)
// 3x2 board, one-cell snake, about to eat the apple at (1,0)
const eating = freeze({
+26
View File
@@ -0,0 +1,26 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { update, replay, history } from '../src/game.js'
import { loadFixture, freeze } from './helpers.js'
const fixtureTests = (name, f) => {
const fx = loadFixture(name)
fx.cases.forEach((c) =>
test(`m05 ${fx.function}: ${c.name}`, () => assert.deepStrictEqual(f(...freeze(c.args)), c.expected)))
}
fixtureTests('m05-update', update)
fixtureTests('m05-replay', replay)
fixtureTests('m05-history', history)
test('m05 history has one more state than there are actions', () => {
const [state, actions] = loadFixture('m05-replay').cases[0].args
assert.equal(history(freeze(state), freeze(actions)).length, actions.length + 1)
})
test('m05 history starts at the start state and ends where replay ends', () => {
const [state, actions] = freeze(loadFixture('m05-replay').cases[0].args)
const states = history(state, actions)
assert.deepStrictEqual(states[0], state)
assert.deepStrictEqual(states[states.length - 1], replay(state, actions))
})
+10
View File
@@ -0,0 +1,10 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { CELL, view } from '../src/view.js'
import { loadFixture, freeze } from './helpers.js'
test('m06 CELL is 20 pixels', () => assert.equal(CELL, 20))
const fx = loadFixture('m06-view')
fx.cases.forEach((c) =>
test(`m06 ${fx.function}: ${c.name}`, () => assert.deepStrictEqual(view(...freeze(c.args)), c.expected)))