// The phrase finder's own parts (release 34): the spec check, the summary, // the slug, and `umtool phrases` over a synthetic corpus — its job form // (--spec/--out/--progress) and its terminal form. import assert from "node:assert/strict"; import { execFile } from "node:child_process"; import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; import { promisify } from "node:util"; import { phraseScan } from "yt-dlp-transcript-common/lib/montage/phraseScan.mjs"; import { wordsPhraseSource } from "yt-dlp-transcript-common/lib/montage/phraseSource.mjs"; import { checkSpec } from "./phrase-finder/scan.mjs"; import { byYear, montageSlug, sparkline, spread, summarisePhrase } from "./phrase-finder/summary.mjs"; const run = promisify(execFile); const CLI = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "bin", "umtool.mjs"); function corpus() { const dir = mkdtempSync(path.join(os.tmpdir(), "phrase-finder-")); const put = (slug, id, rec) => { 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}`, ...rec })); }; put("rum", "r1", { uploadDate: "20200105", duration: 600, platform: "rumble", cues: [{ start: 10, end: 13, text: "what a slow news day" }, { start: 50, end: 53, text: "nobody asked for this" }] }); put("rum", "r2", { uploadDate: "20220310", duration: 900, platform: "rumble", cues: [{ start: 5, end: 8, text: ">> slow, news days again" }] }); put("yt", "y1", { uploadDate: "20180101", duration: 300, platform: "youtube", cues: [{ start: 1, end: 4, text: "a slow news week this is" }, { start: 90, end: 93, text: "and nobody asked" }] }); put("yt", "y2", { uploadDate: "20220601", duration: 400, platform: "youtube", cues: [{ start: 7, end: 9, text: "no one asked" }] }); return dir; } test("checkSpec: phrases become what is searched; a bad one is refused with its sentence", () => { const s = checkSpec({ rows: [{ id: "a", text: " slow news day ", mode: "auto" }, { text: "" }, { text: "no ?one asked", label: "nobody asked" }], channels: "yt, rum", prefer: "rum", exclude: "" }); assert.deepEqual(s.rows, [ { id: "a", text: "slow news day", mode: "auto", source: wordsPhraseSource("slow news day"), used: "words" }, { id: "p3", text: "no ?one asked", label: "nobody asked", mode: "auto", source: "no ?one asked", used: "regex" }, ]); assert.deepEqual(s.channels, ["yt", "rum"]); assert.equal(s.prefer, "rum"); assert.equal(s.exclude, null); assert.equal(checkSpec({ rows: [{ text: "x" }], channels: ["HasanAbiVODs3"] }).channels[0], "HasanAbiVODs3"); assert.throws(() => checkSpec({ rows: [], channels: ["yt"] }), /type a phrase/); assert.throws(() => checkSpec({ rows: [{ text: "slow (news" }], channels: ["yt"] }), /"slow \(news": not a regular expression/); assert.throws(() => checkSpec({ rows: [{ text: "x" }], channels: [] }), /pick at least one channel/); assert.throws(() => checkSpec({ rows: [{ text: "x" }], channels: ["../etc"] }), /is not a channel/); assert.throws(() => checkSpec({ rows: [{ text: "x" }], channels: ["yt"], exclude: "(" }), /exclude is not a regular expression/); assert.throws(() => checkSpec({ rows: Array.from({ length: 31 }, (_, k) => ({ text: `w${k}` })), channels: ["yt"] }), /at most 30/); }); test("the summary: by year with the gaps, the split, the spread of samples", async () => { assert.deepEqual(byYear([{ date: "20180101" }, { date: "20200105" }, { date: "20200301" }, { date: null }]), { 2018: 1, 2019: 0, 2020: 2 }); assert.equal(sparkline({ 2018: 1, 2019: 0, 2020: 2 }), "▄ █"); assert.deepEqual(spread([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 3), [1, 6, 11]); assert.deepEqual(spread([1, 2], 10), [1, 2]); assert.equal(montageSlug("slow news day"), "slow-news-day-montage"); assert.equal(montageSlug(String.raw`\b(?:no ?one|nobody) asked\b`), "no-one-nobody-asked-montage"); assert.equal(montageSlug("!!!"), "phrase-montage"); const dir = corpus(); try { const r = await phraseScan({ channelsDir: dir, channels: ["yt"], prefer: "rum", phrase: wordsPhraseSource("slow news day") }); const s = summarisePhrase(r, { prefer: "rum", samples: 2 }); assert.equal(s.instances, 2); assert.deepEqual(s.years, { 2020: 1, 2021: 0, 2022: 1 }); assert.equal(s.rumble, 2); assert.equal(s.notRumble, 0); assert.equal(s.preferred, 2); assert.equal(s.speakerChecks, 1); assert.deepEqual(s.samples.map((x) => x.videoId), ["r1", "r2"]); assert.equal(s.samples[1].match, "slow, news days"); } finally { rmSync(dir, { recursive: true, force: true }); } }); test("umtool phrases: the job form writes the whole answer and its progress; the terminal form prints the numbers", async () => { const dir = corpus(); const cache = mkdtempSync(path.join(os.tmpdir(), "phrase-finder-cache-")); const env = { ...process.env, CHANNELS_DIR: dir, UMTOOL_CACHE_DIR: cache }; try { const spec = path.join(cache, "spec.json"); const out = path.join(cache, "out.json"); writeFileSync(spec, JSON.stringify({ rows: [{ id: "slow", text: "slow news day" }, { id: "asked", text: String.raw`\b(?:no ?one|nobody) asked\b` }], channels: ["yt", "rum"], prefer: null })); const { stdout } = await run(process.execPath, [CLI, "phrases", "--spec", spec, "--out", out, "--progress"], { env }); const lines = stdout.trim().split("\n").map((l) => JSON.parse(l)); assert.deepEqual(lines.at(-2).progress, { done: 4, total: 4, channel: "rum", channelsDone: 2, channels: 2 }); assert.equal(lines.at(-1).done, true); const answer = JSON.parse(readFileSync(out, "utf8")); assert.equal(answer.scanned, 4); assert.deepEqual(answer.rows.map((r) => [r.id, r.used, r.result.instances.length]), [["slow", "words", 2], ["asked", "regex", 3]]); // Each phrase's answer is phraseScan's for it alone. const alone = await phraseScan({ channelsDir: dir, channels: ["yt", "rum"], prefer: null, phrase: answer.rows[1].source }); assert.deepEqual(answer.rows[1].result, JSON.parse(JSON.stringify(alone))); const text = await run(process.execPath, [CLI, "phrases", "slow news day", "nobody asked", "--channels", "yt,rum", "--samples", "1"], { env }); assert.match(text.stdout, /4 transcripts read/); assert.match(text.stdout, /slow news day {2}2 time\(s\) {2}2020–2022/); assert.match(text.stdout, /searched as \/\\bnobody\[\\s,\.\\-\]\+asked\(\?:s\|es\)\?\\b\/ \(words\)/); const json = JSON.parse((await run(process.execPath, [CLI, "phrases", "slow news", "--channels", "yt", "--prefer", "rum", "--json"], { env })).stdout); assert.equal(json.rows[0].instances, 3); assert.equal(json.rows[0].preferred, 2); const bad = await run(process.execPath, [CLI, "phrases", "slow (news", "--channels", "yt", "--regex"], { env }).catch((e) => e); assert.equal(bad.code, 1); assert.match(bad.stderr, /not a regular expression/); } finally { rmSync(dir, { recursive: true, force: true }); rmSync(cache, { recursive: true, force: true }); } });