import { test } from "node:test";import assert from "node:assert/strict";import { h, load } from "../../lib/harness.mjs";const { movingAverage } = await load();// Inputs: numbers are present readings, `null` marks an absent one.const input = (xs) => xs.map((x) => (x === null ? h.none() : h.some(x)));const run = (xs, window) => { const r = h.res(() => movingAverage(input(xs), window)); return r.ok ? { ok: true, value: Array.isArray(r.value) ? r.value.map(h.opt) : { notAnArray: r.value } } : r;};const out = (xs) => ({ ok: true, value: xs.map((x) => (x === null ? { some: false } : { some: true, value: x })) });const fail = (error) => ({ ok: false, error });test("basic window of 3", () => { assert.deepEqual(run([1, 2, 3, 4, 5], 3), out([1, 1.5, 2, 3, 4]));});test("window of 1 copies the readings", () => { assert.deepEqual(run([4, null, -2], 1), out([4, null, -2]));});test("divides by the number of present readings", () => { assert.deepEqual(run([2, null, 4, null, null, 10], 3), out([2, 2, 3, 4, 4, 10]));});test("zero is a real reading", () => { assert.deepEqual(run([0, null, 0], 2), out([0, 0, 0])); assert.deepEqual(run([10, 0, 0], 3), out([10, 5, 10 / 3])); assert.deepEqual(run([0], 5), out([0]));});test("no readings in range gives absent", () => { assert.deepEqual(run([null, null, 6, null, null, null], 2), out([null, null, 6, 6, null, null])); assert.deepEqual(run([null, null], 3), out([null, null]));});test("window longer than the series", () => { assert.deepEqual(run([3, 5, 7], 10), out([3, 4, 5]));});test("empty series", () => { assert.deepEqual(run([], 3), out([]));});test("negative readings", () => { assert.deepEqual(run([-4, 4, -1, -3], 2), out([-4, 0, 1.5, -2]));});test("window must be a positive integer", () => { for (const w of [0, -1, 2.5, Number.NaN, Number.POSITIVE_INFINITY]) { assert.deepEqual(run([1, 2], w), fail({ kind: "invalid_window" }), String(w)); }});test("non-finite readings", () => { assert.deepEqual(run([1, Number.NaN, 2], 2), fail({ kind: "not_finite", index: 1 })); assert.deepEqual(run([null, 0, Number.POSITIVE_INFINITY, Number.NaN], 2), fail({ kind: "not_finite", index: 2 })); assert.deepEqual(run([Number.NEGATIVE_INFINITY], 1), fail({ kind: "not_finite", index: 0 }));});test("window is checked before readings", () => { assert.deepEqual(run([Number.NaN], 0), fail({ kind: "invalid_window" }));});test("input is not modified", () => { const values = input([1, null, 3]); const copy = [...values]; h.res(() => movingAverage(values, 2)); assert.equal(values.length, 3); values.forEach((v, i) => assert.equal(v, copy[i]));});