import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, stat, 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/evict-clips/route.test.ts" // // Targeted eviction (controller/evictClipWindows.ts): `{slug, id}` evicts one // video's windows, `windows`/`covering` narrow them; the by-age form still // needs its age. Refusals answer 400 before any job. const corpus = await setupOpsCorpus(null); await corpus.writeChannelConfig("demo-yt", { url: "https://www.youtube.com/@demo" }); const DATA = path.join(corpus.transcripts, "channels", "demo-yt", "data"); async function window(id: string, name: string) { const dir = path.join(DATA, id, "clips"); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, `${name}.mp4`), Buffer.alloc(64)); await writeFile(path.join(dir, `${name}.json`), JSON.stringify({ requestedBy: "umtool" })); } await window("vid00000001", "10.00-40.00"); await window("vid00000001", "100.00-130.00"); await window("vid00000001", "200.00-230.00"); await window("vid00000002", "10.00-40.00"); const { POST } = await import("./route"); const { getRegistry } = 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, settleJob } = await import("yt-dlp-transcript-common/testing/jobs/quiesce.js"); test.after(async () => { await quiesceJobs(); await corpus.cleanup(); }); const exists = (p: string) => stat(p).then(() => true, () => false); const clip = (id: string, name: string) => path.join(DATA, id, "clips", name); async function settle(jobId: string): Promise { await settleJob(jobId); return getRegistry().get(jobId)?.status; } test("evict-clips refusals: the shapes, the combinations, nothing queued", async () => { const before = await corpus.listJobIds(); const cases: [Record, RegExp][] = [ [{ slug: "demo-yt", video: "vid00000001" }, /unknown key\(s\): video/], [{ slug: "demo-yt" }, /olderThanDays\) is required unless one video/], [{ id: "vid00000001" }, /give its slug too/], [{ slug: "demo-yt", covering: [{ from: 1, to: 2 }], olderThanDays: 0 }, /give its "slug" and "id" too/], [{ slug: "demo-yt", id: "../x" }, /not a video id/], [{ slug: "no-such", id: "vid00000001" }, /Channel "no-such" not found/], [{ slug: "demo-yt", id: "vid00000001", windows: ["ten-forty"] }, /not a window name/], [{ slug: "demo-yt", id: "vid00000001", windows: [] }, /non-empty array/], [{ slug: "demo-yt", id: "vid00000001", covering: [{ from: 5, to: 1 }] }, /not a \{"from", "to"\} span/], [{ slug: "demo-yt", id: "vid00000001", olderThanDays: "old" }, /number of days/], ]; for (const [body, re] of cases) { const r = await callPost(POST, body); assert.equal(r.status, 400, JSON.stringify(body)); assert.match(r.body.error ?? "", re, JSON.stringify(body)); } assert.deepEqual(await corpus.listJobIds(), before); }); test("evict-clips {slug, id, covering}: the overlapping windows of that video go; a dry run first deletes nothing", async () => { const body = { slug: "demo-yt", id: "vid00000001", covering: [{ from: 20, to: 25 }, { from: 110, to: 140 }] }; const dry = await callPost(POST, { ...body, dryRun: true }); assert.equal(dry.status, 200, JSON.stringify(dry.body)); assert.equal(await settle(dry.body.jobId as string), "done"); assert.equal(await exists(clip("vid00000001", "10.00-40.00.mp4")), true); const r = await callPost(POST, body); assert.equal(r.status, 200, JSON.stringify(r.body)); assert.equal(await settle(r.body.jobId as string), "done"); assert.equal(await exists(clip("vid00000001", "10.00-40.00.mp4")), false); assert.equal(await exists(clip("vid00000001", "10.00-40.00.json")), false); assert.equal(await exists(clip("vid00000001", "100.00-130.00.mp4")), false); assert.equal(await exists(clip("vid00000001", "200.00-230.00.mp4")), true); assert.equal(await exists(clip("vid00000002", "10.00-40.00.mp4")), true); }); test("evict-clips {slug, id, windows}: the named window only", async () => { const r = await callPost(POST, { slug: "demo-yt", id: "vid00000001", windows: ["200-230"] }); assert.equal(r.status, 200, JSON.stringify(r.body)); assert.equal(await settle(r.body.jobId as string), "done"); assert.equal(await exists(clip("vid00000001", "200.00-230.00.mp4")), false); assert.equal(await exists(clip("vid00000002", "10.00-40.00.mp4")), true); });