// Refresh, as umtool sees it (R4): the daily check over the refresh fixture // (an article whose sweep never saw one recording of Research's local corpus), // the two Inbox kinds it adds (refresh-available, refresh-held) and when they // go, and the plan and start asked of a stand-in editor. // // Run with: pnpm --filter umtool exec tsx --test lib/inbox/refresh.test.ts import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { createServer, type Server } from "node:http"; import type { AddressInfo } from "node:net"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; let tmp: string; let editor: Server; const asked: Record[] = []; let mod: { refresh: typeof import("./refresh"); articles: typeof import("../articles/refresh"); pieces: typeof import("../pieces"); lifecycle: typeof import("../lifecycle"); mutes: typeof import("../mutes"); groups: typeof import("./groups.mjs"); }; let fixture: typeof import("../../e2e/fixtures/refresh-fixture.mjs"); before(async () => { tmp = await mkdtemp(path.join(os.tmpdir(), "umtool-refresh-")); const research = await import("../../e2e/fixtures/research-fixture.mjs"); const { pub } = research.makeResearchFixture({ dest: tmp, channels: path.join(tmp, "channels") }); fixture = await import("../../e2e/fixtures/refresh-fixture.mjs"); fixture.makeRefreshFixture({ articles: path.join(tmp, "articles"), source: `local:${pub}` }); // The stand-in editor: report-draft's dry run answers a plan; a start, a job. editor = createServer((req, res) => { let raw = ""; req.on("data", (c) => (raw += c)); req.on("end", () => { const body = JSON.parse(raw || "{}") as Record; asked.push(body); res.setHeader("content-type", "application/json"); if (body.dryRun) return res.end(JSON.stringify({ ok: true, plan: { article: body.article, parent: { n: 1 }, refusals: [], warnings: [] } })); res.end(JSON.stringify({ ok: true, jobId: "job-1", iteration: 2 })); }); }); await new Promise((r) => editor.listen(0, "127.0.0.1", r)); process.env.ARCHILYZER_EDITOR_URL = `http://127.0.0.1:${(editor.address() as AddressInfo).port}`; process.env.WORKER_TOKEN = "t"; process.env.TRANSCRIPT_LOCAL_DIR = pub; process.env.CHANNELS_DIR = path.join(tmp, "channels"); process.env.SITES_DIR = path.join(tmp, "sites"); process.env.ARTICLES_DIR = path.join(tmp, "articles"); process.env.REPORTS_DIR = path.join(tmp, "reports"); process.env.SETTINGS_FILE = path.join(tmp, "settings.json"); delete process.env.TRANSCRIPT_HUB_URL; await mkdir(process.env.REPORTS_DIR, { recursive: true }); mod = { refresh: await import("./refresh"), articles: await import("../articles/refresh"), pieces: await import("../pieces"), lifecycle: await import("../lifecycle"), mutes: await import("../mutes"), groups: await import("./groups.mjs"), }; }); after(async () => { editor?.close(); await rm(tmp, { recursive: true, force: true }); }); const ctx = async () => { mod.pieces.clearPiecesCache(); return Promise.all([mod.pieces.readPieceGraph(), mod.lifecycle.readLifecycle(), mod.mutes.readMutes()]); }; test("the daily check: the current report's enumeration again over the corpus it read — the one recording it never saw, kept a day", async () => { const [graph, life, mutes] = await ctx(); const pieces = mod.refresh.checkable(graph, life, mutes); assert.ok(pieces.some((p) => p.report === fixture.REFRESH_ARTICLE)); const first = await mod.refresh.runRefreshChecks(pieces, { only: fixture.REFRESH_ARTICLE }); assert.equal(first.checked.length, 1); const c = first.checked[0]!; assert.equal(c.error, undefined, c.error); assert.deepEqual({ newCount: c.newCount, newIds: c.newIds, titles: c.titles, iteration: c.iteration }, { newCount: 1, newIds: [fixture.REFRESH_NEW.id], titles: [fixture.REFRESH_NEW.title], iteration: 1 }); assert.match(c.source, /^local:.*research-public$/); assert.equal(c.status, "checked"); assert.match(c.corpus ?? "", /^its own build \(local:.*research-public\)$/); // Kept: today's is not checked again. const again = await mod.refresh.runRefreshChecks(pieces, { only: fixture.REFRESH_ARTICLE }); assert.deepEqual(again, { checked: [], skipped: [{ article: fixture.REFRESH_ARTICLE, why: "checked today" }] }); assert.equal((await mod.refresh.readRefreshChecks()).articles[fixture.REFRESH_ARTICLE]?.newCount, 1); }); test("the Inbox's cards: refresh-available in Review (action Refresh, the plan open); gone once a refresh looks; refresh-held is Your call", async () => { let [graph, life, mutes] = await ctx(); let rows = await mod.refresh.refreshDecisions(graph, life, mutes, { background: false }); const avail = rows.find((r) => r.kind === "refresh-available")!; assert.ok(avail, JSON.stringify(rows)); assert.equal(avail.why, `1 new recording mentions this since 2026-10-14: ${fixture.REFRESH_NEW.title}`); assert.equal(avail.href, `/articles/${fixture.REFRESH_ARTICLE}?refresh=1`); const [a] = mod.groups.annotate([avail], { pieceOf: () => `a:articles/${fixture.REFRESH_ARTICLE}`, lifecycleOf: () => "active" }); assert.deepEqual({ group: a.group, label: a.action.label }, { group: "review", label: "Refresh" }); // A refresh after the check, held by the cap: the suggestion is spent, the hold is the call. const dir = path.join(process.env.ARTICLES_DIR!, fixture.REFRESH_ARTICLE, "iterations", "2"); await mkdir(dir, { recursive: true }); const at = new Date(Date.now() + 1000).toISOString(); await writeFile( path.join(dir, "iteration.json"), JSON.stringify({ format: "archilyzer-iteration", version: 1, article: fixture.REFRESH_ARTICLE, n: 2, kind: "refresh", parent: 1, copyOf: null, model: "claude-sonnet-5-5", status: "done", usage: {}, steps: [], createdAt: at, updatedAt: at, endedAt: at, refresh: { status: "held-by-cap", newIds: 1, changed: 0, drifted: 0, deleted: 0, summary: "1 new recording matches — held by the cap (about $0.40 to update): the daily cap" }, }), ); [graph, life, mutes] = await ctx(); rows = await mod.refresh.refreshDecisions(graph, life, mutes, { background: false }); assert.deepEqual(rows.map((r) => r.kind), ["refresh-held"]); assert.equal(rows[0]!.why, "i2 1 new recording matches — held by the cap (about $0.40 to update): the daily cap"); const [h] = mod.groups.annotate([rows[0]!], { pieceOf: () => `a:articles/${fixture.REFRESH_ARTICLE}`, lifecycleOf: () => "active" }); assert.deepEqual({ group: h.group, label: h.action.label }, { group: "your-call", label: "Refresh" }); // Parked: no check is for it. await mod.lifecycle.setLifecycle(`a:articles/${fixture.REFRESH_ARTICLE}`, "parked"); [graph, life, mutes] = await ctx(); assert.equal(mod.refresh.checkable(graph, life, mutes).some((p) => p.report === fixture.REFRESH_ARTICLE), false); await mod.lifecycle.setLifecycle(`a:articles/${fixture.REFRESH_ARTICLE}`, "active"); }); test("the coverage rule: its own build gone, the research corpus holding the scope is a real check (said); a scope no corpus here holds is UNCHECKED with why — never 0 new, never a card", async () => { const articles = process.env.ARTICLES_DIR!; fixture.makeRefreshFixture({ articles, source: `local:${path.join(tmp, "gone-build", "out")}`, article: "refresh-gone" }); fixture.makeRefreshFixture({ articles, source: `local:${path.join(tmp, "gone-build", "out")}`, article: "refresh-wide", channels: ["harbor-radio", "nowhere-chan"] }); const [graph, life, mutes] = await ctx(); const pieces = mod.refresh.checkable(graph, life, mutes); const gone = (await mod.refresh.runRefreshChecks(pieces, { only: "refresh-gone" })).checked[0]!; assert.equal(gone.status, "checked"); assert.match(gone.corpus ?? "", /^the research corpus \(local:.*research-public\)$/); assert.equal(gone.newCount, 1, "the research corpus holds harbor-radio: a real answer"); const wide = (await mod.refresh.runRefreshChecks(pieces, { only: "refresh-wide" })).checked[0]!; assert.equal(wide.status, "unchecked"); assert.equal(wide.reason, "the build holding harbor-radio, nowhere-chan is not on this machine; channels not in the research corpus: nowhere-chan"); assert.deepEqual({ newCount: wide.newCount, newIds: wide.newIds }, { newCount: 0, newIds: [] }); const rows = await mod.refresh.refreshDecisions(graph, life, mutes, { background: false }); assert.ok(rows.some((r) => r.kind === "refresh-available" && r.piece === "a:articles/refresh-gone")); assert.ok(!rows.some((r) => r.piece === "a:articles/refresh-wide"), "an unchecked article gets no card"); }); test("R6.1: a partial check is never a silent zero — nothing new in what it read still raises the card, saying what it did not reach", async () => { const { readFile: rf, writeFile: wf } = await import("node:fs/promises"); const file = mod.refresh.CHECK_FILE(); const kept = JSON.parse(await rf(file, "utf8")); const entry = kept.articles["refresh-gone"]; assert.ok(entry, "the coverage test checked refresh-gone"); kept.articles["refresh-gone"] = { ...entry, newCount: 0, newIds: [], titles: [], partial: true, unreached: ["Harbor Radio (stopped at page 1 of 2)"] }; await wf(file, JSON.stringify(kept)); const [graph, life, mutes] = await ctx(); const rows = await mod.refresh.refreshDecisions(graph, life, mutes, { background: false }); const card = rows.find((r) => r.kind === "refresh-available" && r.piece === "a:articles/refresh-gone"); assert.ok(card, JSON.stringify(rows.map((r) => [r.kind, r.piece]))); assert.match(card!.why, /^nothing new since \d{4}-\d{2}-\d{2} in what the check read · the check was partial — not reached: Harbor Radio \(stopped at page 1 of 2\); a Refresh reads the rest$/); }); test("the plan and the start are the editor's: a dry run, then report-draft refresh", async () => { const plan = await mod.articles.planArticleRefresh(fixture.REFRESH_ARTICLE, { fetch: false }); assert.equal(plan.article, fixture.REFRESH_ARTICLE); assert.deepEqual(asked.at(-1), { task: "refresh", article: fixture.REFRESH_ARTICLE, dryRun: true, fetch: false }); const started = await mod.articles.startArticleRefresh(fixture.REFRESH_ARTICLE, { iteration: 1 }); assert.deepEqual(started, { jobId: "job-1", iteration: 2, href: `/articles/${fixture.REFRESH_ARTICLE}` }); assert.deepEqual(asked.at(-1), { task: "refresh", article: fixture.REFRESH_ARTICLE, iteration: 1 }); // What the refreshes kept, newest first. const rows = await mod.articles.readRefreshes(fixture.REFRESH_ARTICLE); assert.deepEqual(rows.map((r) => [r.n, r.status, r.refresh]), [[2, "done", null]]); });