// The one gone reading (release 29 E2): deleted or private is gone; members- // only, an error probe, an unlisted video and no availability at all are not. // A post is gone when posts-availability.json says deleted. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/sourceGone.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { goneState, GONE_STATES, SOURCE_GONE_LABELS } from "./sourceGone"; import { postGoneState, recordGoneState, sourceGoneState } from "./sourceGone-server"; test("goneState: deleted and private, nothing else", () => { assert.deepEqual([...GONE_STATES], ["deleted", "private"]); assert.equal(goneState("deleted"), "deleted"); assert.equal(goneState("private"), "private"); for (const s of ["available", "members_only", "unlisted", "maybe_missing", "error", "", null, undefined]) { assert.equal(goneState(s), null, String(s)); } assert.equal(SOURCE_GONE_LABELS.deleted, "Source deleted"); }); test("sourceGoneState reads the video's availability as refresh does; recordGoneState by channel and id; postGoneState", async () => { const channels = await mkdtemp(path.join(os.tmpdir(), "source-gone-")); try { const dir = path.join(channels, "demo", "data", "v1"); await mkdir(dir, { recursive: true }); assert.equal(await sourceGoneState(dir), null, "never checked: not gone"); const write = (availability: string) => writeFile(path.join(dir, "availability.json"), JSON.stringify({ checkedAt: "2026-10-01T00:00:00.000Z", availability })); for (const [a, want] of [ ["deleted", "deleted"], ["private", "private"], ["members_only", null], ["needs_auth", null], ["error", null], ["unlisted", null], ["public", null], ] as const) { await write(a); assert.equal(await sourceGoneState(dir), want, a); } await write("deleted"); assert.equal(await recordGoneState(channels, "demo", "v1"), "deleted"); assert.equal(await recordGoneState(channels, "demo", "nope"), null); assert.equal(await postGoneState(channels, "demo", "p1"), null, "no sidecar"); await writeFile( path.join(channels, "demo", "posts-availability.json"), JSON.stringify({ p1: { availability: "deleted", checkedAt: "2026-10-01T00:00:00.000Z" }, p2: { availability: "account_unavailable", checkedAt: "2026-10-01T00:00:00.000Z" }, }), ); assert.equal(await postGoneState(channels, "demo", "p1"), "deleted"); assert.equal(await postGoneState(channels, "demo", "p2"), null, "an unreachable account is not a deleted post"); } finally { await rm(channels, { recursive: true, force: true }); } });