import { after, test } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, readFileSync, rmSync, existsSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { judgeXTrending, JUDGED_BY_OPERATOR, NOT_IN_THE_LIBRARY, readXTrendingKeptIds, xTrendingJudgeProblem } from "./judgements"; import { listXTrending, readXTrendingJudgementsFile, recordXTrendingRead, writeXTrendingPosts } from "./store"; import type { Post } from "../posts"; const TMP = mkdtempSync(path.join(tmpdir(), "x-trending-judge-")); after(() => rmSync(TMP, { recursive: true, force: true })); const READ = "tr-01JA0000000000000000000000"; const REF = { trendId: "1900000000000000001", url: "https://x.com/i/trending/1900000000000000001" }; function post(id: string, extra: Partial = {}): Post { return { id, slug: `lib/${id}`, channelSlug: "lib", author: "kettle_fan", createdAt: "2026-11-16T12:00:00.000Z", uploadDate: "20261116", text: `post ${id}`, url: `https://x.com/kettle_fan/status/${id}`, platform: "twitter", isReply: false, isRepost: false, links: [], ...extra, }; } // 501 a root; 502 answers 501; 503 answers 502; 504 alone; 505 answers a post // the library does not hold (900). async function library(name: string): Promise { const dir = path.join(TMP, name); const posts = [ post("501"), post("502", { isReply: true, replyTo: { platform: "twitter", id: "501" } }), post("503", { isReply: true, replyTo: { platform: "twitter", id: "502" } }), post("504"), post("505", { isReply: true, replyTo: { platform: "twitter", id: "900" } }), ]; await writeXTrendingPosts(dir, posts); await recordXTrendingRead(dir, { readId: READ, at: "2026-11-17T10:00:00.000Z", ref: REF, topic: "T", postIds: posts.map((p) => p.id), added: posts.length }); return dir; } const clock = (iso: string) => () => Date.parse(iso); test("a judgement is written with who decided and when, and the listing carries it", async () => { const dir = await library("write"); const r = await judgeXTrending( dir, { by: "agent", set: [{ id: "504", state: "keep", note: " the opening " }, { id: "501", state: "later" }] }, { now: clock("2026-11-17T11:00:00.000Z") }, ); assert.deepEqual(r.written, [{ id: "504", state: "keep" }, { id: "501", state: "later" }]); assert.deepEqual(r.skipped, []); assert.deepEqual(r.kept, ["504"]); const file = JSON.parse(readFileSync(path.join(dir, "trending", "judgements.json"), "utf8")); assert.deepEqual(file, { version: 1, posts: { "504": { state: "keep", note: "the opening", by: "agent", at: "2026-11-17T11:00:00.000Z" }, "501": { state: "later", by: "agent", at: "2026-11-17T11:00:00.000Z" }, }, threads: {}, }); const rows = Object.fromEntries((await listXTrending(dir)).posts.map((p) => [p.id, p])); assert.equal(rows["504"].judgement?.state, "keep"); assert.equal(rows["502"].judgement, undefined); }); test("an agent never replaces the operator's judgement: skipped per id, the rest written", async () => { const dir = await library("agent"); await judgeXTrending(dir, { by: "operator", set: [{ id: "501", state: "drop" }] }, { now: clock("2026-11-17T11:00:00.000Z") }); const r = await judgeXTrending( dir, { by: "agent", set: [{ id: "501", state: "keep" }, { id: "504", state: "keep" }] }, { now: clock("2026-11-17T12:00:00.000Z") }, ); assert.deepEqual(r.written, [{ id: "504", state: "keep" }]); assert.deepEqual(r.skipped, [{ id: "501", reason: JUDGED_BY_OPERATOR }]); // Nor may an agent clear it. const cleared = await judgeXTrending(dir, { by: "agent", set: [{ id: "501", state: "undecided" }] }); assert.deepEqual(cleared.skipped, [{ id: "501", reason: JUDGED_BY_OPERATOR }]); assert.equal((await readXTrendingJudgementsFile(dir)).posts["501"].state, "drop"); // The operator overturns the agent. const over = await judgeXTrending(dir, { by: "operator", set: [{ id: "504", state: "drop" }] }, { now: clock("2026-11-17T13:00:00.000Z") }); assert.deepEqual(over.written, [{ id: "504", state: "drop" }]); assert.equal((await readXTrendingJudgementsFile(dir)).posts["504"].by, "operator"); }); test("drop is reversible, and undecided removes the entry", async () => { const dir = await library("reverse"); await judgeXTrending(dir, { by: "operator", set: [{ id: "504", state: "drop" }] }); const back = await judgeXTrending(dir, { by: "operator", set: [{ id: "504", state: "keep" }] }); assert.deepEqual(back.kept, ["504"]); // Keep again: nothing BECAME keep. assert.deepEqual((await judgeXTrending(dir, { by: "operator", set: [{ id: "504", state: "keep", note: "still" }] })).kept, []); await judgeXTrending(dir, { by: "operator", set: [{ id: "504", state: "undecided" }] }); assert.deepEqual((await readXTrendingJudgementsFile(dir)).posts, {}); }); test("a thread is judged by its root post's id; a post's own judgement comes first", async () => { const dir = await library("thread"); await judgeXTrending(dir, { by: "operator", set: [{ id: "503", state: "drop" }] }); const r = await judgeXTrending(dir, { by: "agent", set: [{ thread: "501", state: "keep", note: "the whole exchange" }] }); assert.deepEqual(r.written, [{ thread: "501", state: "keep" }]); // 501 and 502 became keep through the thread; 503 has its own (drop). assert.deepEqual(r.kept, ["501", "502"]); const rows = Object.fromEntries((await listXTrending(dir)).posts.map((p) => [p.id, p])); assert.equal(rows["501"].thread, "501"); assert.equal(rows["503"].thread, "501"); assert.deepEqual(rows["502"].judgement && { state: rows["502"].judgement.state, from: rows["502"].judgement.from }, { state: "keep", from: "thread" }); assert.equal(rows["503"].judgement?.state, "drop"); assert.equal(rows["503"].judgement?.from, undefined); // A thread whose root the library lacks is keyed by that root all the same. assert.equal(rows["505"].thread, "900"); const t = await judgeXTrending(dir, { by: "agent", set: [{ thread: "900", state: "later" }] }); assert.deepEqual(t.written, [{ thread: "900", state: "later" }]); }); test("an id the library does not hold is skipped by name, and nothing is written for it", async () => { const dir = await library("stray"); const r = await judgeXTrending(dir, { by: "agent", set: [{ id: "999", state: "keep" }, { thread: "998", state: "keep" }] }); assert.deepEqual(r.written, []); assert.deepEqual(r.skipped, [ { id: "999", reason: NOT_IN_THE_LIBRARY }, { thread: "998", reason: NOT_IN_THE_LIBRARY }, ]); assert.equal(existsSync(path.join(dir, "trending", "judgements.json")), false); }); test("the ask's shape is refused with a sentence", () => { const ok = { by: "agent", set: [{ id: "1", state: "keep" }] }; assert.equal(xTrendingJudgeProblem(ok), null); assert.match(xTrendingJudgeProblem({ ...ok, by: "someone" })!, /"by" is who decided/); assert.match(xTrendingJudgeProblem({ by: "agent", set: [] })!, /"set" is a list/); assert.match(xTrendingJudgeProblem({ by: "agent", set: [{ id: "1", thread: "1", state: "keep" }] })!, /one of them/); assert.match(xTrendingJudgeProblem({ by: "agent", set: [{ id: "../x", state: "keep" }] })!, /not a post id/); assert.match(xTrendingJudgeProblem({ by: "agent", set: [{ id: "1", state: "maybe" }] })!, /keep, drop, later or undecided/); assert.match(xTrendingJudgeProblem({ by: "agent", set: [{ id: "1", state: "undecided", note: "x" }] })!, /carries no note/); assert.match(xTrendingJudgeProblem({ by: "agent", set: [{ id: "1", state: "keep", topic: "t" }] })!, /unknown key/); assert.match(xTrendingJudgeProblem({ by: "agent", set: [{ id: "1", state: "keep" }, { id: "1", state: "drop" }] })!, /judged twice/); assert.match(xTrendingJudgeProblem({ by: "agent", set: [{ id: "1", state: "keep", note: "n".repeat(2001) }] })!, /at most 2000/); }); // Review 1 M2: no way round the operator through the post-over-thread precedence. test("M2: an agent cannot override the operator's thread judgement with a post-level one, nor the operator's post with a thread-level one", async () => { const dir = await library("precedence"); // The operator drops the thread; an agent then tries to keep one post of it. await judgeXTrending(dir, { by: "operator", set: [{ thread: "501", state: "drop" }] }); const sneak = await judgeXTrending(dir, { by: "agent", set: [{ id: "502", state: "keep" }, { id: "504", state: "keep" }] }); assert.deepEqual(sneak.skipped, [{ id: "502", reason: JUDGED_BY_OPERATOR }]); assert.deepEqual(sneak.written, [{ id: "504", state: "keep" }]); // Nothing of the dropped thread became keep: nothing of it would be archived. assert.deepEqual(sneak.kept, ["504"]); assert.equal((await readXTrendingJudgementsFile(dir)).posts["502"], undefined); let rows = Object.fromEntries((await listXTrending(dir)).posts.map((p) => [p.id, p])); assert.deepEqual([rows["502"].judgement?.state, rows["502"].judgement?.by], ["drop", "operator"]); // Nor may the agent clear it at post level. assert.deepEqual((await judgeXTrending(dir, { by: "agent", set: [{ id: "503", state: "undecided" }] })).skipped, [{ id: "503", reason: JUDGED_BY_OPERATOR }]); // The operator may, of course. assert.deepEqual((await judgeXTrending(dir, { by: "operator", set: [{ id: "502", state: "keep" }] })).kept, ["502"]); // The other direction: the operator drops ONE post; an agent keeps its thread. const other = await library("precedence-2"); await judgeXTrending(other, { by: "operator", set: [{ id: "502", state: "drop" }] }); const thread = await judgeXTrending(other, { by: "agent", set: [{ thread: "501", state: "keep" }] }); assert.deepEqual(thread.written, [{ thread: "501", state: "keep" }]); // 502 keeps the operator's drop and is not among the kept. assert.deepEqual(thread.kept, ["501", "503"]); rows = Object.fromEntries((await listXTrending(other)).posts.map((p) => [p.id, p])); assert.deepEqual([rows["502"].judgement?.state, rows["502"].judgement?.by], ["drop", "operator"]); // And an agent's thread-level write over the operator's thread entry is skipped. await judgeXTrending(other, { by: "operator", set: [{ thread: "501", state: "later" }] }); assert.deepEqual((await judgeXTrending(other, { by: "agent", set: [{ thread: "501", state: "keep" }] })).skipped, [{ thread: "501", reason: JUDGED_BY_OPERATOR }]); }); // Review 2 N2: the operator outranks an agent about the same post, whatever the order. test("N2: an agent's own post entry never outranks the operator's thread entry — not by a later write, not by an older one", async () => { // The three steps: agent {P, later}; operator {thread, drop}; agent {P, keep}. const dir = await library("n2-three-steps"); await judgeXTrending(dir, { by: "agent", set: [{ id: "502", state: "later" }] }); await judgeXTrending(dir, { by: "operator", set: [{ thread: "501", state: "drop" }] }); const third = await judgeXTrending(dir, { by: "agent", set: [{ id: "502", state: "keep" }] }); assert.deepEqual(third.written, []); assert.deepEqual(third.skipped, [{ id: "502", reason: JUDGED_BY_OPERATOR }]); assert.deepEqual(third.kept, []); // The agent's older entry is still in the file, and decides nothing. assert.equal((await readXTrendingJudgementsFile(dir)).posts["502"].state, "later"); let row = (await listXTrending(dir)).posts.find((p) => p.id === "502"); assert.deepEqual([row?.judgement?.state, row?.judgement?.by, row?.judgement?.from], ["drop", "operator", "thread"]); assert.deepEqual([...(await readXTrendingKeptIds(dir))], []); // An OLDER agent keep does not survive the operator's later thread drop. const older = await library("n2-older-keep"); assert.deepEqual((await judgeXTrending(older, { by: "agent", set: [{ id: "502", state: "keep" }, { id: "504", state: "keep" }] })).kept, ["502", "504"]); assert.deepEqual([...(await readXTrendingKeptIds(older))].sort(), ["502", "504"]); await judgeXTrending(older, { by: "operator", set: [{ thread: "501", state: "drop" }] }); assert.deepEqual([...(await readXTrendingKeptIds(older))], ["504"]); row = (await listXTrending(older)).posts.find((p) => p.id === "502"); assert.deepEqual([row?.judgement?.state, row?.judgement?.by], ["drop", "operator"]); // The operator lifting the thread judgement lets the agent's suggestion show again. await judgeXTrending(older, { by: "operator", set: [{ thread: "501", state: "undecided" }] }); assert.deepEqual([...(await readXTrendingKeptIds(older))].sort(), ["502", "504"]); // Two entries of the operator's: the post's own still outranks the thread's, later or not. const both = await library("n2-operator-both"); await judgeXTrending(both, { by: "operator", set: [{ id: "502", state: "keep" }] }); const later = await judgeXTrending(both, { by: "operator", set: [{ thread: "501", state: "drop" }] }); assert.deepEqual(later.kept, []); assert.deepEqual([...(await readXTrendingKeptIds(both))], ["502"]); row = (await listXTrending(both)).posts.find((p) => p.id === "502"); assert.deepEqual([row?.judgement?.state, row?.judgement?.from], ["keep", undefined]); // Two of an agent's: the post's own outranks the thread's. const agents = await library("n2-agent-both"); await judgeXTrending(agents, { by: "agent", set: [{ thread: "501", state: "drop" }, { id: "503", state: "keep" }] }); assert.deepEqual([...(await readXTrendingKeptIds(agents))], ["503"]); // And an agent's thread entry never outranks the operator's entry for a post. await judgeXTrending(agents, { by: "operator", set: [{ id: "502", state: "keep" }] }); await judgeXTrending(agents, { by: "agent", set: [{ thread: "501", state: "later" }] }); assert.deepEqual([...(await readXTrendingKeptIds(agents))].sort(), ["502", "503"]); });