// out/youtube.txt's composition (release 28 M6): the title, the summary, the // chapters on YouTube's rules (0:00 first, at least three, ten seconds each), // the sources in cut order, each once. import { test } from "node:test"; import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import { chapterLines, chapterStamp, composeYoutubeText, markerChapters, momentLinkOf, youtubeChapterRules, youtubeChapters, youtubePreview, youtubeSources, youtubeTextName, YOUTUBE_CHAPTERS, } from "./youtube.mjs"; const FULL = new URL("../../common/testing/draft/manifests/full.video.manifest.json", import.meta.url); const YT = { title: "What the baker says about the bakery", description: "Synth Kitchen on the bakery and the contest." }; test("stamps: m:ss, h:mm:ss from an hour", () => { assert.equal(chapterStamp(0), "0:00"); assert.equal(chapterStamp(65.9), "1:05"); assert.equal(chapterStamp(3725), "1:02:05"); }); test("chapters: 0:00 first, at least three, each ten seconds — short ones folded into the one before", () => { const list = [ { start: 0, title: "Title" }, { start: 4, title: "The bakery" }, // 4 s after the first: folded { start: 12.4, title: "Not for sale" }, { start: 30, title: "The contest" }, { start: 55, title: "Last" }, // 5 s before the end: folded ]; const r = youtubeChapters(list, 60); assert.deepEqual(r.chapters.map((c) => [c.start, c.title]), [[0, "Title"], [12, "Not for sale"], [30, "The contest"]]); assert.match(r.note, /2 chapter\(s\) under 10 s folded/); assert.equal(YOUTUBE_CHAPTERS.min, 3); }); test("chapters: fewer than three, or not starting at 0:00, are left out — and the note says why", () => { const two = youtubeChapters([{ start: 0, title: "A" }, { start: 20, title: "B" }], 40); assert.equal(two.chapters, null); assert.match(two.note, /^no chapters: 2 of at least 3/); const late = youtubeChapters([{ start: 2, title: "A" }, { start: 20, title: "B" }, { start: 40, title: "C" }], 60); assert.equal(late.chapters, null); assert.match(late.note, /first starts at 0:02, not 0:00/); assert.match(youtubeChapters(null).note, /made none/); }); test("sources: a moment link per cited clip in cut order, each once; citeUrl wins; a src clip and a card have none", () => { const prov = { siteOrigin: "https://synth.example/", channelSlug: "synth-kitchen" }; const entries = [ { type: "card", id: "t" }, { type: "clip", id: "c02", video: "bread-day-01", start: 24.2, cite: 25 }, { type: "clip", id: "c01", channel: "synth-kitchen", video: "bread-day-01", start: 15 }, { type: "clip", id: "c02b", video: "bread-day-01", start: 25, cite: 25.9 }, // the same moment again { type: "clip", id: "x", citeUrl: "https://x.example/post/1" }, { type: "clip", id: "local", src: "a.mp4", start: 0 }, ]; assert.deepEqual(youtubeSources(entries, prov), [ "https://synth.example/?v=synth-kitchen%2Fbread-day-01&t=25", "https://synth.example/?v=synth-kitchen%2Fbread-day-01&t=15", "https://x.example/post/1", ]); assert.equal(momentLinkOf({ type: "clip", video: "v" }, {}), null, "no origin: no link"); }); test("the text: title, a blank, the summary, Chapters, Sources; nothing without a title", () => { const text = composeYoutubeText({ youtube: YT, chapters: [{ start: 0, title: "Title" }, { start: 12, title: "Not for sale" }, { start: 75, title: "The contest" }], sources: ["https://a.example/1", "https://a.example/2"], }); assert.equal( text, "What the baker says about the bakery\n\nSynth Kitchen on the bakery and the contest.\n\nChapters\n0:00 Title\n0:12 Not for sale\n1:15 The contest\n\nSources\nhttps://a.example/1\nhttps://a.example/2\n", ); assert.equal(composeYoutubeText({ youtube: { title: " ", description: "x" } }), null); assert.equal(composeYoutubeText({ youtube: { title: "T" } }), "T\n"); assert.equal(youtubeTextName(), "youtube.txt"); assert.equal(youtubeTextName("full"), "youtube-full.txt"); }); test("the preview: the default cut's sources, no chapters", async () => { const m = JSON.parse(await readFile(FULL, "utf8")); m.publish = { youtube: YT }; m.timeline.push({ type: "clip", id: "only-full", variant: "full", channel: "synth-kitchen", video: "qa-long-03", start: 5, cite: 5 }); const p = youtubePreview(m); const lines = p.text.split("\n"); assert.equal(lines[0], YT.title); assert.ok(!p.text.includes("Chapters")); const sources = lines.slice(lines.indexOf("Sources") + 1).filter(Boolean); assert.equal(sources.length, 7, "seven clips, seven moments"); assert.ok(!p.text.includes("qa-long-03&t=5"), "an entry of another variant is not in the default cut"); assert.equal(youtubePreview({ timeline: [] }).text, null); }); test("youtubeChapters: with sections marked, the clips fold into the section that opens before them", () => { const list = [ { start: 0, title: "The title", section: true }, { start: 14, title: "Size and staffing", section: true }, { start: 32, title: "clip a", section: false }, { start: 44, title: "clip a again", section: false }, { start: 72, title: "Insulinoma", section: true }, { start: 82, title: "clip b", section: false }, { start: 110, title: "clip c", section: false }, ]; const r = youtubeChapters(list, 130); assert.deepEqual(r.chapters.map((c) => c.title), ["The title", "Size and staffing", "Insulinoma"]); // Too few sections: the block is left out rather than listing clips. assert.equal(youtubeChapters(list.filter((c) => c.title !== "Insulinoma"), 130).chapters, null); }); // ---- chapters from the deck's markers (2026-11-02) ------------------------ const EMBER = JSON.parse(await (await import("node:fs/promises")).readFile(new URL("./fixtures/ember-draft-24-chapters.json", import.meta.url), "utf8")); const emberSegments = EMBER.segments.map(([id, start]) => ({ id, start })); test("markerChapters: Ember draft-24's 12 markers make the hand script's 13 rows, row for row", () => { const r = markerChapters({ markers: EMBER.markers, segments: emberSegments, total: EMBER.total, first: EMBER.first }); assert.deepEqual(chapterLines(r.chapters), EMBER.expected); assert.deepEqual(r.notes, []); }); test("markerChapters: subsections are no chapters; an untitled opening gives 0:00 to the first section; what YouTube's rules merge is said", () => { const markers = [ ...EMBER.markers.slice(0, 3), { after: "rf1", label: "A subsection", level: 2 }, { after: "rf1", label: "Too close" }, // a segment 5.1 s after "Leaves it open" starts { after: "nope", label: "Gone" }, { after: "ho3", label: "After the last" }, { after: "mk1", label: "" }, ]; const segments = emberSegments.flatMap((s) => (s.id === "rf1" ? [s, { id: "rf1b", start: 330 }] : [s])); const r = markerChapters({ markers, segments, total: EMBER.total }); assert.deepEqual(chapterLines(r.chapters), ["0:00 Sex appeal", "4:59 Not a character", "5:24 Leaves it open"]); assert.ok(!chapterLines(r.chapters).some((l) => l.includes("A subsection"))); const said = r.notes.join("\n"); assert.match(said, /“Gone” follows nope, which is not in the cut/); assert.match(said, /“After the last” follows the cut's last segment/); assert.match(said, /the opening has no title \(deck\.sectionTitle\.first\): “Sex appeal” starts at 0:00, not 2:56/); assert.match(said, /“Too close” \(5:30\) is 6 s after “Leaves it open”, under 10: merged into it/); }); test("youtubeChapterRules: a short opening gives 0:00 to the next; a short last one is merged; under three is left out", () => { const r = youtubeChapterRules([{ start: 0, title: "Open" }, { start: 4.7, title: "First" }, { start: 30, title: "Second" }, { start: 60, title: "Third" }, { start: 95, title: "Tail" }], 100); assert.deepEqual(r.chapters.map((c) => [c.start, c.title]), [[0, "First"], [30, "Second"], [60, "Third"]]); assert.match(r.notes.join("\n"), /the opening “Open” runs 4 s, under 10: “First” starts at 0:00, not 0:04/); assert.match(r.notes.join("\n"), /“Tail” \(1:35\) runs 5 s to the end, under 10: merged into “Third”/); const two = youtubeChapterRules([{ start: 0, title: "A" }, { start: 30, title: "B" }], 60); assert.equal(two.chapters, null); assert.match(two.notes[0], /^no chapters: 2 of at least 3/); }); test("youtubeChapters: a build's list with ids and cut starts, under a deck with markers, is the markers' chapters", () => { const list = emberSegments.map((s, i) => ({ start: i === 0 ? 0 : s.start + 0.4, title: `seg ${s.id}`, id: s.id, cutStart: s.start })); const r = youtubeChapters(list, EMBER.total, { markers: EMBER.markers, first: EMBER.first }); assert.deepEqual(chapterLines(r.chapters), EMBER.expected); // No markers: as before, one per entry folded by the ten-second rule. const plain = youtubeChapters(list, EMBER.total); assert.equal(plain.chapters[1].title, "seg v01t"); });