// TAKE-SCOPED EDITS (takeScope.mjs) and A TAKE'S ITEMS FROM OTHER PROJECTS // (takeImport.mjs): `posts set-item|roles|narrate|reread|fx … --in-take ` // write the take's patch and never the project's manifest; `take --import-item // :` clones an item into a take with its files hard-linked in, // placed, idempotent; `take --draft --from ` starts a draft from a take's // patch; and the editor's token is found in its env files. import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { composePatch, diffPatch, listTakeRevisions } from "./takeEdit.mjs"; import { mergePatch } from "umtool-report-to-video/montage-styles"; import { TakeScopeRefused, withTakeScope } from "./takeScope.mjs"; import { updateRender } from "./manifest.mjs"; import { readManifest } from "../projects/report.mjs"; import { resolveTakeManifest } from "./takeManifest.mjs"; import { ImportRefused, draftImportArgv, postsInDateOrder, parseImportRef } from "./takeImport.mjs"; import { lintCommand, namedFileProblems } from "./lintCli.mjs"; import { postsCommand } from "./postsCli.mjs"; import { takeCommand } from "./takeCli.mjs"; import { timelineCommand } from "./timelineCli.mjs"; import { editorFromEnv } from "umtool-report-to-video/narrate-via-editor"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-take-scope-")); after(() => rm(ROOT, { recursive: true, force: true })); const REPORTS = path.join(ROOT, "reports"); let haveFfmpeg = true; try { execFileSync("ffmpeg", ["-version"], { stdio: "ignore" }); } catch { haveFfmpeg = false; } const REPLY = "That is exactly what I was saying yesterday morning."; const PARENT = "The first thing anyone notices is the noise from the street."; const post = (id, date, o = {}) => ({ type: "post", id: `p-${id}`, post: `subj-x/${id}`, platform: "x", author: "THE SUBJECT", handle: "Subject", date, text: `${REPLY} ${id}`, ...o, }); const narrated = (id, date, o = {}) => post(id, date, { parents: [{ id: `9${id}`, author: "Neighbour", handle: "neighbour_one", date: "2026-10-29T10:00:00.000Z", text: PARENT }], narration: { script: REPLY, takes: [{ file: `narration/posts/${id}/take-1.orpheus-thread.wav`, engine: "orpheus", voice: "leo+zac", seconds: 6, parts: [ { n: 1, para: 0, text: PARENT, voice: "leo", role: "parent:0", piece: 0, file: `narration/parts-cache/${id}a.wav`, start: 0, seconds: 3 }, { n: 2, para: 100, text: REPLY, voice: "zac", role: "post", piece: 1, file: `narration/parts-cache/${id}b.wav`, start: 3.5, seconds: 2.5 }, ], }], pick: 0, }, ...o, }); async function tone(file, seconds = 6) { await mkdir(path.dirname(file), { recursive: true }); if (haveFfmpeg) execFileSync("ffmpeg", ["-nostdin", "-loglevel", "error", "-y", "-f", "lavfi", "-i", `sine=frequency=220:duration=${seconds}:sample_rate=24000`, file]); else await writeFile(file, `tone ${seconds}`); } async function project(name, timeline, extra = {}) { const dir = path.join(REPORTS, name); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: name, title: name, provenance: { siteOrigin: "" }, render: { width: 1920, height: 1080, fps: 30 }, timeline, ...extra }, null, 2) + "\n"); return { id: name, dir }; } const text = (p) => readFile(path.join(p.dir, "video.manifest.json"), "utf8"); const takeDoc = async (p, t) => JSON.parse(await readFile(path.join(p.dir, "takes", t, "take.json"), "utf8")); async function cli(fn, args) { const out = []; const err = []; const code = await fn(args, { root: REPORTS, reportsRoot: REPORTS, out: (s) => out.push(s), log: (s) => out.push(s), err: (s) => err.push(s) }); return { code, out: out.join("\n"), err: err.join("\n") }; } const posts = (...args) => cli(postsCommand, args); const take = (...args) => cli(takeCommand, args); test("THE PATCH ARITHMETIC: a diff composed onto a take's patch gives, over the project's manifest, what the edit left", () => { const base = { render: { a: 1, b: { c: 2, d: 3 }, list: [1, 2] }, keep: "x" }; const p = { render: { b: { c: 5 }, e: 7 }, drop: null }; const resolved = mergePatch(base, p); const edited = structuredClone(resolved); edited.render.b.d = 9; delete edited.render.a; edited.render.list = [3]; edited.added = { z: 1 }; const d = diffPatch(resolved, edited); assert.deepEqual(d, { render: { a: null, b: { d: 9 }, list: [3] }, added: { z: 1 } }); assert.deepEqual(mergePatch(base, composePatch(p, d)), edited); assert.equal(diffPatch(resolved, structuredClone(resolved)), undefined, "nothing changed: no patch"); assert.deepEqual(parseImportRef("emberparadox/mr-obvious-mid-stage:p-1,p-2"), { project: "emberparadox/mr-obvious-mid-stage", items: ["p-1", "p-2"] }); assert.equal(parseImportRef("no-colon"), null); assert.equal(postsInDateOrder([post("1", "2026-10-01T00:00:00Z"), { type: "clip", id: "c" }, post("2", "2026-10-02T00:00:00Z")]), true); assert.equal(postsInDateOrder([post("2", "2026-10-02T00:00:00Z"), post("1", "2026-10-01T00:00:00Z")]), false); }); test("--in-take: set-item, narrate --pick, roles --set and fx land in the take's patch -- the project's manifest byte for byte the same; undo takes each back", async () => { const a = await project("scope-a", [post("1", "2026-10-30T10:00:00.000Z"), narrated("2", "2026-10-30T11:00:00.000Z", { narration: { ...narrated("2", "x").narration, takes: [narrated("2", "x").narration.takes[0], { ...narrated("2", "x").narration.takes[0], file: "narration/posts/2/take-2.orpheus-thread.wav" }] } })]); const before = await text(a); assert.equal((await take("scope-a", "--id", "t1", "--label", "T one", "--draft")).code, 0); // set-item: one knob, into the take's timeline. let r = await posts("set-item", "scope-a", "p-1", "hold=4", "--in-take", "t1"); assert.equal(r.code, 0, r.err); assert.match(r.out, /on take t1 \(its patch; scope-a's manifest untouched\): 1 edit\(s\), undo point takes\/t1\/revisions\/.*-auto-before-post-item\.take\.json -- umtool timeline scope-a --take t1 undo/); assert.equal(await text(a), before, "the project's manifest is never written"); assert.equal((await takeDoc(a, "t1")).patch.timeline.find((e) => e.id === "p-1").hold, 4); // A supported path (12 Nov): it lands, and says nothing on stderr. assert.equal(r.err, ""); // The alias: set-item's --take is --in-take ; --json is the command's own object with the take. r = await posts("set-item", "scope-a", "p-1", "thread.group=g1", "--take", "t1", "--json"); assert.equal(r.code, 0, r.err); const doc = JSON.parse(r.out); assert.equal(doc.ok, true); assert.equal(doc.entry.thread.group, "g1"); assert.equal(doc.take.id, "t1"); assert.equal(doc.take.edits, 1); assert.equal(doc.take.undo, "umtool timeline scope-a --take t1 undo"); // narrate --pick (1 = the first) in the take. r = await posts("narrate", "scope-a", "p-2", "--pick", "2", "--in-take", "t1"); assert.equal(r.code, 0, r.err); assert.equal((await resolveTakeManifest(a.dir, "t1")).manifest.timeline.find((e) => e.id === "p-2").narration.pick, 1); assert.equal(JSON.parse(await text(a)).timeline[1].narration.pick, 0, "the project's pick is its own"); // roles --set and fx in the take. r = await posts("roles", "scope-a", "p-2", "--set", "1=parent:0,2=post", "--in-take", "t1"); assert.equal(r.code, 0, r.err + r.out); assert.match(r.out, /on take t1: its patch is unchanged/, "the roles it already stores: no write, no undo point"); r = await posts("fx", "scope-a", "p-2", "add", "caption", "--match", "exactly", "--text", "A word", "--in-take", "t1"); assert.equal(r.code, 0, r.err + r.out); const tl = (await resolveTakeManifest(a.dir, "t1")).manifest.timeline; assert.equal(tl.find((e) => e.id === "p-2").fx.length, 1); assert.equal(await text(a), before, "still untouched"); // Each edit is an undo point of the take's; undo takes the newest back. const autos = (await listTakeRevisions(a.dir, "t1")).filter((x) => x.auto); assert.equal(autos.length, 4, "set-item twice, the pick, the caption"); const u = await cli(timelineCommand, ["scope-a", "--take", "t1", "undo"]); assert.equal(u.code, 0, u.err); assert.equal((await resolveTakeManifest(a.dir, "t1")).manifest.timeline.find((e) => e.id === "p-2").fx, undefined); }); test("--in-take refuses what it cannot do: no such take (1), another verb (2), a writer not routed to the take writes nothing (TakeScopeRefused), narrate --take is named", async () => { const a = await project("scope-b", [post("1", "2026-10-30T10:00:00.000Z")]); const before = await text(a); let r = await posts("set-item", "scope-b", "p-1", "hold=4", "--in-take", "nope"); assert.equal(r.code, 1); assert.match(r.err, /has no take nope/); assert.equal((await take("scope-b", "--id", "t1", "--label", "T", "--draft")).code, 0); r = await posts("avatar", "scope-b", "--in-take", "t1"); assert.equal(r.code, 2); assert.match(r.err, /works with posts set-item, roles, narrate, reread, fx/); // A writer that is not routed (updateRender writes the manifest directly): refused, the manifest untouched. await assert.rejects(withTakeScope(a.dir, "t1", () => updateRender(a.dir, { transition: 0.3 })), TakeScopeRefused); assert.equal(await text(a), before); // Reads in the scope are the take's; outside it, the project's. assert.equal(await withTakeScope(a.dir, "t1", async () => (await readManifest(a.dir)).timeline.length), 1); // narrate's --take is a new read: a take's id after it was meant as --in-take. r = await posts("narrate", "scope-b", "p-1", "--take", "t1"); assert.equal(r.code, 2); assert.match(r.err, /to edit take t1, say --in-take t1/); assert.equal(await text(a), before); }); test("reread --in-take: the new read's file lands in the project's dir under a fresh number, the take picks it, the project keeps its pick", { skip: !haveFfmpeg && "needs ffmpeg" }, async () => { const a = await project("scope-c", [narrated("1", "2026-10-30T10:00:00.000Z")]); await tone(path.join(a.dir, "narration/posts/1/take-1.orpheus-thread.wav")); const before = await text(a); assert.equal((await take("scope-c", "--id", "t1", "--label", "T", "--draft")).code, 0); const r = await posts("reread", "scope-c", "p-1", "--without", "parent:0", "--in-take", "t1"); assert.equal(r.code, 0, r.err + r.out); assert.equal(await text(a), before); const n = (await resolveTakeManifest(a.dir, "t1")).manifest.timeline[0].narration; assert.equal(n.pick, 1); assert.match(n.takes[1].file, /^narration\/posts\/1\/take-2\./); assert.ok(await stat(path.join(a.dir, n.takes[1].file))); }); test("reread --pause --in-take: the paused read lands in the project's dir, the take picks it with its words moved on, the project keeps its pick", { skip: !haveFfmpeg && "needs ffmpeg" }, async () => { const words = [{ w: "That", start: 3.6, end: 3.8 }, { w: "is", start: 3.85, end: 3.95 }, { w: "exactly", start: 4.0, end: 4.4 }, { w: "what", start: 4.6, end: 4.8 }]; const it = narrated("1", "2026-10-30T10:00:00.000Z"); it.narration.takes[0].words = words; const a = await project("scope-pause", [it]); await tone(path.join(a.dir, "narration/posts/1/take-1.orpheus-thread.wav")); const before = await text(a); assert.equal((await take("scope-pause", "--id", "t1", "--label", "T", "--draft")).code, 0); const r = await posts("reread", "scope-pause", "p-1", "--pause", "exactly=0.5", "--in-take", "t1"); assert.equal(r.code, 0, r.err + r.out); assert.equal(await text(a), before, "the project's manifest untouched"); const n = (await resolveTakeManifest(a.dir, "t1")).manifest.timeline[0].narration; assert.equal(n.pick, 1); assert.match(n.takes[1].file, /^narration\/posts\/1\/take-2\.pause-orpheus\.wav$/); assert.deepEqual(n.takes[1].words.map((w) => w.start), [3.6, 3.85, 4.0, 5.1]); assert.ok(await stat(path.join(a.dir, n.takes[1].file))); }); test("--import-item: an item of another project cloned into the take with its narration and pick, its files HARD-LINKED in, the authors' pictures; placed; again: unchanged; the source and the project untouched", async () => { const src = await project("imp-src", [narrated("10", "2026-10-30T12:30:00.000Z"), narrated("11", "2026-10-30T12:40:00.000Z"), narrated("12", "2026-10-30T12:50:00.000Z")], { postAvatars: { neighbour_one: { src: "posts-media/_avatars/neighbour_one.jpg", source: "archive" } }, }); for (const id of ["10", "11", "12"]) { await tone(path.join(src.dir, `narration/posts/${id}/take-1.orpheus-thread.wav`)); await writeFile(path.join(src.dir, `narration/posts/${id}/take-1.parts.json`), "[]"); await mkdir(path.join(src.dir, "narration/parts-cache"), { recursive: true }); await writeFile(path.join(src.dir, `narration/parts-cache/${id}a.wav`), `a${id}`); await writeFile(path.join(src.dir, `narration/parts-cache/${id}b.wav`), `b${id}`); } await mkdir(path.join(src.dir, "posts-media/_avatars"), { recursive: true }); await writeFile(path.join(src.dir, "posts-media/_avatars/neighbour_one.jpg"), "jpg"); const dst = await project("imp-dst", [post("1", "2026-10-30T10:00:00.000Z"), post("2", "2026-10-30T11:00:00.000Z"), post("3", "2026-10-30T13:00:00.000Z")]); // ANOTHER file already at one of the paths: the import links under imports// and names that. await mkdir(path.join(dst.dir, "narration/parts-cache"), { recursive: true }); await writeFile(path.join(dst.dir, "narration/parts-cache/11a.wav"), "a different read"); const [srcBefore, dstBefore] = [await text(src), await text(dst)]; assert.equal((await take("imp-dst", "--id", "t1", "--label", "T", "--draft")).code, 0); // A dry run: the plan, nothing written, nothing linked. let r = await take("imp-dst", "--id", "t1", "--import-item", "imp-src:p-10", "--dry-run", "--json"); assert.equal(r.code, 0, r.err); let doc = JSON.parse(r.out); assert.equal(doc.dryRun, true); assert.equal(doc.items[0].at.index, 2, "by date: the take's posts are in date order"); assert.ok(!(await stat(path.join(dst.dir, "narration/posts/10")).catch(() => null)), "a dry run links nothing"); assert.equal((await takeDoc(dst, "t1")).patch, null, "nor writes the take"); // Two items after p-1, in the order named; the third by date. r = await take("imp-dst", "--id", "t1", "--import-item", "imp-src:p-11,p-10", "--after", "p-1", "--json"); assert.equal(r.code, 0, r.err); doc = JSON.parse(r.out); assert.deepEqual(doc.items.map((x) => [x.id, x.action, x.at.after]), [["p-11", "added", "p-1"], ["p-10", "added", "p-11"]]); assert.deepEqual(doc.items[0].narration, { takes: 1, pick: 1, file: "narration/posts/11/take-1.orpheus-thread.wav" }); const renamed = doc.items[0].files.find((f) => f.was === "narration/parts-cache/11a.wav"); assert.equal(renamed.path, "imports/imp-src/narration/parts-cache/11a.wav"); assert.deepEqual(doc.items[0].avatars, ["neighbour_one"]); r = await take("imp-dst", "--id", "t1", "--import-item", "imp-src:12", "--before", "p-3"); assert.equal(r.code, 0, r.err); // A take-patch write, a supported path (12 Nov): no deprecation line. assert.doesNotMatch(r.err, /deprecated/); assert.match(r.out, /p-12: added from imp-src:12 at 4, after p-2, before p-3/); const m = (await resolveTakeManifest(dst.dir, "t1")).manifest; assert.deepEqual(m.timeline.map((e) => e.id), ["p-1", "p-11", "p-10", "p-2", "p-12", "p-3"]); assert.equal(m.timeline[1].narration.takes[0].parts[0].file, "imports/imp-src/narration/parts-cache/11a.wav", "the clone names the renamed path"); assert.equal(m.postAvatars.neighbour_one.src, "posts-media/_avatars/neighbour_one.jpg"); // Hard links: the same inode as the source's; the sidecar came too; the other file was not written over. const [s1, d1] = await Promise.all([stat(path.join(src.dir, "narration/posts/11/take-1.orpheus-thread.wav")), stat(path.join(dst.dir, "narration/posts/11/take-1.orpheus-thread.wav"))]); assert.equal(d1.ino, s1.ino); assert.ok(await stat(path.join(dst.dir, "narration/posts/11/take-1.parts.json"))); assert.equal(await readFile(path.join(dst.dir, "narration/parts-cache/11a.wav"), "utf8"), "a different read"); assert.equal(await text(src), srcBefore, "the source is only read"); assert.equal(await text(dst), dstBefore, "the take's project manifest is untouched"); // Again: nothing changes, nothing written (no undo point). const revs = (await listTakeRevisions(dst.dir, "t1")).length; r = await take("imp-dst", "--id", "t1", "--import-item", "imp-src:p-11", "--json"); doc = JSON.parse(r.out); assert.equal(doc.unchanged, true); assert.equal(doc.items[0].action, "unchanged"); assert.equal((await listTakeRevisions(dst.dir, "t1")).length, revs); // An edit in the take, then an import again: updated from the source, in place. assert.equal((await posts("set-item", "imp-dst", "p-11", "hold=5", "--in-take", "t1")).code, 0); r = await take("imp-dst", "--id", "t1", "--import-item", "imp-src:p-11", "--json"); doc = JSON.parse(r.out); assert.equal(doc.items[0].action, "updated"); assert.equal(doc.items[0].at.index, 1, "in place"); // Again with the same --after: where they already stand, as they are -- unchanged, no write. const revs2 = (await listTakeRevisions(dst.dir, "t1")).length; r = await take("imp-dst", "--id", "t1", "--import-item", "imp-src:p-11,p-10", "--after", "p-1", "--json"); doc = JSON.parse(r.out); assert.deepEqual([doc.unchanged, ...doc.items.map((x) => x.action)], [true, "unchanged", "unchanged"]); assert.equal((await listTakeRevisions(dst.dir, "t1")).length, revs2); // The lint of the take: every file its items name is there. const files = await namedFileProblems((await resolveTakeManifest(dst.dir, "t1")).manifest, dst.dir); assert.deepEqual(files, { errors: [], warnings: [] }); // Refusals: no such item (1), a malformed ref (2), an anchor not in the take (1). assert.equal((await take("imp-dst", "--id", "t1", "--import-item", "imp-src:p-99")).code, 1); assert.equal((await take("imp-dst", "--id", "t1", "--import-item", "imp-src")).code, 2); r = await take("imp-dst", "--id", "t1", "--import-item", "imp-src:p-10", "--after", "p-77"); assert.equal(r.code, 1); assert.match(r.err, /has no entry p-77/); }); test("timeline import-item: items of another project INTO THE DRAFT -- cloned, files linked, placed; again: unchanged; an undo point; the source untouched", async () => { const src = await project("dimp-src", [narrated("20", "2026-10-30T12:30:00.000Z"), narrated("21", "2026-10-30T12:40:00.000Z")]); for (const id of ["20", "21"]) { await tone(path.join(src.dir, `narration/posts/${id}/take-1.orpheus-thread.wav`)); await writeFile(path.join(src.dir, `narration/posts/${id}/take-1.parts.json`), "[]"); } await project("dimp-dst", [post("1", "2026-10-30T10:00:00.000Z"), post("3", "2026-10-30T13:00:00.000Z")]); const srcBefore = await text(src); const timeline = (...a) => cli(timelineCommand, a); const dst = { dir: path.join(REPORTS, "dimp-dst") }; const ids = async () => (await readManifest(dst.dir)).timeline.map((e) => e.id); // A dry run: the plan, nothing written, nothing linked. let r = await timeline("dimp-dst", "import-item", "dimp-src:p-20", "--dry-run", "--json"); assert.equal(r.code, 0, r.err); let doc = JSON.parse(r.out); assert.deepEqual([doc.dryRun, doc.take, doc.items[0].at.index], [true, null, 1], "by date: between p-1 and p-3"); assert.deepEqual(await ids(), ["p-1", "p-3"]); assert.ok(!(await stat(path.join(dst.dir, "narration/posts/20")).catch(() => null)), "a dry run links nothing"); // Into the draft after p-1, in the order named; the files hard-linked; an undo point of the project's. r = await timeline("dimp-dst", "import-item", "dimp-src:p-21,p-20", "--after", "p-1", "--json"); assert.equal(r.code, 0, r.err); doc = JSON.parse(r.out); assert.deepEqual(doc.items.map((x) => [x.id, x.action]), [["p-21", "added"], ["p-20", "added"]]); assert.match(doc.revision, /^revisions\/.*-auto-before-import-item\.manifest\.json$/); assert.deepEqual(await ids(), ["p-1", "p-21", "p-20", "p-3"]); const [s1, d1] = await Promise.all([stat(path.join(src.dir, "narration/posts/21/take-1.orpheus-thread.wav")), stat(path.join(dst.dir, "narration/posts/21/take-1.orpheus-thread.wav"))]); assert.equal(d1.ino, s1.ino); assert.equal(await text(src), srcBefore, "the source is only read"); // Again: unchanged, nothing written. const before = await readFile(path.join(dst.dir, "video.manifest.json"), "utf8"); r = await timeline("dimp-dst", "import-item", "dimp-src:p-21", "--json"); doc = JSON.parse(r.out); assert.deepEqual([doc.unchanged, doc.items[0].action, doc.revision], [true, "unchanged", null]); assert.equal(await readFile(path.join(dst.dir, "video.manifest.json"), "utf8"), before); // Words, and the refusals. r = await timeline("dimp-dst", "import-item", "dimp-src:p-20", "--before", "p-1"); assert.equal(r.code, 0, r.err); assert.match(r.out, /p-20: moved from dimp-src:p-20 at 0, before p-1/); // The undo point of a second import-item inside the undo window is the first's (autoSnapshot's rate). assert.match(r.out, /the draft: 1 item\(s\)$/); r = await timeline("dimp-dst", "import-item", "dimp-src:p-20", "--after", "p-77"); assert.equal(r.code, 1); assert.match(r.err, /the draft has no entry p-77/); assert.equal((await timeline("dimp-dst", "import-item", "dimp-src")).code, 2); assert.equal((await timeline("dimp-dst", "import-item", "dimp-src:p-99")).code, 1); }); test("draftImportArgv: a page's import ask becomes checked argv -- no flag rides in on the refs, every word a ref, the anchor an entry id", () => { assert.deepEqual(draftImportArgv("p", { refs: " src:p-1,p-2 other/x:7 ", after: "p-1", dryRun: true }), ["p", "import-item", "src:p-1,p-2", "other/x:7", "--after", "p-1", "--dry-run", "--json"]); assert.deepEqual(draftImportArgv("p", { refs: "src:p-1", before: "c02" }), ["p", "import-item", "src:p-1", "--before", "c02", "--json"]); const refused = (ask, re) => assert.throws(() => draftImportArgv("p", ask), (e) => e instanceof ImportRefused && re.test(e.message)); refused({ refs: "src:p-1 --take t1" }, /"--take" is a flag, not an item/); refused({ refs: "src:p-1 --as x" }, /"--as" is a flag/); refused({ refs: "-x" }, /is a flag/); refused({ refs: "src:p-1 t1" }, /"t1" is not :/); refused({ refs: " " }, /import needs/); refused({ refs: 7 }, /import needs/); refused({ refs: "src:p-1", after: "--take" }, /after "--take" is not an entry id/); refused({ refs: "src:p-1", before: "a b" }, /before "a b" is not an entry id/); refused({ refs: "src:p-1", after: "a", before: "b" }, /not both/); }); test("--draft --from : a draft that starts from another take's patch (a --patch over it), its timeline its own", async () => { const a = await project("draft-from", [post("1", "2026-10-30T10:00:00.000Z"), post("2", "2026-10-30T11:00:00.000Z")]); assert.equal((await take("draft-from", "--id", "base", "--label", "Base", "--draft", "--patch-json", JSON.stringify({ timeline: [post("2", "2026-10-30T11:00:00.000Z")], render: { posts: { slurs: { on: false } } } }))).code, 0); const r = await take("draft-from", "--id", "next", "--draft", "--from", "base", "--patch-json", JSON.stringify({ render: { transition: 0.2 } })); assert.equal(r.code, 0, r.err); const d = await takeDoc(a, "next"); assert.deepEqual(d.patch.timeline.map((e) => e.id), ["p-2"]); assert.equal(d.patch.render.posts.slurs.on, false); assert.equal(d.patch.render.transition, 0.2); assert.match(d.label, /Base \(from base\)/); assert.equal((await take("draft-from", "--id", "x", "--draft", "--from", "nope")).code, 2); }); test("the editor's token: the environment's, else the editor's env files'; an explicit empty one is none; never in the message", async () => { const dir = path.join(ROOT, "env"); await mkdir(dir, { recursive: true }); const file = path.join(dir, ".env"); await writeFile(file, "OTHER=1\nWORKER_TOKEN=from-the-file\n"); assert.equal(editorFromEnv({}, [file]).token, "from-the-file"); assert.equal(editorFromEnv({ WORKER_TOKEN: "env" }, [file]).token, "env"); assert.throws(() => editorFromEnv({ WORKER_TOKEN: "" }, [file]), /WORKER_TOKEN is not set/); assert.throws(() => editorFromEnv({}, [path.join(dir, "missing")]), (e) => /WORKER_TOKEN is not set/.test(e.message) && !e.message.includes("from-the-file")); }); test("umtool lint --take: the take's manifest linted as its render reads it, and a picked read not in the project is an ERROR (exit 1)", async () => { const a = await project("lint-a", [narrated("1", "2026-10-30T10:00:00.000Z")]); assert.equal((await take("lint-a", "--id", "t1", "--label", "T", "--draft")).code, 0); let r = await cli(lintCommand, ["lint-a", "--take", "t1", "--json"]); let doc = JSON.parse(r.out); assert.equal(r.code, 1); assert.ok(doc.errors.some((e) => /timeline p-1: its picked read narration\/posts\/1\/take-1\.orpheus-thread\.wav is not in the project/.test(e)), doc.errors.join("\n")); await tone(path.join(a.dir, "narration/posts/1/take-1.orpheus-thread.wav")); r = await cli(lintCommand, ["lint-a", "--take", "t1", "--json"]); doc = JSON.parse(r.out); assert.equal(doc.errors.filter((e) => /not in the project/.test(e)).length, 0, doc.errors.join("\n")); assert.equal(doc.take, "t1"); assert.ok(!(await stat(path.join(a.dir, ".take-t1.lint.json")).catch(() => null)), "the scratch manifest is gone"); assert.equal((await cli(lintCommand, ["lint-a", "--take", "nope"])).code, 1); assert.equal((await cli(lintCommand, [])).code, 2); });