// The account activity's cheap read (lane x-console): each shard's light rows, // cached by mtime and size, deduped by id, a missing permalink derived. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/postTimes-server.test.ts import test, { after } from "node:test"; import assert from "node:assert/strict"; import { appendFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { clearPostTimesCache, readChannelActivity, readPostTimes } from "./postTimes-server"; const ROOT = mkdtempSync(path.join(tmpdir(), "post-times-")); after(() => rmSync(ROOT, { recursive: true, force: true })); const channel = path.join(ROOT, "demo-x"); mkdirSync(path.join(channel, "posts"), { recursive: true }); const line = (id: string, createdAt: string, extra: Record = {}) => JSON.stringify({ id, channelSlug: "demo-x", createdAt, author: "demo", platform: "twitter", text: `first line ${id}\nsecond`, isReply: false, isRepost: false, links: [], url: `https://x.com/demo/status/${id}`, ...extra }) + "\n"; test("light rows from every shard, deduped by id, a missing url derived, a bad line skipped", async () => { clearPostTimesCache(); writeFileSync(path.join(channel, "posts", "2026-10.jsonl"), line("1", "2026-10-01T00:00:00Z") + "{not json\n" + line("2", "2026-10-02T00:00:00Z")); writeFileSync(path.join(channel, "posts", "2026-11.jsonl"), line("2", "2026-10-02T00:00:00Z") + line("3", "2026-11-01T00:00:00Z", { url: undefined })); const rows = await readPostTimes(channel); assert.deepEqual( rows.map((r) => [r.id, r.createdAt, r.author, r.url, r.text]).sort(), [ ["1", "2026-10-01T00:00:00Z", "demo", "https://x.com/demo/status/1", "first line 1"], ["2", "2026-10-02T00:00:00Z", "demo", "https://x.com/demo/status/2", "first line 2"], ["3", "2026-11-01T00:00:00Z", "demo", "https://x.com/demo/status/3", "first line 3"], ], ); }); test("a shard a fetch appended to is re-read; the others come from the cache", async () => { await readPostTimes(channel); const oct = path.join(channel, "posts", "2026-10.jsonl"); const nov = path.join(channel, "posts", "2026-11.jsonl"); const octRows = globalThis.__yttPostTimes__!.get(oct)!.rows; const novRows = globalThis.__yttPostTimes__!.get(nov)!.rows; appendFileSync(nov, line("4", "2026-11-02T00:00:00Z")); const rows = await readPostTimes(channel); assert.deepEqual(rows.map((r) => r.id).sort(), ["1", "2", "3", "4"]); // October was not re-read (the same rows); November was. assert.equal(globalThis.__yttPostTimes__!.get(oct)!.rows, octRows); assert.notEqual(globalThis.__yttPostTimes__!.get(nov)!.rows, novRows); // No shards: nothing. assert.deepEqual(await readPostTimes(path.join(ROOT, "nope")), []); }); test("the activity is memoised by the shards' signature: the same object until a shard changes", async () => { const one = await readChannelActivity(channel, { author: "demo" }); assert.ok(one); assert.equal(await readChannelActivity(channel, { author: "demo" }), one); // Other options are another key. assert.notEqual(await readChannelActivity(channel, { author: "demo", asOf: "2026-12-01T00:00:00Z" }), one); appendFileSync(path.join(channel, "posts", "2026-11.jsonl"), line("5", "2026-11-03T00:00:00Z")); const two = await readChannelActivity(channel, { author: "demo" }); assert.notEqual(two, one); assert.equal(two!.posts, one!.posts + 1); });