// The archive's authors' pictures found where they are (postReel.mjs // archiveAvatarStores, `umtool posts avatars`, `umtool posts avatar`): // a project whose channels tree is a SHADOW (posts copied in, no _avatars/) // reads the corpus's stores behind it; a channel with no store at all does // not end the search; the store's "TheSubject" is the card's "thesubject" // (quartering-diet rev 9, 11 Nov: "copied 0 picture(s) … no picture yet for // @thequartering, @madamsavvy" while the corpus held both). // // Temp dirs under UMTOOL_TEST_TMP when set (a scratch dir off /tmp), else the // system's. import assert from "node:assert/strict"; import { mkdtempSync } from "node:fs"; import { mkdir, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; const BASE = process.env.UMTOOL_TEST_TMP || tmpdir(); await mkdir(BASE, { recursive: true }); const ROOT = mkdtempSync(path.join(BASE, "umtool-post-avatar-stores-")); after(() => rm(ROOT, { recursive: true, force: true })); const CORPUS = path.join(ROOT, "corpus-channels"); const REPORTS = path.join(ROOT, "reports"); // The corpus is CHANNELS_DIR, read when paths.mjs loads: set before the imports. process.env.CHANNELS_DIR = CORPUS; delete process.env.CHANNELS_FALLBACK; const reel = await import("./postReel.mjs"); const { postsCommand } = await import("./postsCli.mjs"); // First-listed channel: posts only, no posts-media/ anywhere. Then a store keyed // "TheSubject" (as X printed it) and one keyed "MadamOther". const CHANNELS = ["first-x", "subj-X", "other-x"]; async function store(tree, slug, doc) { const dir = path.join(tree, slug, "posts-media", "_avatars"); await mkdir(dir, { recursive: true }); for (const a of Object.values(doc)) await writeFile(path.join(dir, a.file), "picture"); await writeFile(path.join(dir, "avatars.json"), JSON.stringify(doc, null, 2)); } async function posts(tree, slug) { await mkdir(path.join(tree, slug, "posts"), { recursive: true }); await writeFile(path.join(tree, slug, "posts", "2026-10.jsonl"), ""); } const item = (slug, id, handle) => ({ type: "post", id: `p-${id}`, post: `${slug}/${id}`, platform: "x", author: handle.toUpperCase(), handle, date: `2026-10-2${id}T10:00:00.000Z`, text: `post ${id}`, }); async function project(name) { const dir = path.join(REPORTS, name); const shadow = path.join(dir, ".shadow-channels"); for (const c of CHANNELS) await posts(shadow, c); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: name, title: name, provenance: { siteOrigin: "", channelSlug: "subj-X", channelsDir: ".shadow-channels" }, render: { width: 1920, height: 1080, fps: 30, palette: { bg: "#000000", fg: "#ffffff", muted: "#888888", accent: "#c8752a" } }, // The first card spells the author in lower case, as a mention does. timeline: [item("first-x", "1", "thesubject"), item("subj-X", "2", "TheSubject"), item("other-x", "3", "MadamOther")], }, null, 2)); return { id: name, dir, shadow }; } const manifest = async (p) => JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); 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") }; }; for (const c of CHANNELS) await posts(CORPUS, c); await store(CORPUS, "subj-X", { TheSubject: { file: "TheSubject.png", url: "https://pbs.twimg.com/a.png", capturedAt: "2026-11-07T00:00:00Z" }, MadamOther: { file: "MadamOther.jpg", capturedAt: "2026-11-01T00:00:00Z" }, }); await store(CORPUS, "other-x", { MadamOther: { file: "MadamOther.jpg", url: "https://pbs.twimg.com/b.jpg", capturedAt: "2026-11-07T00:00:00Z" } }); test("archiveAvatarStores: every tree, every channel; a missing _avatars/ is noted and skipped; keys any case", async () => { const p = await project("unit"); const { store: got, searched } = await reel.archiveAvatarStores([p.shadow, CORPUS], CHANNELS); assert.deepEqual([...got.keys()].sort(), ["madamother", "thesubject"]); assert.equal(got.get("thesubject").handle, "TheSubject"); // The newest of two stores' pictures of one author. assert.equal(got.get("madamother").file, path.join(CORPUS, "other-x", "posts-media", "_avatars", "MadamOther.jpg")); assert.deepEqual(searched.map((s) => `${s.channel}@${s.tree === CORPUS ? "corpus" : "shadow"}:${s.state}`), [ "first-x@shadow:none", "first-x@corpus:none", "subj-X@shadow:none", "subj-X@corpus:read", "other-x@shadow:none", "other-x@corpus:read", ]); const said = reel.avatarStoresSaid(searched); assert.equal(said.length, 3); assert.match(said[0], /^first-x: no _avatars\/ in .*\.shadow-channels or .*corpus-channels$/); assert.match(said[1], /^subj-X: 2 picture\(s\) in .*corpus-channels\/subj-X\/posts-media\/_avatars; no _avatars\/ in .*\.shadow-channels$/); // One tree, as a string, as before. assert.equal((await reel.archiveAvatars(CORPUS, ["subj-X"])).size, 2); }); test("posts avatars: a shadow-channels project copies the corpus's pictures, names the stores it searched", async () => { const p = await project("shadowed"); const r = await cli(["avatars", "shadowed"]); assert.equal(r.code, 0, r.err); assert.match(r.out, /^copied 2 picture\(s\) from subj-X \+ other-x's stores: /, r.out); assert.doesNotMatch(r.out, /no picture yet/, r.out); assert.match(r.out, /first-x: no _avatars\//); const m = await manifest(p); assert.equal(m.postAvatars.TheSubject.src, "posts-media/_avatars/TheSubject.png"); assert.equal(m.postAvatars.TheSubject.source, "archive"); assert.equal(m.postAvatars.MadamOther.url, "https://pbs.twimg.com/b.jpg"); await readFile(path.join(p.dir, "posts-media", "_avatars", "TheSubject.png")); // Again: nothing more to copy, and nobody without one. const again = await cli(["avatars", "shadowed", "--json"]); const j = JSON.parse(again.out); assert.deepEqual(j.copied, {}); assert.deepEqual(j.without, []); assert.ok(j.stores.some((s) => s.channel === "first-x" && s.state === "none")); }); test("posts avatars: no store anywhere says so, and names the author as the card draws them", async () => { const p = await project("bare"); const m0 = await manifest(p); m0.timeline = [item("first-x", "1", "NoPicture")]; await writeFile(path.join(p.dir, "video.manifest.json"), JSON.stringify(m0, null, 2)); const r = await cli(["avatars", "bare"]); assert.equal(r.code, 0, r.err); assert.match(r.out, /^copied 0 picture\(s\) from no store/); assert.match(r.out, /no picture yet for @NoPicture /); }); test("posts avatar: a handle in any case sets and clears the drawn author's picture", async () => { const p = await project("manual"); const pic = path.join(ROOT, "manual.png"); await writeFile(pic, "picture"); assert.equal((await cli(["avatar", "manual", "@madamother", pic])).code, 0); let m = await manifest(p); assert.deepEqual(m.postAvatars, { MadamOther: { src: "posts-media/_avatars/MadamOther-manual.png", source: "manual" } }); // The archive's never replaces it. assert.equal((await cli(["avatars", "manual"])).code, 0); m = await manifest(p); assert.equal(m.postAvatars.MadamOther.source, "manual"); assert.equal(m.postAvatars.TheSubject.source, "archive"); assert.equal((await cli(["avatar", "manual", "MADAMOTHER", "off"])).code, 0); assert.equal((await manifest(p)).postAvatars.MadamOther, undefined); });