import { test } from "node:test"; import assert from "node:assert/strict"; import { fetchFailureSentence, litCount, spokenWords } from "./words"; test("a rate limit says the platform is rate-limiting", () => { const r = fetchFailureSentence("youtube is cooling down after a 429 (2 min): HTTP Error 429: Too Many Requests"); assert.equal(r.message, "youtube is rate-limiting fetches: try again later."); assert.match(r.detail ?? "", /429/); assert.equal(fetchFailureSentence("the editor refused it (409): rumble is in a rate-limit cooldown").message, "rumble is rate-limiting fetches: try again later."); }); test("an audio-check failure says the source served corrupt audio", () => { const r = fetchFailureSentence("the editor's job failed: The audio check found the window 1.00–9.00 of abc malformed"); assert.match(r.message, /corrupt audio/); }); test("anything else is passed through", () => { assert.deepEqual(fetchFailureSentence("the editor's job failed: no formats"), { message: "the editor's job failed: no formats" }); }); test("spoken words follow the cues, word by word", () => { const cues = [ { start: 0, end: 4, text: "one two three four" }, { start: 4, end: 8, text: "five six seven eight" }, ]; const w = spokenWords("three four five", cues, 0, 8); assert.deepEqual(w.map((x) => x.t), [2, 3, 4]); assert.equal(litCount(w, 3.5), 2); assert.equal(litCount(w, 1), 0); }); test("a misheard word keeps the time before it", () => { const cues = [{ start: 10, end: 14, text: "I bought a cakeup machine" }]; const w = spokenWords("I bought a K-Cup machine", cues, 10, 14); assert.deepEqual(w.map((x) => x.t), [10, 10.8, 11.6, 11.6, 13.2]); }); test("no cues, or a quote mostly not in them: spread evenly over the span", () => { assert.deepEqual(spokenWords("a b", [], 0, 2).map((x) => x.t), [0, 1]); assert.deepEqual(spokenWords("x y z w", [{ start: 0, end: 4, text: "nothing alike here" }], 0, 4).map((x) => x.t), [0, 1, 2, 3]); });