// The reel strip's drag and zoom arithmetic (stripDrag.mjs). import assert from "node:assert/strict"; import test from "node:test"; import { dropSlot, edgeSpeed, followVis, itemSections, labelsThatFit, sectionName } from "./stripDrag.mjs"; import { zoomAt, zoomTo } from "./benchZoom.mjs"; // a b | c d (k card) | e f, each 10 s. const ids = ["a", "b", "c", "d", "k", "e", "f"]; const items = ids.map((id, i) => ({ id, from: i * 10, to: i * 10 + 10, ...(id === "k" ? {} : { clip: {} }) })); const sectionOf = { a: 0, b: 0, c: 1, d: 1, e: 2, f: 2 }; const sections = [{ index: 0, label: null }, { index: 1, label: "Two" }, { index: 2, label: "Three" }]; const secs = itemSections(items, sectionOf); test("itemSections: a card stands in the section of the clip before it", () => { assert.deepEqual(secs, [0, 0, 1, 1, 1, 2, 2]); assert.deepEqual(itemSections([{ id: "k" }, { id: "a", clip: {} }], { a: 3 }), [3, 3], "a card first takes the next clip's"); }); test("dropSlot: the cell's halves; a seam's two sides are two sections of one slot", () => { const d1 = dropSlot(items, secs, sections, ["a"], 52); // f? no: e is 50-60, left half assert.equal(d1.over, "e"); assert.equal(d1.side, "before"); assert.equal(d1.before, "e"); assert.equal(d1.section, 2); assert.equal(d1.at, 50); assert.equal(d1.words, "→ §2 Three, before e"); // The right half of b (end of §0) and the left half of c (start of §1): both before c. const end0 = dropSlot(items, secs, sections, ["f"], 18); const start1 = dropSlot(items, secs, sections, ["f"], 21); assert.equal(end0.before, "c"); assert.equal(start1.before, "c"); assert.equal(end0.section, 0); assert.equal(start1.section, 1); assert.equal(end0.words, "→ §0 (the opening), after b"); assert.ok(end0.seam && start1.seam); // Past the last cell: the end. assert.equal(dropSlot(items, secs, sections, ["a"], 69).before, null); }); test("dropSlot: over the dragged cell nothing; where it already is, a no-op", () => { assert.equal(dropSlot(items, secs, sections, ["c"], 25), null); const here = dropSlot(items, secs, sections, ["c"], 17); // right half of b: before c (skipping c itself)... after b, §0 assert.equal(here.before, "d"); assert.equal(here.noop, false, "§0 is not c's section: it would move into §0"); const same = dropSlot(items, secs, sections, ["c"], 32); // left half of d: before d in §1, after b assert.equal(same.noop, true); assert.equal(same.words, "already here"); }); test("dropSlot: dragging a section's only clip out of it is refused in words", () => { const two = [items[0], items[1], items[2], items[5]]; const s2 = itemSections(two, { a: 0, b: 0, c: 1, e: 2 }); const r = dropSlot(two, s2, sections, ["c"], 5); assert.equal(r.refused, "§1 Two would have no clip left — remove its boundary first, or move another clip into it"); const ok = dropSlot(two, s2, sections, ["a"], 35); assert.equal(ok.refused, null); }); test("labelsThatFit: a label is shown when it clears the last one shown", () => { assert.deepEqual(labelsThatFit([{ x: 0, w: 50 }, { x: 30, w: 40 }, { x: 60, w: 40 }, { x: 101, w: 10 }]), [0, 2]); // 24 sections over 600 px with 80 px labels: one in four. const many = Array.from({ length: 24 }, (_, i) => ({ x: i * 25, w: 80 })); assert.deepEqual(labelsThatFit(many), [0, 4, 8, 12, 16, 20]); }); test("edgeSpeed: zero in the middle, up to max at either edge", () => { assert.equal(edgeSpeed(500, 1000), 0); assert.equal(edgeSpeed(0, 1000), -1.2); assert.equal(edgeSpeed(1000, 1000), 1.2); assert.ok(edgeSpeed(980, 1000) > 0 && edgeSpeed(980, 1000) < 1.2); }); test("followVis: a zoomed strip pages on when the playhead leaves it", () => { const whole = { from: 0, to: 1680 }; assert.equal(followVis(whole, whole, 900), null, "at fit nothing moves"); const vis = { from: 100, to: 200 }; assert.equal(followVis(vis, whole, 150), null); assert.deepEqual(followVis(vis, whole, 199), { from: 179, to: 279 }); assert.deepEqual(followVis(vis, whole, 1675), { from: 1580, to: 1680 }); }); test("the bench's zoom on cut seconds: the pointer's second stays put, a clip fills the strip", () => { const whole = { from: 0, to: 1680 }; const v = zoomAt(null, whole, 420, 4); assert.ok(Math.abs((420 - v.from) / (v.to - v.from) - 420 / 1680) < 1e-6); const c = zoomTo({ from: 400, to: 412 }, whole); assert.ok(c.from < 400 && c.to > 412 && c.to - c.from < 20); assert.equal(sectionName(sections, 1), "§1 Two"); });