// updateArticleLink: the writer behind `umtool link`, the only way to settle a // link-ambiguous article (two projects share its slug) without a hand edit. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { updateArticleLink } from "./manifest.mjs"; test("names the article, is a no-op when it already does, clears, and refuses a bad id", async () => { const dir = await mkdtemp(path.join(tmpdir(), "article-link-")); try { await mkdir(dir, { recursive: true }); const file = path.join(dir, "video.manifest.json"); const manifest = { schemaVersion: 1, slug: "polemic-flips", title: "T", timeline: [{ type: "clip", id: "a", video: "v", start: 1, end: 2 }] }; await writeFile(file, JSON.stringify(manifest, null, 2) + "\n"); const r1 = await updateArticleLink(dir, "articles/polemic-flips"); assert.equal(r1.changed, true); assert.equal(r1.was, null); const after = JSON.parse(await readFile(file, "utf8")); assert.equal(after.article, "articles/polemic-flips"); assert.deepEqual(after.timeline, manifest.timeline, "nothing else moves"); // The CLI's formatting (indent 2, trailing newline), so the diff is one line. assert.match(await readFile(file, "utf8"), /\n {2}"article": "articles\/polemic-flips"\n\}\n$/); // Snapshotted first. assert.ok((await readdir(path.join(dir, "revisions"))).some((f) => f.includes("auto-before-link"))); const r2 = await updateArticleLink(dir, "articles/polemic-flips"); assert.equal(r2.changed, false); await assert.rejects(updateArticleLink(dir, "polemic-flips"), /\//); await assert.rejects(updateArticleLink(dir, "articles/Polemic Flips"), /\//); await assert.rejects(updateArticleLink(dir, "../x/y"), /\//); const r3 = await updateArticleLink(dir, null); assert.equal(r3.changed, true); assert.equal(r3.was, "articles/polemic-flips"); assert.equal("article" in JSON.parse(await readFile(file, "utf8")), false); } finally { await rm(dir, { recursive: true, force: true }); } });