// The mirror matcher (release 31, lane D): match.py's rules on hand-built // cases (each rule branch, the day window's edges, stop words, the topic), // status.py's strong cut and the four statuses, then the synthetic mirror // corpus both ways. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/mirrors/match.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { addDays, contentDate, durationsAgree, isStrong, matchMirror, matchRule, mirrorTopic, statusOf, titleJaccard, titleWords, MIRROR_DURATION_TOLERANCE, MIRROR_LIVE_MIN_SECONDS, type MirrorRecord, } from "./match"; import { CREATOR_RECORDS, CREATOR_SLUG, EXPECTED, MIRROR_RECORDS, MIRROR_SLUG, RECORDED_DATE_PATTERN, RUMBLE_RECORDS, RUMBLE_SLUG, } from "../../testing/mirrors/mirrorCorpus.mjs"; const RULE = new RegExp(RECORDED_DATE_PATTERN, "i"); function rec(id: string, title: string, date: string, extra: Partial = {}): MirrorRecord { return { id, title, uploadDate: date, duration: 0, wasLive: false, gone: null, ...extra }; } function one(mirror: MirrorRecord, creators: MirrorRecord[], platform = "youtube") { return matchMirror({ mirror: { slug: "m", platform: "odysee", records: [mirror], titleRule: RULE }, creators: [{ slug: "c", platform, records: creators }], }).videos[0]; } test("words: lowercase [a-z0-9'] runs, one-letter words and stop words dropped", () => { assert.deepEqual([...titleWords("The Oven's BIG Day & More w/ a Guest: 2023!")].sort(), ["2023", "big", "day", "guest", "oven's"]); assert.deepEqual([...titleWords("This is what he gets, and got")], ["what", "he"]); assert.equal(titleJaccard(new Set(["a", "b"]), new Set(["b", "c"])), 1 / 3); assert.equal(titleJaccard(new Set(), new Set()), 0); }); test("topic: the title after the date the rule reads, before it when nothing follows, whole otherwise", () => { assert.equal(mirrorTopic("[VOD] Name - 03/01/2023 - Big News Day", RULE), "Big News Day"); assert.equal(mirrorTopic("Big News Day | 03/01/2023", RULE), "Big News Day"); assert.equal(mirrorTopic("[VOD] Name - Bonus Stream", RULE), "[VOD] Name - Bonus Stream"); assert.equal(mirrorTopic("Anything", null), "Anything"); }); test("dates: recorded first, else upload; whole calendar days", () => { assert.equal(contentDate({ uploadDate: "20230304", recordedDate: "20230301" }), "20230301"); assert.equal(contentDate({ uploadDate: "20230304" }), "20230304"); assert.equal(contentDate({ uploadDate: "" }), null); assert.equal(addDays("20230228", 1), "20230301"); assert.equal(addDays("20240101", -1), "20231231"); }); test("durations agree under 25% of the mirror's, both known", () => { assert.equal(MIRROR_DURATION_TOLERANCE, 0.25); assert.equal(durationsAgree(1000, 1249), true); assert.equal(durationsAgree(1000, 1250), false, "strictly less"); assert.equal(durationsAgree(1000, 751), true); assert.equal(durationsAgree(0, 1000), false); assert.equal(durationsAgree(1000, 0), false); }); test("rules in order: title ≥ 0.25; ≥ 0.12 with durations; a long stream with durations", () => { const stream = { duration: 4000, wasLive: true }; assert.equal(matchRule(0.25, false, stream), "title"); assert.equal(matchRule(0.24, false, stream), null); assert.equal(matchRule(0.12, true, { duration: 100, wasLive: false }), "title-duration"); assert.equal(matchRule(0.11, true, { duration: 100, wasLive: false }), null); assert.equal(matchRule(0, true, stream), "live-duration"); assert.equal(matchRule(0, true, { duration: MIRROR_LIVE_MIN_SECONDS, wasLive: true }), null, "longer than 3000 s"); assert.equal(matchRule(0, true, { duration: 4000, wasLive: false }), null, "a stream"); assert.equal(matchRule(0, false, stream), null, "durations must agree"); }); test("strong: score ≥ 0.5, or ≥ 0.25 with durations (the score is the Jaccard to two decimals)", () => { assert.equal(isStrong(0.5, false), true); assert.equal(isStrong(0.49, false), false); assert.equal(isStrong(0.25, true), true); assert.equal(isStrong(0.24, true), false); }); test("each rule branch on hand-built videos", () => { const m = rec("m1", "[VOD] X - 03/01/2023 - alpha beta gamma delta", "20230305", { recordedDate: "20230301", duration: 4000 }); // title: 2 of 6 words = 0.33 — a match, strong only with durations. let v = one(m, [rec("c1", "alpha beta zeta eta", "20230301")]); assert.equal(v.matches[0].rule, "title"); assert.equal(v.matches[0].score, 0.33); assert.equal(v.matches[0].strong, false); v = one(m, [rec("c1", "alpha beta zeta eta", "20230301", { duration: 4100 })]); assert.equal(v.matches[0].strong, true); // under 0.25 with no durations: nothing. assert.equal(one(m, [rec("c1", "alpha zeta", "20230301")]).matches.length, 0, "1 of 5 = 0.2"); v = one(m, [rec("c1", "alpha beta gamma", "20230301")]); assert.equal(v.matches[0].score, 0.75); assert.equal(v.matches[0].strong, true); // title-duration: 1/7 ≈ 0.14 with durations. v = one(m, [rec("c2", "alpha one two three", "20230301", { duration: 4100 })]); assert.equal(v.matches[0].rule, "title-duration"); assert.equal(v.matches[0].strong, false); // live-duration: nothing shared, a 4100 s stream. v = one(m, [rec("c3", "unrelated words", "20230301", { duration: 4100, wasLive: true })]); assert.equal(v.matches[0].rule, "live-duration"); assert.equal(v.matches[0].strong, false); assert.equal(v.status, "only-copy", "a weak match decides nothing"); // none. v = one(m, [rec("c4", "unrelated words", "20230301", { duration: 9000, wasLive: true })]); assert.equal(v.matches.length, 0); }); test("the day window: one day before to two after the recorded date", () => { const m = rec("m1", "[VOD] X - 03/10/2023 - alpha beta", "20230320", { recordedDate: "20230310" }); const at = (d: string) => one(m, [rec("c", "alpha beta", d)]).matches.length; assert.equal(at("20230308"), 0); assert.equal(at("20230309"), 1); assert.equal(at("20230310"), 1); assert.equal(at("20230312"), 1); assert.equal(at("20230313"), 0); // No recorded date: the upload date. const u = rec("m2", "alpha beta", "20230320"); assert.equal(one(u, [rec("c", "alpha beta", "20230321")]).matches.length, 1); assert.equal(one(u, [rec("c", "alpha beta", "20230310")]).matches.length, 0); }); test("stop words do not make a match", () => { const m = rec("m1", "[VOD] X - 03/10/2023 - the more of this", "20230320", { recordedDate: "20230310" }); assert.equal(one(m, [rec("c", "The More Of This", "20230310")]).matches.length, 0); }); test("statuses from presence", () => { assert.equal(statusOf({ youtube: "none", rumble: "none" }), "only-copy"); assert.equal(statusOf({ youtube: "gone", rumble: "none" }), "only-copy"); assert.equal(statusOf({ youtube: "gone", rumble: "public" }), "creator-deleted"); assert.equal(statusOf({ youtube: "none", rumble: "public" }), "never-uploaded"); assert.equal(statusOf({ youtube: "public", rumble: "public" }), "creator-public"); assert.equal(statusOf({ youtube: "public" }), "creator-public"); }); test("the synthetic mirror corpus, both directions", () => { const r = matchMirror({ mirror: { slug: MIRROR_SLUG, platform: "odysee", records: MIRROR_RECORDS, titleRule: RULE }, creators: [ { slug: CREATOR_SLUG, platform: "youtube", records: CREATOR_RECORDS }, { slug: RUMBLE_SLUG, platform: "rumble", records: RUMBLE_RECORDS }, ], }); assert.deepEqual(r.window, EXPECTED.window); assert.deepEqual(Object.fromEntries(r.videos.map((v) => [v.id, v.status])), EXPECTED.status); assert.deepEqual(r.totals.byStatus, EXPECTED.byStatus); assert.deepEqual(r.totals.byPlatform, EXPECTED.byPlatform); assert.equal(r.totals.creatorRemoved.inWindow, EXPECTED.creatorRemoved.inWindow); assert.equal(r.totals.creatorRemoved.held, EXPECTED.creatorRemoved.held); assert.deepEqual(Object.fromEntries(r.removed.map((x) => [x.id, x.heldBy])), EXPECTED.held); assert.deepEqual(r.totals.creatorRemoved.byChannel, { [CREATOR_SLUG]: { inWindow: 3, held: 2 }, [RUMBLE_SLUG]: { inWindow: 0, held: 0 }, }); const m2 = r.videos.find((v) => v.id === "vod-m2")!; assert.deepEqual( m2.matches.map((x) => [x.channel, x.id, x.gone ?? null, x.strong]), [ [CREATOR_SLUG, "yt-s2", "deleted", true], [RUMBLE_SLUG, "v4embed2", null, true], ], ); assert.equal(m2.matches[1].dir, "v1abd-oven-lawsuit", "a Rumble match names its dir"); assert.equal(r.totals.matched, 4); assert.equal(r.totals.dated, 6); });