import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readdir, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/compact-saved-videos/route.test.ts" // // The body's shape and the refusals that come before any job, then a dry run // that answers with the plan and writes nothing. Nothing is encoded. const ROOT = await mkdtemp(path.join(os.tmpdir(), "compact-route-")); process.env.WORKER_TOKEN = "test-token"; process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); const { POST } = await import("./route"); test.after(() => rm(ROOT, { recursive: true, force: true })); const videoDir = path.join(ROOT, "channels", "kick", "data", "aaaaaaaaaaa"); await mkdir(videoDir, { recursive: true }); await writeFile(path.join(ROOT, "channels", "kick", "config.json"), JSON.stringify({ handling: "transcribe", platform: "kick" })); await writeFile( path.join(videoDir, "saved-video.json"), JSON.stringify({ storedAt: "x", dir: path.join(ROOT, "saved-videos", "kick", "aaaaaaaaaaa"), file: "source-media.mp4", bytes: 5, keepReason: "keep-latest" }), ); async function post(body: Record) { const res = await POST( new Request("http://localhost/api/ops/compact-saved-videos", { method: "POST", headers: { authorization: "Bearer test-token", "content-type": "application/json" }, body: JSON.stringify(body), }), ); return { status: res.status, json: (await res.json()) as { ok?: boolean; error?: string; dryRun?: boolean; plan?: { items: { compact: boolean; reason?: string }[]; lines: string[] } } }; } test("refusals before any job", async () => { const cases: [Record, RegExp][] = [ [{}, /"items" is required/], [{ items: [{ slug: "kick", id: "aaaaaaaaaaa" }], force: 1 }, /unknown key\(s\): force/], [{ items: [{ slug: "kick", id: "../x" }] }, /not a video id|one path segment/], [{ items: [{ slug: "kick", id: "aaaaaaaaaaa" }], crf: 5 }, /"crf" must be a number from 20 to 55/], [{ items: [{ slug: "kick", id: "aaaaaaaaaaa" }], chunkMinutes: 0 }, /"chunkMinutes" must be a number from 1 to 60/], [{ select: true, slugs: ["../x"] }, /not a valid channel slug/], ]; for (const [body, re] of cases) { const r = await post(body); assert.equal(r.status, 400, JSON.stringify(body)); assert.match(r.json.error!, re, JSON.stringify(body)); } }); test("a dry run answers the plan — a keep-latest container is skipped — and writes nothing", async () => { const before = await readdir(ROOT, { recursive: true }); const r = await post({ items: [{ slug: "kick", id: "aaaaaaaaaaa" }], dryRun: true }); assert.equal(r.status, 200, JSON.stringify(r.json)); assert.equal(r.json.dryRun, true); assert.equal(r.json.plan!.items[0].compact, false); assert.match(r.json.plan!.items[0].reason!, /keep-latest/); assert.match(r.json.plan!.lines.at(-1)!, /^total: 0 container/); assert.deepEqual((await readdir(ROOT, { recursive: true })).sort(), before.sort()); // The selector over the whole store, the same. const s = await post({ select: true, dryRun: true }); assert.equal(s.status, 200, JSON.stringify(s.json)); assert.equal(s.json.plan!.items.length, 1); }); test("the selector is a dry run unless it says dryRun: false; a named video below the selector needs belowThreshold", async () => { const before = await readdir(ROOT, { recursive: true }); const s = await post({ select: true }); assert.equal(s.status, 200, JSON.stringify(s.json)); assert.equal(s.json.dryRun, true); assert.deepEqual((await readdir(ROOT, { recursive: true })).sort(), before.sort()); // The keep-latest container is refused whatever the flags say. const r = await post({ items: [{ slug: "kick", id: "aaaaaaaaaaa" }], belowThreshold: true }); assert.equal(r.status, 400); assert.match(r.json.error!, /Nothing to compact — kick\/aaaaaaaaaaa: .*keep-latest/); });