// lib/copies/laneHold.ts: which pending copies a lane skips or holds. Pure; // synthetic channels and ids. import { test } from "node:test"; import assert from "node:assert/strict"; import { applyCopyHolds, copyPartnersToRead, type CopyPartner, type CopyPlan } from "./laneHold"; const NOW = Date.parse("2026-03-20T00:00:00Z"); const RECENT = "2026-03-18T00:00:00Z"; const LONG_AGO = "2026-03-01T00:00:00Z"; // pref-chan is preferred over mirror-chan. // Channels by rank: pref-chan 0, mirror-chan 1, third-chan 2. A video's // channel is its id's first letter (p, m, t). const CHANNEL: Record = { p: ["pref-chan", 0], m: ["mirror-chan", 1], t: ["third-chan", 2] }; const chan = (id: string) => CHANNEL[id[0]]; type PairSpec = { x: string; y: string; link: CopyPartner["link"]; firstSeenAt?: string; waitDays?: number; confirmedBy?: string }; function planOf(pairs: PairSpec[], released: string[] = []): CopyPlan { const byVideo = new Map(); const add = (k: string, p: CopyPartner) => byVideo.set(k, [...(byVideo.get(k) ?? []), p]); for (const s of pairs) { const [xc, xr] = chan(s.x); const [yc, yr] = chan(s.y); const pair = `${xc}/${s.x}|${yc}/${s.y}`; const common = { group: "g", pair, link: s.link, firstSeenAt: s.firstSeenAt ?? RECENT, waitDays: s.waitDays ?? 7, ...(s.confirmedBy ? { confirmedBy: s.confirmedBy } : {}), }; add(`${xc}/${s.x}`, { ...common, channel: yc, dir: s.y, rank: yr, selfRank: xr }); add(`${yc}/${s.y}`, { ...common, channel: xc, dir: s.x, rank: xr, selfRank: yr }); } return { byVideo, released: new Set(released) }; } // pref-chan is preferred over mirror-chan. function plan( pairs: { pref: string; mirror: string; link: CopyPartner["link"]; firstSeenAt?: string; waitDays?: number }[], released: string[] = [], ): CopyPlan { return planOf(pairs.map((x) => ({ ...x, x: x.pref, y: x.mirror })), released); } function owner(ids: string[]): Map { return new Map(ids.map((id) => [id, chan(id)[0]])); } function run( pendingIds: string[], p: CopyPlan, done: string[] = [], inFlight: string[] = [], everyone: string[] = pendingIds, ) { return applyCopyHolds({ lane: "transcription", work: "transcript", pending: { all: pendingIds }, owner: owner([...everyone, ...done, ...inFlight]), plan: p, inFlight: new Set(inFlight), done: new Map(done.map((k) => [k, true])), now: NOW, }); } test("an empty plan hands the lists back untouched", () => { const pending = { all: ["p1", "m1"] }; const out = applyCopyHolds({ lane: "transcription", work: "transcript", pending, owner: owner(["p1", "m1"]), plan: { byVideo: new Map(), released: new Set() }, inFlight: new Set(), done: new Map(), now: NOW, }); assert.equal(out.pending, pending); assert.deepEqual(out.decisions, []); }); test("a confirmed copy is skipped only when the other copy's work is done", () => { const p = plan([{ pref: "p1", mirror: "m1", link: "confirmed" }]); // Not done: the mirror waits for its queued preferred copy (held, not skipped). const notDone = run(["p1", "m1"], p); assert.deepEqual(notDone.pending.all, ["p1"]); assert.equal(notDone.decisions[0].action, "hold"); assert.match(notDone.decisions[0].reason, /waiting for the preferred copy pref-chan\/p1 \(queued/); // Done on disk: skipped. const done = run(["m1"], p, ["pref-chan/p1"]); assert.deepEqual(done.pending.all, []); assert.equal(done.decisions[0].action, "skip"); assert.equal(done.decisions[0].partner, "pref-chan/p1"); // Done on disk but still queued on this lane: not done. const stillQueued = run(["p1", "m1"], p, ["pref-chan/p1"]); assert.equal(stillQueued.decisions[0].action, "hold"); }); test("the copy that already has the transcript keeps it whatever the order", () => { const p = plan([{ pref: "p1", mirror: "m1", link: "confirmed" }]); // The FALLBACK copy has the transcript; the preferred one is pending. const out = run(["p1"], p, ["mirror-chan/m1"]); assert.deepEqual(out.pending.all, [], "the preferred copy is not transcribed again"); assert.equal(out.decisions[0].channel, "pref-chan"); assert.equal(out.decisions[0].action, "skip"); }); test("a metadata-only match is held, never skipped, then released after the wait", () => { const recent = plan([{ pref: "p1", mirror: "m1", link: "metadata", firstSeenAt: RECENT }]); const held = run(["m1", "p1"], recent); assert.deepEqual(held.pending.all, ["p1"]); assert.equal(held.decisions[0].action, "hold"); assert.equal(held.decisions[0].until, "2026-03-25T00:00:00.000Z"); assert.match(held.decisions[0].reason, /date and runtime only/); assert.match(held.decisions[0].reason, /copies-confirm/); assert.match(held.decisions[0].reason, /copies-release/); // The wait is over and the preferred copy still has no transcript: released. const old = plan([{ pref: "p1", mirror: "m1", link: "metadata", firstSeenAt: LONG_AGO }]); const released = run(["m1", "p1"], old); assert.deepEqual(released.pending.all, ["m1", "p1"]); assert.deepEqual(released.decisions, []); // Released by a person: the lane is today's lane for it. const byHand = run(["m1"], plan([{ pref: "p1", mirror: "m1", link: "metadata" }], ["mirror-chan/m1"]), ["pref-chan/p1"]); assert.deepEqual(byHand.pending.all, ["m1"]); assert.deepEqual(byHand.decisions, []); }); test("a metadata hold ALWAYS ends: the other copy done is held only until the wait, then runs (fails against the open-ended hold)", () => { // Within the wait, the done copy holds it (a confirm would make it a skip). const recent = run(["m1"], plan([{ pref: "p1", mirror: "m1", link: "metadata", firstSeenAt: RECENT }]), ["pref-chan/p1"]); assert.equal(recent.decisions[0].action, "hold"); assert.equal(recent.decisions[0].until, "2026-03-25T00:00:00.000Z"); // After it: the lane does the work, whichever copy is the done one. const old = plan([{ pref: "p1", mirror: "m1", link: "metadata", firstSeenAt: LONG_AGO }]); assert.deepEqual(run(["m1"], old, ["pref-chan/p1"]).pending.all, ["m1"], "the preferred copy done"); assert.deepEqual(run(["p1"], old, ["mirror-chan/m1"]).pending.all, ["p1"], "the fallback copy done"); }); test("waitDays 0 means no hold at all; a confirmed skip is not a hold and still applies", () => { const meta = plan([{ pref: "p1", mirror: "m1", link: "metadata", waitDays: 0 }]); assert.deepEqual(run(["m1"], meta, ["pref-chan/p1"]).pending.all, ["m1"]); assert.deepEqual(run(["p1", "m1"], meta).pending.all, ["p1", "m1"]); const conf = plan([{ pref: "p1", mirror: "m1", link: "confirmed", waitDays: 0 }]); assert.deepEqual(run(["p1", "m1"], conf).pending.all, ["p1", "m1"], "nobody waits"); assert.equal(run(["m1"], conf, ["pref-chan/p1"]).decisions[0].action, "skip"); }); test("a pair a person confirmed is a skip, and says whose word it was", () => { const p = planOf([{ x: "p1", y: "m1", link: "confirmed", confirmedBy: "operator", firstSeenAt: LONG_AGO }]); const out = run(["m1"], p, ["pref-chan/p1"]); assert.deepEqual(out.pending.all, []); assert.equal(out.decisions[0].action, "skip"); assert.match(out.decisions[0].reason, /by the operator's word/); }); test("chains: a copy never waits behind a copy that is itself skipped or held (fails against the one-pass hold)", () => { // p1 ~ m1 ~ t1, and p1, t1 not paired (dated two days apart). const chain = [ { x: "p1", y: "m1", link: "confirmed" as const }, { x: "m1", y: "t1", link: "confirmed" as const }, ]; // p1 is done: m1 is skipped, and t1 resolves THROUGH m1 to p1 — skipped too, // not held behind an m1 that will never run. const done = run(["m1", "t1"], planOf(chain), ["pref-chan/p1"]); assert.deepEqual(done.pending.all, []); const t1 = done.decisions.find((d) => d.id === "t1")!; assert.deepEqual([t1.action, t1.partner, t1.via], ["skip", "pref-chan/p1", "mirror-chan/m1"]); // p1 queued: m1 waits for it, and so does t1 — for p1, the copy that will run. const queued = run(["p1", "m1", "t1"], planOf(chain)); assert.deepEqual(queued.pending.all, ["p1"]); assert.deepEqual(queued.decisions.map((d) => [d.id, d.partner]), [["m1", "pref-chan/p1"], ["t1", "pref-chan/p1"]]); // A weaker link anywhere in the chain is the chain's link: metadata holds, never skips. const weak = run(["m1", "t1"], planOf([chain[0], { ...chain[1], link: "metadata" }]), ["pref-chan/p1"]); const t1w = weak.decisions.find((d) => d.id === "t1")!; assert.deepEqual([t1w.action, t1w.link], ["hold", "metadata"]); // m1 skipped because the FALLBACK t1 is done (work already done is kept): // p1, preferred and pending, resolves through m1 to t1 and is not redone. const back = run(["p1", "m1"], planOf([chain[0], chain[1]]), ["third-chan/t1"]); assert.deepEqual(back.pending.all, []); }); test("a preferred copy's job dying releases the copy held behind it", () => { const p = plan([{ pref: "p1", mirror: "m1", link: "confirmed" }]); // Tick 1: p1 in flight, m1 held behind it. const t1 = run(["m1"], p, [], ["pref-chan/p1"]); assert.deepEqual(t1.pending.all, []); assert.match(t1.decisions[0].reason, /in flight/); // p1's job failed: the runner retires it (neither in flight nor pending), and // it left no transcript. Tick 2: m1 runs. const t2 = run(["m1"], p, [], []); assert.deepEqual(t2.pending.all, ["m1"]); assert.deepEqual(t2.decisions, []); // Failed and queued again for a retry: m1 waits again, within the wait. assert.deepEqual(run(["p1", "m1"], p).pending.all, ["p1"]); }); test("a preferred copy that is gone or unavailable does not block the mirror", () => { // Confirmed, the preferred copy neither done, queued nor in flight (gone at // its source, on a paused channel, never downloaded): the mirror runs now. const p = plan([{ pref: "p1", mirror: "m1", link: "confirmed" }]); assert.deepEqual(run(["m1"], p).pending.all, ["m1"]); // In flight on this lane: held. assert.deepEqual(run(["m1"], p, [], ["pref-chan/p1"]).pending.all, []); // Queued, but the wait is over: runs. const old = plan([{ pref: "p1", mirror: "m1", link: "confirmed", firstSeenAt: LONG_AGO }]); assert.deepEqual(run(["p1", "m1"], old).pending.all, ["p1", "m1"]); }); test("the preferred copy is never held by a fallback that is not done", () => { const p = plan([ { pref: "p1", mirror: "m1", link: "confirmed" }, { pref: "p2", mirror: "m2", link: "metadata" }, ]); const out = run(["p1", "p2"], p); assert.deepEqual(out.pending.all, ["p1", "p2"]); assert.deepEqual(out.decisions, []); }); test("the partners to read are the pending videos' partners, released ones left out", () => { const p = plan([{ pref: "p1", mirror: "m1", link: "confirmed" }, { pref: "p2", mirror: "m2", link: "confirmed" }], ["mirror-chan/m2"]); assert.deepEqual([...copyPartnersToRead({ a: ["m1", "m2"] }, owner(["m1", "m2"]), p)], ["pref-chan/p1"]); });