Files
functional-programming-snake/js/test/m08-stream.test.js
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

74 lines
3.6 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { keyDowns, keyUps, interval, map, filter, filterMap, merge, scan, startWith, run } from '../src/stream.js'
import { freeze as deepFreeze } from './helpers.js'
const time = (t) => ({ tag: 'Time', t })
const down = (key) => ({ tag: 'KeyDown', key })
const up = (key) => ({ tag: 'KeyUp', key })
const inputs = (...xs) => deepFreeze(xs)
test('m08 keyDowns emits keys of KeyDown inputs only', () =>
assert.deepStrictEqual(run(keyDowns, inputs(down('a'), time(5), up('a'), down('b'))), ['a', 'b']))
test('m08 keyUps emits keys of KeyUp inputs only', () =>
assert.deepStrictEqual(run(keyUps, inputs(down('a'), time(5), up('a'), down('b'), up('b'))), ['a', 'b']))
test('m08 run of no inputs is empty', () =>
assert.deepStrictEqual(run(keyDowns, []), []))
test('m08 interval emits each multiple once as time passes', () =>
assert.deepStrictEqual(run(interval(10), inputs(time(5), time(10), time(35))), [10, 20, 30]))
test('m08 interval emits several multiples after one big time jump', () =>
assert.deepStrictEqual(run(interval(10), inputs(time(35))), [10, 20, 30]))
test('m08 interval(1) emits every millisecond crossed', () =>
assert.deepStrictEqual(run(interval(1), inputs(time(1), time(3))), [1, 2, 3]))
test('m08 interval ignores key inputs', () =>
assert.deepStrictEqual(run(interval(10), inputs(down('a'), up('a'))), []))
test('m08 map transforms every output', () =>
assert.deepStrictEqual(run(map(keyDowns, (k) => k + '!'), inputs(down('a'), down('b'))), ['a!', 'b!']))
test('m08 filter keeps outputs satisfying the predicate', () =>
assert.deepStrictEqual(run(filter(keyDowns, (k) => k !== 'a'), inputs(down('a'), down('b'))), ['b']))
test('m08 filterMap transforms outputs and drops nulls', () =>
assert.deepStrictEqual(
run(filterMap(keyDowns, (k) => (k === 'a' ? null : k.toUpperCase())), inputs(down('a'), down('b'))),
['B']))
test('m08 merge puts outputs of the first stream before the second at the same input', () =>
assert.deepStrictEqual(
run(merge(map(keyDowns, (k) => 'first ' + k), map(keyDowns, (k) => 'second ' + k)), inputs(down('x'))),
['first x', 'second x']))
test('m08 merge interleaves sources by input order', () =>
assert.deepStrictEqual(
run(merge(interval(10), map(keyDowns, () => -1)), inputs(time(10), down('a'), time(20))),
[10, -1, 20]))
test('m08 scan emits each new accumulator, not the initial value', () =>
assert.deepStrictEqual(run(scan(keyDowns, (n) => n + 1, 0), inputs(down('a'), up('a'), down('b'), down('c'))), [1, 2, 3]))
test('m08 scan passes the output to the folding function', () =>
assert.deepStrictEqual(run(scan(keyDowns, (acc, k) => acc + k, ''), inputs(down('a'), down('b'))), ['a', 'ab']))
test('m08 startWith emits the value once, before the outputs of the first step', () =>
assert.deepStrictEqual(run(startWith(keyDowns, 'init'), inputs(down('a'), down('b'))), ['init', 'a', 'b']))
test('m08 startWith emits at the first input even if that step has no outputs', () =>
assert.deepStrictEqual(run(startWith(keyDowns, 'init'), inputs(time(1), down('a'))), ['init', 'a']))
test('m08 startWith emits nothing when no input ever arrives', () =>
assert.deepStrictEqual(run(startWith(keyDowns, 'init'), []), []))
test('m08 running the same stream value twice gives the same outputs', () => {
const s = scan(merge(interval(10), map(keyDowns, () => 100)), (a, x) => a + x, 0)
const xs = inputs(time(25), down('a'), time(40))
assert.deepStrictEqual(run(s, xs), run(s, xs))
assert.deepStrictEqual(run(s, xs), [10, 30, 130, 160, 200])
})