feat(js): m04 random and spawn with fixtures and tests

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
Lucas Winther
2026-10-28 23:42:36 +01:00
co-authored by Claude Sonnet 5.5
parent 2e39f23bb3
commit 08b462d5cc
11 changed files with 1332 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { nextUint32, pure, map, flatMap, intBelow, run } from '../src/random.js'
import { loadFixture, freeze } from './helpers.js'
const fixtureTests = (name, f) => {
const fx = loadFixture(name)
fx.cases.forEach((c) =>
test(`m04 ${fx.function}: ${c.name}`, () => assert.deepStrictEqual(f(...freeze(c.args)), c.expected)))
}
fixtureTests('m04-random', (seed) => run(nextUint32, seed))
fixtureTests('m04-int-below', (n, seed) => run(intBelow(n), seed))
test('m04 the recorded steps chain: each step starts from the previous step\'s seed', () => {
const cases = loadFixture('m04-random').cases
cases.forEach((c, i) => {
if (!c.name.endsWith('-step-1')) assert.equal(c.args[0], cases[i - 1].expected[1])
})
})
test('m04 pure returns its value and leaves the seed alone', () =>
assert.deepStrictEqual(run(pure('x'), 99), ['x', 99]))
test('m04 map applies f to the value and keeps the seed from the underlying step', () => {
const [v, s] = run(nextUint32, 5)
assert.deepStrictEqual(run(map(nextUint32, (x) => x + 1), 5), [v + 1, s])
})
test('m04 map with the identity function changes nothing', () =>
assert.deepStrictEqual(run(map(nextUint32, (x) => x), 5), run(nextUint32, 5)))
test('m04 flatMap threads the seed through both steps', () => {
const [a, s1] = run(nextUint32, 5)
const [b, s2] = run(nextUint32, s1)
assert.deepStrictEqual(run(flatMap(nextUint32, (x) => map(nextUint32, (y) => [x, y])), 5), [[a, b], s2])
})
test('m04 flatMap with pure on the left or right changes nothing', () => {
assert.deepStrictEqual(run(flatMap(pure(3), (x) => map(nextUint32, (v) => v + x)), 5), run(map(nextUint32, (v) => v + 3), 5))
assert.deepStrictEqual(run(flatMap(nextUint32, pure), 5), run(nextUint32, 5))
})
test('m04 intBelow always lands in [0, n)', () => {
const draws = (n, seed, count) =>
count === 0 ? [] : ((([v, s]) => [v, ...draws(n, s, count - 1)])(run(intBelow(n), seed)))
;[1, 2, 7, 400].forEach((n) =>
draws(n, 123, 50).forEach((v) => assert.ok(Number.isInteger(v) && v >= 0 && v < n, `${v} not in [0, ${n})`)))
})
test('m04 running a Random does not mutate anything: same seed, same answer', () =>
assert.deepStrictEqual(run(nextUint32, 8), run(nextUint32, 8)))
+37
View File
@@ -0,0 +1,37 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { freeCells, spawnApple, tick, newGame } from '../src/spawn.js'
import { advance } from '../src/rules.js'
import { loadFixture, freeze } from './helpers.js'
const fixtureTests = (name, f) => {
const fx = loadFixture(name)
fx.cases.forEach((c) =>
test(`m04 ${fx.function}: ${c.name}`, () => assert.deepStrictEqual(f(...freeze(c.args)), c.expected)))
}
fixtureTests('m04-free-cells', freeCells)
fixtureTests('m04-spawn', spawnApple)
fixtureTests('m04-new-game', newGame)
// 3x2 board, one-cell snake, about to eat the apple at (1,0)
const eating = freeze({
width: 3, height: 2, snake: [{ x: 0, y: 0 }], dir: 'Right', nextDir: 'Right',
apple: { x: 1, y: 0 }, seed: 0, score: 0, status: 'Playing', frame: 0,
})
test('m04 tick: eating the apple spawns a new one in the same step', () => {
// snake becomes [(1,0),(0,0)]; free cells are (2,0),(0,1),(1,1),(2,1); seed 0 draws 1144304738 % 4 = 2 -> (1,1)
assert.deepStrictEqual(tick(eating), {
...eating, snake: [{ x: 1, y: 0 }, { x: 0, y: 0 }], apple: { x: 1, y: 1 }, score: 1, seed: 1831565813,
})
})
test('m04 tick is spawnApple after advance', () => assert.deepStrictEqual(tick(eating), spawnApple(advance(eating))))
test('m04 tick leaves the seed alone when no apple needs spawning', () => {
const s = freeze({ ...eating, apple: { x: 2, y: 1 } })
assert.equal(tick(s).seed, 0)
})
test('m04 newGame is reproducible: same seed, same game', () => assert.deepStrictEqual(newGame(42), newGame(42)))