// Organise, publish and "Archive this account" from the Posts library page // and from `umtool library` (lane x-trending-publish): each the editor's // action; the page's presses stamped the operator's whatever the body says; // the CLI's `--by` "agent" unless told, with a merge and a publish the // operator's. A fake `ask` (no socket), a temp reports root. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdtemp, readFile, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { libraryCommand } from "./cli.mjs"; import { archivePress, organisePress, PressError, publishPress } from "./presses.mjs"; import { markSeen, mergeSeen, seenFile, seenOf } from "./seen.mjs"; const CONFIRM = "FAKE-TEST-VALUE01234567"; const SYNC_OFF = (slug, handle) => `Made the channel "${slug}" for @${handle}, on no site, with sync off (its priority is paused): nothing was asked of X, and nothing will be until you say so. To turn sync on, set its priority to normal on /channels. Its first fetch is Fetch posts on its page.`; function editor({ refuse } = {}) { const asked = []; const ask = async (route, init = {}) => { asked.push({ route, ...init }); if (refuse) throw new Error(refuse); const b = init.body ?? {}; if (route === "/api/ops/organise-trending") { if (b.op === "rename") return { ok: true, op: "rename", topic: b.topic, title: b.title, was: "Kettles", changed: true, sentence: `Topic "Kettles" is now "${b.title}".` }; if (b.op === "move") return { ok: true, op: "move", to: { id: "cups", title: "Cups", created: false }, moved: b.ids ?? ["11", "12"], skipped: [{ id: "99", reason: "not in this library" }], sentence: "2 posts moved." }; return { ok: true, op: "merge", into: { id: "kettles", title: "Kettles" }, merged: [{ id: "cups", title: "Cups", posts: 2, pages: 1 }], posts: 2, pages: 2, watch: { dropped: [] }, sentence: '"Cups" is now part of "Kettles". This cannot be undone.' }; } if (route === "/api/ops/publish-trending") { return b.dryRun ? { ok: true, dryRun: true, channel: b.channel ?? "kettles", creates: true, posts: 2, files: 3, bytes: 204, confirm: CONFIRM, sentence: 'Publishing "Kettles" would make the channel "kettles", on no site.' } : { ok: true, dryRun: false, channel: "kettles", creates: true, posts: 2, files: 3, bytes: 204, confirm: CONFIRM, sentence: 'Publishing "Kettles" made the channel "kettles", on no site.' }; } if (route === "/api/ops/x-search-lead") { if (b.dryRun) return { ok: true, dryRun: true, slug: `${b.handle}-x`, fields: {} }; // From a library with no "fetch": sync off, and the editor's own sentence. return b.fetch ? { ok: true, slug: `${b.handle}-x`, jobIds: ["job-1"] } : { ok: true, slug: `${b.handle}-x`, jobIds: [], syncOff: true, sentence: SYNC_OFF(`${b.handle}-x`, b.handle) }; } if (route.startsWith("/api/ops/trending?")) { return { ok: true, accounts: [{ handle: "teapot_news", posts: 3, kept: 2, keptByOperator: 1, topics: ["kettles", "cups"], archivedAs: ["teapot_news-x"] }, { handle: "saucer_fan", posts: 1, kept: 0, keptByOperator: 0, topics: ["cups"] }], total: 2, posts: 4, more: 0 }; } throw new Error(`unexpected ${route}`); }; return { ask, asked, posts: () => asked.filter((a) => a.method === "POST") }; } async function withRoot(fn) { const root = await mkdtemp(path.join(tmpdir(), "umtool-library-organise-")); try { return await fn(root); } finally { await rm(root, { recursive: true, force: true }); } } async function cli(args, e, root) { const lines = []; const errors = []; const code = await libraryCommand(args, { ask: e.ask, out: (s) => lines.push(s), err: (s) => errors.push(s), reportsRoot: root }); return { code, lines, errors }; } test("the page's organise presses are the operator's, whatever the body says about who", async () => { const e = editor(); await organisePress(e.ask, { lib: "lib", op: "rename", topic: " kettles ", title: " Teapots ", by: "agent" }); await organisePress(e.ask, { lib: "lib", op: "move", to: "cups", ids: ["11", "11", "12"], by: "agent" }); await organisePress(e.ask, { lib: "lib", op: "move", to: "Spoons", thread: "11", create: true }); assert.deepEqual(e.posts().map((p) => p.body), [ { slug: "lib", op: "rename", topic: "kettles", title: "Teapots", by: "operator" }, { slug: "lib", op: "move", to: "cups", ids: ["11", "12"], by: "operator" }, { slug: "lib", op: "move", to: "Spoons", thread: "11", create: true, by: "operator" }, ]); for (const [body, re] of [ [{ lib: "lib", op: "split" }, /"op" is/], [{ lib: "../x", op: "rename", topic: "a", title: "b" }, /"lib" is the library's slug/], [{ lib: "lib", op: "rename", topic: "a" }, /"title" is/], [{ lib: "lib", op: "move", to: "cups" }, /one of them/], [{ lib: "lib", op: "move", to: "cups", ids: ["x"] }, /list of post ids/], [{ lib: "lib", op: "move", ids: ["11"] }, /"to" is/], [{ lib: "lib", op: "merge", into: "kettles", from: [] }, /"from" is/], ]) { await assert.rejects(organisePress(e.ask, body), (err) => err instanceof PressError && re.test(err.message), JSON.stringify(body)); } assert.equal(e.posts().length, 3, "a refused press asks the editor nothing"); }); test("a merge press is the operator's, and umtool's Mark seen state follows it: the earliest mark stands", async () => { await withRoot(async (root) => { await markSeen(root, { slug: "lib", topics: ["cups"], now: Date.parse("2026-11-17T08:00:00.000Z") }); await markSeen(root, { slug: "lib", topics: ["kettles", "spoons"], now: Date.parse("2026-11-18T08:00:00.000Z") }); const e = editor(); const out = await organisePress(e.ask, { lib: "lib", op: "merge", into: "Kettles", from: [" Cups "], by: "agent" }, { mergeSeen: (lib, into, from) => mergeSeen(root, { slug: lib, into, from }) }); assert.deepEqual(e.posts()[0].body, { slug: "lib", op: "merge", into: "Kettles", from: ["Cups"], by: "operator" }); // By the EDITOR's ids (kettles, cups), not the names the press was sent with. assert.deepEqual(out.seen, { slug: "lib", into: "kettles", seenAt: "2026-11-17T08:00:00.000Z", changed: true }); const seen = await seenOf(root, "lib"); assert.deepEqual(Object.keys(seen).sort(), ["kettles", "spoons"]); assert.equal(seen.kettles, "2026-11-17T08:00:00.000Z"); assert.equal(JSON.parse(await readFile(seenFile(root), "utf8")).libraries.lib.topics.kettles.previous, "2026-11-18T08:00:00.000Z"); // A topic that stays and was never marked stays unmarked; nothing to change writes nothing. await markSeen(root, { slug: "other", topics: ["cups"], now: Date.parse("2026-11-17T08:00:00.000Z") }); assert.deepEqual(await mergeSeen(root, { slug: "other", into: "kettles", from: ["cups"] }), { slug: "other", into: "kettles", seenAt: null, changed: true }); assert.deepEqual(await seenOf(root, "other"), {}); assert.deepEqual(await mergeSeen(root, { slug: "other", into: "kettles", from: ["cups"] }), { slug: "other", into: "kettles", seenAt: null, changed: false }); // The mark failing to follow is said, never thrown: the merge is done. const said = await organisePress(e.ask, { lib: "lib", op: "merge", into: "kettles", from: ["cups"] }, { mergeSeen: async () => { throw new Error("posts-library.json does not parse"); } }); assert.deepEqual(said.seen, { error: "posts-library.json does not parse" }); assert.equal(said.ok, true); }); }); test("the publish press: two steps, the operator's; a confirm that is not a dry run's digest asks nothing", async () => { const e = editor(); const dry = await publishPress(e.ask, { lib: "lib", topic: "kettles", dryRun: true, by: "agent", channel: "Kettles-2026" }); assert.equal(dry.confirm, CONFIRM); await publishPress(e.ask, { lib: "lib", topic: "kettles", confirm: CONFIRM, by: "agent" }); assert.deepEqual(e.posts().map((p) => p.body), [ { slug: "lib", topic: "kettles", channel: "Kettles-2026", dryRun: true, by: "operator" }, { slug: "lib", topic: "kettles", confirm: CONFIRM, by: "operator" }, ]); for (const [body, re] of [ [{ lib: "lib", topic: "kettles" }, /carries the "confirm" its dry run answered/], [{ lib: "lib", topic: "kettles", confirm: "yes" }, /carries the "confirm"/], [{ lib: "lib", dryRun: true }, /"topic" is/], [{ lib: "lib", topic: "kettles", dryRun: true, channel: "../x" }, /"channel" is the channel's slug/], ]) { await assert.rejects(publishPress(e.ask, body), (err) => err instanceof PressError && re.test(err.message), JSON.stringify(body)); } assert.equal(e.posts().length, 2); const refused = editor({ refuse: "A read or a capture of this library is queued or running: publish once it has finished (its job is on /jobs)." }); await assert.rejects(publishPress(refused.ask, { lib: "lib", topic: "kettles", confirm: CONFIRM }), /queued or running/); }); test("Archive this account from the page: the lead action, and never a fetch", async () => { const e = editor(); const dry = await archivePress(e.ask, { lib: "lib", handle: "@saucer_fan", name: "Saucer Fan", dryRun: true, fetch: true }); assert.equal(dry.sentence, 'Would make the channel "saucer_fan-x" for @saucer_fan, on no site, with sync off. Nothing is asked of X.'); const done = await archivePress(e.ask, { lib: "lib", handle: "saucer_fan", fetch: true }); // The editor's sentence, whole: sync off, nothing asked of X, how to turn sync on and run the first fetch. assert.equal(done.sentence, SYNC_OFF("saucer_fan-x", "saucer_fan")); assert.equal(done.syncOff, true); assert.deepEqual(e.posts().map((p) => p.body), [ { slug: "lib", handle: "saucer_fan", action: "archive", name: "Saucer Fan", dryRun: true }, { slug: "lib", handle: "saucer_fan", action: "archive" }, ]); await assert.rejects(archivePress(e.ask, { lib: "lib", handle: "not a handle" }), /"handle" is the account's handle/); }); test("umtool library rename / move: --by is agent unless told", async () => { await withRoot(async (root) => { const e = editor(); assert.equal((await cli(["rename", "lib", "kettles", "--title", "Teapots"], e, root)).code, 0); assert.equal((await cli(["move", "lib", "--to", "cups", "--id", "11, 12", "--by", "operator"], e, root)).code, 0); const moved = await cli(["move", "lib", "--to", "Spoons", "--thread", "11", "--create"], e, root); assert.match(moved.lines[0], /2 posts moved\.\n skipped 99: not in this library/); assert.deepEqual(e.posts().map((p) => p.body), [ { slug: "lib", op: "rename", topic: "kettles", title: "Teapots", by: "agent" }, { slug: "lib", op: "move", to: "cups", ids: ["11", "12"], by: "operator" }, { slug: "lib", op: "move", to: "Spoons", thread: "11", create: true, by: "agent" }, ]); for (const [args, re] of [ [["rename", "lib", "kettles"], /rename: umtool library rename/], [["move", "lib", "--id", "11"], /--to names the topic/], [["move", "lib", "--to", "cups"], /--id or --thread, one of them/], [["move", "lib", "--to", "cups", "--id", "1", "--thread", "1"], /one of them/], ]) { const r = await cli(args, e, root); assert.equal(r.code, 1); assert.match(r.errors[0], re); } assert.equal(e.posts().length, 3); }); }); test("umtool library merge and publish want --by operator; a dry run of a publish is anyone's and prints the confirm", async () => { await withRoot(async (root) => { const e = editor(); const agent = await cli(["merge", "lib", "--into", "kettles", "--from", "cups"], e, root); assert.equal(agent.code, 1); assert.match(agent.errors[0], /merging topics is the operator's \(it cannot be undone\): say --by operator/); assert.equal(e.posts().length, 0, "an agent's merge asks the editor nothing"); await markSeen(root, { slug: "lib", topics: ["cups"], now: Date.parse("2026-11-17T08:00:00.000Z") }); await markSeen(root, { slug: "lib", topics: ["kettles"], now: Date.parse("2026-11-18T08:00:00.000Z") }); const merged = await cli(["merge", "lib", "--into", "kettles", "--from", "cups, spoons", "--by", "operator", "--json"], e, root); assert.equal(merged.code, 0); assert.deepEqual(e.posts()[0].body, { slug: "lib", op: "merge", into: "kettles", from: ["cups", "spoons"], by: "operator" }); assert.deepEqual(JSON.parse(merged.lines[0]).seen, { slug: "lib", into: "kettles", seenAt: "2026-11-17T08:00:00.000Z", changed: true }); const dry = await cli(["publish", "lib", "kettles", "--dry-run"], e, root); assert.equal(dry.code, 0); assert.match(dry.lines[0], /would make the channel "kettles", on no site\.\nchannel: kettles \(new\) · 2 posts · 3 files · 204 bytes\nconfirm: FAKE-TEST-VALUE01234567 \(umtool library publish lib kettles --confirm FAKE-TEST-VALUE01234567 --by operator\)/); assert.deepEqual(e.posts()[1].body, { slug: "lib", topic: "kettles", dryRun: true, by: "agent" }); const asAgent = await cli(["publish", "lib", "kettles", "--confirm", CONFIRM], e, root); assert.equal(asAgent.code, 1); assert.match(asAgent.errors[0], /publishing a topic is the operator's: say --by operator/); const none = await cli(["publish", "lib", "kettles"], e, root); assert.match(none.errors[0], /--dry-run says what would be written/); assert.equal(e.posts().length, 2); const done = await cli(["publish", "lib", "kettles", "--confirm", CONFIRM, "--by", "operator", "--channel", "kettles"], e, root); assert.equal(done.code, 0); assert.deepEqual(e.posts()[2].body, { slug: "lib", topic: "kettles", channel: "kettles", confirm: CONFIRM, by: "operator" }); assert.match(done.lines[0], /made the channel "kettles", on no site\./); }); }); test("umtool library accounts and archive: the accounts by posts; archive asks X nothing unless --fetch", async () => { await withRoot(async (root) => { const e = editor(); const view = await cli(["accounts", "lib", "--topic", "cups", "--limit", "20"], e, root); assert.equal(view.code, 0); assert.equal(e.asked[0].route, "/api/ops/trending?slug=lib&accounts=1&topic=cups&limit=20"); assert.equal(view.lines[0], "2 accounts over 4 posts\n@teapot_news 3 posts · 2 kept · topics: kettles, cups · archived as teapot_news-x\n@saucer_fan 1 post · 0 kept · topics: cups"); const dry = await cli(["archive", "lib", "@saucer_fan", "--dry-run"], e, root); assert.equal(dry.lines[0], 'Would make the channel "saucer_fan-x" for @saucer_fan, on no site, with sync off. Nothing is asked of X. A dry run creates nothing.'); const made = await cli(["archive", "lib", "saucer_fan", "--name", "Saucer Fan"], e, root); assert.equal(made.lines[0], SYNC_OFF("saucer_fan-x", "saucer_fan")); const fetched = await cli(["archive", "lib", "saucer_fan", "--fetch"], e, root); assert.equal(fetched.lines[0], 'Made the channel "saucer_fan-x" for @saucer_fan, on no site. Its first fetch was started (job-1).'); assert.deepEqual(e.posts().map((p) => p.body), [ { slug: "lib", handle: "saucer_fan", action: "archive", dryRun: true }, { slug: "lib", handle: "saucer_fan", action: "archive", name: "Saucer Fan" }, { slug: "lib", handle: "saucer_fan", action: "archive", fetch: true }, ]); assert.equal((await cli(["archive", "lib"], e, root)).code, 1); }); });