import { test } from "node:test"; import assert from "node:assert/strict"; import { COVERED_THRESHOLD, addDays, airDateOf, foldRecordings, inWindow, isIsoDate, localDateOf, lookoutWindow, type DedupeVideo, } from "./lookout"; import { shingles } from "./duplicates"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/lookout.test.ts // A real start time: 2026-11-04T01:20:53Z, an evening stream in New York. const EVENING_STREAM = 1793755253; test("the UTC trap: an evening stream airs on the local date, not on upload_date's", () => { const ny = airDateOf({ releaseTimestamp: EVENING_STREAM, timestamp: EVENING_STREAM - 488, uploadDate: "20261104" }, "America/New_York"); assert.deepEqual(ny, { date: "2026-11-03", at: "2026-11-04T01:20:53.000Z", source: "release_timestamp" }); assert.equal(airDateOf({ releaseTimestamp: EVENING_STREAM, uploadDate: "20261104" }, "UTC")?.date, "2026-11-04"); assert.equal(airDateOf({ releaseTimestamp: EVENING_STREAM, uploadDate: "20261104" }, "Europe/Berlin")?.date, "2026-11-04"); }); test("release_timestamp, else timestamp, else upload_date — and a far-off timestamp is not believed", () => { assert.equal(airDateOf({ timestamp: EVENING_STREAM, uploadDate: "20261104" }, "America/New_York")?.source, "timestamp"); assert.deepEqual(airDateOf({ uploadDate: "20261104" }, "America/New_York"), { date: "2026-11-04", at: null, source: "upload_date" }); // A nested key read by mistake, months away: the upload date wins. assert.deepEqual(airDateOf({ timestamp: 1600000000, uploadDate: "20261104" }, "UTC"), { date: "2026-11-04", at: null, source: "upload_date" }); assert.equal(airDateOf({}, "UTC"), null); assert.equal(airDateOf({ timestamp: 0, uploadDate: "garbage" }, "UTC"), null); }); test("the window counts calendar days in the lookout's zone, both ends included", () => { assert.deepEqual(lookoutWindow({ days: 7, timeZone: "America/New_York", until: "2026-11-10" }), { from: "2026-11-04", to: "2026-11-10", days: 7, timeZone: "America/New_York", }); // "Today" is the zone's today: 03:00Z on 11 Nov is still 10 Nov in New York. const now = new Date("2026-11-11T03:00:00Z"); assert.equal(lookoutWindow({ days: 1, timeZone: "America/New_York", now }).to, "2026-11-10"); assert.equal(lookoutWindow({ days: 1, timeZone: "UTC", now }).to, "2026-11-11"); assert.ok(inWindow("2026-11-04", { from: "2026-11-04", to: "2026-11-10" })); assert.ok(!inWindow("2026-11-03", { from: "2026-11-04", to: "2026-11-10" })); assert.equal(addDays("2026-02-28", 1), "2026-03-01"); assert.equal(addDays("2026-03-01", -1), "2026-02-28"); assert.ok(isIsoDate("2026-11-10") && !isIsoDate("2026-02-30") && !isIsoDate("20261110")); assert.equal(localDateOf(Date.UTC(2026, 10, 4, 4, 59), "America/New_York"), "2026-11-03"); }); function text(seed: number, n: number): string[] { let x = seed >>> 0; return Array.from({ length: n }, () => { x = (Math.imul(x, 1664525) + 1013904223) >>> 0; return `w${(x >>> 16) % 3000}`; }); } const sh = (ws: string[]) => shingles(ws.join(" "), 5); function vid(key: string, o: Partial & { words: string[] }): DedupeVideo { return { key, title: o.title ?? key, platform: o.platform ?? "youtube", duration: o.duration ?? 600, airAt: o.airAt ?? "2026-11-04T12:00:00.000Z", shingles: sh(o.words), }; } test("a twin upload with the same title and runtime is folded, the first on the air read", () => { const ws = text(1, 2000); const groups = foldRecordings([ vid("b/yt", { words: ws, title: "Big News!", duration: 824, airAt: "2026-11-04T15:00:00.000Z" }), vid("a/rumble", { words: ws.map((w, i) => (i % 12 === 0 ? "zz" : w)), title: "BIG NEWS", platform: "rumble", duration: 825, airAt: "2026-11-04T14:00:00.000Z" }), ]); assert.equal(groups.length, 1); assert.equal(groups[0]!.primary, "a/rumble"); assert.deepEqual(groups[0]!.folded.map((f) => [f.key, f.reason, f.titleMatch]), [["b/yt", "duplicate-upload", true]]); }); test("a re-titled mirror is caught by its text; a same-titled different video is caught by its runtime", () => { const ws = text(2, 2000); const retitled = foldRecordings([ vid("a/one", { words: ws, title: "THIS IS DISGRACEFUL!", duration: 825 }), vid("b/two", { words: ws, title: "Massive Story Revealed & More", duration: 824, platform: "rumble" }), ]); assert.equal(retitled.length, 1); assert.equal(retitled[0]!.folded[0]!.titleMatch, undefined); assert.ok(retitled[0]!.folded[0]!.score > 0.9); // The same title, a runtime 10 % off, unrelated text: two recordings. const series = foldRecordings([ vid("a/ep1", { words: text(3, 2000), title: "Daily Show", duration: 900 }), vid("a/ep2", { words: text(4, 2000), title: "Daily Show", duration: 1000 }), ]); assert.equal(series.length, 2); // The same title and the same runtime, unrelated text: two episodes, both read. const episodes = foldRecordings([ vid("a/mon", { words: text(5, 2000), title: "Daily Show", duration: 900 }), vid("a/tue", { words: text(6, 2000), title: "Daily Show", duration: 905 }), ]); assert.equal(episodes.length, 2); // ...unless one has no text to compare: then the title and runtime decide. const silent = foldRecordings([ vid("a/mon", { words: text(5, 2000), title: "Daily Show", duration: 900 }), vid("b/copy", { words: [], title: "Daily Show", duration: 905 }), ]); assert.equal(silent.length, 1); // Near-identical text but runtimes far apart is not a twin (it may be a cut-down; see below). const apart = foldRecordings([ vid("a/x", { words: ws, duration: 600 }), vid("b/y", { words: ws, duration: 700 }), ]); assert.equal(apart.length, 2); }); test("a cut-down is credited to the stream it is cut from, with its share", () => { const stream = text(5, 20000); const groups = foldRecordings([ vid("s/stream", { words: stream, duration: 7200, platform: "rumble", airAt: "2026-11-04T01:20:53.000Z" }), vid("c/cut", { words: stream.slice(5000, 6500), duration: 700, title: "Clip" }), vid("c/cut-twin", { words: stream.slice(5000, 6500), duration: 701, title: "Clip", platform: "rumble" }), vid("o/other", { words: text(6, 1500), duration: 700, title: "Other" }), ]); assert.deepEqual(groups.map((g) => g.primary), ["s/stream", "o/other"]); const s = groups[0]!; assert.deepEqual( s.folded.map((f) => [f.key, f.reason, f.matched]).sort(), [ ["c/cut", "covered-by-stream", "s/stream"], ["c/cut-twin", "covered-by-stream", "s/stream"], ], ); assert.ok(s.folded.every((f) => f.score >= COVERED_THRESHOLD)); }); test("a short video sharing a little of a stream is not its cut-down", () => { const stream = text(7, 20000); const mixed = [...stream.slice(100, 300), ...text(8, 800)]; const groups = foldRecordings([ vid("s/stream", { words: stream, duration: 7200 }), vid("c/mixed", { words: mixed, duration: 400 }), ]); assert.equal(groups.length, 2); }); test("every input appears exactly once, as a primary or folded", () => { const stream = text(9, 10000); const vids = [ vid("s/1", { words: stream, duration: 4000 }), vid("s/2", { words: stream, duration: 4010, platform: "rumble" }), vid("c/1", { words: stream.slice(0, 900), duration: 500 }), vid("c/2", { words: text(10, 900), duration: 500 }), vid("c/3", { words: text(10, 900), duration: 501, platform: "rumble" }), ]; const groups = foldRecordings(vids); const seen = groups.flatMap((g) => [g.primary, ...g.folded.map((f) => f.key)]).sort(); assert.deepEqual(seen, vids.map((v) => v.key).sort()); });