// Mark seen (lane posts-library): umtool's own state under a reports root's // .umtool/, moved only by the press. A temp root, never the operator's. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { utimes } from "node:fs/promises"; import { markSeen, readSeen, SEEN_LOCK_STALE_MS, seenFile, seenOf } from "./seen.mjs"; test("nothing is marked until the press; a press marks the topics named and remembers the mark it replaced", async () => { const root = await mkdtemp(path.join(tmpdir(), "umtool-library-seen-")); try { assert.deepEqual(await readSeen(root), { libraries: {} }); assert.deepEqual(await seenOf(root, "lib"), {}); // Reading wrote nothing. assert.deepEqual(await readdir(root), []); const t1 = Date.parse("2026-11-18T10:00:00.000Z"); assert.deepEqual(await markSeen(root, { slug: "lib", topics: ["kettles"], now: t1 }), { slug: "lib", topics: { kettles: { seenAt: "2026-11-18T10:00:00.000Z" } } }); assert.equal(seenFile(root), path.join(root, ".umtool", "posts-library.json")); assert.deepEqual(await seenOf(root, "lib"), { kettles: "2026-11-18T10:00:00.000Z" }); const t2 = Date.parse("2026-11-18T12:00:00.000Z"); await Promise.all([markSeen(root, { slug: "lib", topics: ["kettles", "cups"], now: t2 }), markSeen(root, { slug: "other", topics: ["cups"], now: t2 })]); const doc = JSON.parse(await readFile(seenFile(root), "utf8")); assert.equal(doc.format, "umtool-posts-library"); assert.deepEqual(doc.libraries.lib.topics, { kettles: { seenAt: "2026-11-18T12:00:00.000Z", previous: "2026-11-18T10:00:00.000Z" }, cups: { seenAt: "2026-11-18T12:00:00.000Z" } }); assert.deepEqual(await seenOf(root, "other"), { cups: "2026-11-18T12:00:00.000Z" }); assert.deepEqual((await readdir(path.join(root, ".umtool"))).sort(), ["posts-library.json"]); } finally { await rm(root, { recursive: true, force: true }); } }); test("refused: an ill-shaped slug or topic, no topics; a file that does not parse is never written over", async () => { const root = await mkdtemp(path.join(tmpdir(), "umtool-library-seen-")); try { await assert.rejects(markSeen(root, { slug: "../x", topics: ["kettles"] }), /not a library's slug/); await assert.rejects(markSeen(root, { slug: "lib", topics: [] }), /topic ids/); await assert.rejects(markSeen(root, { slug: "lib", topics: ["../x"] }), /topic ids/); await mkdir(path.join(root, ".umtool"), { recursive: true }); await writeFile(seenFile(root), "{ not json"); assert.match((await readSeen(root)).error, /does not parse/); await assert.rejects(markSeen(root, { slug: "lib", topics: ["kettles"] }), /does not parse/); assert.equal(await readFile(seenFile(root), "utf8"), "{ not json"); } finally { await rm(root, { recursive: true, force: true }); } }); test("nit: the write is under a lock file: it waits for another process's, takes over one left by a process that died, and leaves none behind", async () => { const root = await mkdtemp(path.join(tmpdir(), "umtool-library-seen-")); try { await mkdir(path.join(root, ".umtool"), { recursive: true }); const lock = `${seenFile(root)}.lock`; // Another process holds it, and writes its own mark before letting go. await writeFile(lock, "999999\n"); const mine = markSeen(root, { slug: "lib", topics: ["kettles"], now: Date.parse("2026-11-18T12:00:00.000Z") }); await new Promise((r) => setTimeout(r, 120)); assert.deepEqual(await readSeen(root), { libraries: {} }, "nothing written while the lock is held"); await writeFile(seenFile(root), JSON.stringify({ format: "umtool-posts-library", version: 1, libraries: { lib: { topics: { cups: { seenAt: "2026-11-18T11:00:00.000Z" } } } } })); await rm(lock); await mine; // Both marks are there: the read was made under the lock, after the other write. assert.deepEqual(await seenOf(root, "lib"), { cups: "2026-11-18T11:00:00.000Z", kettles: "2026-11-18T12:00:00.000Z" }); assert.deepEqual((await readdir(path.join(root, ".umtool"))).sort(), ["posts-library.json"]); // A lock older than the limit is a dead process's. await writeFile(lock, "999999\n"); const old = new Date(Date.now() - SEEN_LOCK_STALE_MS - 5_000); await utimes(lock, old, old); await markSeen(root, { slug: "lib", topics: ["lids"] }); assert.ok((await seenOf(root, "lib")).lids); assert.deepEqual((await readdir(path.join(root, ".umtool"))).sort(), ["posts-library.json"]); } finally { await rm(root, { recursive: true, force: true }); } });