// The Play view's frame queue (frameQueue.mjs): bounded, in presentation // order, the frame due at a time, and every frame closed when it leaves. // // Run with: node --test lib/report/frameQueue.test.mjs import assert from "node:assert/strict"; import test from "node:test"; import { FrameQueue } from "./frameQueue.mjs"; const made = []; const frame = (ms) => { const f = { timestamp: ms * 1000, closed: false, close() { if (this.closed) throw new Error(`closed twice: ${ms}`); this.closed = true; } }; made.push(f); return f; }; const open = () => made.filter((f) => !f.closed).length; test("bounded: past `max` the oldest frame goes, closed", () => { made.length = 0; const q = new FrameQueue({ max: 4 }); for (let i = 0; i < 10; i += 1) q.push(frame(i * 33)); assert.equal(q.size, 4); assert.equal(open(), 4); assert.deepEqual(q.frames.map((f) => f.timestamp / 1000), [198, 231, 264, 297]); assert.equal(q.room, false); q.clear(); assert.equal(open(), 0); }); test("in presentation order whatever the decode order; a repeat replaces (and closes) the old one", () => { made.length = 0; const q = new FrameQueue({ max: 8 }); for (const ms of [0, 100, 33, 66, 133]) q.push(frame(ms)); assert.deepEqual(q.frames.map((f) => f.timestamp / 1000), [0, 33, 66, 100, 133]); q.push(frame(66)); assert.equal(q.size, 5); assert.equal(open(), 5); q.clear(); }); test("the frame due: the latest at or before the time, kept; older ones closed; none before the first", () => { made.length = 0; const q = new FrameQueue({ max: 8 }); for (const ms of [100, 133, 166, 200]) q.push(frame(ms)); assert.equal(q.frameAt(90_000), null); assert.equal(open(), 4); assert.equal(q.frameAt(150_000).timestamp, 133_000); assert.equal(open(), 3); // Paused: the same frame again, nothing closed. assert.equal(q.frameAt(150_000).timestamp, 133_000); assert.equal(open(), 3); // Past the last: the last frame (a held clip's last frame), kept. assert.equal(q.frameAt(10_000_000).timestamp, 200_000); assert.equal(q.size, 1); assert.equal(open(), 1); q.dropBefore(1e12); assert.equal(q.size, 1, "dropBefore keeps the newest it has"); q.clear(); assert.equal(open(), 0); });