// Account activity (lane x-console): posts per day over the archive's range, // and long silences — ≥ 3× the median gap AND ≥ 48 h — each with the post // before and after. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/accountActivity.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { accountActivity, median, medianText, spanHours } from "./accountActivity"; const post = (id: string, createdAt: string, author = "acct", text = `post ${id}`) => ({ id, createdAt, url: `https://x.com/${author}/status/${id}`, text, author, }); // A busy account (a post every 15 min on 3–4 Nov), three days of nothing, // then busy again on 8 Nov — the shape of Mr Obvious's 5–8 Nov 2026 gap. function busyWithGap() { const out = []; let n = 0; for (const day of ["2026-11-03", "2026-11-04"]) { for (let m = 0; m < 8 * 60; m += 15) { const h = String(10 + Math.floor(m / 60)).padStart(2, "0"); const mm = String(m % 60).padStart(2, "0"); out.push(post(String(++n), `${day}T${h}:${mm}:00.000Z`)); } } for (let m = 0; m < 4 * 60; m += 15) { const h = String(19 + Math.floor(m / 60)).padStart(2, "0"); out.push(post(String(++n), `2026-11-08T${h}:${String(m % 60).padStart(2, "0")}:00.000Z`)); } return out; } test("a busy account's three-day gap is one silence, with the posts on either side; nights are not", () => { const a = accountActivity(busyWithGap())!; assert.equal(a.medianGapMinutes, 15); // 3 × 15 min is far below 48 h: the 48 h floor decides. assert.equal(a.thresholdHours, 48); assert.equal(a.silences.length, 1); const s = a.silences[0]; assert.equal(s.before.createdAt, "2026-11-04T17:45:00.000Z"); assert.equal(s.after.createdAt, "2026-11-08T19:00:00.000Z"); assert.equal(s.before.url, `https://x.com/acct/status/${s.before.id}`); assert.equal(spanHours(s.hours), "4 d 1 h"); // The overnight gap 3 → 4 Nov (≈ 16 h) is not a silence. // Days: 3 Nov .. 8 Nov, empty days included. assert.deepEqual( a.days.map((d) => [d.day, d.count]), [["2026-11-03", 32], ["2026-11-04", 32], ["2026-11-05", 0], ["2026-11-06", 0], ["2026-11-07", 0], ["2026-11-08", 16]], ); assert.equal(a.maxPerDay, 32); }); test("a sparse account: 3× its median gap decides, so its usual week is not a silence", () => { // Every 4 days, then a 20-day gap, then every 4 days again. const days = ["2026-01-01", "2026-01-05", "2026-01-09", "2026-01-13", "2026-02-02", "2026-02-06", "2026-02-10"]; const a = accountActivity(days.map((d, i) => post(String(i), `${d}T12:00:00.000Z`)))!; assert.equal(a.medianGapMinutes, 4 * 24 * 60); assert.equal(a.thresholdHours, 12 * 24); assert.equal(a.silences.length, 1); assert.equal(a.silences[0].before.createdAt, "2026-01-13T12:00:00.000Z"); assert.equal(a.silences[0].ratio, 5); }); test("silences are newest first; a trailing silence runs to the last fetch", () => { const posts = [ post("1", "2026-10-01T00:00:00Z"), post("2", "2026-10-01T00:10:00Z"), post("3", "2026-10-05T00:00:00Z"), post("4", "2026-10-05T00:10:00Z"), post("5", "2026-10-05T00:20:00Z"), post("6", "2026-10-10T00:00:00Z"), post("7", "2026-10-10T00:10:00Z"), ]; const a = accountActivity(posts, { asOf: "2026-10-14T00:10:00.000Z" })!; assert.deepEqual(a.silences.map((s) => [s.before.id, s.after.id]), [["5", "6"], ["2", "3"]]); assert.equal(a.trailing?.since.id, "7"); assert.equal(spanHours(a.trailing!.hours), "4 d"); assert.equal(accountActivity(posts, { asOf: "2026-10-11T00:00:00.000Z" })!.trailing, null); }); test("only the account's own posts count when any carry its handle; none, and every post counts", () => { const posts = [ post("1", "2026-10-01T00:00:00Z", "Acct"), post("2", "2026-10-02T00:00:00Z", "someone_else"), post("3", "2026-10-03T00:00:00Z", "acct"), ]; const own = accountActivity(posts, { author: "@acct" })!; assert.equal(own.posts, 2); assert.equal(own.author, "acct"); const all = accountActivity(posts, { author: "nobody" })!; assert.equal(all.posts, 3); assert.equal(all.author, null); }); test("fewer than two datable posts: nothing to measure", () => { assert.equal(accountActivity([]), null); assert.equal(accountActivity([post("1", "2026-10-01T00:00:00Z"), post("2", "not a date")]), null); }); test("the words", () => { assert.equal(median([3, 1, 2]), 2); assert.equal(median([4, 1, 2, 3]), 2.5); assert.equal(spanHours(0.5), "30 min"); assert.equal(spanHours(47.6), "48 h"); assert.equal(spanHours(48), "2 d"); assert.equal(spanHours(4 * 24 + 8.2), "4 d 8 h"); assert.equal(spanHours(2 * 24 + 23.8), "3 d"); assert.equal(medianText(13), "13 min"); assert.equal(medianText(150), "2.5 h"); }); test("a bogus date is left out: before 2006 or more than a day ahead never stretches the strip", () => { const now = Date.parse("2026-11-09T12:00:00Z"); const a = accountActivity( [ post("0", "1970-01-01T00:00:00Z"), post("1", "2026-11-01T00:00:00Z"), post("2", "2026-11-02T00:00:00Z"), post("3", "9999-12-31T00:00:00Z"), ], { now }, )!; assert.equal(a.posts, 2); assert.equal(a.days.length, 2); assert.equal(accountActivity([post("0", "1970-01-01T00:00:00Z"), post("3", "9999-12-31T00:00:00Z")], { now }), null); });