// `umtool fetch

--take `: a take's clips asked of the editor as the // cut's are -- one fetch-windows batch, recorded in fetch-runs.json -- from // the take's manifest as its walk reads it, so a DRAFT take (nothing // rendered) whose clip is only in its patch is fetched with no hand JSON. // // Run with: pnpm test:scripts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; const root = await mkdtemp(path.join(os.tmpdir(), "umtool-fetch-take-")); process.env.REPORTS_DIR = root; process.env.UMTOOL_CACHE_DIR = path.join(root, ".cache"); delete process.env.UMTOOL_BENCH_PREP; const { lastStartedRun, projectFetchBody, readFetchRuns } = await import("./fetchWindows.mjs"); const { fetchCommand } = await import("./fetchCli.mjs"); const trigger = await import("./benchPrepTrigger.mjs"); test.after(() => rm(root, { recursive: true, force: true })); const clip = (id, video, start, end) => ({ type: "clip", id, channel: "chan", video, start, end }); const MANIFEST = { slug: "p", title: "In her words", provenance: { channelSlug: "chan" }, render: { fetchPad: 3 }, timeline: [clip("kf3", "vid", 10, 14)] }; const TAKE = { id: "spin", group: "takes", order: 1, label: "Spin-off", kind: "different", summary: "", changes: [], preview: "preview.mp4", seconds: null, builtAt: null, style: null, window: null, draft: true, draftedAt: "2026-11-02T12:00:00.000Z", // Its own timeline: the cut's kf3 widened, and ke1 only the take has. patch: { timeline: [clip("kf3", "vid", 8, 20), clip("ke1", "vnew", 100, 110)] }, }; const dir = path.join(root, "folder", "p"); /** A fake editor: routes -> answers, every request recorded. */ function fakeEditor(routes) { const seen = []; const f = async (url, init = {}) => { const u = new URL(url); const key = `${init.method ?? "GET"} ${u.pathname}`; seen.push({ key, body: init.body ? JSON.parse(init.body) : null }); const r = typeof routes[key] === "function" ? routes[key](init.body ? JSON.parse(init.body) : null) : routes[key]; if (!r) return new Response(JSON.stringify({ ok: false, error: "not found" }), { status: 404 }); return new Response(JSON.stringify(r), { status: 200, headers: { "content-type": "application/json" } }); }; return { fetch: f, seen }; } test("fetch --take: the take's clips (a draft's), one batch, the run recorded as the take's; the cut's last run is not it", async () => { await mkdir(path.join(dir, "takes", "spin"), { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(MANIFEST, null, 2)); await writeFile(path.join(dir, "takes", "spin", "take.json"), JSON.stringify(TAKE, null, 2)); const manifestBefore = await readFile(path.join(dir, "video.manifest.json"), "utf8"); const { body } = await projectFetchBody({ id: "folder/p", dir }, { take: "spin" }); assert.deepEqual(body.items.map((i) => [i.clipId, i.id, i.from, i.to]), [ ["kf3", "vid", 5, 23], ["ke1", "vnew", 97, 113], ]); assert.equal(body.manifest, path.join(dir, "takes", "spin", "take.json")); assert.equal(body.requestedBy, "umtool:folder/p"); const cutBody = (await projectFetchBody({ id: "folder/p", dir })).body; assert.deepEqual(cutBody.items.map((i) => [i.clipId, i.from, i.to]), [["kf3", 7, 17]]); let polls = 0; const ed = fakeEditor({ "POST /api/ops/fetch-windows": (b) => ({ ok: true, dryRun: false, groups: [], cached: [], inFlight: [], unresolved: [], refused: [], jobs: [{ platform: "youtube", queueKey: "clips:youtube", jobId: "J9", items: b.items.length }], jobIds: ["J9"], jobId: "J9" }), "GET /api/ops/job/J9": () => ({ ok: true, job: { id: "J9", status: ++polls < 2 ? "running" : "done" } }), "GET /api/jobs/J9/log": { content: "Fetch windows: 2 fetched.\n", nextOffset: 10, status: "archived" }, }); const cfg = { url: "http://editor.test", token: "tok", fetch: ed.fetch }; const lines = []; const asked = []; trigger.setBenchPrepQueue(async (p, clips, opts) => { asked.push([p.id, clips, opts.take ?? null, opts.excerptsOnly]); }); try { const run = (args) => fetchCommand(args, { root, out: (s) => lines.push(s), err: (s) => lines.push(s), cfg, pollMs: 1 }); assert.equal(await run(["folder/p", "--take", "spin", "--wait"]), 0, lines.join("\n")); await trigger.settleBenchPrepTriggers(); assert.deepEqual(ed.seen[0].body.items.map((i) => i.clipId), ["kf3", "ke1"]); assert.match(lines.join("\n"), /folder\/p take spin: 2 window\(s\) asked/); // What landed is prepared for the TAKE's walk. assert.deepEqual(asked, [["folder/p", null, "spin", false]]); const doc = await readFetchRuns(dir); assert.equal(doc.runs.length, 1); assert.equal(doc.runs[0].take, "spin"); assert.equal(lastStartedRun(doc), null, "the cut has no started run of its own"); assert.deepEqual(lastStartedRun(doc, { take: "spin" }).jobIds, ["J9"]); lines.length = 0; assert.equal(await run(["folder/p", "--status", "--take", "spin"]), 0); assert.match(lines.join("\n"), /folder\/p take spin: the run of/); lines.length = 0; assert.equal(await run(["folder/p", "--status"]), 1, "the cut's --status does not read the take's run"); assert.match(lines.join("\n"), /folder\/p has no started fetch run/); lines.length = 0; assert.equal(await run(["folder/p", "--take", "nope", "--body"]), 1); assert.match(lines.join("\n"), /no take nope/); assert.equal(await run(["folder/p", "--take"]), 2); } finally { trigger.setBenchPrepQueue(null); } assert.equal(await readFile(path.join(dir, "video.manifest.json"), "utf8"), manifestBefore, "the project's manifest is untouched"); });