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