// REVIEW 2 OF lane/reads-find (N1-N10, and the three questions): each a test, written before its fix. Temp dirs, a stub // tts, invented people; noLiveEditor first. import "./noLiveEditor.mjs"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { readsPost } from "./readsApi.mjs"; import { readsCommand } from "./readsCli.mjs"; import { applyReplace, replaceUndoState, undoReplace } from "./readsReplace.mjs"; import { runOneItem } from "./readsQueue.mjs"; import { projectReads } from "./reads.mjs"; import { manifestToken, updatePostNarration } from "./manifest.mjs"; import { castDoc, LIVELY, readManifestOf, readsProject, wavOf } from "./reads.fixtures.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-reads-review2-")); after(() => rm(ROOT, { recursive: true, force: true })); delete process.env.UMTOOL_CAST_FILE; const ctx = (p) => ({ reportsRoot: p.root, queueDir: path.join(p.root, ".umtool-queue") }); const stub = async (body) => { await wavOf(body.out); return { jobId: "j", result: { wav: body.out, engine: body.engine, voice: body.voice, tuning: { temperature: 0.55, top_p: 0.85, top_k: 50, min_p: 0, repeat_penalty: 1.2, takes: 3, ...(body.tuning ?? {}) }, reading: body.reading ?? 0 } }; }; async function cli(p, args, o = {}) { const lines = []; const code = await readsCommand(["p", ...args], { root: p.root, out: (s) => lines.push(s), err: () => {}, queueDir: ctx(p).queueDir, ...o }); return { code, lines, text: lines.join("\n") }; } /** Every file under a dir, its path and contents: what a dry run must leave exactly as it was. */ async function snapshotOf(dir) { const out = {}; for (const f of (await readdir(dir, { recursive: true })).sort()) { const abs = path.join(dir, f); out[f] = await readFile(abs).then((b) => b.toString("base64"), () => ""); } return out; } test("N1: a These-reads preview names its reads (the rows Find counted), as apply does; without them, 400", async () => { const p = await readsProject(ROOT, "n1"); const r = await readsPost(p, { op: "preview", scope: "reads", from: "north", to: "northern" }, ctx(p)); assert.equal(r.status, 400); const ok = await readsPost(p, { op: "preview", scope: "reads", from: "north", to: "northern", only: ["c-1"] }, ctx(p)); assert.deepEqual(ok.json.lines.map((l) => l.id), ["c-1"]); }); test("N2: the preview's token goes back with Apply; the draft changed since: 409, nothing written; no token: 400", async () => { const p = await readsProject(ROOT, "n2"); const prev = await readsPost(p, { op: "preview", scope: "reads", from: "north", to: "northern", only: ["p-1", "c-1"] }, ctx(p)); assert.equal(typeof prev.json.token, "string"); assert.equal((await readsPost(p, { op: "replace", scope: "reads", from: "north", to: "northern", only: ["p-1"] }, ctx(p))).status, 400); // A script changes between the preview and Apply. await updatePostNarration(p.dir, "p-1", { script: "The ferry leaves the north pier at one." }, { token: await manifestToken(p.dir) }); const m0 = await readManifestOf(p); const r = await readsPost(p, { op: "replace", scope: "reads", from: "north", to: "northern", only: ["p-1"], token: prev.json.token }, ctx(p)); assert.equal(r.status, 409); assert.match(r.json.error, /the draft changed since the preview: preview again/); assert.deepEqual(await readManifestOf(p), m0); }); test("N3: --only with no value, empty or only commas is refused (never every read)", async () => { const p = await readsProject(ROOT, "n3"); await cli(p, ["replace", "north", "northern"]); for (const args of [["reread", "--only"], ["reread", "--only", ""], ["reread", "--only", ","], ["pick", "--new", "--only", ""], ["replace", "a", "b", "--only", ","]]) { const r = await cli(p, [...args, "--json"]); assert.equal(r.code, 2, args.join(" ")); assert.match(JSON.parse(r.text).error, /--only names no read/); } assert.equal((await readsPost(p, { op: "reread", only: ["", " "] }, ctx(p))).status, 400); }); test("N4: a write landing between the replace and its hash makes it not undoable (said); the undo goes under the token it checked; its snapshot must be the newest", async () => { const p = await readsProject(ROOT, "n4"); // A take lands in the gap (a queue step in another process). await applyReplace(p, "north", "northern", { reportsRoot: p.root, onWritten: async () => updatePostNarration(p.dir, "p-2", { lead: 0.3 }, { token: await manifestToken(p.dir) }) }); const st = await replaceUndoState(p); assert.equal(st.available, false); assert.match(st.why, /the draft changed while the replace was written/); // A clean replace, then its recorded undo point tampered: refused. const q = await readsProject(ROOT, "n4b"); await applyReplace(q, "north", "northern", { reportsRoot: q.root }); assert.equal((await replaceUndoState(q)).available, true); const file = path.join(q.dir, "narration", "reads", "replaces.json"); const j = JSON.parse(await readFile(file, "utf8")); j.records.at(-1).snapshot = "revisions/not-this-one.manifest.json"; await writeFile(file, JSON.stringify(j)); assert.match((await replaceUndoState(q)).why, /its undo point is not the one it made/); await assert.rejects(undoReplace(q), /not undone/); }); test("N5: a stale read the queue cannot re-read (a piece from a synthesized take) is never re-read by `--one` with no --only", async () => { const { postsCommand } = await import("./postsCli.mjs"); const p = await readsProject(ROOT, "n5", { cast: castDoc(LIVELY) }); assert.equal(await postsCommand(["narrate", "p", "p-1", "--take", "--thread", "--post-take", "1"], { root: p.root, tts: stub, out: () => {}, err: () => {} }), 0); const row = (await projectReads(p, { reportsRoot: p.root })).rows.find((r) => r.id === "p-1/post"); assert.deepEqual([row.state, row.queueable], ["stale", false]); let ran = 0; const r = await runOneItem(p, "p-1", [], { reportsRoot: p.root, run: async () => (ran += 1, 0), log: () => {} }); assert.equal(r.argv, null); assert.equal(ran, 0); }); test("N6: `reread --one` is the queue step's form: it needs --only and refuses --dry-run, --wait, --stale", async () => { const p = await readsProject(ROOT, "n6"); for (const args of [["reread", "--one", "p-1"], ["reread", "--one", "p-1", "--only", "p-1", "--dry-run"], ["reread", "--one", "p-1", "--only", "p-1", "--wait"], ["reread", "--one", "p-1", "--only", "p-1", "--stale"]]) { const r = await cli(p, [...args, "--json"]); assert.equal(r.code, 2, args.join(" ")); } }); test("N7: every --dry-run writes NOTHING -- replace, undo, reread, pick: the project's files byte for byte", async () => { const p = await readsProject(ROOT, "n7"); await cli(p, ["replace", "north", "northern"]); const before = await snapshotOf(p.dir); for (const args of [["replace", "pier", "peer", "--dry-run"], ["replace", "pier", "peer", "--say-only", "--dry-run"], ["undo", "--dry-run"], ["reread", "--dry-run"], ["pick", "--new", "--dry-run"]]) await cli(p, [...args, "--json"]); assert.deepEqual(await snapshotOf(p.dir), before); // And on a project that never queued: no narration/reads/ made. const q = await readsProject(ROOT, "n7b"); await updatePostNarration(q.dir, "p-1", { script: "The ferry leaves the north pier at one." }, { token: await manifestToken(q.dir) }); await cli(q, ["reread", "--dry-run", "--json"]); await assert.rejects(readdir(path.join(q.dir, "narration", "reads")), /ENOENT/); }); test("N8: a subcommand named like an Object property is refused, not looked up", async () => { const p = await readsProject(ROOT, "n8"); for (const sub of ["constructor", "toString", "__proto__", "hasOwnProperty"]) { assert.equal((await cli(p, [sub, "--json"])).code, 2, sub); assert.equal((await cli(p, [sub, "--stale", "--json"])).code, 2, `${sub} with a flag`); } }); test("N10: a piece from a synthesized take is judged on its words too -- the take's words against the line's now", async () => { const { postsCommand } = await import("./postsCli.mjs"); const p = await readsProject(ROOT, "n10"); assert.equal(await postsCommand(["narrate", "p", "p-1", "--take", "--thread", "--post-take", "1"], { root: p.root, tts: stub, out: () => {}, err: () => {} }), 0); let row = (await projectReads(p, { reportsRoot: p.root })).rows.find((r) => r.id === "p-1/post"); assert.deepEqual([row.state, row.because], ["fresh", []]); await updatePostNarration(p.dir, "p-1", { script: "The ferry leaves the north pier at one." }, { token: await manifestToken(p.dir) }); row = (await projectReads(p, { reportsRoot: p.root })).rows.find((r) => r.id === "p-1/post"); assert.deepEqual([row.state, row.because], ["stale", ["words"]]); }); test("(a) `reread --one --json`: stdout is ONE JSON document -- the inner command's words go to stderr", async () => { const p = await readsProject(ROOT, "qa"); await updatePostNarration(p.dir, "p-1", { script: "The ferry leaves the north pier at one." }, { token: await manifestToken(p.dir) }); const errs = []; const r = await cli(p, ["reread", "--one", "p-1", "--only", "p-1", "--json"], { tts: stub, err: (s) => errs.push(s) }); assert.equal(r.code, 0, r.text); const j = JSON.parse(r.text); assert.equal(j.ok, true); assert.ok(errs.join("\n").includes("p-1"), "the inner command spoke on stderr"); }); test("(c) a thread take carries no tuningOwn; a piece's own values are read.tuned[role], and tuningStale reads them there", async () => { const { postsCommand } = await import("./postsCli.mjs"); const { tuningStale } = await import("umtool-report-to-video/cast"); const { resolverFor } = await import("./castProject.mjs"); const p = await readsProject(ROOT, "qc", { cast: castDoc(LIVELY) }); assert.equal(await postsCommand(["narrate", "p", "p-1", "--take", "--thread", "--tuned", "post=takes:5"], { root: p.root, tts: stub, out: () => {}, err: () => {} }), 0); const t = (await readManifestOf(p)).timeline[0].narration.takes.at(-1); assert.equal("tuningOwn" in t, false); assert.deepEqual(t.read.tuned.post.tuning, { takes: 5 }); // The character names 4 takes: the piece owns its 5 -- not stale. const now = { ...LIVELY, takes: 4 }; assert.deepEqual(tuningStale(await readManifestOf(p), resolverFor({ cast: castDoc(now) }), { settingTakes: 3 }).filter((r) => r.id === "p-1"), []); void mkdir; });