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>
93 lines
3.2 KiB
Scala
93 lines
3.2 KiB
Scala
package snake
|
|
|
|
import Stream.{keyDowns, keyUps, interval}
|
|
|
|
class M08StreamSuite extends munit.FunSuite:
|
|
private def time(t: Long): Event = Event.Time(t)
|
|
private def down(key: String): Event = Event.KeyDown(key)
|
|
private def up(key: String): Event = Event.KeyUp(key)
|
|
|
|
test("keyDowns emits keys of KeyDown events only") {
|
|
assertEquals(keyDowns.run(List(down("a"), time(5), up("a"), down("b"))), List("a", "b"))
|
|
}
|
|
|
|
test("keyUps emits keys of KeyUp events only") {
|
|
assertEquals(keyUps.run(List(down("a"), time(5), up("a"), down("b"), up("b"))), List("a", "b"))
|
|
}
|
|
|
|
test("run of no events is empty") {
|
|
assertEquals(keyDowns.run(Nil), Nil)
|
|
}
|
|
|
|
test("interval emits each multiple once as time passes") {
|
|
assertEquals(interval(10).run(List(time(5), time(10), time(35))), List(10L, 20L, 30L))
|
|
}
|
|
|
|
test("interval emits several multiples after one big time jump") {
|
|
assertEquals(interval(10).run(List(time(35))), List(10L, 20L, 30L))
|
|
}
|
|
|
|
test("interval(1) emits every millisecond crossed") {
|
|
assertEquals(interval(1).run(List(time(1), time(3))), List(1L, 2L, 3L))
|
|
}
|
|
|
|
test("interval ignores key events") {
|
|
assertEquals(interval(10).run(List(down("a"), up("a"))), Nil)
|
|
}
|
|
|
|
test("map transforms every output") {
|
|
assertEquals(keyDowns.map(_ + "!").run(List(down("a"), down("b"))), List("a!", "b!"))
|
|
}
|
|
|
|
test("filter keeps outputs satisfying the predicate") {
|
|
assertEquals(keyDowns.filter(_ != "a").run(List(down("a"), down("b"))), List("b"))
|
|
}
|
|
|
|
test("filterMap transforms outputs and drops Nones") {
|
|
assertEquals(
|
|
keyDowns.filterMap(k => if k == "a" then None else Some(k.toUpperCase)).run(List(down("a"), down("b"))),
|
|
List("B")
|
|
)
|
|
}
|
|
|
|
test("merge puts outputs of the first stream before the second at the same event") {
|
|
assertEquals(
|
|
keyDowns.map("first " + _).merge(keyDowns.map("second " + _)).run(List(down("x"))),
|
|
List("first x", "second x")
|
|
)
|
|
}
|
|
|
|
test("merge interleaves sources by event order") {
|
|
assertEquals(
|
|
interval(10).merge(keyDowns.map(_ => -1L)).run(List(time(10), down("a"), time(20))),
|
|
List(10L, -1L, 20L)
|
|
)
|
|
}
|
|
|
|
test("scan emits each new accumulator, not the initial value") {
|
|
assertEquals(keyDowns.scan(0)((n, _) => n + 1).run(List(down("a"), up("a"), down("b"), down("c"))), List(1, 2, 3))
|
|
}
|
|
|
|
test("scan passes the output to the folding function") {
|
|
assertEquals(keyDowns.scan("")(_ + _).run(List(down("a"), down("b"))), List("a", "ab"))
|
|
}
|
|
|
|
test("startWith emits the value once, before the outputs of the first step") {
|
|
assertEquals(keyDowns.startWith("init").run(List(down("a"), down("b"))), List("init", "a", "b"))
|
|
}
|
|
|
|
test("startWith emits at the first event even if that step has no outputs") {
|
|
assertEquals(keyDowns.startWith("init").run(List(time(1), down("a"))), List("init", "a"))
|
|
}
|
|
|
|
test("startWith emits nothing when no event ever arrives") {
|
|
assertEquals(keyDowns.startWith("init").run(Nil), Nil)
|
|
}
|
|
|
|
test("running the same stream value twice gives the same outputs") {
|
|
val s = interval(10).merge(keyDowns.map(_ => 100L)).scan(0L)(_ + _)
|
|
val events = List(time(25), down("a"), time(40))
|
|
assertEquals(s.run(events), s.run(events))
|
|
assertEquals(s.run(events), List(10L, 30L, 130L, 160L, 200L))
|
|
}
|