import { beforeEach, test } from "node:test"; import assert from "node:assert/strict"; import { mediaDriveHeldText, mediaStallHold } from "./mediaStallHold"; import { recordLocationHealth, registerLocationHealth, resetStorageHealth, } from "./storageHealth"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/mediaStallHold.test.ts // // The batch hold (lane drive-stall): memory only, edge-logged. beforeEach(() => resetStorageHealth()); const USB = { id: "usb", label: "USB drive", root: "/mnt/usb" }; test("the hold follows the channel's media location, and logs once per edge", () => { registerLocationHealth([USB]); const lines: string[] = []; const hold = mediaStallHold({ mediaDir: "/mnt/usb/chan/media" }, (l) => lines.push(l), "transcription"); assert.equal(hold.check(), null); recordLocationHealth(USB, "stalled", { now: Date.now() }); assert.equal(hold.check()?.id, "usb"); assert.equal(hold.check()?.id, "usb"); recordLocationHealth(USB, "ok"); recordLocationHealth(USB, "ok"); assert.equal(hold.check(), null); assert.equal(hold.check(), null); assert.equal(lines.length, 2); assert.match(lines[0], /^Holding: the media drive "USB drive" is not answering \(since \d\d:\d\d\) — no new transcription starts until it answers again\.$/); assert.equal(lines[1], `The media drive "USB drive" answers again — resuming.`); }); test("a channel in place, or on another location, is never held", () => { registerLocationHealth([USB]); recordLocationHealth(USB, "stalled"); const lines: string[] = []; assert.equal(mediaStallHold({}, (l) => lines.push(l), "x").check(), null); assert.equal(mediaStallHold(null, (l) => lines.push(l), "x").check(), null); assert.equal(mediaStallHold({ mediaDir: "/mnt/other/c/media" }, (l) => lines.push(l), "x").check(), null); assert.deepEqual(lines, []); // A legacy channel's retired dataDir on the location is held too. assert.equal(mediaStallHold({ dataDir: "/mnt/usb/c/data" }, () => {}, "x").check()?.id, "usb"); }); test("the refusal sentence names the drive and since when", () => { assert.match(mediaDriveHeldText({ label: "USB drive", since: Date.now() }), /^the media drive "USB drive" is not answering \(since \d\d:\d\d\)$/); }); // ── the hold heals itself (review of lane drive-stall) ────────────────────── const tick = () => new Promise((r) => setImmediate(r)); test("held past reprobeAfterMs with no pass keeping the record: one out-of-process probe answers and clears it", async () => { const { locationHealth } = await import("./storageHealth"); let t = 1_000_000; registerLocationHealth([USB], t); // Recorded once, long ago — no pass refreshes it (a CLI, an idle record). recordLocationHealth(USB, "stalled", { now: t }); const probed: string[] = []; const lines: string[] = []; const hold = mediaStallHold({ mediaDir: "/mnt/usb/chan/media" }, (l) => lines.push(l), "transcription", { now: () => t, reprobeAfterMs: 60_000, stillHoldingEveryMs: 600_000, reprobe: async (root) => { probed.push(root); return "ok"; }, }); assert.ok(hold.check()); t += 30_000; assert.ok(hold.check()); await tick(); assert.deepEqual(probed, [], "not before reprobeAfterMs"); t += 31_000; assert.ok(hold.check(), "the probe is asynchronous: this pull is still held"); await tick(); await tick(); assert.deepEqual(probed, ["/mnt/usb"]); assert.notEqual(locationHealth("usb")?.state, "stalled", "a stale record is cleared by the answer"); assert.equal(hold.check(), null, "and the next pull resumes"); assert.match(lines.join("\n"), /answered a stat of its root after 1 min held — no health pass was keeping its stall record/); assert.equal(lines.at(-1), `The media drive "USB drive" answers again — resuming.`); }); test("a record a pass still keeps: the probe's answer counts once, and the pass decides", async () => { const { locationHealth } = await import("./storageHealth"); let t = Date.now(); registerLocationHealth([USB], t); recordLocationHealth(USB, "stalled", { now: t }); const hold = mediaStallHold({ mediaDir: "/mnt/usb/c/media" }, () => {}, "x", { now: () => t, reprobeAfterMs: 1_000, reprobe: async () => "ok", }); hold.check(); t += 1_500; // A pass answered moments ago (the counters still say stalled). recordLocationHealth(USB, "stalled", { now: t }); hold.check(); await tick(); await tick(); const h = locationHealth("usb"); assert.equal(h?.state, "stalled", "one clean answer does not clear (two are needed)"); assert.equal(h?.cleanStreak, 1); }); test("a drive that does not answer the probe stays held, and 'still holding' is logged every interval", async () => { let t = 5_000_000; registerLocationHealth([USB], t); recordLocationHealth(USB, "stalled", { now: t }); const lines: string[] = []; let probes = 0; const hold = mediaStallHold({ mediaDir: "/mnt/usb/c/media" }, (l) => lines.push(l), "x", { now: () => t, reprobeAfterMs: 300_000, stillHoldingEveryMs: 600_000, reprobe: async () => { probes++; return "stalled"; }, }); for (let i = 0; i < 25; i++) { hold.check(); await tick(); t += 60_000; } assert.ok(hold.check()); const still = lines.filter((l) => l.startsWith("Still holding")); assert.deepEqual(still, [ `Still holding: the media drive "USB drive" has not answered after 10 min.`, `Still holding: the media drive "USB drive" has not answered after 20 min.`, ]); // Once per reprobeAfterMs (5 min) after the first 5 min: at 5, 10, 15, 20, 25. assert.equal(probes, 5); }); test("the config is re-read: a channel moved off the stalled drive mid-batch resumes", async () => { let t = 0; registerLocationHealth([USB], t); recordLocationHealth(USB, "stalled", { now: t }); let config = { mediaDir: "/mnt/usb/c/media" }; const hold = mediaStallHold(config, () => {}, "x", { now: () => t, reloadEveryMs: 10_000, reloadConfig: async () => config, }); assert.ok(hold.check()); config = { mediaDir: "/mnt/other/c/media" }; t += 5_000; hold.check(); await tick(); assert.ok(hold.check(), "not re-read before reloadEveryMs"); t += 6_000; hold.check(); await tick(); assert.equal(hold.check(), null); }); test("pruning a location no longer configured clears its record, and a hold on it releases", async () => { const { pruneLocationHealth, locationHealth } = await import("./storageHealth"); registerLocationHealth([USB]); recordLocationHealth(USB, "stalled"); const hold = mediaStallHold({ mediaDir: "/mnt/usb/c/media" }, () => {}, "x"); assert.ok(hold.check()); pruneLocationHealth(["some-other-location"]); assert.equal(locationHealth("usb"), undefined); assert.equal(hold.check(), null); });