// lib/copies/dating.ts: the date a copies scan pairs a video by. Every title, // channel and date here is synthetic. import { test } from "node:test"; import assert from "node:assert/strict"; import { recordedDatePatternProblem } from "../recordedDate"; import { dateChannel, dateEvidence, datingAdvice, detectTitleDate, noCandidatesAdvice, recordedDatePatchCommand, TITLE_DATE_SHAPES, type DatingRecord, } from "./dating"; const upload = (title: string, uploadDate = "20260310"): DatingRecord => ({ title, uploadDate, wasLive: false }); const live = (title: string, uploadDate: string): DatingRecord => ({ title, uploadDate, wasLive: true }); test("every built-in title shape is a valid recordedDate rule, so the one detected can be proposed verbatim", () => { for (const s of TITLE_DATE_SHAPES) assert.equal(recordedDatePatternProblem(s.pattern), null, s.id); }); test("the order: the channel's rule, else a live stream's day, else a title date, else the upload", () => { const re = new RegExp(TITLE_DATE_SHAPES[0].pattern, "i"); assert.deepEqual(dateEvidence({ ...upload("2023-01-02 x"), recordedDate: "20230101" }, re), { date: "20230101", from: "rule" }); assert.deepEqual(dateEvidence(live("2023-01-02 x", "20230103"), re), { date: "20230103", from: "live" }); assert.deepEqual(dateEvidence(upload("2023-01-02 x"), re), { date: "20230102", from: "title" }); assert.deepEqual(dateEvidence(upload("2023-01-02 x"), null), { date: "20260310", from: "upload" }); // A title date after the upload is a date the title mentions, not the stream's. assert.deepEqual(dateEvidence(upload("2027-01-02 preview"), re), { date: "20260310", from: "upload" }); assert.equal(dateEvidence({ title: "x", uploadDate: "", wasLive: false }, re), null); }); test("a re-upload channel's leading title dates are detected in their mixed spellings (fails against upload-date dating)", () => { const d = dateChannel( [upload("2023 01 04 a stream"), upload("2023-01-05 the stream"), upload("2023 Jan 06 more"), upload("2012 a stream part 2")], false, ); assert.deepEqual( d.dated.map((x) => [x.date, x.from]), [ ["20230104", "title"], ["20230105", "title"], ["20230106", "title"], ["20260310", "upload"], ], ); assert.equal(d.dating.detectedPattern, TITLE_DATE_SHAPES[0].pattern); assert.deepEqual(d.dating.span, { from: "20230104", to: "20260310" }); const advice = datingAdvice("pref-chan", d, null).join("\n"); assert.match(advice, /pref-chan: 3 of 4 videos are dated by a date their titles carry/); assert.ok(advice.includes(recordedDatePatchCommand("pref-chan", TITLE_DATE_SHAPES[0].pattern))); assert.match(advice, /pref-chan: 1 of 4 videos have only an upload date/); }); test("a mirror's tagged titles are detected; a live channel is dated by its streams whatever its titles say", () => { const tagged = dateChannel([upload("[Copy] 2023-01-04"), upload("[Copy] 2023-01-05 Part 2/2"), upload("[Copy] 2023-01-07")], false); assert.equal(tagged.dating.byFrom.title, 3); const lives = dateChannel([live("[Copy] 2023-01-04", "20230105"), live("A stream", "20230106")], false); assert.deepEqual(lives.dating.byFrom, { rule: 0, live: 2, title: 0, upload: 0, none: 0 }); assert.equal(lives.dating.detectedPattern, undefined); assert.deepEqual(datingAdvice("mirror-chan", lives, null), [], "nothing to set on a live channel"); }); test("a shape that reads only a few titles does not stand for the channel; the advice names it with what it reads", () => { const recs = [upload("Episode 1"), upload("Episode 2"), upload("Recap of 2023-01-04"), upload("Episode 3"), upload("Episode 4")]; assert.equal(detectTitleDate(recs), null); const d = dateChannel(recs, false); assert.equal(d.dating.byFrom.upload, 5); const advice = datingAdvice("pref-chan", d, "searched by runtime.").join("\n"); assert.match(advice, /closest title shape reads 1 of 5/); assert.match(advice, /searched by runtime\./); }); test("with the channel's rule set, no shape is guessed past it", () => { const d = dateChannel([{ ...upload("2023-01-04 x"), recordedDate: "20230104" }, upload("2023 Jan 05 y")], true); assert.deepEqual(d.dating.byFrom, { rule: 1, live: 0, title: 0, upload: 1, none: 0 }); assert.doesNotMatch(datingAdvice("pref-chan", d, null).join("\n"), /recordedDateTitlePattern/); }); test("the patch command is one valid JSON body, and a scan with no dated pair names each member's span", () => { const cmd = recordedDatePatchCommand("pref-chan", TITLE_DATE_SHAPES[0].pattern); const body = JSON.parse(/--json '(.*)'$/.exec(cmd)![1]); assert.deepEqual(body, { slug: "pref-chan", patch: { recordedDateTitlePattern: TITLE_DATE_SHAPES[0].pattern } }); assert.equal( noCandidatesAdvice(1, [ { slug: "pref-chan", span: { from: "20260303", to: "20261118" } }, { slug: "mirror-chan", span: { from: "20230104", to: "20260225" } }, ]), "no two members' videos are dated within 1 day(s) of each other (pref-chan 2026-03-03…2026-11-18; mirror-chan 2023-01-04…2026-02-25): check the dates above before reading 0 as \"no copies\".", ); });