// TWO TAKES SIDE BY SIDE -- of one project, or of a project and its variants // (UI notes 2026-10-30, item 19; variantFork.mjs): what each is, and what // differs between them. // // a take ref `` (of the project asked about) or `:` // -- a take id has no colon, and neither has a project id // the facts label, project, length, built, window, style, quick // the cut lib/report/manifest-diff.mjs over the two manifests (entries // added, removed, moved, their windows and fields), one line each // the look every leaf of the manifests outside the timeline (render.*, // title, subtitle …) that differs, a before and an after // // The manifests are the ones each take was rendered from (takes// // video.manifest.json), a draft's resolved from its patch (takeManifest.mjs). // Reads only. // // Plain ESM: the CLI runs it under bare node. import { readFile } from "node:fs/promises"; import path from "node:path"; import { diffManifests, formatChange } from "./manifest-diff.mjs"; import { MANIFEST } from "./takeMaker.mjs"; import { isTakeId, listTakes, takesDirOf } from "./takes.mjs"; const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); /** How many look differences are listed at most. */ export const LOOK_LIMIT = 200; /** * `` or `:` as `{ project, take }` (project defaulting to `defaultProject`); null when malformed. * @param {unknown} s * @param {string} defaultProject */ export function parseTakeRef(s, defaultProject) { const v = String(s ?? "").trim(); if (!v) return null; const i = v.lastIndexOf(":"); const project = i >= 0 ? v.slice(0, i) : defaultProject; const take = i >= 0 ? v.slice(i + 1) : v; if (!project || !isTakeId(take)) return null; return { project, take }; } /** A ref back to its string: `:`. */ export const takeRefOf = (r) => `${r.project}:${r.take}`; /** * Every leaf of `obj` as `[dotted path, value]`; arrays are leaves (their JSON). * @param {unknown} obj * @param {string} [prefix] * @returns {Array<[string, unknown]>} */ export function leaves(obj, prefix = "") { if (!isObj(obj)) return prefix ? [[prefix, obj]] : []; const keys = Object.keys(obj); if (!keys.length) return prefix ? [[prefix, {}]] : []; return keys.flatMap((k) => leaves(/** @type {any} */ (obj)[k], prefix ? `${prefix}.${k}` : k)); } /** A value for a difference line: short. */ export function shortValue(v) { if (v === undefined) return "—"; const s = typeof v === "string" ? v : JSON.stringify(v); return s.length > 80 ? `${s.slice(0, 79)}…` : s; } /** * What differs between two manifests outside the timeline: `[{ path, a, b }]`, sorted by path. * @param {Record} a * @param {Record} b */ export function lookDiff(a, b) { const skip = (p) => p === "timeline" || p.startsWith("timeline.") || p === "generatedOn" || p === "timelineNodes" || p.startsWith("timelineNodes."); const la = new Map(leaves(a).filter(([p]) => !skip(p))); const lb = new Map(leaves(b).filter(([p]) => !skip(p))); const out = []; for (const p of [...new Set([...la.keys(), ...lb.keys()])].sort()) { const x = la.get(p); const y = lb.get(p); if (JSON.stringify(x) !== JSON.stringify(y)) out.push({ path: p, a: x, b: y }); } return out; } /** A take's manifest: the one it was rendered from, else resolved from its patch. */ async function manifestOf(dir, take) { try { const m = JSON.parse(await readFile(path.join(takesDirOf(dir), take, MANIFEST), "utf8")); if (isObj(m)) return m; } catch { /* a draft, or a take made by hand */ } const { resolveTakeManifest } = await import("./takeManifest.mjs"); return (await resolveTakeManifest(dir, take)).manifest; } /** * Two takes, compared. * * @param {{ ref: { project: string, take: string }, dir: string }} a * @param {{ ref: { project: string, take: string }, dir: string }} b */ export async function compareTakes(a, b) { const side = async (s) => { const t = (await listTakes(s.dir)).takes.find((x) => x.id === s.ref.take); if (!t) throw new Error(`${s.ref.project} has no take ${s.ref.take}`); const manifest = await manifestOf(s.dir, s.ref.take); return { ref: takeRefOf(s.ref), project: s.ref.project, take: t.id, label: t.label, seconds: t.seconds, builtAt: t.builtAt, window: t.window, style: t.style, quick: t.quick === true, draft: t.draft === true, hasPreview: !t.draft, clips: (manifest.timeline ?? []).filter((e) => isObj(e) && e.type === "clip").length, entries: (manifest.timeline ?? []).length, manifest, }; }; const [x, y] = await Promise.all([side(a), side(b)]); const cut = diffManifests(x.manifest, y.manifest); const look = lookDiff(x.manifest, y.manifest); const strip = ({ manifest: _m, ...rest }) => rest; return { a: strip(x), b: strip(y), cut: { changes: cut.changes, lines: cut.changes.map(formatChange), counts: cut.counts }, look: look.slice(0, LOOK_LIMIT), lookMore: Math.max(0, look.length - LOOK_LIMIT), seconds: typeof x.seconds === "number" && typeof y.seconds === "number" ? Number((y.seconds - x.seconds).toFixed(2)) : null, same: cut.same && look.length === 0, }; } /** The comparison as lines (`umtool takes compare`). */ export function compareLines(c) { const lines = []; const fact = (s) => `${s.ref} "${s.label}" ${typeof s.seconds === "number" ? `${s.seconds.toFixed(1)} s` : "not rendered"} ${s.clips} clips${s.style ? ` style ${s.style}` : ""}${s.window && s.window !== "full" ? ` window ${s.window.at}+${s.window.dur}` : ""}`; lines.push(`A ${fact(c.a)}`, `B ${fact(c.b)}`); if (c.seconds !== null) lines.push(`length: ${c.seconds > 0 ? "+" : ""}${c.seconds} s (B against A)`); if (c.same) lines.push("the same cut and the same look"); if (c.cut.lines.length) lines.push(`the cut — ${c.cut.lines.length} change(s), A → B:`, ...c.cut.lines.map((l) => ` ${l}`)); if (c.look.length) lines.push(`the look — ${c.look.length}${c.lookMore ? `+${c.lookMore}` : ""} difference(s):`, ...c.look.map((d) => ` ${d.path}: ${shortValue(d.a)} → ${shortValue(d.b)}`)); return lines; }