// `umtool reads` (lane reads-find, readsCli.mjs): the CLI twin of the Reads page, every verb with --json -- the list // and Find, Replace (dry run, applied, undone), Everywhere, the queue, the picks -- and THE ONE COUNT: the reads "made // with other settings" here are exactly `umtool cast stale`'s. Temp dirs, invented people; noLiveEditor first. import "./noLiveEditor.mjs"; import assert from "node:assert/strict"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { readsCommand } from "./readsCli.mjs"; import { castCommand } from "./castCli.mjs"; import { LIVELY, castDoc, readManifestOf, readsProject } from "./reads.fixtures.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-reads-cli-")); after(() => rm(ROOT, { recursive: true, force: true })); delete process.env.UMTOOL_CAST_FILE; async function cli(p, args) { const lines = []; const code = await readsCommand(["p", ...args], { root: p.root, out: (s) => lines.push(s), err: (s) => lines.push(s), queueDir: path.join(p.root, ".umtool-queue") }); const text = lines.join("\n"); let json = null; try { json = JSON.parse(text); } catch { json = null; } return { code, text, json }; } test("reads --find --json: the matches and lines; --stale; replace --dry-run writes nothing; replace; undo", async () => { const p = await readsProject(ROOT, "cli"); let r = await cli(p, ["--find", "north", "--json"]); assert.equal(r.code, 0); assert.deepEqual([r.json.find.matches, r.json.find.lines], [4, 3]); const m0 = await readManifestOf(p); r = await cli(p, ["replace", "north", "northern", "--dry-run", "--json"]); assert.equal(r.json.dryRun, true); assert.deepEqual(r.json.lines.map((l) => l.id), ["p-1", "c-1"]); assert.deepEqual(await readManifestOf(p), m0); r = await cli(p, ["replace", "north", "northern"]); assert.equal(r.code, 0, r.text); assert.match(r.text, /one undo point/); r = await cli(p, ["--stale", "--json"]); assert.deepEqual(r.json.reads.map((x) => x.id), ["p-1", "c-1"]); r = await cli(p, ["undo", "--json"]); assert.equal(r.code, 0, r.text); assert.deepEqual(await readManifestOf(p), m0); r = await cli(p, ["undo"]); assert.equal(r.code, 1); // Everywhere: the preview, whole words implied. r = await cli(p, ["replace", "pier", "peer", "--say-only", "--dry-run", "--json"]); assert.deepEqual(r.json.stale.map((x) => x.id), ["p-1"]); assert.deepEqual(r.json.cards, ["c-1"]); }); test("THE ONE COUNT: the reads made with other settings are `cast stale`'s, read by read", async () => { const p = await readsProject(ROOT, "count", { cast: castDoc(LIVELY) }); const r = await cli(p, ["--json"]); const logs = []; await castCommand(["stale", "p", "--json"], { reportsRoot: p.root, log: (s) => logs.push(s), err: (s) => logs.push(s) }); const cast = JSON.parse(logs.join("\n")); assert.ok(cast.count >= 1); assert.equal(r.json.settingsStale, cast.count); assert.deepEqual(r.json.reads.filter((x) => x.because.includes("settings")).map((x) => x.id).sort(), cast.reads.map((x) => (x.role === "post" && !x.thread ? x.id : `${x.id}/${x.role}`)).sort()); }); test("reread queues one step per item; queue --json says why they wait; --stop; pick with none waiting is refused", async () => { const p = await readsProject(ROOT, "q"); await cli(p, ["replace", "north", "northern"]); let r = await cli(p, ["reread", "--json"]); assert.deepEqual(r.json.steps.map((s) => s.item), ["p-1", "c-1"]); r = await cli(p, ["queue", "--json"]); assert.equal(r.json.counts.waiting, 2); assert.match(r.json.waiting, /queue runner is not running/); r = await cli(p, ["queue", "--stop", "--json"]); assert.equal(r.json.cancelled.length, 2); r = await cli(p, ["pick", "--new", "--json"]); assert.equal(r.code, 1); assert.match(r.json.error, /no read waits for a pick/); r = await cli(p, ["bogus"]); assert.equal(r.code, 2); }); // REVIEW 1, SHOULD 6: a thread read whose own line is ANOTHER take of the item (`--post-take`) that was SYNTHESIZED is // judged as that take is (madeOf follows it, so `cast stale` lists it): stale by settings here too, never "recorded". test("SHOULD 6: his line from a synthesized take (--post-take) -- the page and `cast stale` agree it is stale", async () => { const { postsCommand } = await import("./postsCli.mjs"); const { wavOf } = await import("./reads.fixtures.mjs"); const p = await readsProject(ROOT, "post-take", { cast: castDoc(LIVELY) }); 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 }, reading: 0 } }; }; // p-1's take 1 was made with the defaults (the fixture); a thread read of it with his line from that take. assert.equal(await postsCommand(["narrate", "p", "p-1", "--take", "--thread", "--post-take", "1"], { root: p.root, tts: stub, out: () => {}, err: () => {} }), 0); const r = await cli(p, ["--json"]); const row = r.json.reads.find((x) => x.id === "p-1/post"); const logs = []; await castCommand(["stale", "p", "--json"], { reportsRoot: p.root, log: (s) => logs.push(s), err: (s) => logs.push(s) }); const cast = JSON.parse(logs.join("\n")); assert.ok(cast.reads.some((x) => x.id === "p-1" && x.role === "post"), JSON.stringify(cast.reads)); assert.deepEqual([row.state, row.because], ["stale", ["settings"]]); assert.match(row.why, /take 1/); assert.equal(r.json.settingsStale, cast.count); }); // REVIEW 1, SHOULD 8: each subcommand refuses a flag it does not take (never ignores it); `--` ends the flags; // --dry-run on undo, reread and pick changes nothing; errors keep the --json shape. test("SHOULD 8: unknown flags refused; -- ends the flags; --dry-run changes nothing; --json errors", async () => { const p = await readsProject(ROOT, "flags"); let r = await cli(p, ["reread", "--bogus", "--json"]); assert.equal(r.code, 2); assert.match(r.json.error, /--bogus: not a flag of `reads
reread`/); r = await cli(p, ["queue", "--dry-run"]); assert.equal(r.code, 2, "queue takes no --dry-run"); // A `to` that starts with dashes, after --. r = await cli(p, ["replace", "--dry-run", "--json", "--", "north", "--north--"]); assert.equal(r.code, 0, r.text); assert.equal(r.json.lines[0].after.includes("--north-- pier"), true); const m0 = await readManifestOf(p); await cli(p, ["replace", "north", "northern"]); const m1 = await readManifestOf(p); r = await cli(p, ["undo", "--dry-run", "--json"]); assert.deepEqual([r.code, r.json.dryRun, r.json.available], [0, true, true]); assert.deepEqual(await readManifestOf(p), m1, "undo --dry-run undoes nothing"); r = await cli(p, ["reread", "--dry-run", "--json"]); assert.deepEqual(r.json.steps.map((s) => s.item), ["p-1", "c-1"]); r = await cli(p, ["queue", "--json"]); assert.equal(r.json.rows.length, 0, "reread --dry-run queues nothing"); r = await cli(p, ["pick", "--new", "--dry-run", "--json"]); assert.equal(r.code, 1, "nothing to pick: refused, as the real one would be"); // queue --retry of a read never queued, and of a step that cannot be retried: the --json error shape. r = await cli(p, ["queue", "--retry", "p-1", "--json"]); assert.deepEqual([r.code, r.json.ok], [1, false]); await cli(p, ["undo"]); assert.deepEqual(await readManifestOf(p), m0); });