// The cited clip's server half (release 32 R1): a span's target parsed, a // fetch asked of a fake editor exactly as fetch_clip asks, queued then polled // to ready, and a refusal said in plain words. No network: the editor is a // function. // // Run with: pnpm --filter umtool exec tsx --test lib/clips/cited.test.ts import { before, test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; let lib: typeof import("./cited"); let ROOT = ""; before(async () => { ROOT = await mkdtemp(path.join(os.tmpdir(), "umtool-cited-")); process.env.CHANNELS_DIR = path.join(ROOT, "channels"); process.env.REPORTS_DIR = path.join(ROOT, "reports"); lib = await import("./cited"); }); type Call = { url: string; body: Record | null }; function editor(answers: ((c: Call) => { status: number; body: unknown })[]) { const calls: Call[] = []; let i = 0; return { calls, deps: { env: { ARCHILYZER_EDITOR_URL: "http://editor.invalid", WORKER_TOKEN: "t" } as unknown as NodeJS.ProcessEnv, fetch: async (url: string, init?: { body?: string }) => { const call = { url, body: init?.body ? JSON.parse(init.body) : null }; calls.push(call); const a = answers[Math.min(i++, answers.length - 1)](call); return { status: a.status, json: async () => a.body }; }, sleep: async () => {}, now: () => 0, }, }; } test("a target: corpus directories and a span, the project's clip only as a pair", () => { const t = lib.targetOf((k) => ({ channel: "chan", video: "v1", from: "3", to: "6.5", project: "reports/p" })[k as "channel"]); assert.deepEqual(t, { channel: "chan", video: "v1", from: 3, to: 6.5 }); assert.throws(() => lib.targetOf((k) => ({ channel: "..", video: "v1", from: 1, to: 2 })[k as "channel"]), /channel/); assert.throws(() => lib.targetOf((k) => ({ channel: "c", video: "v", from: 5, to: 2 })[k as "channel"]), /from < to/); }); test("fetch asks the editor as fetch_clip does (umtool, padded window), then a poll is ready", async () => { const e = editor([() => ({ status: 202, body: { cached: false, jobId: "j1", from: 0, to: 9 } })]); const target = { channel: "chan", video: "v1", from: 3, to: 6 }; const q = await lib.fetchCitedClip({ target, reason: "review" }, e.deps); assert.deepEqual(q, { state: "queued", job: "j1", status: "queued" }); assert.equal(e.calls[0].url, "http://editor.invalid/api/media/fetch-window"); assert.deepEqual( { ...e.calls[0].body }, { channelSlug: "chan", videoId: "v1", from: 0, to: 9, pad: 3, requestedBy: "umtool", reason: "review" }, ); const p = editor([() => ({ status: 200, body: { status: "done", file: "/x/clips/0.00-9.00.mp4", from: 0, to: 9 } })]); const ready = await lib.fetchCitedClip({ job: "j1", target }, p.deps); assert.equal(p.calls[0].url, "http://editor.invalid/api/media/fetch-window/j1"); // Nothing in this empty corpus to play: ready, and the player says so. assert.deepEqual(ready, { state: "ready", cached: false, from: 0, to: 9, play: null }); }); test("a cooldown says the platform is rate-limiting; an audio-check failure says corrupt audio", async () => { const target = { channel: "chan", video: "v1", from: 3, to: 6 }; const c = editor([() => ({ status: 409, body: { error: "youtube is in a rate-limit cooldown (42s remaining).", cooldownMs: 42000, platform: "youtube" } })]); const r = await lib.fetchCitedClip({ target, reason: "review" }, c.deps); assert.equal(r.state, "failed"); assert.equal(r.state === "failed" && r.message, "youtube is rate-limiting fetches: try again later."); const m = editor([() => ({ status: 200, body: { status: "failed", error: "The audio check found the window 0.00–9.00 of v1 malformed" } })]); const f = await lib.fetchCitedClip({ job: "j2", target }, m.deps); assert.match(f.state === "failed" ? f.message : "", /corrupt audio/); }); test("Cut from the stored media URL rides on the fetch as fetch_clip's storedUrl (release 32 R6)", async () => { const e = editor([() => ({ status: 202, body: { cached: false, jobId: "j3", from: 0, to: 9 } })]); const target = { channel: "chan", video: "v1", from: 3, to: 6 }; await lib.fetchCitedClip({ target, reason: "review", storedUrl: "fallback" }, e.deps); assert.equal(e.calls[0].body?.storedUrl, "fallback"); const plain = editor([() => ({ status: 202, body: { cached: false, jobId: "j4", from: 0, to: 9 } })]); await lib.fetchCitedClip({ target, reason: "review" }, plain.deps); assert.equal("storedUrl" in (plain.calls[0].body ?? {}), false); }); test("a cited span of a project carries the regions it hides on that recording: its clip's, else the recording's", async () => { const dir = path.join(ROOT, "reports", "hide"); await mkdir(path.join(dir, "out", "clips-raw"), { recursive: true }); await writeFile(path.join(dir, "out", "clips-raw", "vid1_0.00-10.00.mp4"), "a window"); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: "hide", title: "Hide", provenance: { channelSlug: "chan", channelsDir: path.join(ROOT, "channels") }, render: { fps: 30, censor: [{ video: "vid1", boxes: [[0.75, 0, 0.25, 1]], style: "black" }] }, timeline: [{ type: "clip", id: "c1", channel: "chan", video: "vid1", start: 2, end: 6, censor: { boxes: [[0, 0, 0.2, 0.2]] } }], })); const play = await lib.findCitedPlay({ channel: "chan", video: "vid1", from: 2, to: 6, project: "hide", clip: "c1" }); assert.ok(play, "the project's window plays"); assert.equal(play.kind, "window"); assert.equal(play.hidden?.length, 2, "the recording's box and the clip's own"); });