import { test } from "node:test"; import assert from "node:assert/strict"; import { agree, changesOnly, dropMisreads, nearest, ratioOf } from "./series"; const P = (pairs: [number, number][]) => pairs.map(([t, v]) => ({ t, v })); test("ratioOf and agree", () => { assert.equal(ratioOf(10, 20), 2); assert.equal(ratioOf(0, 0), 1); assert.equal(ratioOf(0, 5), Infinity); assert.ok(agree(9, 7, { ratio: 1.1, absolute: 2 })); assert.ok(!agree(9, 6, { ratio: 1.1, absolute: 2 })); }); test("a single frame that jumps 100× is a misread when its neighbours agree", () => { const { kept, dropped } = dropMisreads(P([[0, 20000], [10, 2000000], [20, 21000]]), { ratio: 3 }); assert.deepEqual(dropped, P([[10, 2000000]])); assert.equal(kept.length, 2); }); test("a real change is followed by points that agree with it, so it stays", () => { const { dropped } = dropMisreads(P([[0, 20000], [10, 41000], [20, 41000], [30, 40500]]), { ratio: 1.5 }); assert.deepEqual(dropped, []); }); test("an end point that disagrees hard with the two next to it is a misread", () => { assert.deepEqual(dropMisreads(P([[0, 5], [10, 500], [20, 510]]), { ratio: 2 }).dropped, P([[0, 5]])); assert.deepEqual(dropMisreads(P([[0, 500], [10, 510], [20, 50000]]), { ratio: 2 }).dropped, P([[20, 50000]])); // A series that ends at 0 ended there (a bust); a first reading before a change stays. assert.deepEqual(dropMisreads(P([[0, 500], [10, 510], [20, 0]]), { ratio: 2 }).dropped, []); assert.deepEqual(dropMisreads(P([[0, 20000], [10, 41000], [20, 41000]]), { ratio: 1.5 }).dropped, []); }); test("changesOnly and nearest", () => { assert.deepEqual(changesOnly(P([[0, 1], [10, 1], [20, 2], [30, 2], [40, 1]])), P([[0, 1], [20, 2], [40, 1]])); assert.deepEqual(nearest(P([[0, 1], [50, 2], [90, 3]]), 60, 20), { t: 50, v: 2 }); assert.equal(nearest(P([[0, 1]]), 60, 20), null); }); test("dropExcursions: a mid-hand dip that returns within the window is set aside; a level left for good stays", async () => { const { dropExcursions } = await import("./series"); const dip = P([[0, 345000], [10, 345000], [20, 296000], [30, 296000], [40, 153000], [50, 153000], [60, 344900], [70, 140000], [80, 344900], [90, 345000]]); const r = dropExcursions(dip, { returnRatio: 1.1, windowS: 90 }); assert.deepEqual(r.dropped.map((p) => p.t), [20, 30, 40, 50, 70]); assert.deepEqual(r.kept.map((p) => p.v), [345000, 345000, 344900, 344900, 345000]); // A real double-up holds: nothing to return to. assert.deepEqual(dropExcursions(P([[0, 20000], [10, 41000], [20, 41000]]), { returnRatio: 1.1, windowS: 90 }).dropped, []); // A return after the window is a change and a change back. assert.deepEqual(dropExcursions(P([[0, 100], [10, 300], [200, 100]]), { returnRatio: 1.1, windowS: 90 }).dropped, []); });