// The purge of dropped posts' captures (lane posts-library): dropped posts // only, two steps held together by a digest, shared media left, judgements // kept, refused while a capture is in flight. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/xTrending/purge.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import type { Post } from "../posts"; import { judgeXTrending } from "./judgements"; import { adoptXTrendingMedia, CAPTURE_IN_FLIGHT, readXTrendingMediaIndex, removeXTrendingPostCaptures } from "./mediaStore"; import { purgeSentence, purgeXTrendingDropped, xTrendingPurgeConfirm } from "./purge"; import { recordXTrendingRead, writeXTrendingPosts } from "./store"; const TMP = mkdtempSync(path.join(tmpdir(), "x-trending-purge-")); after(() => rmSync(TMP, { recursive: true, force: true })); const IDLE = () => false; const REF = { trendId: "1900000000000000001", url: "https://x.com/i/trending/1900000000000000001" }; const REF_2 = { trendId: "1900000000000000002", url: "https://x.com/i/trending/1900000000000000002" }; function post(id: string, extra: Partial = {}): Post { return { id, slug: `lib/${id}`, channelSlug: "lib", author: "teapot_news", createdAt: "2026-11-16T12:00:00.000Z", uploadDate: "20261116", text: `post ${id}`, url: `https://x.com/teapot_news/status/${id}`, platform: "twitter", isReply: false, isRepost: false, links: [], ...extra }; } const cap = (dir: string, id: string) => path.join(dir, "trending", "posts-media", id); async function put(dir: string, id: string, files: Record) { mkdirSync(cap(dir, id), { recursive: true }); for (const [name, bytes] of Object.entries(files)) { writeFileSync(path.join(cap(dir, id), name), bytes); if (/_\d+\.(jpg|mp4)$/.test(name)) await adoptXTrendingMedia(dir, { postId: id, name }); } } // 701 dropped (own), 702+703 a dropped THREAD, 704 kept, 705 later, 706 // undecided, 707 dropped with no capture, 708 dropped in another topic. async function library(name: string): Promise { const dir = path.join(TMP, name); mkdirSync(dir, { recursive: true }); await writeXTrendingPosts(dir, [post("701"), post("702"), post("703", { isReply: true, threadId: "702", replyTo: { platform: "twitter", id: "702" } }), post("704"), post("705"), post("706"), post("707"), post("708")]); await recordXTrendingRead(dir, { readId: "tr-01JA0000000000000000000000", at: "2026-11-17T10:00:00.000Z", ref: REF, topic: "Kettles", postIds: ["701", "702", "703", "704", "705", "706", "707"], added: 7, threads: { "703": "702" } }); await recordXTrendingRead(dir, { readId: "tr-01JB0000000000000000000000", at: "2026-11-17T11:00:00.000Z", ref: REF_2, topic: "Cups", postIds: ["708"], added: 1 }); await put(dir, "701", { "shot.png": "SHOT701", "701_1.jpg": "ONLY-701", "701_2.jpg": "SHARED-DROPPED", "capture.json": "{}" }); await put(dir, "702", { "702_1.jpg": "SHARED-DROPPED" }); await put(dir, "703", { "703_1.jpg": "SHARED-WITH-KEPT" }); await put(dir, "704", { "704_1.jpg": "SHARED-WITH-KEPT", "shot.png": "SHOT704" }); await put(dir, "705", { "705_1.jpg": "LATER" }); await put(dir, "706", { "706_1.jpg": "UNDECIDED" }); await put(dir, "708", { "708_1.jpg": "OTHER-TOPIC" }); await judgeXTrending(dir, { by: "operator", set: [{ id: "701", state: "drop" }, { thread: "702", state: "drop" }, { id: "704", state: "keep" }, { id: "705", state: "later" }, { id: "707", state: "drop" }, { id: "708", state: "drop" }], }); return dir; } test("a dry run counts what would go and touches nothing; a blob two dropped posts share goes with them, one a kept post carries stays", async () => { const dir = await library("dry"); const before = readFileSync(path.join(dir, "trending", "media-index.json"), "utf8"); const out = await purgeXTrendingDropped(dir, { slug: "lib", topic: "kettles", dryRun: true, captureInFlight: IDLE }); assert.ok(out.ok); const r = out.result; // 707 is dropped and has no capture; 708 is another topic's. assert.deepEqual(r.ids, ["701", "702", "703"]); assert.equal(r.posts, 3); assert.equal(r.files, 6); // ONLY-701 and SHARED-DROPPED are freed; SHARED-WITH-KEPT is 704's too. assert.equal(r.blobsFreed, 2); assert.equal(r.blobsKept, 1); assert.equal(r.bytes, "ONLY-701".length + "SHARED-DROPPED".length + "SHOT701".length + "{}".length); assert.equal(r.confirm, xTrendingPurgeConfirm("lib", ["701", "702", "703"])); assert.equal(r.sentence, "Would remove the captures of 3 dropped posts: 6 files, 31 B. 1 media file another post still uses is left. The judgements stay. Keeping one of them again captures it again."); assert.deepEqual(r.gone, []); assert.equal(r.agentDropped, 0); for (const id of ["701", "702", "703"]) assert.equal(existsSync(cap(dir, id)), true); assert.equal(readFileSync(path.join(dir, "trending", "media-index.json"), "utf8"), before); }); test("the removal does what the dry run said, needs its digest, and leaves the judgements and everyone else's media", async () => { const dir = await library("real"); const dry = await purgeXTrendingDropped(dir, { slug: "lib", topic: "kettles", dryRun: true, captureInFlight: IDLE }); assert.ok(dry.ok); // No digest, a wrong one, another library's: refused, nothing removed. for (const confirm of [undefined, "0000000000000000", xTrendingPurgeConfirm("other", dry.result.ids)]) { const no = await purgeXTrendingDropped(dir, { slug: "lib", topic: "kettles", dryRun: false, ...(confirm ? { confirm } : {}), captureInFlight: IDLE }); assert.equal(no.ok, false); assert.match(no.ok ? "" : no.error, /have changed since the dry run \(3 now\): nothing was removed/); } assert.equal(existsSync(cap(dir, "701")), true); const judgements = readFileSync(path.join(dir, "trending", "judgements.json"), "utf8"); const done = await purgeXTrendingDropped(dir, { slug: "lib", topic: "kettles", dryRun: false, confirm: dry.result.confirm, captureInFlight: IDLE }); assert.ok(done.ok); const { sentence: _a, dryRun: _b, ...said } = dry.result; const { sentence, dryRun: _c, ...did } = done.result; assert.deepEqual(did, said); assert.match(sentence, /^Removed the captures of 3 dropped posts/); for (const id of ["701", "702", "703"]) assert.equal(existsSync(cap(dir, id)), false); // The kept post, the later one, the undecided one and the other topic's are whole. assert.equal(readFileSync(path.join(cap(dir, "704"), "704_1.jpg"), "utf8"), "SHARED-WITH-KEPT"); for (const id of ["705", "706", "708"]) assert.equal(existsSync(cap(dir, id)), true); const index = await readXTrendingMediaIndex(dir); assert.deepEqual(Object.values(index.blobs).map((b) => Object.keys(b.posts).join()).sort(), ["704", "705", "706", "708"]); assert.equal(readFileSync(path.join(dir, "trending", "judgements.json"), "utf8"), judgements); // Again: nothing left to remove. const again = await purgeXTrendingDropped(dir, { slug: "lib", topic: "kettles", dryRun: true, captureInFlight: IDLE }); assert.ok(again.ok); assert.equal(again.result.posts, 0); assert.equal(again.result.sentence, "No post the operator dropped has a capture: nothing to remove."); }); test("a set that changed between the two steps is refused", async () => { const dir = await library("changed"); const dry = await purgeXTrendingDropped(dir, { slug: "lib", dryRun: true, captureInFlight: IDLE }); assert.ok(dry.ok); assert.deepEqual(dry.result.ids, ["701", "702", "703", "708"]); await judgeXTrending(dir, { by: "operator", set: [{ id: "701", state: "keep" }] }); const no = await purgeXTrendingDropped(dir, { slug: "lib", dryRun: false, confirm: dry.result.confirm, captureInFlight: IDLE }); assert.equal(no.ok, false); assert.equal(existsSync(cap(dir, "702")), true); }); test("ids: only dropped ones are taken, the rest named; never a kept, later or undecided post", async () => { const dir = await library("ids"); const dry = await purgeXTrendingDropped(dir, { slug: "lib", ids: ["701", "704", "705", "706", "999"], dryRun: true, captureInFlight: IDLE }); assert.ok(dry.ok); assert.deepEqual(dry.result.ids, ["701"]); assert.deepEqual(dry.result.skipped.map((s) => s.id), ["704", "705", "706", "999"]); const bad = await purgeXTrendingDropped(dir, { slug: "lib", ids: ["../x"], dryRun: true, captureInFlight: IDLE }); assert.equal(bad.ok, false); }); test("while a capture is queued or running: a dry run says so, the removal is refused", async () => { const dir = await library("in-flight"); const dry = await purgeXTrendingDropped(dir, { slug: "lib", dryRun: true, captureInFlight: () => true }); assert.ok(dry.ok); assert.equal(dry.result.captureInFlight, true); assert.match(dry.result.sentence, /the removal waits for it/); const no = await purgeXTrendingDropped(dir, { slug: "lib", dryRun: false, confirm: dry.result.confirm, captureInFlight: () => true }); assert.deepEqual(no, { ok: false, error: CAPTURE_IN_FLIGHT }); assert.equal(existsSync(cap(dir, "701")), true); }); test("removeXTrendingPostCaptures: a dry run of the walk equals the walk", async () => { const dir = await library("walk"); const dry = await removeXTrendingPostCaptures(dir, ["701", "702"], { captureInFlight: IDLE, dryRun: true }); assert.deepEqual(await removeXTrendingPostCaptures(dir, ["701", "702"], { captureInFlight: IDLE }), dry); assert.deepEqual(dry, { posts: 2, files: 5, ownBytes: 9, blobsFreed: 2, bytesFreed: 22, blobsKept: 0 }); }); // ── review 1 ─────────────────────────────────────────────────────────────── test("S1: only the OPERATOR's drops are purged: an agent's drop (a post's own, or through its thread) is counted and left alone", async () => { const dir = path.join(TMP, "agent-drops"); mkdirSync(dir, { recursive: true }); await writeXTrendingPosts(dir, [post("801"), post("802"), post("803"), post("804", { isReply: true, threadId: "803", replyTo: { platform: "twitter", id: "803" } }), post("805")]); await recordXTrendingRead(dir, { readId: "tr-01JA0000000000000000000000", at: "2026-11-17T10:00:00.000Z", ref: REF, topic: "Kettles", postIds: ["801", "802", "803", "804", "805"], added: 5, threads: { "804": "803" } }); for (const id of ["801", "802", "803", "804", "805"]) await put(dir, id, { "shot.png": `SHOT${id}` }); await judgeXTrending(dir, { by: "operator", set: [{ id: "801", state: "drop" }] }); // 802: the agent's own drop. 803+804: a thread the agent dropped. 805: dropped by the agent, then by the operator's hand. await judgeXTrending(dir, { by: "agent", set: [{ id: "802", state: "drop" }, { thread: "803", state: "drop" }, { id: "805", state: "drop" }] }); await judgeXTrending(dir, { by: "operator", set: [{ id: "805", state: "drop" }] }); const dry = await purgeXTrendingDropped(dir, { slug: "lib", dryRun: true, captureInFlight: IDLE }); assert.ok(dry.ok); assert.deepEqual(dry.result.ids, ["801", "805"]); assert.equal(dry.result.agentDropped, 3); assert.match(dry.result.sentence, / 3 posts an agent dropped are left alone: a purge takes the operator's drops only\.$/); // Named by id, an agent's drop is skipped with that reason, never removed. const named = await purgeXTrendingDropped(dir, { slug: "lib", ids: ["801", "802", "804"], dryRun: true, captureInFlight: IDLE }); assert.ok(named.ok); assert.deepEqual(named.result.ids, ["801"]); assert.deepEqual(named.result.skipped, [ { id: "802", reason: "an agent dropped it: a purge takes the operator's drops only" }, { id: "804", reason: "an agent dropped it: a purge takes the operator's drops only" }, ]); assert.equal(named.result.agentDropped, 2); const done = await purgeXTrendingDropped(dir, { slug: "lib", dryRun: false, confirm: dry.result.confirm, captureInFlight: IDLE }); assert.ok(done.ok); for (const id of ["801", "805"]) assert.equal(existsSync(cap(dir, id)), false); for (const id of ["802", "803", "804"]) assert.equal(existsSync(cap(dir, id)), true, `${id}: an agent's drop is left`); assert.match(done.result.sentence, /^Removed the captures of 2 dropped posts: .* 3 posts an agent dropped are left alone/); // Only agents' drops left: nothing to remove, and it says why. const none = await purgeXTrendingDropped(dir, { slug: "lib", dryRun: true, captureInFlight: IDLE }); assert.ok(none.ok); assert.equal(none.result.sentence, "No post the operator dropped has a capture: nothing to remove. 3 posts an agent dropped are left alone: a purge takes the operator's drops only."); }); test("S1: posts gone from the page are named, and the sentence is true of them: the only copy, which keeping again may not bring back", async () => { const dir = path.join(TMP, "gone"); mkdirSync(dir, { recursive: true }); await writeXTrendingPosts(dir, [post("811"), post("812"), post("813")]); await recordXTrendingRead(dir, { readId: "tr-01JA0000000000000000000000", at: "2026-11-17T10:00:00.000Z", ref: REF, topic: "Kettles", postIds: ["811", "812", "813"], added: 3, complete: true }); // The page's end was reached at 11:00 and showed only 812: the others are gone from it. await recordXTrendingRead(dir, { readId: "tr-01JB0000000000000000000000", at: "2026-11-17T11:00:00.000Z", ref: REF, topic: "Kettles", postIds: ["812"], added: 0, complete: true }); for (const id of ["811", "812", "813"]) await put(dir, id, { "shot.png": `SHOT${id}` }); await judgeXTrending(dir, { by: "operator", set: [{ id: "811", state: "drop" }, { id: "812", state: "drop" }] }); const two = await purgeXTrendingDropped(dir, { slug: "lib", dryRun: true, captureInFlight: IDLE }); assert.ok(two.ok); assert.deepEqual(two.result.gone, [{ id: "811", at: "2026-11-17T11:00:00.000Z" }]); assert.match(two.result.sentence, / The judgements stay\. 1 of them is gone from the page: this is the only copy: X no longer showed this post at 2026-11-17 11:00 UTC; removing it cannot be undone\. Keeping the other one again captures it again\.$/); // Only the gone one: nothing is promised about keeping it again. const one = await purgeXTrendingDropped(dir, { slug: "lib", ids: ["811"], dryRun: true, captureInFlight: IDLE }); assert.ok(one.ok); assert.match(one.result.sentence, / It is gone from the page: this is the only copy: X no longer showed this post at 2026-11-17 11:00 UTC; removing it cannot be undone\.$/); assert.doesNotMatch(one.result.sentence, /captures it again/); // Several gone. await judgeXTrending(dir, { by: "operator", set: [{ id: "813", state: "drop" }] }); const three = await purgeXTrendingDropped(dir, { slug: "lib", dryRun: true, captureInFlight: IDLE }); assert.ok(three.ok); assert.match(three.result.sentence, / 2 of them are gone from the page: these are the only copies: X no longer showed these posts \(the first at 2026-11-17 11:00 UTC\); removing them cannot be undone\. Keeping the other one again captures it again\.$/); const done = await purgeXTrendingDropped(dir, { slug: "lib", dryRun: false, confirm: three.result.confirm, captureInFlight: IDLE }); assert.ok(done.ok); assert.match(done.result.sentence, / The judgements stay\. 2 of them were gone from the page: those copies cannot be had again\.$/); assert.equal(purgeSentence({ dryRun: true, posts: 2, files: 2, bytes: 10, blobsKept: 0, gone: [], agentDropped: 1, captureInFlight: true }), "Would remove the captures of 2 dropped posts: 2 files, 10 B. The judgements stay. Keeping one of them again captures it again. 1 post an agent dropped is left alone: a purge takes the operator's drops only. A capture of this library is queued or running: the removal waits for it."); }); test("S3: the set is read again inside the media index's lock: a post kept after the digest was checked is not removed, and nothing else is either", async () => { const dir = await library("toctou"); const dry = await purgeXTrendingDropped(dir, { slug: "lib", topic: "kettles", dryRun: true, captureInFlight: IDLE }); assert.ok(dry.ok); assert.deepEqual(dry.result.ids, ["701", "702", "703"]); const index = readFileSync(path.join(dir, "trending", "media-index.json"), "utf8"); // captureInFlight is asked AFTER the purge checked the digest and before the lock is taken: a keep lands there. const no = await purgeXTrendingDropped(dir, { slug: "lib", topic: "kettles", dryRun: false, confirm: dry.result.confirm, captureInFlight: async () => { await judgeXTrending(dir, { by: "operator", set: [{ id: "701", state: "keep" }] }); return false; }, }); assert.equal(no.ok, false); assert.match(no.ok ? "" : no.error, /have changed since the dry run \(2 now\): nothing was removed/); for (const id of ["701", "702", "703"]) assert.equal(existsSync(cap(dir, id)), true, `${id} is whole`); assert.equal(readFileSync(path.join(dir, "trending", "media-index.json"), "utf8"), index); });