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) }