Files
functional-programming-snake/scala/test/M10Replay.test.scala
T
Lucas WintherandClaude Fable 5.2 edd57f0148 feat(course): audit fixes and twelve extensions (Modules 7b, 12, 13)
Audit pass:
- Frame rule counts frames since the last move, so every step lasts
  exactly movesEvery(score) frames, also right after a speed-up
- clampTime + MAX_GAP_MS: a hidden tab no longer replays the whole gap
- Pong Restart keeps held keys
- Fact corrections (mulberry32 output coverage, Haskell IO, uniform)
- New fixture cases from a mutation analysis

Extensions (all three tracks, shared fixtures, course text):
- Turn queue (Exercise 10.6), persisted high score (10.7),
  replay viewer (10.8), play() end-to-end helper (9.5)
- Optional State (4.7) and optional Module 7b, stack-safe IO
- Module 12, property-based testing with generators, shrinking,
  invariants and a model-based oracle
- Module 13, Conway's Life with a Click event
- JS type checking via jsconfig, JSDoc and types.d.ts
- Maintainer tools: scripts/mutate.mjs, scripts/typecheck-js.sh,
  Playwright end-to-end suite (e2e/, scripts/e2e.sh)

Docs: docs/contract.md is the canonical contract; specs and build log
under docs/superpowers.

Verified: JS 728, Scala 689, F# 691 unit tests; stubs and progression
M01-M13 and M07b; 176 end-to-end tests on Chromium and WebKit.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 12:05:29 +01:00

126 lines
5.3 KiB
Scala

package snake
import Fixtures.*
import Replay.*
import M09Helpers.drawn
class M10ReplaySuite extends munit.FunSuite:
private def viewer(v: ujson.Value): Viewer = Viewer(v("index").num.toInt, v("last").num.toInt, v("playing").bool)
private def viewerAction(v: ujson.Value): ViewerAction = ViewerAction.valueOf(v("tag").str)
cases("m10-viewer-update").foreach { c =>
test(s"viewerUpdate: ${c("name").str}") {
val a = c("args")
assertEquals(viewerUpdate(viewer(a(0)), viewerAction(a(1))), viewer(c("expected")))
}
}
cases("m10-viewer-key").foreach { c =>
test(s"viewerKey: ${c("name").str}") {
assertEquals(viewerKey(c("args")(0).str), opt(c("expected"))(viewerAction))
}
}
cases("m10-viewer-view").foreach { c =>
test(s"viewerView: ${c("name").str}") {
val a = c("args")
assertEquals(viewerView(list(a(0))(gameState), viewer(a(1))), list(c("expected"))(shape))
}
}
// The sample recording, as the page loads it. Not a fixture of cases: a recording `{seed, actions}`.
private lazy val sample: Recording = {
val v = load("replay-sample")
Recording(v("seed").num.toLong, list(v("actions"))(action))
}
private def time(t: Long): Event = Event.Time(t)
private def key(k: String): Event = Event.KeyDown(k)
test("the sample recording is worth watching: Tick and Turn only, under 400 actions, at least five apples, then game over") {
assert(sample.actions.forall {
case Action.Tick | Action.Turn(_) => true
case _ => false
})
assert(sample.actions.length >= 150 && sample.actions.length <= 400, s"${sample.actions.length} actions")
val fs = Game.history(Spawn.newGame(sample.seed), sample.actions)
assert(fs.last.score >= 5, s"score ${fs.last.score}")
assertEquals(fs.last.status, Status.GameOver)
assertEquals(fs.init.last.status, Status.Playing)
}
test("frames replays the recording with history, from the new game of its seed") {
val recording = Recording(42, List(Action.Tick, Action.Turn(Direction.Up), Action.Tick))
assertEquals(frames(recording), Game.history(Spawn.newGame(42), recording.actions))
assertEquals(frames(sample).length, sample.actions.length + 1)
}
test("viewerStart is the first frame, not playing") {
assertEquals(viewerStart(12), Viewer(0, 12, playing = false))
}
test("viewerUpdate keeps the index in [0, last] whatever the actions") {
import ViewerAction.*
val actions = List(Prev, Next, Last, Next, TogglePlay, Beat, Beat, First, Prev, Beat)
val viewers = actions.scanLeft(viewerStart(3))(viewerUpdate)
assert(viewers.forall(v => v.index >= 0 && v.index <= 3 && !(v.playing && v.index == 3)))
}
test("the autoplay clock beats every 100 ms") { assertEquals(REPLAY_MS, 100L) }
test("replayActions turns the clock into Beats and keys into viewer actions") {
assertEquals(
replayActions.run(List(time(100), key("ArrowRight"), key("x"), key(" "), time(300))),
List(ViewerAction.Beat, ViewerAction.Next, ViewerAction.TogglePlay, ViewerAction.Beat, ViewerAction.Beat)
)
}
test("autoplay: Space, then the clock steps one frame per 100 ms and stops at the end") {
val out = viewerStates(3, replayActions).run(List(time(0), key(" "), time(100), time(200), time(300), time(400), time(500)))
assertEquals(out.map(_.index), List(0, 0, 1, 2, 3, 3, 3))
assertEquals(out.map(_.playing), List(false, true, true, true, false, false, false))
}
test("without Space nothing plays: the clock alone never moves the viewer") {
assert(viewerStates(3, replayActions).run(List(time(0), time(1000), time(2000))).forall(_.index == 0))
}
test("replayMain asks for nothing and draws the first frame of the recording first") {
val traced = IO.trace(replayMain(sample), 0)
assertEquals(traced.ops, Nil)
val (drawings, _) = traced.result.step(time(0))
assertEquals(drawings.length, 1)
assertEquals(drawn(drawings.head), viewerView(frames(sample), viewerStart(sample.actions.length)))
val newGameView = View.view(Spawn.newGame(sample.seed))
assertEquals(drawn(drawings.head).take(newGameView.length), newGameView)
}
test("replayMain: End shows the last frame of the game, the game over") {
val out = IO.trace(replayMain(sample), 0).result.run(List(time(0), key("End")))
val fs = frames(sample)
assertEquals(drawn(out.last), viewerView(fs, Viewer(fs.length - 1, fs.length - 1, playing = false)))
assert(drawn(out.last).exists {
case Shape.Text(_, _, "Game Over", _, _, _) => true
case _ => false
})
}
test("stepping back shows exactly the earlier scene") {
val out = IO
.trace(replayMain(sample), 0)
.result
.run(List(time(0), key("ArrowRight"), key("ArrowRight"), key("ArrowRight"), key("ArrowLeft"), key("d"), key("a"), key("a")))
val scenes = out.map(drawn)
assertEquals(scenes(4), scenes(2))
assertEquals(scenes(6), scenes(4))
assertEquals(scenes(7), scenes(1))
assertNotEquals(scenes(3), scenes(2))
}
test("replayMain: after ten seconds in a background tab autoplay moves 2 frames (250 ms), not 100") {
val (_, afterPlay) = IO.trace(replayMain(sample), 0).result.step(key(" "))
val (_, afterFirst) = afterPlay.step(time(0))
assertEquals(afterFirst.step(time(10000))._1.length, 2)
}