// node --test umtool/lib/ingredients/actions.test.mjs import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { planAction, startAction } from "./actions.mjs"; import { readIngredient } from "./stages.mjs"; /** An editor that records what it was asked and answers like the real one would, enough for these tests. */ function editor() { const asked = []; const fetch = async (url, init) => { const body = init?.body ? JSON.parse(init.body) : null; const route = new URL(url).pathname; asked.push({ route, body }); let answer = { ok: true, jobId: `job-${asked.length}` }; if (route === "/api/ops/fetch-windows") answer = body.dryRun ? { ok: true, dryRun: true, groups: [{ platform: "youtube", queueKey: "clips:youtube", items: body.items }], cached: [], inFlight: [], unresolved: [] } : { ok: true, jobs: [], jobIds: ["fw-1"], jobId: "fw-1", groups: [], cached: [], inFlight: [], unresolved: [], refused: [] }; if (route === "/api/ops/persist-videos" && body.dryRun) answer = { ok: true, dryRun: true, plan: { saved: { count: 0, items: [] }, toFetch: { count: 1, items: [] }, willFetch: 1 } }; return new Response(JSON.stringify(answer), { status: 200, headers: { "content-type": "application/json" } }); }; return { asked, cfg: { url: "http://editor.test", token: "t", fetch } }; } async function corpus() { const root = await mkdtemp(path.join(tmpdir(), "actions-")); const channels = path.join(root, "channels"); const reports = path.join(root, "reports"); const v = path.join(channels, "chan", "data", "vid"); await mkdir(path.join(v, "clips"), { recursive: true }); await writeFile(path.join(channels, "chan", "config.json"), JSON.stringify({ platform: "youtube" })); await writeFile(path.join(v, "metadata.info.json"), JSON.stringify({ id: "vid", title: "t" })); await writeFile(path.join(v, "clips", "10.00-40.00.mp4"), Buffer.alloc(100)); await writeFile(path.join(v, "clips", "10.00-40.00.desynced.mp4"), Buffer.alloc(70)); await writeFile(path.join(v, "clips", "50.00-60.00.mp4"), Buffer.alloc(10)); const a = path.join(reports, "f", "a"); await mkdir(a, { recursive: true }); await writeFile(path.join(a, "video.manifest.json"), JSON.stringify({ provenance: { channelSlug: "chan" }, render: { fetchPad: 3 }, timeline: [{ type: "clip", id: "a1", video: "vid", start: 100, end: 110 }] })); return { root, channels, reports, v, a }; } test("an unmounted media drive: transcribe and persist plan 'not now', name the drive, offer no Start, and a start is refused", async () => { const { root, channels, reports, v } = await corpus(); try { // The channel's media/ links to a drive that is not there; the audio is a link into it. await symlink(path.join(root, "gone-drive", "chan", "media"), path.join(channels, "chan", "media")); await symlink("../../media/vid/audio.mp3", path.join(v, "audio.mp3")); const { cfg, asked } = editor(); const ctx = { cfg, reportsRoot: reports, channelsDir: channels }; for (const action of ["transcribe", "persist"]) { const plan = await planAction({ action, ch: "chan", id: "vid" }, ctx); assert.equal(plan.cost, null, action); assert.equal(plan.start, null, action); assert.match(plan.unreachable, /media drive does not answer .*gone-drive/, action); await assert.rejects(startAction({ action, ch: "chan", id: "vid", key: "x" }, ctx), (e) => e.status === 409 && /refused: the channel's media drive/.test(e.message)); } assert.deepEqual(asked, [], "the editor was never asked"); } finally { await rm(root, { recursive: true, force: true }); } }); test("a dangling audio link alone (the channel's media/ fine) is 'not now' too -- never 'download the audio'", async () => { const { root, channels, reports, v } = await corpus(); try { await mkdir(path.join(channels, "chan", "media"), { recursive: true }); await symlink("../../media/vid/audio.mp3", path.join(v, "audio.mp3")); const { cfg } = editor(); const plan = await planAction({ action: "transcribe", ch: "chan", id: "vid" }, { cfg, reportsRoot: reports, channelsDir: channels }); assert.match(plan.what, /^Not now: its media does not answer \(audio\.mp3 -> \.\.\/\.\.\/media\/vid\/audio\.mp3\)/); assert.equal(plan.start, null); } finally { await rm(root, { recursive: true, force: true }); } }); test("a saved video whose drive is gone: persist is 'not now', and the Windows stage says it does not answer", async () => { const { root, channels, reports, v } = await corpus(); try { await writeFile(path.join(v, "saved-video.json"), JSON.stringify({ dir: path.join(root, "gone-store", "chan"), file: "vid.mp4" })); const { cfg } = editor(); const plan = await planAction({ action: "persist", ch: "chan", id: "vid" }, { cfg, reportsRoot: reports, channelsDir: channels }); assert.match(plan.unreachable, /saved video does not answer/); const r = await readIngredient({ channelsDir: channels, reportsRoot: reports, ch: "chan", id: "vid", indexFile: path.join(root, "state", "u.json") }); const win = r.stages.find((s) => s.key === "windows"); assert.equal(win.pill.kind, "warn"); assert.match(win.line, /the saved whole recording does not answer/); assert.equal(win.savedWhole, false); assert.equal(win.uncovered.length, 1, "nothing cuts from an unreachable recording: the clip is not covered"); } finally { await rm(root, { recursive: true, force: true }); } }); test("start carries the plan's key: none, or a stale one, is refused before the editor is asked", async () => { const { root, channels, reports, a } = await corpus(); try { const { cfg, asked } = editor(); const ctx = { cfg, reportsRoot: reports, channelsDir: channels }; const plan = await planAction({ action: "fetch", ch: "chan", id: "vid", project: "f/a", clips: ["a1"] }, ctx); assert.equal(plan.start.body.key.length, 16); await assert.rejects(startAction({ ...plan.start.body, key: undefined }, ctx), /ask for the plan first/); // The clip moves after the plan was shown: the fetch would differ, so the start is refused. await writeFile(path.join(a, "video.manifest.json"), JSON.stringify({ provenance: { channelSlug: "chan" }, render: { fetchPad: 3 }, timeline: [{ type: "clip", id: "a1", video: "vid", start: 200, end: 210 }] })); await assert.rejects(startAction(plan.start.body, ctx), (e) => e.status === 409 && /changed since the plan was shown/.test(e.message)); const again = await planAction({ action: "fetch", ch: "chan", id: "vid", project: "f/a", clips: ["a1"] }, ctx); const r = await startAction(again.start.body, ctx); assert.deepEqual(r.jobIds, ["fw-1"]); const starts = asked.filter((x) => x.route === "/api/ops/fetch-windows" && !x.body.dryRun); assert.equal(starts.length, 1); assert.deepEqual(starts[0].body.items.map((i) => [i.from, i.to, i.clipId]), [[197, 213, "a1"]]); } finally { await rm(root, { recursive: true, force: true }); } }); test("evict names one window by its FILE, says its set-aside twin goes with it, and refuses a window changed since", async () => { const { root, channels, reports, v } = await corpus(); try { const { cfg, asked } = editor(); const ctx = { cfg, reportsRoot: reports, channelsDir: channels }; await assert.rejects(planAction({ action: "evict", ch: "chan", id: "vid", window: "10.00-40.00" }, ctx), /no window file/); const plan = await planAction({ action: "evict", ch: "chan", id: "vid", window: "10.00-40.00.mp4" }, ctx); assert.match(plan.what, /^Delete the cached window 10\.00-40\.00\.mp4 .*and with it 10\.00-40\.00\.desynced\.mp4/); const lone = await planAction({ action: "evict", ch: "chan", id: "vid", window: "50.00-60.00.mp4" }, ctx); assert.doesNotMatch(lone.what, /and with it/); // Refetched since (other bytes under the same name): the key no longer matches. await writeFile(path.join(v, "clips", "50.00-60.00.mp4"), Buffer.alloc(99)); await assert.rejects(startAction(lone.start.body, ctx), (e) => e.status === 409); const ok = await startAction(plan.start.body, ctx); assert.equal(ok.ok, true); assert.deepEqual(asked.at(-1), { route: "/api/ops/evict-clips", body: { slug: "chan", id: "vid", windows: ["10.00-40.00"] } }); } finally { await rm(root, { recursive: true, force: true }); } });