import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readdir, rm, stat, 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/compress-text/route.test.ts" // // The body's shape and the refusals that come before any job, then a dry run // that answers per channel and writes nothing. Nothing is compressed here. const ROOT = await mkdtemp(path.join(os.tmpdir(), "compress-text-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", "synth", "data", "aaaaaaaaaaa"); await mkdir(videoDir, { recursive: true }); await writeFile(path.join(ROOT, "channels", "synth", "config.json"), JSON.stringify({ handling: "youtube" })); await writeFile(path.join(videoDir, "transcript.live_chat.json"), '{"replayChatItemAction":{}}\n'.repeat(100)); async function post(body: Record) { const res = await POST( new Request("http://localhost/api/ops/compress-text", { method: "POST", headers: { authorization: "Bearer test-token", "content-type": "application/json" }, body: JSON.stringify(body), }), ); return { status: res.status, json: (await res.json()) as Record }; } test("refusals before any job", async () => { const cases: [Record, RegExp][] = [ [{ force: true }, /unknown key\(s\): force/], [{ slugs: ["../x"] }, /not a valid channel slug/], [{ slugs: ["nope"], dryRun: true }, /Channel\(s\) not found: nope/], [{ kinds: ["metadata"] }, /"kinds" must be a list of: live-chat, captions/], [{ kinds: [] }, /"kinds" must be a list of: live-chat, captions/], [{ kinds: "captions" }, /"kinds" must be a list of: live-chat, captions/], [{ limit: 0 }, /limit/], [{ limit: 5 }, /"limit" is per channel: name exactly one in "slugs"/], [{ dryRun: true, verify: true }, /two different runs/], [{ restore: true, verify: true }, /two different runs/], [{ restore: "yes" }, /restore/], ]; for (const [body, re] of cases) { const r = await post(body); assert.equal(r.status, 400, JSON.stringify(body)); assert.match(String(r.json.error), re, JSON.stringify(body)); } }); test("a dry run answers per channel and writes nothing", async () => { const before = (await stat(path.join(videoDir, "transcript.live_chat.json"))).mtimeMs; const r = await post({ dryRun: true, kinds: ["live-chat"] }); assert.equal(r.status, 200, JSON.stringify(r.json)); const result = r.json.result as { channels: { slug: string; plainFiles: number; plainBytes: number }[] }; const synth = result.channels.find((c) => c.slug === "synth"); assert.equal(synth?.plainFiles, 1); assert.equal(synth?.plainBytes, '{"replayChatItemAction":{}}\n'.length * 100); assert.deepEqual(await readdir(videoDir), ["transcript.live_chat.json"]); assert.equal((await stat(path.join(videoDir, "transcript.live_chat.json"))).mtimeMs, before); }); test("a restore's dry run answers the stored files and writes nothing", async () => { const r = await post({ dryRun: true, restore: true }); assert.equal(r.status, 200, JSON.stringify(r.json)); const result = r.json.result as { restore: boolean; channels: { slug: string; storedFiles: number; pending: number }[] }; assert.equal(result.restore, true); const synth = result.channels.find((c) => c.slug === "synth"); assert.equal(synth?.storedFiles, 0, "nothing is stored here"); assert.equal(synth?.pending, 0); assert.deepEqual(await readdir(videoDir), ["transcript.live_chat.json"]); }); test("kinds captions: a dry run answers the caption files per channel, the pairs of equal size, and writes nothing", async () => { const vtt = "WEBVTT\n\n00:00:00.000 --> 00:00:01.000\nwords\n"; await writeFile(path.join(videoDir, "transcript.en.vtt"), vtt); await writeFile(path.join(videoDir, "transcript.en-orig.vtt"), vtt); try { const r = await post({ dryRun: true, kinds: ["captions"] }); assert.equal(r.status, 200, JSON.stringify(r.json)); const result = r.json.result as { kinds: string[]; channels: { slug: string; plainFiles: number; byKind: { captions?: { plainFiles: number; pairsEqualSize?: number } } }[]; }; assert.deepEqual(result.kinds, ["captions"]); const synth = result.channels.find((c) => c.slug === "synth"); assert.equal(synth?.plainFiles, 2, "the captions only: the chat is not asked"); assert.equal(synth?.byKind.captions?.pairsEqualSize, 1); assert.deepEqual((await readdir(videoDir)).sort(), ["transcript.en-orig.vtt", "transcript.en.vtt", "transcript.live_chat.json"]); } finally { await rm(path.join(videoDir, "transcript.en.vtt")); await rm(path.join(videoDir, "transcript.en-orig.vtt")); } });