feat(js): m09 input, app, runtime, browser, main, index.html and smoke test

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
Lucas Winther
2026-10-28 23:53:09 +01:00
co-authored by Claude Sonnet 5.5
parent 584f1d4825
commit 8028bb2fc0
16 changed files with 622 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { states, classicActions, render, main, TICK_MS } from '../src/app.js'
import { run } from '../src/stream.js'
import { trace } from '../src/io.js'
import { view } from '../src/view.js'
import { newGame } from '../src/spawn.js'
import { Action } from '../src/model.js'
import { freeze } from './helpers.js'
const time = (t) => ({ tag: 'Time', t })
const down = (key) => ({ tag: 'KeyDown', key })
test('m09 the game ticks every 150 ms', () => assert.equal(TICK_MS, 150))
test('m09 classicActions turns Tick into actions on the clock and keys into Turn actions', () =>
assert.deepStrictEqual(
run(classicActions, freeze([time(150), down('ArrowUp'), down('x'), time(300)])),
[Action.Tick, Action.Turn('Up'), Action.Tick]))
test('m09 states starts with the new game, then yields one state per action', () => {
const out = run(states(42, classicActions), freeze([time(150), down('ArrowUp'), time(300)]))
assert.equal(out.length, 4)
assert.deepStrictEqual(out[0], newGame(42))
assert.deepStrictEqual(out.map((s) => s.snake[0]), [
{ x: 10, y: 10 }, { x: 11, y: 10 }, { x: 11, y: 10 }, { x: 11, y: 9 },
])
assert.deepStrictEqual(out.map((s) => s.nextDir), ['Right', 'Right', 'Up', 'Up'])
assert.deepStrictEqual(out.map((s) => s.dir), ['Right', 'Right', 'Right', 'Up'])
})
test('m09 render draws the whole view of the state', () =>
assert.deepStrictEqual(trace(render(newGame(42)), 0).ops, view(newGame(42)).map((shape) => ({ tag: 'Draw', shape }))))
test('m09 main asks for a random seed, then builds the stream from it', () => {
const { ops, result: stream } = trace(main, 42)
assert.deepStrictEqual(ops, [{ tag: 'RandomSeed' }])
const [outs] = stream.step(time(0))
assert.equal(outs.length, 1)
assert.deepStrictEqual(trace(outs[0], 0).ops, trace(render(newGame(42)), 0).ops)
})
test('m09 main with seed 42 draws the new game first, then the ticked game after 150 ms', () => {
const { result: stream } = trace(main, 42)
const [first, next] = stream.step(time(0))
const [second] = next.step(time(150))
assert.deepStrictEqual(trace(first[0], 0).ops.map((o) => o.shape), view(newGame(42)))
assert.equal(second.length, 1)
assert.notDeepEqual(trace(second[0], 0).ops, trace(first[0], 0).ops)
})