// THE READS PAGE's REQUESTS (lane reads-find, review 1 MUST 1; readsApi.mjs, the route's body): a write acts only on // the reads it names -- the ids the page counted -- and a list naming an id no read could have is refused whole, never // narrowed to an unscoped run. Temp dirs, invented people; noLiveEditor first. import "./noLiveEditor.mjs"; import assert from "node:assert/strict"; import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { readsGet, readsPost } from "./readsApi.mjs"; import { listSteps } from "../queue/store.mjs"; import { readManifestOf, readsProject } from "./reads.fixtures.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-reads-api-")); after(() => rm(ROOT, { recursive: true, force: true })); delete process.env.UMTOOL_CAST_FILE; async function changed(name) { const p = await readsProject(ROOT, name); const m = await readManifestOf(p); m.timeline[0].narration.script = m.timeline[0].narration.script.replace("noon", "twelve noon"); m.timeline[3].narration.text = "Six months later, the north pier opened again."; await writeFile(path.join(p.dir, "video.manifest.json"), JSON.stringify(m, null, 2)); return { p, o: { reportsRoot: p.root, queueDir: path.join(p.root, ".umtool-queue") } }; } test("MUST 1: a filtered view re-reads ONLY the reads it counted; no `only`, a bad id, an empty list: 400, nothing queued", async () => { const { p, o } = await changed("only"); // The page's view: the stale reads of the ferry character -- p-1 only (the card has no character). const g = await readsGet(p, new URLSearchParams({ stale: "1", character: "ferry" }), o); assert.deepEqual(g.reads.map((r) => r.id), ["p-1"]); for (const body of [{ op: "reread" }, { op: "reread", only: [] }, { op: "reread", only: ["p-1", "p 1"] }, { op: "reread", only: "p-1" }]) { const r = await readsPost(p, body, o); assert.equal(r.status, 400, JSON.stringify(body)); } assert.deepEqual(await listSteps(o.queueDir), [], "nothing queued by a refused request"); const r = await readsPost(p, { op: "reread", only: g.reads.map((x) => x.id) }, o); assert.equal(r.status, 200); assert.deepEqual(r.json.steps.map((s) => s.item), ["p-1"], "the card, stale too, is not re-read"); // An id as long as the model makes (a 64-character item, a 24-character screen id, line 12): accepted. const long = `${"x".repeat(64)}/screen:${"s".repeat(24)}:12`; const r2 = await readsPost(p, { op: "reread", only: [long] }, o); assert.equal(r2.status, 200); assert.match(r2.json.skipped[0].why, /no such read/); }); test("MUST 1: pick and a replace of These reads name their reads; refused unscoped", async () => { const { p, o } = await changed("scoped"); assert.equal((await readsPost(p, { op: "pick", which: "new" }, o)).status, 400); assert.equal((await readsPost(p, { op: "replace", scope: "reads", from: "north", to: "northern" }, o)).status, 400); const before = await readManifestOf(p); assert.equal((await readsPost(p, { op: "replace", scope: "reads", from: "north", to: "northern", only: ["c-1", "bad id"] }, o)).status, 400); assert.deepEqual(await readManifestOf(p), before); // The previewed ids: only those change. const prev = await readsPost(p, { op: "preview", scope: "reads", from: "north", to: "northern", only: ["p-1", "p-3", "c-1"] }, o); assert.deepEqual(prev.json.lines.map((l) => l.id), ["p-1", "c-1"]); const done = await readsPost(p, { op: "replace", scope: "reads", from: "north", to: "northern", only: ["c-1"], token: prev.json.token }, o); assert.equal(done.status, 200); const m = await readManifestOf(p); assert.match(m.timeline[3].narration.text, /northern/); assert.doesNotMatch(m.timeline[0].narration.script, /northern/); assert.equal(done.json.undo.available, true); // Everywhere with Whole word off: refused in the page's words. assert.match((await readsPost(p, { op: "preview", scope: "say", from: "pier", to: "peer" }, o)).json.error, /whole words only/); });