import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { platformPresence, recordAvailability, recordFetchObservation, resolveEffectiveAvailabilityObserved, } from "./availability-server"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/availability-server.test.ts // // The fetch observation (lane missing-source) and the day a state was first // seen. Every case is a temp video dir. async function dir(): Promise { return mkdtemp(path.join(tmpdir(), "avail-")); } const PUBLIC_BACKFILL = { checkedAt: "2026-01-02T03:00:00.000Z", checkedBy: "backfill", availability: "public", history: [{ availability: "public", observedAt: "2026-01-02T03:00:00.000Z", source: "backfill" }], webpageUrl: "https://www.youtube.com/watch?v=synthvid001", }; test("a fetch observation over a backfilled 'public' moves the top level and keeps the page URL", async () => { const d = await dir(); try { await writeFile(path.join(d, "availability.json"), JSON.stringify(PUBLIC_BACKFILL)); assert.equal(await recordFetchObservation(d, "members_only", "2026-01-03T10:00:00.000Z"), true); const rec = JSON.parse(await readFile(path.join(d, "availability.json"), "utf8")); assert.equal(rec.availability, "members_only"); assert.equal(rec.checkedBy, "fetch"); assert.equal(rec.checkedAt, "2026-01-03T10:00:00.000Z"); assert.equal(rec.webpageUrl, PUBLIC_BACKFILL.webpageUrl); assert.equal(rec.history.length, 2); // The same reading again writes nothing. assert.equal(await recordFetchObservation(d, "members_only", "2026-01-04T10:00:00.000Z"), false); assert.equal(JSON.parse(await readFile(path.join(d, "availability.json"), "utf8")).checkedAt, "2026-01-03T10:00:00.000Z"); // A fetch never says a video is ON its platform. const presence = await platformPresence(d, { maxAgeDays: 30, now: new Date("2026-01-03T12:00:00Z") }); assert.equal(presence.onPlatform, false); } finally { await rm(d, { recursive: true, force: true }); } }); test("a fetch that reads 'public' with no record writes nothing", async () => { const d = await dir(); try { assert.equal(await recordFetchObservation(d, "public", "2026-01-03T10:00:00.000Z"), false); await assert.rejects(readFile(path.join(d, "availability.json"), "utf8")); } finally { await rm(d, { recursive: true, force: true }); } }); test("observed: the first entry of the trailing run; a day only a history entry anchors", async () => { const d = await dir(); try { assert.equal(await resolveEffectiveAvailabilityObserved(d), null); await writeFile( path.join(d, "availability.json"), JSON.stringify({ checkedAt: "2026-01-09T00:00:00.000Z", checkedBy: "check", availability: "members_only", history: [ { availability: "public", observedAt: "2026-01-01T00:00:00.000Z", source: "backfill" }, { availability: "members_only", observedAt: "2026-01-05T00:00:00.000Z", source: "fetch" }, ], }), ); assert.deepEqual(await resolveEffectiveAvailabilityObserved(d), { availability: "members_only", observedAt: "2026-01-05T00:00:00.000Z", }); // A legacy record (no history): its checkedAt is when it was last checked, // not when it changed, so no day. await writeFile( path.join(d, "availability.json"), JSON.stringify({ checkedAt: "2026-01-09T00:00:00.000Z", availability: "private" }), ); assert.deepEqual(await resolveEffectiveAvailabilityObserved(d), { availability: "private", observedAt: null, }); // A newer download outcome that found it removed wins; with no history // entry agreeing, its finish (which moves with every attempt) is no day. await writeFile( path.join(d, "download-outcome.json"), JSON.stringify({ videoId: "synthvid001", webpageUrl: "https://www.youtube.com/watch?v=synthvid001", status: "failed", startedAt: "2026-01-10T00:00:00.000Z", finishedAt: "2026-01-10T00:01:00.000Z", attempts: [{ n: 0, kind: "download", handling: "youtube", usedCookies: false, ytdlpExitCode: 1, availabilityClass: "deleted" }], }), ); const got = await resolveEffectiveAvailabilityObserved(d); assert.equal(got?.availability, "deleted"); assert.equal(got?.observedAt, null); } finally { await rm(d, { recursive: true, force: true }); } }); test("a legacy record's first write seeds its history, so a same-value recheck never moves the day", async () => { const d = await dir(); try { // Removed long ago, recorded before history existed. await writeFile( path.join(d, "availability.json"), JSON.stringify({ checkedAt: "2023-04-02T00:00:00.000Z", checkedBy: "check", availability: "deleted", history: [] }), ); assert.equal((await resolveEffectiveAvailabilityObserved(d))?.observedAt, null); await recordAvailability(d, { availability: "deleted", observedAt: "2026-11-19T00:00:00.000Z", source: "check" }); const rec = JSON.parse(await readFile(path.join(d, "availability.json"), "utf8")); assert.equal(rec.checkedAt, "2026-11-19T00:00:00.000Z"); assert.deepEqual(rec.history, [{ availability: "deleted", observedAt: "2023-04-02T00:00:00.000Z", source: "check" }]); assert.equal((await resolveEffectiveAvailabilityObserved(d))?.observedAt, "2023-04-02T00:00:00.000Z"); // And again: the day stays where it was. await recordAvailability(d, { availability: "deleted", observedAt: "2026-11-20T00:00:00.000Z", source: "check" }); assert.equal((await resolveEffectiveAvailabilityObserved(d))?.observedAt, "2023-04-02T00:00:00.000Z"); // A legacy record with no checkedBy, then a change: the old reading is kept // ahead of the new one, as the weakest claim ("backfill"). await writeFile( path.join(d, "availability.json"), JSON.stringify({ checkedAt: "2026-01-02T00:00:00.000Z", availability: "public" }), ); await recordAvailability(d, { availability: "members_only", observedAt: "2026-01-05T00:00:00.000Z", source: "fetch" }); const moved = JSON.parse(await readFile(path.join(d, "availability.json"), "utf8")); assert.deepEqual(moved.history, [ { availability: "public", observedAt: "2026-01-02T00:00:00.000Z", source: "backfill" }, { availability: "members_only", observedAt: "2026-01-05T00:00:00.000Z", source: "fetch" }, ]); assert.equal((await resolveEffectiveAvailabilityObserved(d))?.observedAt, "2026-01-05T00:00:00.000Z"); } finally { await rm(d, { recursive: true, force: true }); } });