import { test } from "node:test"; import assert from "node:assert/strict"; import { CHUNK_MAX_CHARS, CHUNK_PAUSE, chunkTtsText, NPREDICT_MIN_EXPECTED_S, nPredictFor, ORPHEUS_TOKENS_PER_AUDIO_S, splitLong, splitSentences, type TtsChunking, wordsIn, } from "./ttsChunk"; import { normalizeTtsText } from "./ttsText"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/ttsChunk.test.ts // // The chunker against the tuned round's own (run_orpheus2.py chunk(normalize())): // the bake-off lines (synthetic text written for the bake-off) and the // chunks, endings, pauses and token budgets that pipeline produced for them, // pasted here as data. const ORPHEUS = { tags: "keep", years: "guarded", paragraphs: "keep" } as const; // The tuned round's limits (2026-10-11), which these pasted chunks were made with. const TUNED_ROUND: TtsChunking = { maxChars: 250, maxSentences: 3, pause: { sentence: 0.3, clause: 0.15, paragraph: 0.6, end: 0 } }; const LINES: Record = { "card": "Two weeks later, the same channel posted the opposite claim — and deleted the first video.", "numbers": "On March 14th, 2024, he said fifteen thousand people watched; the stream's own counter showed 3,812.", "quote": "In his words: \"I never said that.\" The clip below is from forty minutes earlier.", "podcast": "Welcome back to the archive. Every video we cover here has a transcript on file, saved the day it was published, so when a claim changes we can show exactly what was said before. That matters, because memories drift and old uploads quietly disappear. Each clip you hear comes with a citation: the channel, the date, and the minute it was spoken, so you can check it yourself. Nothing is paraphrased when the original words are available, and if you are new here, every source we use is linked in the description. Today, we follow one story across three years of streams, and let the record speak for itself.", "podcast-tagged": "Welcome back to the archive. Every video we cover here has a transcript on file, saved the day it was published, so when a claim changes we can show exactly what was said before. That matters, because memories drift and old uploads quietly disappear. Each clip you hear comes with a citation: the channel, the date, and the minute it was spoken, so you can check it yourself. Nothing is paraphrased when the original words are available, and if you are new here, every source we use is linked in the description. Today, we follow one story across three years of streams, and let the record speak for itself.", "quote-tagged": "In his words: \"I never said that.\" The clip below is from forty minutes earlier.", "story": "Nobody was supposed to find it. The video had been gone for three years. Deleted on a quiet Tuesday night... without a single word.\n\nThe channel moved on. The comments moved on. And honestly? So did I.\n\nThen, one rainy afternoon, I was clearing out an old hard drive. Folders inside folders. Files called final, and final two, and final-final. You know the kind.\n\nAnd then... there it was. One file. No title. Just a date.\n\nI almost deleted it. Why wouldn't I? But something made me press play.\n\n It was the video. The whole thing. Every word, every pause, exactly the way it was said the first time.\n\nHere's the thing about the internet. It feels like it forgets. It doesn't. Not always. Somewhere, somebody hit save.\n\nSo the next time someone tells you it never happened... ask them one simple question. Are you sure?", "story-card": "Three years ago, it vanished without a word. Everyone said it was gone for good... They were wrong.", "story-plain": "An archivist found a copy of a video that had been deleted three years earlier. The video was removed on a Tuesday night without explanation, and the channel and its audience moved on. The copy was found while the archivist was clearing out an old hard drive that contained many nested folders, with files named final, final two, and final-final. One untitled file was labelled only with a date. The archivist considered deleting it, but played it first. It was the deleted video, complete, with every statement intact as it was originally recorded. This shows that online content is not always permanently lost, because copies are sometimes saved. Claims that something never happened can therefore be checked against preserved records." }; const EXPECTED: Record = { "card": [ { "text": "Two weeks later, the same channel posted the opposite claim, and deleted the first video.", "ends": "end", "pauseAfter": 0.0 } ], "numbers": [ { "text": "On March fourteenth, twenty twenty-four, he said fifteen thousand people watched; the stream's own counter showed three thousand eight hundred twelve.", "ends": "end", "pauseAfter": 0.0 } ], "quote": [ { "text": "In his words: \"I never said that.\" The clip below is from forty minutes earlier.", "ends": "end", "pauseAfter": 0.0 } ], "podcast": [ { "text": "Welcome back to the archive. Every video we cover here has a transcript on file, saved the day it was published, so when a claim changes we can show exactly what was said before.", "ends": "sentence", "pauseAfter": 0.3 }, { "text": "That matters, because memories drift and old uploads quietly disappear. Each clip you hear comes with a citation: the channel, the date, and the minute it was spoken, so you can check it yourself.", "ends": "sentence", "pauseAfter": 0.3 }, { "text": "Nothing is paraphrased when the original words are available, and if you are new here, every source we use is linked in the description. Today, we follow one story across three years of streams, and let the record speak for itself.", "ends": "end", "pauseAfter": 0.0 } ], "podcast-tagged": [ { "text": "Welcome back to the archive. Every video we cover here has a transcript on file, saved the day it was published, so when a claim changes we can show exactly what was said before.", "ends": "sentence", "pauseAfter": 0.3 }, { "text": "That matters, because memories drift and old uploads quietly disappear. Each clip you hear comes with a citation: the channel, the date, and the minute it was spoken, so you can check it yourself.", "ends": "sentence", "pauseAfter": 0.3 }, { "text": "Nothing is paraphrased when the original words are available, and if you are new here, every source we use is linked in the description. Today, we follow one story across three years of streams, and let the record speak for itself.", "ends": "end", "pauseAfter": 0.0 } ], "quote-tagged": [ { "text": "In his words: \"I never said that.\" The clip below is from forty minutes earlier.", "ends": "end", "pauseAfter": 0.0 } ], "story": [ { "text": "Nobody was supposed to find it. The video had been gone for three years. Deleted on a quiet Tuesday night... without a single word.", "ends": "paragraph", "pauseAfter": 0.6 }, { "text": "The channel moved on. The comments moved on. And honestly? So did I.", "ends": "paragraph", "pauseAfter": 0.6 }, { "text": "Then, one rainy afternoon, I was clearing out an old hard drive. Folders inside folders. Files called final, and final two, and final-final. You know the kind.", "ends": "paragraph", "pauseAfter": 0.6 }, { "text": "And then... there it was. One file. No title. Just a date.", "ends": "paragraph", "pauseAfter": 0.6 }, { "text": "I almost deleted it. Why wouldn't I? But something made me press play.", "ends": "paragraph", "pauseAfter": 0.6 }, { "text": " It was the video. The whole thing. Every word, every pause, exactly the way it was said the first time.", "ends": "paragraph", "pauseAfter": 0.6 }, { "text": "Here's the thing about the internet. It feels like it forgets. It doesn't. Not always. Somewhere, somebody hit save.", "ends": "paragraph", "pauseAfter": 0.6 }, { "text": "So the next time someone tells you it never happened... ask them one simple question. Are you sure?", "ends": "end", "pauseAfter": 0.0 } ], "story-card": [ { "text": "Three years ago, it vanished without a word. Everyone said it was gone for good... They were wrong.", "ends": "end", "pauseAfter": 0.0 } ], "story-plain": [ { "text": "An archivist found a copy of a video that had been deleted three years earlier. The video was removed on a Tuesday night without explanation, and the channel and its audience moved on.", "ends": "sentence", "pauseAfter": 0.3 }, { "text": "The copy was found while the archivist was clearing out an old hard drive that contained many nested folders, with files named final, final two, and final-final.", "ends": "sentence", "pauseAfter": 0.3 }, { "text": "One untitled file was labelled only with a date. The archivist considered deleting it, but played it first. It was the deleted video, complete, with every statement intact as it was originally recorded.", "ends": "sentence", "pauseAfter": 0.3 }, { "text": "This shows that online content is not always permanently lost, because copies are sometimes saved. Claims that something never happened can therefore be checked against preserved records.", "ends": "end", "pauseAfter": 0.0 } ] }; const N_PREDICT: Record = { "Nobody was supposed to find it. The video had been gone for three years. Deleted on a quiet Tuesday night... without a single word.": 1498, "The channel moved on. The comments moved on. And honestly? So did I.": 763, "Then, one rainy afternoon, I was clearing out an old hard drive. Folders inside folders. Files called final, and final two, and final-final. You know the kind.": 1673, "And then... there it was. One file. No title. Just a date.": 749, "I almost deleted it. Why wouldn't I? But something made me press play.": 763, " It was the video. The whole thing. Every word, every pause, exactly the way it was said the first time.": 1253, "Here's the thing about the internet. It feels like it forgets. It doesn't. Not always. Somewhere, somebody hit save.": 1078, "So the next time someone tells you it never happened... ask them one simple question. Are you sure?": 1064 }; test("every bake-off line chunks exactly as the tuned pipeline did", () => { for (const [key, text] of Object.entries(LINES)) { assert.deepEqual(chunkTtsText(normalizeTtsText(text, ORPHEUS), TUNED_ROUND), EXPECTED[key], key); } }); test("the pipeline's own chunker assertions", () => { const pod = chunkTtsText(normalizeTtsText(LINES.podcast, ORPHEUS), TUNED_ROUND); assert.ok(pod.every((c) => c.text.length <= TUNED_ROUND.maxChars)); assert.ok(pod.length >= 2 && pod.length <= 4); assert.equal(pod[pod.length - 1].ends, "end"); assert.ok(pod.slice(0, -1).every((c) => c.ends === "sentence")); const q = chunkTtsText(normalizeTtsText(LINES["quote-tagged"], ORPHEUS), TUNED_ROUND); assert.equal(q.length, 1); assert.ok(q[0].text.includes("")); const st = chunkTtsText(normalizeTtsText(LINES.story, ORPHEUS), TUNED_ROUND); assert.equal(st.filter((c) => c.ends === "paragraph").length, LINES.story.split("\n\n").length - 1); assert.ok(st.map((c) => c.text).join("").includes("")); assert.ok(st.every((c) => c.pauseAfter === { sentence: 0.3, clause: 0.15, paragraph: 0.6, end: 0 }[c.ends])); }); test("a long sentence is cut at clause boundaries, never mid-phrase", () => { const long = "word ".repeat(20) + "and this keeps going; " + "more words here, ".repeat(12) + "but then it ends."; const parts = splitLong(long, TUNED_ROUND.maxChars); assert.deepEqual(parts, ["word word word word word word word word word word word word word word word word word word word word and this keeps going;", "more words here, more words here, more words here, more words here, more words here, more words here, more words here, more words here, more words here, more words here, more words here, more words here, but then it ends."]); assert.ok(parts.every((p) => p.length <= TUNED_ROUND.maxChars)); }); test("sentences: a reaction tag joins the sentence before it; an ellipsis continues one", () => { assert.deepEqual(splitSentences("It was gone. Then... it was back. And then... there it was."), [ "It was gone. ", "Then... it was back.", "And then... there it was.", ]); assert.deepEqual(splitSentences("Deleted on a Tuesday night... without a word."), ["Deleted on a Tuesday night... without a word."]); assert.equal(wordsIn("It's one file."), 3); }); test("the token budget follows the words, tags and beats", () => { for (const [text, n] of Object.entries(N_PREDICT)) assert.equal(nPredictFor(text), n, text); assert.equal(nPredictFor("x") % 7, 0); }); test("the defaults: longer chunks and shorter joins than the tuned round (the operator's pick, 2026-10-12)", () => { assert.equal(CHUNK_MAX_CHARS, 420); assert.deepEqual({ ...CHUNK_PAUSE }, { sentence: 0.2, clause: 0.1, paragraph: 0.4, end: 0 }); const story = chunkTtsText(normalizeTtsText(LINES.podcast, ORPHEUS)); const round = chunkTtsText(normalizeTtsText(LINES.podcast, ORPHEUS), TUNED_ROUND); assert.ok(story.length < round.length, `${story.length} chunks by default, ${round.length} at the tuned round's limits`); assert.ok(story.every((c) => c.text.length <= 420)); }); test("a four-word line gets at least four seconds of budget", () => { const n = nPredictFor("Well, then this drops."); assert.ok(n >= NPREDICT_MIN_EXPECTED_S * ORPHEUS_TOKENS_PER_AUDIO_S * 1.6, String(n)); assert.equal(n % 7, 0); });