// The dial's claim ghosts (measure-ghosts.mjs, chrome-instruments.mjs): the // setting, the accumulation in cut order, the repeat ramp and its tolerance, // and that what a frame shows at t is a pure function of the schedule. import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; import { estimateSchedule } from "./deck.mjs"; import { mix } from "./chrome-deck.mjs"; import { dialFinaleHtml, dialHtml } from "./chrome-instruments.mjs"; import { frameKeys } from "./frame-keys.mjs"; import { GHOST_DEFAULTS, claimValue, ghostOpacity, ghostRamp, ghostTicks, ghostsAt, isClaim, resolveGhosts, validateGhosts } from "./measure-ghosts.mjs"; import { validateMeasureSettings } from "./measure-slice.mjs"; import { moduleById } from "./modules/index.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const MEASURE = JSON.parse(readFileSync(path.join(HERE, "fixtures", "measure-dial", "measure.json"), "utf8")); const FPS = 30; const fonts = { DeckSans: "assets/DeckSans.ttf", DeckSansBold: "assets/DeckSansBold.ttf" }; const PALETTE = { bg: "#0F1312", fg: "#EDF0EC", muted: "#98A29B", accent: "#22AB83", amber: "#E8A33F" }; const renderWith = (measure) => ({ width: 1920, height: 1080, fps: FPS, transition: 0.4, palette: PALETTE, chrome: { engine: "hyperframes", layout: "deck", deck: { dial: true, odometer: true, trail: true, qr: { show: false }, finale: { video: "side", totals: false }, ...(measure ? { measure } : {}) } }, }); function cut(render) { const timeline = Array.from({ length: 9 }, (_, i) => ({ type: "clip", id: `c${i + 1}`, video: "vidx", start: 10 * i, end: 10 * i + (i === 8 ? 14 : 8), cite: 1, section: 0, quote: "q", onscreen: { title: `Clip ${i + 1}` }, ...(i === 8 ? { layout: "finale" } : {}), })); const manifest = { slug: "m", title: "M", provenance: {}, render, timeline }; return { ...estimateSchedule(manifest), measure: MEASURE }; } const dataOf = (html, id = "dial") => JSON.parse(new RegExp(``, "s").exec(html)[1]); /** An element's property at cut second `t` from a page's init and cues, as the interpreter lands on it at rest. */ function valueAt(data, k, p, t) { let v = data.init[k]?.[p]; for (const c of data.cues) if (c.k === k && p in c.to && c.at + c.dur <= t + 1e-9) v = c.to[p]; return v; } test("the setting: off unless asked; true, false or its knobs; checked where deck.measure is", () => { assert.equal(resolveGhosts(renderWith({})), null); assert.equal(resolveGhosts(renderWith({ ghosts: false })), null); assert.deepEqual(resolveGhosts(renderWith({ ghosts: true })), { ...GHOST_DEFAULTS }); assert.deepEqual(resolveGhosts(renderWith({ ghosts: { tolerance: 1, pulse: false } })), { ...GHOST_DEFAULTS, tolerance: 1, pulse: false }); assert.deepEqual(validateGhosts(true), []); assert.deepEqual(validateGhosts({ opacity: 0.3, tolerance: 2, ramp: ["#888888", "#e8a33f", "#ff5a4e"], pulse: true }), []); assert.deepEqual(validateGhosts({ opacity: 2, tolerance: -1, ramp: ["red"], pulse: "yes", glow: 1 }), [ "render.chrome.deck.measure.ghosts.glow is not a setting of it (opacity, tolerance, ramp, pulse)", "render.chrome.deck.measure.ghosts.opacity must be a number from 0.15 to 0.9", "render.chrome.deck.measure.ghosts.tolerance must be pounds from 0 to 5", "render.chrome.deck.measure.ghosts.ramp must be 2 to 4 #hex colours (1x first), or null for the palette's", "render.chrome.deck.measure.ghosts.pulse must be true or false", ]); assert.deepEqual(validateGhosts("on"), ["render.chrome.deck.measure.ghosts must be true, false or { opacity, tolerance, ramp, pulse }"]); assert.deepEqual(validateMeasureSettings({ ghosts: { ramp: [] } }), [ "render.chrome.deck.measure.ghosts.ramp must be 2 to 4 #hex colours (1x first), or null for the palette's", ]); // The palette's ramp: muted, amber, amber half way to the needle. assert.deepEqual(ghostRamp(GHOST_DEFAULTS, { muted: "#98a29b", amber: "#e8a33f", needle: "#ff5a4e" }, mix), ["#98a29b", "#e8a33f", mix("#e8a33f", "#ff5a4e", 0.5)]); assert.deepEqual([0, 1, 2, 5].map((k) => ghostOpacity(0.5, k)), [0.5, 0.7, 0.9, 0.95]); }); test("every claim he makes leaves a tick, in cut order: stated and remembered, his own, a number, on screen", () => { assert.equal(isClaim({ kind: "stated", value: 300, t: 1 }), true); assert.equal(isClaim({ kind: "remembered", value: 390, t: 1 }), true); assert.equal(isClaim({ kind: "goal", value: 250, t: 1 }), false); assert.equal(isClaim({ kind: "stated", value: null, t: 1 }), false); assert.equal(isClaim({ kind: "stated", value: 300, t: null }), false); assert.equal(isClaim({ kind: "stated", value: 300, t: 1, speaker: "madamsavvy" }), false); const ticks = ghostTicks(MEASURE, GHOST_DEFAULTS); // The fixture's 21 claims on 15 values: 390 four times (≈390 thrice and 390 once), 300 four times (>300, 300, >300, <300). assert.equal(ticks.reduce((n, x) => n + x.claims.length, 0), 21); assert.equal(ticks.length, 15); const by = new Map(ticks.map((x) => [x.value, x.claims.map((c) => c.n)])); assert.deepEqual(by.get(390), [1, 2, 3, 4]); assert.deepEqual(by.get(300), [1, 2, 3, 4]); assert.deepEqual(by.get(291.5), [1]); // In the order first claimed. assert.deepEqual(ticks.slice(0, 3).map((x) => x.value), [240, 390, 365.7]); }); test("a repeat is a claim within the tolerance of a tick's first value; an ≈ counts as its rounded number", () => { const ev = (t, value, qualifier = null, kind = "stated") => ({ t, kind, value, qualifier, row: `r${t}` }); const m = { events: [ev(1, 300), ev(2, 300.4), ev(3, 300.6), ev(4, 389.6, "≈", "remembered"), ev(5, 390), ev(6, 299.5)] }; const half = ghostTicks(m, { tolerance: 0.5 }); assert.deepEqual(half.map((x) => [x.value, x.claims.length]), [[300, 3], [300.6, 1], [390, 2]]); assert.equal(claimValue({ value: 389.6, qualifier: "≈" }), 390); const none = ghostTicks(m, { tolerance: 0 }); assert.deepEqual(none.map((x) => x.value), [300, 300.4, 300.6, 390, 299.5]); const wide = ghostTicks(m, { tolerance: 1 }); assert.deepEqual(wide.map((x) => [x.value, x.claims.length]), [[300, 4], [390, 2]]); }); test("the ramp: 1x, 2x, 3x and up held on the last; a repeat pulses, a first claim does not", () => { const ticks = ghostTicks(MEASURE, GHOST_DEFAULTS); const t390 = ticks.find((x) => x.value === 390).claims.map((c) => c.t); const at = (t) => ghostsAt(ticks, t, { steps: 3, pulse: 1 }).find((x) => x.value === 390) ?? null; assert.equal(at(t390[0] - 0.01), null); assert.deepEqual(at(t390[0]), { value: 390, count: 1, step: 0, pulsing: false }); assert.deepEqual(at(t390[1] + 0.5), { value: 390, count: 2, step: 1, pulsing: true }); assert.deepEqual(at(t390[1] + 1.5), { value: 390, count: 2, step: 1, pulsing: false }); assert.deepEqual(at(t390[3] + 2), { value: 390, count: 4, step: 2, pulsing: false }); }); test("the dial draws them only when asked; the page is a cue list the frame keys model, and a frame at t shows the ticks at t", () => { const off = renderWith({}); const on = renderWith({ ghosts: true }); const plain = dialHtml(cut(off), off, { fonts }); assert.doesNotMatch(plain, /data-k="gt0/); const html = dialHtml(cut(on), on, { fonts }); assert.match(html, /data-k="gt0"/); // Pure: the same schedule draws the same page. assert.equal(html, dialHtml(cut(on), on, { fonts })); const schedule = cut(on); const frames = Math.round(schedule.total * FPS); const keys = frameKeys({ html, region: "dial", fps: FPS, frames, version: "t" }); assert.ok(keys, "the frame keys model the dial's page with its ghosts"); assert.ok(new Set(keys).size < frames / 2, "the ghosts at rest are reused frame to frame"); const data = dataOf(html); const ticks = ghostTicks(MEASURE, GHOST_DEFAULTS); // SEEK-SAFE: at any second, each tick's visible step is ghostsAt's -- read from init and the cues, never from playback. for (const t of [0, 5, 7.25 + 1, 21.25 + 1, 44 + 1, 58 + 1, 61]) { const want = new Map(ghostsAt(ticks, t, { steps: 3 }).map((x) => [x.value, x.step])); ticks.forEach((x, i) => { const shown = [0, 1, 2].filter((s) => (valueAt(data, `gt${i}s${s}`, "autoAlpha", t) ?? 0) > 0); assert.deepEqual(shown, want.has(x.value) ? [want.get(x.value)] : [], `tick ${x.value} at ${t}s`); }); } // The 1x opacity, faint; the third step the strongest; a pulse decays to nothing. const i390 = ticks.findIndex((x) => x.value === 390); assert.equal(valueAt(data, `gt${i390}s2`, "autoAlpha", 61), 0.9); assert.equal(valueAt(data, `gt${i390}p1`, "autoAlpha", 61), 0); assert.ok(data.cues.some((c) => c.k === `gt${i390}p1` && c.to.autoAlpha === 0.9), "the second telling pulses"); // pulse: false draws no pulse lines. const quiet = renderWith({ ghosts: { pulse: false } }); assert.doesNotMatch(dialHtml(cut(quiet), quiet, { fonts }), /data-k="gt\d+p/); }); test("the finale keeps them: every tick at its count at the cut's end, in with the finale, no pulses", () => { const on = renderWith({ ghosts: true }); const schedule = cut(on); const win = moduleById("dial-finale").window(schedule, { fps: FPS, total: schedule.total }); const html = dialFinaleHtml(schedule, on, win, { fonts }); const data = dataOf(html, "dial-finale"); const ticks = ghostTicks(MEASURE, GHOST_DEFAULTS); ticks.forEach((x, i) => { const s = Math.min(2, x.claims.length - 1); assert.equal(valueAt(data, `fgt${i}s${s}`, "autoAlpha", win.to), ghostOpacity(GHOST_DEFAULTS.opacity, s)); }); assert.ok(!data.cues.some((c) => /^fgt\d+p/.test(c.k) && c.to.autoAlpha > 0)); assert.ok(frameKeys({ html, region: "dial-finale", fps: FPS, frames: win.frames, version: "t" })); });