// Post cards from Play the cut (UI notes item 13): the WARNING from a post's // own data (a quoted post, a repost, media), the archive searched (every word, // newest first, the last line of a re-fetch winning, captured and in-the-cut // said), the channels offered, and a post's own crop -- `posts set crop=`, the // writer, the build's check and the card's drawing (its top, never panned). // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdir, mkdtemp, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { validatePosts } from "umtool-report-to-video/deck"; import { postChannels, postWarnings, searchPostArchive } from "./postCards.mjs"; import { postPatchOf } from "./postsCli.mjs"; import { MANIFEST_NAME, normalizePostPatches, updatePosts } from "./manifest.mjs"; import { readFile } from "node:fs/promises"; const row = (id, text, extra = {}) => ({ id, slug: "chan-x", channelSlug: "chan-x", author: "somebody", createdAt: `2026-10-${String(10 + Number(id.slice(-1))).padStart(2, "0")}T12:00:00Z`, text, url: `https://x.com/somebody/status/${id}`, platform: "twitter", isReply: false, isRepost: false, links: [], ...extra }); test("the warning is the post's own data: a quoted post, a repost, media; none for words alone", () => { assert.deepEqual(postWarnings(row("1001", "just words")), []); const q = postWarnings(row("1002", "look at this", { quoted: { platform: "twitter", id: "9", author: "other" } })); assert.equal(q.length, 1); assert.match(q[0], /quotes a post by @other/); assert.match(postWarnings(row("1003", "pic", { mediaCount: 2 }))[0], /2 pictures or videos/); assert.match(postWarnings(row("1004", "rt", { repostOf: { platform: "twitter", id: "8", author: "x" } }))[0], /repost of @x/); assert.deepEqual(postWarnings(null), []); }); async function corpus() { const dir = await mkdtemp(path.join(tmpdir(), "umtool-posts-")); const posts = path.join(dir, "chan-x", "posts"); await mkdir(posts, { recursive: true }); await writeFile( path.join(posts, "2026-10.jsonl"), [row("1001", "the voice changer again"), row("1002", "Voice reveal soon", { quoted: { platform: "twitter", id: "9", author: "other" } }), row("1003", "pictures", { mediaCount: 1 }), row("1001", "the voice changer again (edited)")].map((r) => JSON.stringify(r)).join("\n") + "\n{torn", ); await mkdir(path.join(dir, "chan-x", "posts-media", "1002"), { recursive: true }); await writeFile(path.join(dir, "chan-x", "posts-media", "1002", "shot.png"), "png"); await mkdir(path.join(dir, "chan"), { recursive: true }); await mkdir(path.join(dir, "zeta-x", "posts"), { recursive: true }); return dir; } test("the archive searched: every word, any case, newest first; captured and in-the-cut said; the last line wins", async () => { const dir = await corpus(); const r = await searchPostArchive(dir, "chan-x", "VOICE", { manifest: { posts: [{ id: "x-1002" }] } }); assert.deepEqual(r.hits.map((h) => h.id), ["1002", "1001"]); assert.equal(r.hits[0].captured, true); assert.equal(r.hits[0].inCut, true); assert.equal(r.hits[0].warnings.length, 1); assert.equal(r.hits[1].text, "the voice changer again (edited)"); assert.equal(r.hits[1].captured, false); assert.equal((await searchPostArchive(dir, "chan-x", "")).total, 3); assert.equal((await searchPostArchive(dir, "chan", "")).archive, false); assert.deepEqual(await postChannels(dir, { posts: [] }, ["chan"]), [{ slug: "chan-x", suggested: true }, { slug: "zeta-x", suggested: false }]); }); test("a post's own crop: posts set crop=, the patch writer, the build's check (a shot needed, 120–1080)", async () => { assert.deepEqual(postPatchOf(["crop=420"]), { shotMaxHeight: 420 }); assert.deepEqual(postPatchOf(["crop=off"]), { shotMaxHeight: null }); assert.throws(() => postPatchOf(["crop=big"]), /whole number/); assert.deepEqual(normalizePostPatches({ a: { shotMaxHeight: 300 } }), { a: { shotMaxHeight: 300 } }); assert.throws(() => normalizePostPatches({ a: { shotMaxHeight: "300" } }), /whole number/); const post = { id: "x-1", platform: "x", date: "2026-10-01", text: "t", url: "https://x.com/a/status/1", shot: "posts-media/c/1/shot.png" }; assert.deepEqual(validatePosts([{ ...post, shotMaxHeight: 400 }], []), []); assert.match(validatePosts([{ ...post, shotMaxHeight: 50 }], []).join(), /120 to 1080/); assert.match(validatePosts([{ ...post, shot: undefined, shotMaxHeight: 400 }], []).join(), /has no shot/); const dir = await mkdtemp(path.join(tmpdir(), "umtool-crop-")); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify({ slug: "s", timeline: [{ type: "clip", id: "c1", video: "v", start: 0, end: 2 }], posts: [post] })); const r = await updatePosts(dir, { "x-1": { shotMaxHeight: 360 } }); assert.equal(r.posts["x-1"].shotMaxHeight, 360); assert.equal(JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")).posts[0].shotMaxHeight, 360); await updatePosts(dir, { "x-1": { shotMaxHeight: null } }); assert.equal("shotMaxHeight" in JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")).posts[0], false, "null removes the key"); });