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>
110 lines
5.0 KiB
FSharp
110 lines
5.0 KiB
FSharp
module Snake.Tests.M08Stream
|
|
|
|
open Expecto
|
|
open Snake
|
|
open Snake.Stream
|
|
|
|
[<Tests>]
|
|
let tests =
|
|
testList "M08 Stream" [
|
|
testCase "keyDowns emits keys of KeyDown events only" (fun () ->
|
|
Expect.equal (Stream.run [ KeyDown "a"; Time 5L; KeyUp "a"; KeyDown "b" ] Stream.keyDowns) [ "a"; "b" ] "keys")
|
|
|
|
testCase "keyUps emits keys of KeyUp events only" (fun () ->
|
|
Expect.equal
|
|
(Stream.run [ KeyDown "a"; Time 5L; KeyUp "a"; KeyDown "b"; KeyUp "b" ] Stream.keyUps)
|
|
[ "a"; "b" ]
|
|
"keys")
|
|
|
|
testCase "run of no events is empty" (fun () -> Expect.equal (Stream.run [] Stream.keyDowns) [] "outputs")
|
|
|
|
testCase "interval emits each multiple once as time passes" (fun () ->
|
|
Expect.equal (Stream.run [ Time 5L; Time 10L; Time 35L ] (Stream.interval 10L)) [ 10L; 20L; 30L ] "ticks")
|
|
|
|
testCase "interval emits several multiples after one big time jump" (fun () ->
|
|
Expect.equal (Stream.run [ Time 35L ] (Stream.interval 10L)) [ 10L; 20L; 30L ] "ticks")
|
|
|
|
testCase "interval 1 emits every millisecond crossed" (fun () ->
|
|
Expect.equal (Stream.run [ Time 1L; Time 3L ] (Stream.interval 1L)) [ 1L; 2L; 3L ] "ticks")
|
|
|
|
testCase "interval ignores key events" (fun () ->
|
|
Expect.equal (Stream.run [ KeyDown "a"; KeyUp "a" ] (Stream.interval 10L)) [] "ticks")
|
|
|
|
testCase "map transforms every output" (fun () ->
|
|
Expect.equal
|
|
(Stream.keyDowns |> Stream.map (fun k -> k + "!") |> Stream.run [ KeyDown "a"; KeyDown "b" ])
|
|
[ "a!"; "b!" ]
|
|
"outputs")
|
|
|
|
testCase "filter keeps outputs satisfying the predicate" (fun () ->
|
|
Expect.equal
|
|
(Stream.keyDowns |> Stream.filter (fun k -> k <> "a") |> Stream.run [ KeyDown "a"; KeyDown "b" ])
|
|
[ "b" ]
|
|
"outputs")
|
|
|
|
testCase "choose transforms outputs and drops Nones" (fun () ->
|
|
Expect.equal
|
|
(Stream.keyDowns
|
|
|> Stream.choose (fun k -> if k = "a" then None else Some(k.ToUpperInvariant()))
|
|
|> Stream.run [ KeyDown "a"; KeyDown "b" ])
|
|
[ "B" ]
|
|
"outputs")
|
|
|
|
testCase "merge puts outputs of the first stream before the second at the same event" (fun () ->
|
|
Expect.equal
|
|
(Stream.merge (Stream.map (fun k -> "first " + k) Stream.keyDowns) (Stream.map (fun k -> "second " + k) Stream.keyDowns)
|
|
|> Stream.run [ KeyDown "x" ])
|
|
[ "first x"; "second x" ]
|
|
"outputs")
|
|
|
|
testCase "merge interleaves sources by event order" (fun () ->
|
|
Expect.equal
|
|
(Stream.merge (Stream.interval 10L) (Stream.map (fun _ -> -1L) Stream.keyDowns)
|
|
|> Stream.run [ Time 10L; KeyDown "a"; Time 20L ])
|
|
[ 10L; -1L; 20L ]
|
|
"outputs")
|
|
|
|
testCase "scan emits each new accumulator, not the initial value" (fun () ->
|
|
Expect.equal
|
|
(Stream.keyDowns
|
|
|> Stream.scan (fun n _ -> n + 1) 0
|
|
|> Stream.run [ KeyDown "a"; KeyUp "a"; KeyDown "b"; KeyDown "c" ])
|
|
[ 1; 2; 3 ]
|
|
"outputs")
|
|
|
|
testCase "scan passes the output to the folding function" (fun () ->
|
|
Expect.equal
|
|
(Stream.keyDowns |> Stream.scan (+) "" |> Stream.run [ KeyDown "a"; KeyDown "b" ])
|
|
[ "a"; "ab" ]
|
|
"outputs")
|
|
|
|
testCase "startWith emits the value once, before the outputs of the first step" (fun () ->
|
|
Expect.equal
|
|
(Stream.keyDowns |> Stream.startWith "init" |> Stream.run [ KeyDown "a"; KeyDown "b" ])
|
|
[ "init"; "a"; "b" ]
|
|
"outputs")
|
|
|
|
testCase "startWith emits at the first event even if that step has no outputs" (fun () ->
|
|
Expect.equal
|
|
(Stream.keyDowns |> Stream.startWith "init" |> Stream.run [ Time 1L; KeyDown "a" ])
|
|
[ "init"; "a" ]
|
|
"outputs")
|
|
|
|
testCase "startWith emits nothing when no event ever arrives" (fun () ->
|
|
Expect.equal (Stream.keyDowns |> Stream.startWith "init" |> Stream.run []) [] "outputs")
|
|
|
|
testCase "step feeds one event and returns its outputs and the next stream" (fun () ->
|
|
let out, next = Stream.step (Time 25L) (Stream.interval 10L)
|
|
Expect.equal out [ 10L; 20L ] "first step"
|
|
Expect.equal (fst (Stream.step (Time 30L) next)) [ 30L ] "next stream remembers the last time")
|
|
|
|
testCase "running the same stream value twice gives the same outputs" (fun () ->
|
|
let s =
|
|
Stream.merge (Stream.interval 10L) (Stream.map (fun _ -> 100L) Stream.keyDowns)
|
|
|> Stream.scan (+) 0L
|
|
|
|
let events = [ Time 25L; KeyDown "a"; Time 40L ]
|
|
Expect.equal (Stream.run events s) (Stream.run events s) "same outputs"
|
|
Expect.equal (Stream.run events s) [ 10L; 30L; 130L; 160L; 200L ] "outputs")
|
|
]
|