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 { liveDownloadRefusal, liveNowState } from "./liveNow"; import { liveDownloadRefusalFor, readLiveNowState } from "./liveNow-server"; test("liveNowState: is_live, is_upcoming and post_live are streams in progress; was_live and not_live are not", () => { assert.equal(liveNowState({ live_status: "is_live" }), "is_live"); assert.equal(liveNowState({ live_status: "is_upcoming" }), "is_upcoming"); assert.equal(liveNowState({ live_status: "post_live" }), "post_live"); assert.equal(liveNowState({ live_status: "was_live" }), null, "a finished VOD downloads normally"); assert.equal(liveNowState({ live_status: "not_live" }), null); assert.equal(liveNowState({}), null); assert.equal(liveNowState(null), null); // The boolean alone (an older record), and a status that overrules it. assert.equal(liveNowState({ is_live: true }), "is_live"); assert.equal(liveNowState({ is_live: true, live_status: "was_live" }), null, "the newer field wins"); assert.equal(liveNowState({ is_live: "yes" }), null, "only a real true"); }); test("liveDownloadRefusal names record-live and refresh-metadata with runnable bodies", () => { const ids = { slug: "demo-yt", videoId: "abcdefghijk" }; const live = liveDownloadRefusal("is_live", ids); assert.match(live, /^abcdefghijk is live now \(live_status is_live\)/); assert.match(live, /at 1x/); assert.ok(live.includes(`pnpm ops record-live --json '{"slug":"demo-yt","id":"abcdefghijk"}'`), live); assert.ok(live.includes(`pnpm ops refresh-metadata --json '{"slug":"demo-yt","id":"abcdefghijk"}'`), live); assert.match(live, /wait for the VOD/); assert.match(liveDownloadRefusal("is_upcoming", ids), /scheduled stream that has not started/); const post = liveDownloadRefusal("post_live", ids); assert.match(post, /VOD is still being processed/); assert.equal(post.includes("record-live"), false, "an ended stream is not recorded"); }); test("readLiveNowState reads metadata.info.json; no record (or junk) is no answer", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "live-now-")); try { const dir = path.join(root, "v1"); await mkdir(dir); assert.equal(await readLiveNowState(dir), null); await writeFile(path.join(dir, "metadata.info.json"), "{not json"); assert.equal(await readLiveNowState(dir), null); await writeFile(path.join(dir, "metadata.info.json"), JSON.stringify({ id: "v1", live_status: "is_live" })); assert.equal(await readLiveNowState(dir), "is_live"); assert.match((await liveDownloadRefusalFor(dir, { slug: "s", videoId: "v1" })) ?? "", /v1 is live now/); await writeFile(path.join(dir, "metadata.info.json"), JSON.stringify({ id: "v1", live_status: "was_live" })); assert.equal(await liveDownloadRefusalFor(dir, { slug: "s", videoId: "v1" }), null); } finally { await rm(root, { recursive: true, force: true }); } });