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>
132 lines
4.9 KiB
Scala
132 lines
4.9 KiB
Scala
package snake
|
|
|
|
import scala.collection.mutable.ListBuffer
|
|
|
|
/** A fake platform: records every call and lets the test fire events by hand. `best` answers loadBest (m10). */
|
|
class FakePlatform(seed: Long = 42, best: Int = 0) extends Platform:
|
|
val calls: ListBuffer[List[Any]] = ListBuffer.empty
|
|
private var onTime: Long => Unit = _ => ()
|
|
private var onDown: String => Unit = _ => ()
|
|
private var onUp: String => Unit = _ => ()
|
|
private var onClickCb: (Int, Int) => Unit = (_, _) => ()
|
|
// Which callbacks the runtime registered, by name: "click", "down", "time", "up" (m13).
|
|
private var handlers: Set[String] = Set.empty
|
|
|
|
def fillRect(x: Int, y: Int, w: Int, h: Int, color: String): Unit = calls += List("fillRect", x, y, w, h, color)
|
|
def fillText(x: Int, y: Int, text: String, color: String, size: Int, align: String): Unit =
|
|
calls += List("fillText", x, y, text, color, size, align)
|
|
def randomSeed(): Long =
|
|
calls += List("randomSeed")
|
|
seed
|
|
def loadBest(): Int =
|
|
calls += List("loadBest")
|
|
best
|
|
def saveBest(score: Int): Unit = calls += List("saveBest", score)
|
|
def startClock(cb: Long => Unit): Unit =
|
|
onTime = cb
|
|
handlers += "time"
|
|
def onKeyDown(cb: String => Unit): Unit =
|
|
onDown = cb
|
|
handlers += "down"
|
|
def onKeyUp(cb: String => Unit): Unit =
|
|
onUp = cb
|
|
handlers += "up"
|
|
// m13: a runtime from Module 9 does not register this yet; nothing in m09-m12 fires a click.
|
|
def onClick(cb: (Int, Int) => Unit): Unit =
|
|
onClickCb = cb
|
|
handlers += "click"
|
|
|
|
def fireTime(t: Long): Unit = onTime(t)
|
|
def fireKey(key: String): Unit = onDown(key)
|
|
def fireKeyUp(key: String): Unit = onUp(key)
|
|
def fireClick(x: Int, y: Int): Unit = onClickCb(x, y)
|
|
def registered: List[String] = handlers.toList.sorted
|
|
|
|
class M09RuntimeSuite extends munit.FunSuite:
|
|
// The m09 program (classicMain) ticks every 150 ms; the shipped `main` uses speedActions after m10.
|
|
private def main = M09Helpers.classicMain
|
|
|
|
// What the platform should have been asked to draw for a scene.
|
|
private def expectedCalls(scene: Scene): List[List[Any]] = scene.map {
|
|
case Shape.Rect(x, y, w, h, color) => List("fillRect", x, y, w, h, color)
|
|
case Shape.Text(x, y, text, color, size, align) => List("fillText", x, y, text, color, size, align)
|
|
}
|
|
|
|
private val rect = Shape.Rect(1, 2, 3, 4, "#abcdef")
|
|
private val text = Shape.Text(5, 6, "hi", "#fff", 16, "center")
|
|
|
|
test("runIO of a pure value returns it without touching the platform") {
|
|
val p = FakePlatform()
|
|
assertEquals(Runtime.runIO(IO.pure(7), p), 7)
|
|
assertEquals(p.calls.toList, Nil)
|
|
}
|
|
|
|
test("runIO maps Draw Rect to fillRect(x, y, w, h, color)") {
|
|
val p = FakePlatform()
|
|
Runtime.runIO(IO.draw(rect), p)
|
|
assertEquals(p.calls.toList, List(List("fillRect", 1, 2, 3, 4, "#abcdef")))
|
|
}
|
|
|
|
test("runIO maps Draw Text to fillText(x, y, text, color, size, align)") {
|
|
val p = FakePlatform()
|
|
Runtime.runIO(IO.draw(text), p)
|
|
assertEquals(p.calls.toList, List(List("fillText", 5, 6, "hi", "#fff", 16, "center")))
|
|
}
|
|
|
|
test("runIO answers RandomSeed with platform.randomSeed() and continues with it") {
|
|
val p = FakePlatform(99)
|
|
assertEquals(Runtime.runIO(IO.randomSeed.map(_ + 1), p), 100L)
|
|
assertEquals(p.calls.toList, List(List("randomSeed")))
|
|
}
|
|
|
|
test("runIO performs effects in order") {
|
|
val p = FakePlatform()
|
|
Runtime.runIO(IO.sequence(List(IO.draw(rect), IO.draw(text), IO.draw(rect))), p)
|
|
assertEquals(p.calls.toList.map(_.head), List("fillRect", "fillText", "fillRect"))
|
|
}
|
|
|
|
test("runIO handles 100000 effects without overflowing the stack") {
|
|
val p = FakePlatform()
|
|
val n = 100000
|
|
Runtime.runIO(IO.sequence(List.fill(n)(IO.draw(rect))), p)
|
|
assertEquals(p.calls.length, n)
|
|
}
|
|
|
|
test("runApp asks the platform for a seed once, at start, and draws nothing before the first event") {
|
|
val p = FakePlatform()
|
|
Runtime.runApp(main, p)
|
|
assertEquals(p.calls.toList, List(List("randomSeed")))
|
|
}
|
|
|
|
test("runApp draws the initial scene on the first clock tick, then the ticked scene after 150 ms") {
|
|
val p = FakePlatform(42)
|
|
Runtime.runApp(main, p)
|
|
p.calls.clear()
|
|
p.fireTime(150)
|
|
assertEquals(
|
|
p.calls.toList,
|
|
expectedCalls(View.view(Spawn.newGame(42))) ++ expectedCalls(View.view(Spawn.tick(Spawn.newGame(42))))
|
|
)
|
|
}
|
|
|
|
test("runApp feeds key presses to the game") {
|
|
val p = FakePlatform(42)
|
|
Runtime.runApp(main, p)
|
|
p.fireTime(0)
|
|
p.calls.clear()
|
|
p.fireKey("ArrowUp")
|
|
p.fireTime(150)
|
|
val turned = Spawn.newGame(42).copy(nextDir = Direction.Up)
|
|
assertEquals(p.calls.toList, expectedCalls(View.view(turned)) ++ expectedCalls(View.view(Spawn.tick(turned))))
|
|
}
|
|
|
|
test("runApp ignores key releases and unmapped keys (no redraw)") {
|
|
val p = FakePlatform(42)
|
|
Runtime.runApp(main, p)
|
|
p.fireTime(0)
|
|
p.calls.clear()
|
|
p.fireKeyUp("ArrowUp")
|
|
p.fireKey("x")
|
|
assertEquals(p.calls.toList, Nil)
|
|
}
|