// The bench waveform's zoom (benchZoom.mjs): a zoom keeps the pointer's second // under the pointer, never past the window or closer than MIN_ZOOM_SPAN; a pan // stops at the window's edges; fit is the whole; the ruler's round ticks; the // pauses between words. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { MIN_ZOOM_SPAN, centreOn, clampVis, isFit, panBy, quietPoints, rulerFor, tickLabel, wheelFactor, zoomAt, zoomTo } from "./benchZoom.mjs"; const W = { from: 10, to: 40 }; const near = (a, b, what, tol = 1e-3) => assert.ok(Math.abs(a - b) <= tol, `${what}: ${a} vs ${b}`); test("zoomAt: the second under the pointer stays there; never closer than MIN_ZOOM_SPAN, never wider than the window", () => { const v = zoomAt(null, W, 16, 2); near(v.to - v.from, 15, "half the window"); near((16 - v.from) / (v.to - v.from), (16 - 10) / 30, "the pointer's fraction"); const out = zoomAt(v, W, 16, 1 / 4); assert.deepEqual(out, W, "out past the whole: the whole"); assert.equal(isFit(out, W), true); let z = W; for (let i = 0; i < 40; i += 1) z = zoomAt(z, W, 25, 3); near(z.to - z.from, MIN_ZOOM_SPAN, "the closest"); assert.ok(z.from <= 25 && z.to >= 25); // Near an edge: kept inside the window. const e = zoomAt({ from: 10, to: 12 }, W, 10, 1 / 2); assert.ok(e.from >= 10 && e.to <= 40); }); test("panBy, centreOn, zoomTo, clampVis: inside the window, the width kept", () => { assert.deepEqual(panBy({ from: 20, to: 25 }, W, 3), { from: 23, to: 28 }); assert.deepEqual(panBy({ from: 20, to: 25 }, W, 100), { from: 35, to: 40 }); assert.deepEqual(panBy({ from: 20, to: 25 }, W, -100), { from: 10, to: 15 }); assert.deepEqual(centreOn({ from: 20, to: 24 }, W, 30), { from: 28, to: 32 }); assert.deepEqual(zoomTo({ from: 20, to: 30 }, W), { from: 18.5, to: 31.5 }); assert.deepEqual(clampVis({ from: 5, to: 5.01 }, W), { from: 10, to: 10.25 }); assert.equal(isFit(null, W), true); assert.equal(isFit({ from: 10, to: 39 }, W), false); }); test("wheelFactor: a notch up zooms in, down out; a trackpad's small delta a little", () => { assert.ok(wheelFactor(-100) > 1); assert.ok(wheelFactor(100) < 1); near(wheelFactor(-100) * wheelFactor(100), 1, "symmetric"); assert.ok(Math.abs(wheelFactor(-4) - 1) < 0.02); assert.ok(wheelFactor(-3, 1) > 1.1, "a line-mode notch"); }); test("rulerFor / tickLabel: round steps, finer as it zooms; labels to the step's precision", () => { assert.deepEqual(rulerFor({ from: 0, to: 9 }), { step: 1, ticks: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] }); const r = rulerFor({ from: 2.53, to: 3.4 }); assert.equal(r.step, 0.1); assert.deepEqual(r.ticks, [2.6, 2.7, 2.8, 2.9, 3, 3.1, 3.2, 3.3, 3.4]); assert.equal(rulerFor({ from: 0, to: 600 }).step, 60); assert.equal(tickLabel(65, 5), "1:05"); assert.equal(tickLabel(65.25, 0.05), "1:05.25"); assert.equal(tickLabel(3.5, 0.5), "0:03.5"); assert.equal(tickLabel(3725, 60), "1:02:05"); }); test("quietPoints: the middle of each pause between words, and of each silence, in the span, once", () => { const words = [{ start: 1, end: 1.4 }, { start: 1.6, end: 2 }, { start: 2, end: 2.5 }, { start: 3.1, end: 3.3 }]; assert.deepEqual(quietPoints({ words }, 0, 10), [1.5, 2, 2.8]); assert.deepEqual(quietPoints({ words, silences: [{ from: 2.7, to: 2.91 }, { from: 5, to: 6 }] }, 0, 5.4), [1.5, 2, 2.8]); assert.deepEqual(quietPoints({ silences: [{ from: 5, to: 6 }] }, 0, 10), [5.5]); assert.deepEqual(quietPoints({}, 0, 10), []); });