// The source status (lane source-status): what became of a recording's source and // what it was at first -- "was public" only from the evidence the records hold, never // from a backfill default, a live recording or a cookieless download alone. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/sourceStatus.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { deriveSourceStatus, metadataSnapshots } from "./sourceStatus.mjs"; import { readSourceStatus } from "./sourceStatus-server.mjs"; const sec = (iso: string) => Date.parse(iso) / 1000; // A synthetic stream: live 10:00 -> 14:00 on 1 Mar 2031. const LIVE_META = { availability: "public", live_status: "is_live", epoch: sec("2031-03-01T10:05:00Z"), release_timestamp: sec("2031-03-01T10:00:00Z"), }; /** A download-outcome.json of coerceDownloadOutcome's shape. */ const outcomeOf = (o: Record) => ({ videoId: "synth-vid-001", startedAt: "2031-03-01T00:00:00Z", ...o }); const RECORDING = { status: "landed", startedAt: "2031-03-01T10:01:00Z", endedAt: "2031-03-01T14:00:00Z" }; test("public when streamed, then members-only: the metadata saved during the live is the evidence", () => { const s = deriveSourceStatus({ availability: { checkedAt: "2031-03-01T20:00:00Z", checkedBy: "fetch", availability: "members_only", history: [ { availability: "public", observedAt: "2031-03-01T10:30:00Z", source: "backfill" }, { availability: "members_only", observedAt: "2031-03-01T20:00:00Z", source: "fetch" }, ], }, metadata: LIVE_META, liveRecording: RECORDING, }); assert.equal(s?.now, "members_only"); assert.equal(s?.since, "2031-03-01T20:00:00Z"); assert.equal(s?.asOf, "2031-03-01T20:00:00Z"); assert.equal(s?.first, "public"); assert.equal(s?.firstWhen, "live"); assert.equal(s?.firstAt, "2031-03-01T10:05:00.000Z"); assert.ok(s?.evidence.some((e) => e.kind === "metadata" && e.counts && e.live)); assert.ok(s?.evidence.some((e) => e.kind === "backfill" && !e.counts)); }); test("one observation only (private): the metadata saved while it was live says public", () => { const s = deriveSourceStatus({ availability: { checkedAt: "2031-03-01T14:00:00Z", checkedBy: "fetch", availability: "private", history: [{ availability: "private", observedAt: "2031-03-01T14:00:00Z", source: "fetch" }], }, metadata: LIVE_META, liveRecording: RECORDING, }); assert.deepEqual([s?.now, s?.first, s?.firstWhen], ["private", "public", "live"]); }); test("no availability record is no state", () => { assert.equal(deriveSourceStatus({ metadata: LIVE_META, liveRecording: RECORDING }), null); assert.equal(deriveSourceStatus({}), null); }); test("NOT EVIDENCE: a backfill's public alone (the metadata has no availability field)", () => { const s = deriveSourceStatus({ availability: { checkedAt: "2031-03-02T00:00:00Z", availability: "private", history: [ { availability: "public", observedAt: "2031-03-01T10:30:00Z", source: "backfill" }, { availability: "private", observedAt: "2031-03-02T00:00:00Z", source: "fetch" }, ], }, metadata: { live_status: "is_live", epoch: sec("2031-03-01T10:05:00Z") }, }); assert.equal(s?.now, "private"); assert.equal(s?.first, null); assert.equal(s?.firstWhen, null); }); test("NOT EVIDENCE: a live recording alone (it may have been made with a member's cookies)", () => { const s = deriveSourceStatus({ availability: { checkedAt: "2031-03-02T00:00:00Z", availability: "members_only", history: [{ availability: "members_only", observedAt: "2031-03-02T00:00:00Z", source: "fetch" }] }, liveRecording: RECORDING, }); assert.equal(s?.first, null); assert.ok(s?.evidence.some((e) => e.kind === "live-recording" && e.counts === false)); }); test("NOT EVIDENCE: a download without cookies alone (public or unlisted)", () => { const s = deriveSourceStatus({ availability: { checkedAt: "2031-03-05T00:00:00Z", availability: "private", history: [{ availability: "private", observedAt: "2031-03-05T00:00:00Z", source: "fetch" }] }, outcome: outcomeOf({ status: "ok", finishedAt: "2031-03-02T00:00:00Z", attempts: [{ n: 0, usedCookies: false, ytdlpExitCode: 0 }] }), }); assert.equal(s?.now, "private"); assert.equal(s?.first, null); assert.ok(s?.evidence.some((e) => e.kind === "download" && e.counts === false)); }); test("the metadata's earlier state is rebuilt from metadata.history.json", () => { const metadata = { availability: "private", live_status: "was_live", epoch: sec("2031-03-03T00:00:00Z"), release_timestamp: sec("2031-03-01T10:00:00Z"), duration: 14400 }; const history = { entries: [ { at: "2031-03-03T00:00:00Z", by: "refresh", from: { at: "2031-03-01T10:05:00Z" }, changed: { availability: { from: "public", to: "private" }, live_status: { from: "is_live", to: "was_live" } }, added: {}, removed: {} }, ], }; const shots = metadataSnapshots(metadata, history); assert.deepEqual(shots.map((x) => [x.at, x.availability, x.liveStatus]), [ ["2031-03-01T10:05:00.000Z", "public", "is_live"], ["2031-03-03T00:00:00.000Z", "private", "was_live"], ]); const s = deriveSourceStatus({ availability: { checkedAt: "2031-03-03T00:00:00Z", availability: "private", history: [{ availability: "private", observedAt: "2031-03-03T00:00:00Z", source: "fetch" }] }, metadata, metadataHistory: history, }); assert.deepEqual([s?.first, s?.firstWhen, s?.firstAt], ["public", "live", "2031-03-01T10:05:00.000Z"]); }); test("a check of public outside the live is 'other'; a removal a check read is 'deleted'", () => { const s = deriveSourceStatus({ availability: { checkedAt: "2031-03-20T00:00:00Z", checkedBy: "check", availability: "deleted", history: [ { availability: "public", observedAt: "2031-03-10T00:00:00Z", source: "check" }, { availability: "deleted", observedAt: "2031-03-20T00:00:00Z", source: "check" }, ], }, metadata: { availability: "public", live_status: "was_live", epoch: sec("2031-03-09T00:00:00Z"), release_timestamp: sec("2031-03-01T10:00:00Z"), duration: 14400 }, }); assert.deepEqual([s?.now, s?.asOf, s?.since, s?.first, s?.firstWhen], ["deleted", "2031-03-20T00:00:00Z", "2031-03-20T00:00:00Z", "public", "other"]); }); test("a check of public inside the stream's span is 'live' (its extractor reports the field)", () => { const s = deriveSourceStatus({ availability: { checkedAt: "2031-03-02T00:00:00Z", availability: "private", history: [ { availability: "public", observedAt: "2031-03-01T12:00:00Z", source: "check" }, { availability: "private", observedAt: "2031-03-02T00:00:00Z", source: "check" }, ], }, // The field is there (it reads private now): the check's public was the platform's word. metadata: { availability: "private", epoch: sec("2031-03-02T00:00:00Z"), release_timestamp: sec("2031-03-01T10:00:00Z"), duration: 14400 }, }); assert.deepEqual([s?.now, s?.first, s?.firstWhen], ["private", "public", "live"]); }); // REVIEW 1, finding 1: "now deleted" only from a check. test("FALSE CLAIM GUARD: a download's 'deleted' newer than a check of public draws nothing", () => { const s = deriveSourceStatus({ availability: { checkedAt: "2031-03-10T00:00:00Z", checkedBy: "check", availability: "public", history: [{ availability: "public", observedAt: "2031-03-10T00:00:00Z", source: "check" }] }, // "Video unavailable. The uploader has not made this video available in your country." classifies deleted. outcome: outcomeOf({ status: "failed", finishedAt: "2031-03-20T00:00:00Z", attempts: [{ n: 0, usedCookies: false, availabilityClass: "deleted" }] }), metadata: { availability: "public", live_status: "was_live", epoch: sec("2031-03-05T00:00:00Z") }, }); assert.equal(s?.now, null); assert.equal(s?.first, null); assert.equal(s?.asOf, "2031-03-20T00:00:00Z"); assert.ok(s?.evidence.some((e) => e.kind === "download" && e.availability === "deleted" && e.counts === false)); }); test("FALSE CLAIM GUARD: a top-level 'deleted' not written by a check draws nothing; a legacy one a check's history backs is deleted", () => { const fetched = deriveSourceStatus({ availability: { checkedAt: "2031-03-10T00:00:00Z", checkedBy: "fetch", availability: "deleted", history: [{ availability: "deleted", observedAt: "2031-03-10T00:00:00Z", source: "fetch" }] } }); assert.equal(fetched?.now, null); const legacy = deriveSourceStatus({ availability: { checkedAt: "2031-03-10T00:00:00Z", availability: "deleted", history: [{ availability: "deleted", observedAt: "2031-03-10T00:00:00Z", source: "check" }] } }); assert.equal(legacy?.now, "deleted"); const bare = deriveSourceStatus({ availability: { checkedAt: "2031-03-10T00:00:00Z", availability: "deleted" } }); assert.equal(bare?.now, null); }); // REVIEW 1, finding 2: a newer first-hand public reading. test("FALSE CLAIM GUARD: privated, then public again and persisted (metadata rewritten, no availability entry): nothing", () => { const s = deriveSourceStatus({ availability: { checkedAt: "2031-03-02T00:00:00Z", checkedBy: "fetch", availability: "private", history: [{ availability: "private", observedAt: "2031-03-02T00:00:00Z", source: "fetch" }], }, metadata: { availability: "public", live_status: "was_live", epoch: sec("2031-03-04T00:00:00Z"), release_timestamp: sec("2031-03-01T10:00:00Z") }, metadataHistory: { entries: [{ at: "2031-03-04T00:00:00Z", from: { at: "2031-03-01T10:05:00Z" }, changed: { live_status: { from: "is_live", to: "was_live" } } }] }, }); assert.equal(s?.now, null); assert.equal(s?.asOf, "2031-03-04T00:00:00.000Z", "as of the newer reading"); assert.ok(s?.evidence.some((e) => e.kind === "disagree" && e.from === "metadata" && e.counts === false)); // A legacy record (no history) says the same. const legacy = deriveSourceStatus({ availability: { checkedAt: "2031-03-02T00:00:00Z", availability: "members_only" }, metadata: { availability: "public", epoch: sec("2031-03-04T00:00:00Z") }, }); assert.equal(legacy?.now, null); // A download that succeeded without cookies after the private reading: anyone could watch it. const played = deriveSourceStatus({ availability: { checkedAt: "2031-03-02T00:00:00Z", availability: "private", history: [{ availability: "private", observedAt: "2031-03-02T00:00:00Z", source: "check" }] }, outcome: outcomeOf({ status: "ok", finishedAt: "2031-03-03T00:00:00Z", attempts: [{ n: 0, usedCookies: false, ytdlpExitCode: 0 }] }), }); assert.equal(played?.now, null); // An open reading OLDER than the state is the first state, not a disagreement. const older = deriveSourceStatus({ availability: { checkedAt: "2031-03-02T00:00:00Z", availability: "private", history: [{ availability: "private", observedAt: "2031-03-02T00:00:00Z", source: "fetch" }] }, metadata: { availability: "public", epoch: sec("2031-03-01T00:00:00Z") }, }); assert.deepEqual([older?.now, older?.first, older?.firstWhen], ["private", "public", "other"]); }); test("an outcome's error or login wall newer than an explicit private: that is now, and the line draws nothing", () => { for (const cls of ["error", "needs_auth"]) { const s = deriveSourceStatus({ availability: { checkedAt: "2031-03-02T00:00:00Z", checkedBy: "check", availability: "private", history: [{ availability: "private", observedAt: "2031-03-02T00:00:00Z", source: "check" }] }, outcome: outcomeOf({ status: "failed", finishedAt: "2031-03-03T00:00:00Z", attempts: [{ n: 0, usedCookies: false, availabilityClass: cls }] }), metadata: LIVE_META, }); assert.deepEqual([s?.now, s?.first], [cls, null], cls); } }); // REVIEW 1, nit 9: the TypeScript reader's rules. test("as availability-server.ts: an unparseable checkedAt lets the record win; an outcome of the wrong shape is no outcome", () => { const record = { checkedAt: "not a time", checkedBy: "check", availability: "private" }; const late = outcomeOf({ status: "failed", finishedAt: "2031-03-03T00:00:00Z", attempts: [{ n: 0, usedCookies: false, availabilityClass: "members_only" }] }); assert.equal(deriveSourceStatus({ availability: record, outcome: late })?.now, "private"); const { videoId: _drop, ...shapeless } = late; const good = { checkedAt: "2031-03-01T00:00:00Z", checkedBy: "check", availability: "private" }; assert.equal(deriveSourceStatus({ availability: good, outcome: shapeless })?.now, "private", "a shapeless outcome is ignored"); assert.equal(deriveSourceStatus({ availability: good, outcome: late })?.now, "members_only"); assert.equal(deriveSourceStatus({ outcome: shapeless }), null); }); // REVIEW 1, nit 10: a check reads a missing field as public. test("a check's public counts only where the video's metadata shows its extractor reports the field", () => { const record = { checkedAt: "2031-03-05T00:00:00Z", checkedBy: "check", availability: "members_only", history: [ { availability: "public", observedAt: "2031-03-01T12:00:00Z", source: "check" }, { availability: "members_only", observedAt: "2031-03-05T00:00:00Z", source: "check" }, ], }; const none = deriveSourceStatus({ availability: record, metadata: { live_status: "was_live", epoch: sec("2031-03-01T00:00:00Z") } }); assert.deepEqual([none?.now, none?.first], ["members_only", null]); assert.ok(none?.evidence.some((e) => e.kind === "check" && e.counts === false && /missing field/.test(e.why))); const reported = deriveSourceStatus({ availability: record, metadata: { availability: "subscriber_only", epoch: sec("2031-03-05T00:00:00Z") } }); assert.deepEqual([reported?.now, reported?.first, reported?.firstWhen], ["members_only", "public", "other"]); // A download's public in the history is its own kind, with its own reason. const dl = deriveSourceStatus({ availability: { ...record, history: [{ availability: "public", observedAt: "2031-03-01T12:00:00Z", source: "download" }, record.history[1]] } }); assert.ok(dl?.evidence.some((e) => e.kind === "download" && e.counts === false && /stderr/.test(e.why))); assert.equal(dl?.first, null); }); test("a public source has no first state to name; evidence after the state began does not count", () => { const pub = deriveSourceStatus({ availability: { checkedAt: "2031-03-01T00:00:00Z", availability: "public" }, metadata: LIVE_META }); assert.deepEqual([pub?.now, pub?.first], ["public", null]); const late = deriveSourceStatus({ availability: { checkedAt: "2031-03-05T00:00:00Z", availability: "private", history: [{ availability: "private", observedAt: "2031-03-02T00:00:00Z", source: "fetch" }] }, metadata: { availability: "public", epoch: sec("2031-03-04T00:00:00Z") }, }); assert.equal(late?.first, null); }); test("readSourceStatus reads one video directory, and keeps no big metadata", async () => { const dir = await mkdtemp(path.join(os.tmpdir(), "source-status-")); try { const v = path.join(dir, "synth-vid-001"); await mkdir(v); await writeFile(path.join(v, "availability.json"), JSON.stringify({ checkedAt: "2031-03-01T14:00:00Z", availability: "private", history: [{ availability: "private", observedAt: "2031-03-01T14:00:00Z", source: "fetch" }] })); await writeFile(path.join(v, "metadata.info.json"), JSON.stringify({ ...LIVE_META, formats: [{ id: "x" }] })); await writeFile(path.join(v, "live-recording.json"), "{ not json"); const s = await readSourceStatus(v); assert.deepEqual([s?.now, s?.first, s?.firstWhen], ["private", "public", "live"]); assert.equal(await readSourceStatus(path.join(dir, "absent")), null); } finally { await rm(dir, { recursive: true, force: true }); } });