import test from 'node:test' import assert from 'node:assert/strict' import { REPLAY_MS, frames, viewerStart, ViewerAction, viewerUpdate, viewerKey, viewerView, replayActions, viewerStates, replayMain, } from '../src/replay.js' import { history } from '../src/game.js' import { newGame } from '../src/spawn.js' import { view } from '../src/view.js' import { run } from '../src/stream.js' import { trace } from '../src/io.js' import { loadFixture, freeze } from './helpers.js' const fixtureTests = (name, f) => { const fx = loadFixture(name) fx.cases.forEach((c) => test(`m10 ${fx.function}: ${c.name}`, () => assert.deepStrictEqual(f(...freeze(c.args)), c.expected))) } fixtureTests('m10-viewer-update', viewerUpdate) fixtureTests('m10-viewer-key', viewerKey) fixtureTests('m10-viewer-view', viewerView) // The sample recording, as the page loads it. Not a fixture of cases: a recording `{seed, actions}`. const sample = freeze(loadFixture('replay-sample')) const time = (t) => ({ tag: 'Time', t }) const key = (k) => ({ tag: 'KeyDown', key: k }) const shapes = (io) => trace(io, 0).ops.map((o) => o.shape) test('m10 the sample recording is worth watching: Tick and Turn only, under 400 actions, at least five apples, then game over', () => { assert.ok(sample.actions.every((a) => a.tag === 'Tick' || a.tag === 'Turn')) assert.ok(sample.actions.length >= 150 && sample.actions.length <= 400, `${sample.actions.length} actions`) const fs = history(newGame(sample.seed), sample.actions) assert.ok(fs.at(-1).score >= 5, `score ${fs.at(-1).score}`) assert.equal(fs.at(-1).status, 'GameOver') assert.equal(fs.at(-2).status, 'Playing') }) test('m10 frames replays the recording with history, from the new game of its seed', () => { const recording = freeze({ seed: 42, actions: [{ tag: 'Tick' }, { tag: 'Turn', dir: 'Up' }, { tag: 'Tick' }] }) assert.deepStrictEqual(frames(recording), history(newGame(42), recording.actions)) assert.equal(frames(sample).length, sample.actions.length + 1) }) test('m10 viewerStart is the first frame, not playing', () => assert.deepStrictEqual(viewerStart(12), { index: 0, last: 12, playing: false })) test('m10 ViewerAction has the six actions as plain values', () => assert.deepStrictEqual( [ViewerAction.Next, ViewerAction.Prev, ViewerAction.First, ViewerAction.Last, ViewerAction.TogglePlay, ViewerAction.Beat], [{ tag: 'Next' }, { tag: 'Prev' }, { tag: 'First' }, { tag: 'Last' }, { tag: 'TogglePlay' }, { tag: 'Beat' }])) test('m10 viewerUpdate keeps the index in [0, last] whatever the actions', () => { const actions = [ViewerAction.Prev, ViewerAction.Next, ViewerAction.Last, ViewerAction.Next, ViewerAction.TogglePlay, ViewerAction.Beat, ViewerAction.Beat, ViewerAction.First, ViewerAction.Prev, ViewerAction.Beat] const viewers = actions.reduce((acc, a) => [...acc, viewerUpdate(acc.at(-1), a)], [freeze(viewerStart(3))]) assert.ok(viewers.every((v) => v.index >= 0 && v.index <= 3 && !(v.playing && v.index === 3))) }) test('m10 the autoplay clock beats every 100 ms', () => assert.equal(REPLAY_MS, 100)) test('m10 replayActions turns the clock into Beats and keys into viewer actions', () => assert.deepStrictEqual( run(replayActions, freeze([time(100), key('ArrowRight'), key('x'), key(' '), time(300)])), [ViewerAction.Beat, ViewerAction.Next, ViewerAction.TogglePlay, ViewerAction.Beat, ViewerAction.Beat])) test('m10 autoplay: Space, then the clock steps one frame per 100 ms and stops at the end', () => { const out = run(viewerStates(3, replayActions), freeze([time(0), key(' '), time(100), time(200), time(300), time(400), time(500)])) assert.deepStrictEqual(out.map((v) => v.index), [0, 0, 1, 2, 3, 3, 3]) assert.deepStrictEqual(out.map((v) => v.playing), [false, true, true, true, false, false, false]) }) test('m10 without Space nothing plays: the clock alone never moves the viewer', () => assert.ok(run(viewerStates(3, replayActions), freeze([time(0), time(1000), time(2000)])).every((v) => v.index === 0))) test('m10 replayMain asks for nothing and draws the first frame of the recording first', () => { const { ops, result: stream } = trace(replayMain(sample), 0) assert.deepStrictEqual(ops, []) const [drawings] = stream.step(time(0)) assert.equal(drawings.length, 1) assert.deepStrictEqual(shapes(drawings[0]), viewerView(frames(sample), viewerStart(sample.actions.length))) assert.deepStrictEqual(shapes(drawings[0]).slice(0, view(newGame(sample.seed)).length), view(newGame(sample.seed))) }) test('m10 replayMain: End shows the last frame of the game, the game over', () => { const out = run(trace(replayMain(sample), 0).result, freeze([time(0), key('End')])) const fs = frames(sample) assert.deepStrictEqual(shapes(out.at(-1)), viewerView(fs, { index: fs.length - 1, last: fs.length - 1, playing: false })) assert.ok(shapes(out.at(-1)).some((s) => s.tag === 'Text' && s.text === 'Game Over')) }) test('m10 stepping back shows exactly the earlier scene', () => { const out = run(trace(replayMain(sample), 0).result, freeze([time(0), key('ArrowRight'), key('ArrowRight'), key('ArrowRight'), key('ArrowLeft'), key('d'), key('a'), key('a')])) const scenes = out.map(shapes) assert.deepStrictEqual(scenes[4], scenes[2]) assert.deepStrictEqual(scenes[6], scenes[4]) assert.deepStrictEqual(scenes[7], scenes[1]) assert.notDeepStrictEqual(scenes[3], scenes[2]) }) test('m10 replayMain: after ten seconds in a background tab autoplay moves 2 frames (250 ms), not 100', () => { const [, afterPlay] = trace(replayMain(sample), 0).result.step(key(' ')) const [, afterFirst] = afterPlay.step(time(0)) assert.equal(afterFirst.step(time(10000))[0].length, 2) })