import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readdir, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; // Run with: // pnpm -C editor exec tsx --test "app/channels/*/videos/fetchWindowLocalMedia.test.ts" // // The editor's fetch doors refuse a local media library channel and a local // recording with one sentence (lib/localMedia.ts localMediaFetchRefusal), before // the window cache and before any job: fetch-window, full source, and the // fetch-windows planner. Nothing reaches the network. const ROOT = await mkdtemp(path.join(os.tmpdir(), "fetch-window-local-")); process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); await writeFile(process.env.SETTINGS_FILE, JSON.stringify({ minFreeDiskGB: 0 })); const LOCAL_ID = "local-" + "a".repeat(32); async function channel(slug: string, config: Record, ids: string[]) { await mkdir(path.join(ROOT, "channels", slug), { recursive: true }); await writeFile(path.join(ROOT, "channels", slug, "config.json"), JSON.stringify(config)); for (const id of ids) { await mkdir(path.join(ROOT, "channels", slug, "data", id), { recursive: true }); await writeFile(path.join(ROOT, "channels", slug, "data", id, "metadata.info.json"), JSON.stringify({ id, title: id, webpage_url: `https://www.youtube.com/watch?v=${id}` })); } } // A Local Archive channel (no URL), holding a provider-metadata record too. await channel("library", { name: "Library", handling: "youtube", sourceKind: "video", localMedia: { sourceId: "library-1", directory: "/mnt/library" } }, [LOCAL_ID, "aaaaaaaaaaa"]); // An online channel with a library: its online records fetch as before, its local one does not. await channel("hybrid", { name: "Hybrid", handling: "youtube", url: "https://www.youtube.com/@hybrid", localMedia: { sourceId: "library-1", directory: "/mnt/library" } }, [LOCAL_ID]); const { fetchWindowAction, fetchFullSourceAction } = await import("./[id]/videoActions"); const { fetchWindowsAction } = await import("./fetchWindowsAction"); test.after(() => rm(ROOT, { recursive: true, force: true })); const SENTENCE = /with no online source, so no clip can be fetched for it\./; test("fetch-window and full source refuse a local library channel and a local recording", async () => { for (const [slug, videoId, who] of [["library", "aaaaaaaaaaa", /"library" is a local media library channel/], ["library", LOCAL_ID, /is a local recording/], ["hybrid", LOCAL_ID, /is a local recording/]] as const) { const w = await fetchWindowAction({ slug, videoId, from: 5, to: 15, provenance: { requestedBy: "test" } }); assert.equal(w.ok, false, JSON.stringify(w)); if (w.ok) continue; assert.equal(w.status, 409); assert.match(w.error, SENTENCE); assert.match(w.error, who); const f = await fetchFullSourceAction({ slug, videoId, provenance: { requestedBy: "test" } }); assert.equal(f.ok, false); if (!f.ok) assert.match(f.error, SENTENCE); } // Nothing was made: no clips/ dir, no job. assert.deepEqual(await readdir(path.join(ROOT, "channels", "library", "data", LOCAL_ID)), ["metadata.info.json"]); }); test("the fetch-windows planner puts a local item in unresolved with the sentence", async () => { const r = await fetchWindowsAction({ items: [{ slug: "library", id: "aaaaaaaaaaa", from: 5, to: 15 }, { slug: "hybrid", id: LOCAL_ID, from: 5, to: 15 }], requestedBy: "test", dryRun: true, }); assert.ok(r.ok, JSON.stringify(r)); if (!r.ok) return; assert.equal(r.unresolved.length, 2); for (const u of r.unresolved) assert.match(u.error, SENTENCE); assert.deepEqual(r.groups, []); });