// A post item's EXCERPT (post-item.mjs; rev 9, 6 Nov): a long post read in part -- verbatim spans of its words, in
// order -- the read the spans joined with " … ", the card the whole post with the unread words dimmed. Its writer
// (`umtool posts set-item
- excerpt='[…]'`) and its script (`posts narrate --all --draft-scripts`).
// Made-up posts only.
import assert from "node:assert/strict";
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { after, test } from "node:test";
import { postsCommand } from "./postsCli.mjs";
import {
excerptCutWords, excerptErrors, excerptRanges, postCardHtml, postReadText, readSeconds, validatePostItem,
} from "umtool-report-to-video/post-item";
const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-post-excerpt-"));
after(() => rm(ROOT, { recursive: true, force: true }));
const REPORTS = path.join(ROOT, "reports");
const TEXT = "I bought your coffee. You have my name and my address. Also a long aside about scrubs nobody needs. I look forward to the video.";
const item = (o = {}) => ({ type: "post", id: "p-1", post: "savvy-x/1", platform: "x", author: "A Person", handle: "aperson", date: "2026-10-23T10:00:00.000Z", text: TEXT, ...o });
const cli = async (args) => {
const lines = [];
const errs = [];
const code = await postsCommand(args, { root: REPORTS, out: (s) => lines.push(s), err: (s) => errs.push(s) });
return { code, out: lines.join("\n"), err: errs.join("\n") };
};
async function project(name, timeline) {
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: "", channelSlug: "savvy-x" },
render: { width: 1920, height: 1080, fps: 30 }, timeline,
}, null, 2));
return { id: name, dir };
}
const manifest = async (p) => JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8"));
test("the excerpt: spans found in order, the read joined with \" … \", the unread words dimmed on the whole card", () => {
const e = item({ excerpt: ["I bought your coffee.", "I look forward to the video."] });
assert.deepEqual(validatePostItem(e), []);
assert.deepEqual(excerptRanges(TEXT, e.excerpt), [{ start: 0, end: 21 }, { start: TEXT.indexOf("I look"), end: TEXT.length }]);
assert.equal(postReadText(e), "I bought your coffee. … I look forward to the video.");
assert.ok(readSeconds(e) < readSeconds(item()), "it reads in less time than the whole post");
const cut = excerptCutWords(e);
assert.ok(cut.has(4) && !cut.has(0) && !cut.has(3), "words 0-3 read; 4 on dimmed");
const html = postCardHtml(e, { width: 1248, height: 702 });
assert.match(html, /data-post-cut=""/);
for (const w of ["scrubs", "address", "coffee", "video"]) assert.ok(html.includes(w), `the card shows "${w}" (the whole post)`);
assert.equal(postCardHtml(item(), { width: 1248, height: 702 }).includes("data-post-cut"), false, "no excerpt: nothing dimmed");
// Refused: a span not in the text, spans out of order, an empty list.
assert.match(excerptErrors(item({ excerpt: ["I bought your tea."] }))[0], /is not in the post's words \(an excerpt is verbatim\)/);
assert.match(excerptErrors(item({ excerpt: ["I look forward", "I bought"] }))[0], /before the span ahead of it/);
assert.match(excerptErrors(item({ excerpt: [] }))[0], /must be a list of the post's own words/);
});
test("posts set-item excerpt='[…]': written through the item writer, the narrated script redrafted from it; excerpt=off reads the whole post again", async () => {
const p = await project("x", [item({ narration: { script: "I bought your coffee. You have my name and my address. Also a long aside about scrubs nobody needs. I look forward to the video." } })]);
let r = await cli(["set-item", "x", "p-1", `excerpt=["I bought your coffee.","I look forward to the video."]`]);
assert.equal(r.code, 0, r.err);
let e = (await manifest(p)).timeline[0];
assert.deepEqual(e.excerpt, ["I bought your coffee.", "I look forward to the video."]);
assert.equal(e.narration.script, "I bought your coffee. … I look forward to the video.");
assert.equal(e.narration.excerpt, true);
assert.equal(e.text, TEXT, "the post's words are never rewritten");
// A span that is not verbatim is refused in the build's words; the manifest untouched.
r = await cli(["set-item", "x", "p-1", `excerpt=["I bought your tea."]`]);
assert.notEqual(r.code, 0);
assert.match(r.err + r.out, /not in the post's words/);
assert.deepEqual((await manifest(p)).timeline[0].excerpt, ["I bought your coffee.", "I look forward to the video."]);
r = await cli(["set-item", "x", "p-1", "excerpt=nope"]);
assert.notEqual(r.code, 0);
assert.match(r.err + r.out, /JSON list/);
r = await cli(["set-item", "x", "p-1", "excerpt=off"]);
assert.equal(r.code, 0, r.err);
e = (await manifest(p)).timeline[0];
assert.equal(e.excerpt, undefined);
assert.equal(e.narration.excerpt, undefined);
assert.match(e.narration.script, /scrubs nobody needs/);
});
test("posts narrate --all --draft-scripts drafts an excerpted item from its spans", async () => {
const p = await project("d", [item({ excerpt: ["I bought your coffee.", "I look forward to the video."] })]);
const r = await cli(["narrate", "d", "--all", "--draft-scripts"]);
assert.equal(r.code, 0, r.err);
const e = (await manifest(p)).timeline[0];
assert.equal(e.narration.script, "I bought your coffee. … I look forward to the video.");
assert.equal(e.narration.excerpt, true);
});