// THE LEFT PANEL'S RULES — THE one copy of what a cut's flips // (`render.chrome.flips`) and threads (`render.chrome.threads`, each clip's // `thread`) may be: their limits and their validators (release 28, M4/M5: // moved here from umtool/report-to-video/flips.mjs and threads.mjs, which // re-export them unchanged, so the flips and threads draft tasks check // exactly what the build refuses). // // Plain JS (with ./panels.d.mts for TypeScript) for the reason // ./verdicts.mjs is: umtool's report-to-video scripts are `.mjs` run by bare // `node`, and the draft tasks are TypeScript. PURE: no fs. // // The two share the frame's left side: a cut has one or the other // (umtool's deck.mjs validateChrome refuses both). import { VERDICTS } from "./verdicts.mjs"; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); // ─── flips ─── /** The limits: pairs, a topic's, a `when`'s and a side's words' characters. */ export const FLIPS_LIMITS = Object.freeze({ pairs: Object.freeze([1, 24]), topic: 32, when: 18, words: 110 }); const ID_RE = /^[A-Za-z0-9][A-Za-z0-9_-]{0,47}$/; const oneLineMax = (v, max) => typeof v === "string" && v.trim() !== "" && !/[\n\r]/.test(v) && v.length <= max; /** Is the flips panel on: a `render.chrome.flips` with at least one pair. */ export function flipsOn(render) { const f = render?.chrome?.flips; return isObj(f) && Array.isArray(f.pairs) && f.pairs.length > 0; } /** * Every reason `render.chrome.flips` cannot be built, as sentences. Shape * only; `validateFlipEntries` checks the pairs against the timeline. * * @returns {string[]} */ export function validateFlips(f, where = "render.chrome.flips") { if (f === undefined) return []; if (!isObj(f)) return [`${where} must be an object`]; const errors = []; for (const k of Object.keys(f)) if (k !== "pairs") errors.push(`${where}.${k} is not a flips setting`); const [lo, hi] = FLIPS_LIMITS.pairs; if (!Array.isArray(f.pairs) || f.pairs.length < lo || f.pairs.length > hi) return [...errors, `${where}.pairs must be ${lo} to ${hi} pairs`]; const ids = new Set(); f.pairs.forEach((p, i) => { const w = `${where}.pairs[${i}]`; if (!isObj(p)) { errors.push(`${w} must be an object`); return; } for (const k of Object.keys(p)) if (!["id", "topic", "then", "now"].includes(k)) errors.push(`${w}.${k} is not a pair key`); if (typeof p.id !== "string" || !ID_RE.test(p.id)) errors.push(`${w}.id must be letters, digits, _ or -`); else if (ids.has(p.id)) errors.push(`${w}.id ${p.id} is listed twice`); else ids.add(p.id); if (!oneLineMax(p.topic, FLIPS_LIMITS.topic)) errors.push(`${w}.topic must be one line of at most ${FLIPS_LIMITS.topic} characters`); for (const side of ["then", "now"]) { const s = p[side]; const ws = `${w}.${side}`; if (!isObj(s)) { errors.push(`${ws} must be an object`); continue; } for (const k of Object.keys(s)) if (!["entry", "when", "words"].includes(k)) errors.push(`${ws}.${k} is not a side key`); if (typeof s.entry !== "string" || !s.entry) errors.push(`${ws}.entry must name a timeline entry`); if (!oneLineMax(s.when, FLIPS_LIMITS.when)) errors.push(`${ws}.when must be one line of at most ${FLIPS_LIMITS.when} characters`); if (!oneLineMax(s.words, FLIPS_LIMITS.words)) errors.push(`${ws}.words must be one line of at most ${FLIPS_LIMITS.words} characters`); } }); return errors; } /** * The pairs against the timeline: each side names an entry of the cut, a clip * (not a teaser), the THEN side before the NOW side, and no entry in two pairs. * * @returns {string[]} */ export function validateFlipEntries(manifest) { const render = manifest?.render ?? {}; if (!flipsOn(render)) return []; const errors = []; const at = new Map((manifest.timeline ?? []).map((e, i) => [e?.id, { e, i }])); const used = new Map(); render.chrome.flips.pairs.forEach((p, i) => { const w = `render.chrome.flips.pairs[${i}] (${p?.id ?? "?"})`; const idx = {}; for (const side of ["then", "now"]) { const id = p?.[side]?.entry; const hit = at.get(id); if (!hit) { errors.push(`${w}.${side}.entry ${id} is not in the timeline`); continue; } if (hit.e.type === "teaser") errors.push(`${w}.${side}.entry ${id} is a teaser, not a clip`); if (used.has(id)) errors.push(`${w}.${side}.entry ${id} is already in pair ${used.get(id)}`); used.set(id, p.id); idx[side] = hit.i; } if (idx.then !== undefined && idx.now !== undefined && idx.then >= idx.now) errors.push(`${w}: its THEN entry must come before its NOW entry`); }); return errors; } // ─── threads ─── /** The limits: how many threads, a label's and an outcome label's characters. */ export const THREADS_LIMITS = Object.freeze({ threads: Object.freeze([1, 8]), label: 32, outcome: 24 }); /** A thread id: letters, digits and `_ -`. */ const THREAD_ID_RE = /^[A-Za-z0-9][A-Za-z0-9_-]{0,47}$/; const oneLine = (v) => typeof v === "string" && v.trim() !== "" && !/[\n\r]/.test(v); /** Is the rail on: a `render.chrome.threads` with at least one thread. */ export function threadsOn(render) { const t = render?.chrome?.threads; return isObj(t) && Array.isArray(t.list) && t.list.length > 0; } /** * Every reason `render.chrome.threads` cannot be built, as sentences (empty: * it can). validateChrome calls this, so umtool's writer and the build refuse * the same things. * * @returns {string[]} */ export function validateThreads(t, where = "render.chrome.threads") { if (t === undefined) return []; if (!isObj(t)) return [`${where} must be an object`]; const errors = []; for (const k of Object.keys(t)) if (k !== "list") errors.push(`${where}.${k} is not a threads setting`); const [lo, hi] = THREADS_LIMITS.threads; if (!Array.isArray(t.list) || t.list.length < lo || t.list.length > hi) { return [...errors, `${where}.list must be ${lo} to ${hi} threads`]; } const seen = new Set(); t.list.forEach((th, i) => { const w = `${where}.list[${i}]`; if (!isObj(th)) { errors.push(`${w} must be an object`); return; } for (const k of Object.keys(th)) if (!["id", "label", "outcome"].includes(k)) errors.push(`${w}.${k} is not a thread key`); if (typeof th.id !== "string" || !THREAD_ID_RE.test(th.id)) errors.push(`${w}.id must be letters, digits, _ or -`); else if (seen.has(th.id)) errors.push(`${w}.id ${th.id} is listed twice`); else seen.add(th.id); if (!oneLine(th.label) || th.label.length > THREADS_LIMITS.label) { errors.push(`${w}.label must be one line of at most ${THREADS_LIMITS.label} characters`); } if (th.outcome !== undefined) { const o = th.outcome; if (!isObj(o)) { errors.push(`${w}.outcome must be an object`); return; } for (const k of Object.keys(o)) if (!["verdict", "label"].includes(k)) errors.push(`${w}.outcome.${k} is not an outcome key`); if (!VERDICTS.includes(o.verdict)) errors.push(`${w}.outcome.verdict must be one of ${VERDICTS.join(", ")}`); if (o.label !== undefined && (!oneLine(o.label) || o.label.length > THREADS_LIMITS.outcome)) { errors.push(`${w}.outcome.label must be one line of at most ${THREADS_LIMITS.outcome} characters`); } } }); return errors; } /** The thread an entry belongs to, or null. */ export function threadOf(entry) { return typeof entry?.thread === "string" && entry.thread ? entry.thread : null; } /** * Every `thread` in the timeline, checked against the list: it names a listed * thread, it is not on a teaser, and every listed thread has a clip. The * build refuses with these before it fetches. * * @returns {string[]} */ export function validateThreadEntries(manifest) { const errors = []; const render = manifest?.render ?? {}; const listed = threadsOn(render) ? new Set(render.chrome.threads.list.map((t) => t?.id)) : new Set(); const used = new Set(); (manifest?.timeline ?? []).forEach((e, i) => { if (e?.thread === undefined || e?.thread === null) return; // A post item's `thread` OBJECT is its thread-read knobs (post-thread.mjs validatePostThread), not a rail thread. if (e?.type === "post" && typeof e.thread === "object" && !Array.isArray(e.thread)) return; const where = `timeline[${i}] (${e.id ?? "?"}).thread`; if (typeof e.thread !== "string" || !e.thread) { errors.push(`${where} must be a thread id`); return; } if (e.type === "teaser") { errors.push(`${where}: a teaser belongs to no thread`); return; } if (!listed.has(e.thread)) { errors.push(`${where} ${e.thread} is not in render.chrome.threads.list`); return; } used.add(e.thread); }); for (const id of listed) if (!used.has(id)) errors.push(`render.chrome.threads: thread ${id} has no clip`); return errors; }