// Home's Quick clip box before the render (umtool redesign L4): a pasted moment // found in a corpus, its span from the cues, and the words to cut by. import assert from "node:assert/strict"; import test from "node:test"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { MomentError, findRecording, quickClipStrip, spanOf } from "./quickClipStrip.mjs"; import { quickClipCommand, quickClipSpec } from "./quickClip.mjs"; const cues = [ { start: 0, end: 4, text: "first we go" }, { start: 4, end: 8, text: "and then we stop." }, { start: 8, end: 12, text: "a new thought" }, { start: 12, end: 16, text: "runs on" }, { start: 16, end: 20, text: "and on" }, { start: 20, end: 24, text: "and ends." }, ]; test("a span: the link's own; else from the cue it lands in to its sentence's end, at most three cues on", () => { assert.deepEqual(spanOf({ start: 3, end: 9 }, cues), { start: 3, end: 9 }); assert.deepEqual(spanOf({ start: 1, end: null }, cues), { start: 1, end: 8 }); assert.deepEqual(spanOf({ start: 9, end: null }, cues), { start: 9, end: 20 }); assert.deepEqual(spanOf({ start: 30, end: null }, cues), { start: 30, end: 50 }); assert.deepEqual(spanOf({ start: 5, end: null }, []), { start: 5, end: 25 }); }); test("the recording found on the disk: by its channel, by the one channel holding it, refused when none or two do", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "quick-clip-strip-")); try { const rec = async (slug, id, doc) => { const d = path.join(root, slug, "data", id); await mkdir(d, { recursive: true }); await writeFile(path.join(root, slug, "config.json"), JSON.stringify({ platform: "youtube" })); if (doc) await writeFile(path.join(d, "transcript.cues.json"), JSON.stringify(doc)); }; await rec("chan", "vid1", { title: "A fixture", duration: 30, cues }); await rec("chan", "both"); await rec("other", "both"); assert.deepEqual(await findRecording({ channel: null, video: "vid1" }, { channelsDir: root }), { channel: "chan", video: "vid1" }); assert.deepEqual(await findRecording({ channel: "chan", video: "vid1" }, { channelsDir: root }), { channel: "chan", video: "vid1" }); await assert.rejects(findRecording({ channel: null, video: "nope" }, { channelsDir: root }), /no channel in this corpus holds nope/); await assert.rejects(findRecording({ channel: null, video: "both" }, { channelsDir: root }), /2 channels hold both \(chan, other\)/); await assert.rejects(findRecording({ channel: "other", video: "vid1" }, { channelsDir: root }), MomentError); const s = await quickClipStrip("https://www.youtube.com/watch?v=vid1&t=1", { channelsDir: root }); assert.equal(s.spec, "chan/vid1@1-8"); assert.deepEqual([s.from, s.to, s.title, s.duration, s.via], [0, 20, "A fixture", 30, "youtube"]); // No words sidecar here: the cues around the span. assert.equal(s.words, null); assert.equal(s.wordsFrom, "cues"); assert.deepEqual(s.cues.map((c) => c.text), ["first we go", "and then we stop.", "a new thought", "runs on", "and on", "and ends."]); await assert.rejects(quickClipStrip("not a moment", { channelsDir: root }), /not a moment/); } finally { await rm(root, { recursive: true, force: true }); } }); test("a quick clip's spec: a YouTube id starting with - is one (the job passes the spec after --); `-x`, a dash channel or dots are not", () => { assert.deepEqual(quickClipSpec("chan/-abcDEF1234@1-2"), { channel: "chan", video: "-abcDEF1234", start: 1, end: 2 }); assert.ok(quickClipSpec("chan/_abcDEF1234@1-2")); for (const bad of ["chan/-x@1-2", "-rf/vid@1-2", "../vid@1-2", "chan/..@1-2", "chan/vid@2-1"]) assert.equal(quickClipSpec(bad), null, bad); }); test("umtool clip -- : the word after -- is the spec, never a flag", async () => { const out = []; const err = []; // A spec that parses but names no recording here: the command reaches the spec check, not "unknown flag". const code = await quickClipCommand(["--json", "--", "nochan/-abcDEF1234@1-2"], { root: "/nonexistent-umtool-root", log: (s) => out.push(s), err: (s) => err.push(s), channelsDir: "/nonexistent-umtool-channels" }); assert.notEqual(code, 0); assert.doesNotMatch(err.join("\n"), /unknown flag/); });