import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import { capInstances, dayNumber, hitsInCues, instancesAsClips, niceDate, phraseRegExp, phraseScan } from "./phraseScan.mjs"; const PHRASE = String.raw`\bslow[\s,.\-]+news\b(?:[\s,.\-]+(?:day|week|cycle))?`; type C = { start: number; end: number; text: string }; function corpus(videos: Record>) { const dir = mkdtempSync(path.join(os.tmpdir(), "phrase-scan-")); for (const [slug, vids] of Object.entries(videos)) { for (const [id, v] of Object.entries(vids)) { const d = path.join(dir, slug, "data", id); mkdirSync(d, { recursive: true }); writeFileSync(path.join(d, "transcript.cues.json"), JSON.stringify({ id, title: `t ${id}`, platform: v.platform ?? "youtube", ...v })); if (v.availability) writeFileSync(path.join(d, "availability.json"), JSON.stringify({ state: v.availability })); } } return dir; } test("hitsInCues: a phrase split over two cues is found; a rolling repeat is one hit", () => { const cues = [ { start: 0, end: 2, text: "well it must be a slow" }, { start: 2, end: 4, text: "news day for them" }, { start: 4, end: 6, text: "slow news day for them" }, // the rolling caption repeating its line { start: 30, end: 33, text: "another slow news week" }, ]; const hits = hitsInCues(cues, phraseRegExp(PHRASE)); assert.equal(hits.length, 2); assert.deepEqual([hits[0].start, hits[0].end, hits[0].cueFirst, hits[0].cueLast], [0, 4, 0, 1]); assert.equal(hits[0].match, "slow news day"); assert.match(hits[0].context, /must be a slow news day for them/); assert.equal(hits[1].match, "slow news week"); }); test("phraseScan: a twin on the preferred channel wins; a hit it lacks is placed by its words; a clip upload folds; exclude drops", async () => { const dir = corpus({ rumble: { // the same stream as yt1, and it has the hit itself r1: { uploadDate: "20230102", duration: 1000, platform: "rumble", cues: [{ start: 100, end: 103, text: "kind of a slow news day today folks" }] }, // the same stream as yt2, transcribed differently: no hit, but the words are there r2: { uploadDate: "20230301", duration: 2000, platform: "rumble", cues: [ { start: 500, end: 503, text: "so the admin tweeted a statement" }, { start: 503, end: 506, text: "must be a really slow new state" }, ] }, // a clip upload of r1's moment, two days on r3: { uploadDate: "20230104", duration: 60, platform: "rumble", cues: [{ start: 5, end: 8, text: "kind of a slow news day today folks" }] }, // another upload of yt4's day, near its length: not a twin (no title word in common, 4 % apart) r5: { uploadDate: "20260507", duration: 843, platform: "rumble", cues: [{ start: 40, end: 44, text: "restored upon complaining on X which is the only way you can get attention" }] }, // a read-out chat line r4: { uploadDate: "20240101", duration: 900, platform: "rumble", cues: [{ start: 9, end: 12, text: "slow news week Jeremy says chat" }] }, }, yt: { yt1: { uploadDate: "20230101", duration: 1010, cues: [{ start: 110, end: 113, text: "kind of a slow news day today folks" }] }, yt2: { uploadDate: "20230301", duration: 2010, cues: [ { start: 520, end: 523, text: "so the admin tweeted a statement" }, { start: 523, end: 526, text: "must be a really slow news day" }, ] }, yt4: { uploadDate: "20260507", duration: 810, cues: [{ start: 769, end: 772, text: "don't get sucked into this it's uh slow news cycle panic" }] }, // no twin: kept on its own channel, with its availability yt3: { uploadDate: "20190502", duration: 900, availability: "public", cues: [{ start: 2, end: 5, text: ">> it must be a slow news week" }] }, }, }); try { const r = await phraseScan({ channelsDir: dir, channels: ["yt"], prefer: "rumble", phrase: PHRASE, exclude: String.raw`slow news \w+,? jeremy` }); assert.equal(r.scanned, 9); assert.deepEqual( r.instances.map((i) => [i.date, i.slug, i.videoId, i.start]), [ ["20190502", "yt", "yt3", 2], ["20230102", "rumble", "r1", 100], ["20230301", "rumble", "r2", 500], ["20260507", "yt", "yt4", 769], ], ); assert.deepEqual(r.instances[1].alsoIn, [{ slug: "rumble", videoId: "r3", start: 5 }]); assert.equal(r.instances[2].via?.videoId, "yt2", "placed in the twin from the other channel's hit"); assert.equal(r.instances[0].speakerCheck, true); assert.equal(r.instances[0].availability, "public"); assert.deepEqual(r.dropped.map((d) => d.videoId), ["r4"]); const clips = instancesAsClips(r.instances); assert.deepEqual(clips[0], { type: "clip", id: "s01", channel: "yt", video: "yt3", start: 0, end: 7.5, cutStart: 2, cutEnd: 5, cite: 2, quote: ">> it must be a slow news week", onscreen: { title: "slow news week", subtitle: "2 May 2019" }, note: "voice check: a speaker change in the captions", montage: { n: 1, of: 4, date: "20190502", platform: "youtube", match: "slow news week", speakerCheck: true }, }); assert.match(String(clips[2].note), /placed from yt\/yt2/); } finally { rmSync(dir, { recursive: true, force: true }); } }); test("dates and the phrase", () => { assert.equal(niceDate("20260727"), "27 Jul 2026"); assert.equal(dayNumber("20230102")! - dayNumber("20221231")!, 2); assert.equal(dayNumber("nope"), null); assert.throws(() => phraseRegExp(" "), /phrase is required/); }); // Release 35: the cap. N instances spread across years in proportion to each // year's count (≥ 1 per year that has any; largest remainder), chronological, // the ones without a speakerCheck first within a year, spread through it. type I = { id: string; date: string | null; speakerCheck?: boolean }; const inst = (spec: Record, checked: string[] = []): I[] => { const out: I[] = []; for (const [year, n] of Object.entries(spec)) { for (let k = 0; k < n; k++) { const id = `${year}-${String(k).padStart(2, "0")}`; out.push({ id, date: year === "none" ? null : `${year}${String(1 + (k % 12)).padStart(2, "0")}15`, speakerCheck: checked.includes(id) }); } } return out.sort((a, b) => String(a.date ?? "~").localeCompare(String(b.date ?? "~")) || a.id.localeCompare(b.id)); }; const years = (l: I[]) => l.reduce>((m, i) => ((m[i.id.slice(0, 4)] = (m[i.id.slice(0, 4)] ?? 0) + 1), m), {}); test("capInstances: proportional to each year, at least one each, largest remainder, chronological", () => { const all = inst({ 2019: 2, 2020: 30, 2021: 10, 2022: 1, 2023: 7 }); const { kept, skipped } = capInstances(all, 10); assert.equal(kept.length, 10); assert.equal(skipped.length, all.length - 10); // 50 instances, 10 kept: quotas 0.4, 6, 2, 0.2, 1.4 -> floors with a floor of one 1, 6, 2, 1, 1 = 11 -> one back // from a year above one at its quota (2020 and 2021 tie at 0; the bigger year gives it up). assert.deepEqual(years(kept), { 2019: 1, 2020: 5, 2021: 2, 2022: 1, 2023: 1 }); const order = all.map((i) => i.id); const pos = (l: I[]) => l.map((i) => order.indexOf(i.id)); assert.deepEqual(pos(kept), [...pos(kept)].sort((a, b) => a - b), "kept is chronological"); assert.deepEqual(pos(skipped), [...pos(skipped)].sort((a, b) => a - b), "skipped is chronological"); assert.deepEqual(new Set([...kept, ...skipped].map((i) => i.id)), new Set(order)); // Largest remainder: 20 of the same 50 -> 0.8, 12, 4, 0.4, 2.8 -> 1, 12, 4, 1, 2 = 20. assert.deepEqual(years(capInstances(all, 20).kept), { 2019: 1, 2020: 12, 2021: 4, 2022: 1, 2023: 2 }); // 24 -> 0.96, 14.4, 4.8, 0.48, 3.36 -> 1, 14, 4, 1, 3 = 23, +1 to 2021 (.8 under). assert.deepEqual(years(capInstances(all, 24).kept), { 2019: 1, 2020: 14, 2021: 5, 2022: 1, 2023: 3 }); }); test("capInstances: spread through the year, not its first few; the speaker-checked last", () => { const all = inst({ 2020: 12 }, ["2020-00", "2020-06"]); const { kept } = capInstances(all, 3); // Ten clean ones, three spread through them (the middle of each third): clean #1, #5, #8. assert.deepEqual(kept.map((i) => i.id), ["2020-02", "2020-07", "2020-10"]); assert.ok(kept.every((i) => !i.speakerCheck)); // More asked than clean: every clean one, then the checked. const few = inst({ 2021: 4 }, ["2021-00", "2021-01", "2021-02"]); // The one clean (03) and the middle of the three checked (01), chronological. assert.deepEqual(capInstances(few, 2).kept.map((i) => i.id), ["2021-01", "2021-03"]); }); test("capInstances: fewer than the years -> one each for years spread across them; no cap -> all", () => { const all = inst({ 2016: 3, 2017: 3, 2018: 3, 2019: 3, 2020: 3, 2021: 3 }); assert.deepEqual(Object.keys(years(capInstances(all, 3).kept)), ["2017", "2019", "2021"]); for (const max of [undefined, null, 0, -1, 2.5, 18, 100]) { const r = capInstances(all, max as number); assert.equal(r.kept.length, 18); assert.deepEqual(r.skipped, []); } // A dateless instance is its own year, last. const mixed = [...inst({ 2020: 4 }), { id: "none-00", date: null }]; assert.ok(capInstances(mixed, 2).kept.some((i) => i.id === "none-00")); });