import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, utimes, writeFile } from "node:fs/promises"; import path from "node:path"; import { setupOpsCorpus, callPost } from "../_testCorpus"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/transcribe-bucket/route.test.ts" // // A REPORT BEHIND THE DISK IS REGENERATED FIRST (2026-10-26): 64 archive.org // imports sat untranscribed because the channel's report predated them, and // `transcribe-bucket` answered "N of ids not in the downloadedNoTranscript // bucket". The route now regenerates the report once and reads the bucket // again. The job it starts waits behind a held queue and is cancelled: nothing // is transcribed. const corpus = await setupOpsCorpus(null); await corpus.writeSettings(); await corpus.writeChannelConfig("demo-audio", { handling: "transcribe", url: "https://archive.org/details/demo" }); const CHANNEL = path.join(corpus.transcripts, "channels", "demo-audio"); // A report from before the import: no videos at all. await writeFile( path.join(CHANNEL, "snapshot.json"), JSON.stringify({ generatedAt: "2026-10-26T01:10:00.000Z", totals: { videos: 0 }, buckets: { downloadedNoTranscript: [] } }), ); const past = new Date(Date.now() - 60 * 60 * 1000); await utimes(path.join(CHANNEL, "snapshot.json"), past, past); // What the import landed since. const dir = path.join(CHANNEL, "data", "imported0001"); await mkdir(dir, { recursive: true }); await writeFile( path.join(dir, "metadata.info.json"), JSON.stringify({ id: "imported0001", title: "Episode 1", upload_date: "20200101", duration: 60 }), ); await writeFile(path.join(dir, "audio.mp3"), Buffer.alloc(4096, 1)); const { POST } = await import("./route"); const { getRegistry, newJobId } = await import("yt-dlp-transcript-common/jobs/registry"); // A job's end still writes after its status reads done (its log and sidecar, // and the report regeneration it arms in the channel dir): waited for before // the corpus is removed (testing/jobs/quiesce.ts). const { quiesceJobs } = await import("yt-dlp-transcript-common/testing/jobs/quiesce.js"); test.after(async () => { await quiesceJobs(); await corpus.cleanup(); }); test("ids the stale report does not hold: the report is regenerated, and the job starts", async () => { const registry = getRegistry(); const queueKey = `test-hold-${newJobId()}`; const holder = { id: newJobId(), kind: "test-holder", queueKey, status: "queued" as const, queuedAt: Date.now(), logPath: "/dev/null", }; registry.register(holder); registry.enqueue(holder, { start: () => {}, onCancel: () => {} }); try { const res = await callPost(POST, { slug: "demo-audio", ids: ["imported0001"], queueKey }); assert.equal(res.status, 200, JSON.stringify(res.body)); const jobId = String(res.body.jobId); assert.equal(registry.get(jobId)?.kind, "whisper-bucket-downloaded-no-transcript"); assert.deepEqual(registry.get(jobId)?.spec?.params?.ids, ["imported0001"], "an ad-hoc selection replays as its ids"); registry.cancel(jobId); } finally { registry.finalize(holder.id, "done"); } }); test("an id no report holds is still refused, after the one regeneration", async () => { const res = await callPost(POST, { slug: "demo-audio", ids: ["notavideo01"] }); assert.equal(res.status, 400); assert.match(String(res.body.error), /1 of "ids" not in the "downloadedNoTranscript" bucket of demo-audio: notavideo01/); });