import { test } from "node:test"; import assert from "node:assert/strict"; import { TTS_EXPRESSIVE_TAGS, collapseTtsWhitespace, normalizeTtsText, sayDecimal, sayInt, sayOrdinal, speakYears, stripTtsTags, twoDigitWords, unknownTtsTags, unknownTtsTagsError, yearWords, } from "./ttsText"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/ttsText.test.ts test("years are written out the way they are said", () => { const cases: [number, string][] = [ [1900, "nineteen hundred"], [1901, "nineteen oh one"], [1909, "nineteen oh nine"], [1910, "nineteen ten"], [1984, "nineteen eighty-four"], [1999, "nineteen ninety-nine"], [2000, "two thousand"], [2001, "two thousand one"], [2005, "two thousand five"], [2009, "two thousand nine"], [2010, "twenty ten"], [2019, "twenty nineteen"], [2024, "twenty twenty-four"], [2099, "twenty ninety-nine"], ]; for (const [y, words] of cases) assert.equal(yearWords(y), words, String(y)); assert.equal(yearWords(1990, true), "nineteen nineties"); assert.equal(yearWords(1900, true), "nineteen hundreds"); assert.equal(yearWords(2000, true), "two thousands"); assert.equal(yearWords(2010, true), "twenty tens"); assert.equal(yearWords(2020, true), "twenty twenties"); assert.throws(() => yearWords(1899)); assert.throws(() => yearWords(2100)); assert.throws(() => yearWords(2024, true)); assert.equal(twoDigitWords(0), "zero"); assert.equal(twoDigitWords(17), "seventeen"); assert.equal(twoDigitWords(40), "forty"); assert.equal(twoDigitWords(73), "seventy-three"); }); test("a year in running text is spoken; every other number is left to the engine", () => { assert.equal( speakYears("In 2024 he said so, and again in 1999."), "In twenty twenty-four he said so, and again in nineteen ninety-nine.", ); assert.equal(speakYears("From 2005 to 2010."), "From two thousand five to twenty ten."); assert.equal(speakYears("the 1990s and the 2020s"), "the nineteen nineties and the twenty twenties"); assert.equal(speakYears("1999-2001"), "nineteen ninety-nine-two thousand one"); // Not years: outside the range, part of a longer number, an amount, a // decimal, a percentage, a time, a slashed date, a code. for (const s of [ "1899 and 2100", "12024", "20245", "$2024", "£1999", "2,024 people", "1,2024", "2024.5", "3.2024", "2024%", "20:24", "10/11/2024", "#2024", "COVID2019", "2024th", "2024s", ]) { assert.equal(speakYears(s), s, s); } // A sentence's full stop after a year is not a decimal. assert.equal(speakYears("It was 2024."), "It was twenty twenty-four."); }); test("the expressive tag set is exported, and unknown tags are named", () => { assert.deepEqual( [...TTS_EXPRESSIVE_TAGS], ["laugh", "chuckle", "sigh", "cough", "sniffle", "groan", "yawn", "gasp"], ); assert.deepEqual(unknownTtsTags("Fine and ."), []); assert.deepEqual(unknownTtsTags("Oh no bold "), ["", "", ""]); assert.deepEqual(unknownTtsTags("a < b and c > d"), ["< b and c >"]); assert.deepEqual(unknownTtsTags("x < y, z > w? no: 3 < 4"), ["< y, z >"]); assert.deepEqual(unknownTtsTags("plain text, 3 < 4"), []); assert.equal(unknownTtsTagsError("fine "), null); const err = unknownTtsTagsError("ha ")!; assert.match(err, /tags no engine knows: \(did you mean \?\), /); assert.match(err, / /); }); test("stripping a tag leaves no stray or doubled space", () => { assert.equal(stripTtsTags("That's rich ."), "That's rich."); assert.equal(stripTtsTags(" Here we go."), "Here we go."); assert.equal(stripTtsTags("Here we go."), "Here we go."); assert.equal(stripTtsTags("Herewe go."), "Here we go."); assert.equal(stripTtsTags("Well, , fine."), "Well, fine."); assert.equal(stripTtsTags("End "), "End"); assert.equal(stripTtsTags(" Sorry !"), "Sorry!"); assert.equal(stripTtsTags("a\n\nb"), "a b"); // Only the expressive tags go: anything else is the refusal's business. assert.equal(stripTtsTags("keep this"), "keep this"); for (const t of TTS_EXPRESSIVE_TAGS) { assert.equal(stripTtsTags(`one <${t}> two`), "one two", t); } }); test("whitespace collapses, so a re-wrapped paragraph is the same text", () => { assert.equal(collapseTtsWhitespace(" a\n\n b\t c "), "a b c"); assert.equal(collapseTtsWhitespace("a , b ."), "a, b."); assert.equal( normalizeTtsText("In 2024,\n the show\nended .", { tags: "strip", years: "plain", paragraphs: "join" }), "In twenty twenty-four, the show ended.", ); assert.equal( normalizeTtsText("In 2024 it ended.", { tags: "keep", years: "guarded", paragraphs: "keep" }), "In twenty twenty-four it ended.", ); // NFC: a decomposed accent is the same text as a composed one. assert.equal( normalizeTtsText("café", { tags: "strip", years: "plain", paragraphs: "join" }), normalizeTtsText("café", { tags: "strip", years: "plain", paragraphs: "join" }), ); }); // The tuned Orpheus round's 17 self-tests (run_orpheus2.py selftest), ported // as they stand: tags kept, the guarded year rule, paragraphs kept. const ORPHEUS = { tags: "keep", years: "guarded", paragraphs: "keep" } as const; const SELFTEST: [string, string][] = [ ["On March 14th, 2024, he said", "On March fourteenth, twenty twenty-four, he said"], ["the counter showed 3,812.", "the counter showed three thousand eight hundred twelve."], ["in 2005 and 1999 and 1905 and 1900 and 2010", "in two thousand five and nineteen ninety-nine and nineteen oh five and nineteen hundred and twenty ten"], ["1950 people", "one thousand nine hundred fifty people"], ["the 1990s and the \u201990s", "the nineteen nineties and the nineties"], ["21st, 22nd, 103rd, 12th, 40th", "twenty-first, twenty-second, one hundred third, twelfth, fortieth"], ["up 45% or 3.5%", "up forty-five percent or three point five percent"], ["$12.50, $1, $3.5 million, $1,200", "twelve dollars and fifty cents, one dollar, three point five million dollars, one thousand two hundred dollars"], ["Dr. Smith vs. Mr. Jones, e.g. today", "Doctor Smith versus Mister Jones, for example, today"], ["the claim \u2014 and the video", "the claim, and the video"], ["from 2019\u20132021", "from twenty nineteen to twenty twenty-one"], ["see https://example.pages.dev/c/x?y=1 now", "see example dot pages dot dev now"], ["ask @some_handle about #TrueCrime", "ask at some handle about hashtag TrueCrime"], ["at 3:45 and 10:00 and 7:05", "at three forty-five and ten o'clock and seven oh five"], ["He laughed. 2024 was long.", "He laughed. twenty twenty-four was long."], ["It\u2019s 3.14 and 0.5", "It's three point one four and zero point five"], ["No. 5 & #1", "number five and number one"], ]; test("the spoken-form normalizer passes the tuned round's 17 self-tests", () => { assert.equal(SELFTEST.length, 17); for (const [src, want] of SELFTEST) assert.equal(normalizeTtsText(src, ORPHEUS), want, src); }); test("numbers: cardinals, ordinals, decimals", () => { assert.equal(sayInt(0), "zero"); assert.equal(sayInt(115), "one hundred fifteen"); assert.equal(sayInt(1_000_001), "one million one"); assert.equal(sayInt(-3), "minus three"); assert.equal(sayOrdinal(1), "first"); assert.equal(sayOrdinal(30), "thirtieth"); assert.equal(sayOrdinal(1002), "one thousand second"); assert.equal(sayDecimal("1,200.75"), "one thousand two hundred point seven five"); }); test("Kokoro keeps T1's year rule; Orpheus guards counts; paragraphs kept or joined", () => { const kokoro = { tags: "strip", years: "plain", paragraphs: "join" } as const; assert.equal(normalizeTtsText("1950 people came.", kokoro), "nineteen fifty people came."); assert.equal(normalizeTtsText("1950 people came.", ORPHEUS), "one thousand nine hundred fifty people came."); assert.equal(normalizeTtsText("It was 42 episodes.", kokoro), "It was forty-two episodes."); assert.equal(normalizeTtsText("One.\n\n\nTwo three.\nFour.", ORPHEUS), "One.\n\nTwo three. Four."); assert.equal(normalizeTtsText("One.\n\nTwo three.", kokoro), "One. Two three."); });