import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import type { Paths } from "../lib/paths"; import { persistSourceVideo } from "../lib/savedVideo-server"; import { listSavedVideos, savedVideoTotals } from "./savedVideoInventory"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test controller/savedVideoInventory.test.ts async function withPaths(fn: (paths: Paths) => Promise): Promise { const dir = await mkdtemp(path.join(tmpdir(), "ttb-inv-")); const paths = { channelsDir: path.join(dir, "channels"), savedVideosDir: path.join(dir, "saved"), } as Paths; try { await fn(paths); } finally { await rm(dir, { recursive: true, force: true }); } } async function seed( paths: Paths, slug: string, id: string, content: string, ): Promise { const videoDir = path.join(paths.channelsDir, slug, "data", id); await mkdir(videoDir, { recursive: true }); await writeFile(path.join(videoDir, "source-media.mp4"), content); await persistSourceVideo({ videoDir, sourceFilename: "source-media.mp4", storeDir: path.join(paths.savedVideosDir, slug, id), keepReason: "keep-latest", }); } test("listSavedVideos enumerates every pointer across channels, sorted", async () => { await withPaths(async (paths) => { await seed(paths, "beta", "v2", "22"); await seed(paths, "alpha", "v1", "1"); await seed(paths, "alpha", "v0", "000"); // A data dir with no pointer is ignored. await mkdir(path.join(paths.channelsDir, "alpha", "data", "plain"), { recursive: true, }); const all = await listSavedVideos({ paths }); assert.deepEqual( all.map((e) => `${e.slug}/${e.videoId}`), ["alpha/v0", "alpha/v1", "beta/v2"], ); assert.equal(all[0].pointer.file, "source-media.mp4"); const onlyAlpha = await listSavedVideos({ paths, channelSlug: "alpha" }); assert.equal(onlyAlpha.length, 2); const totals = await savedVideoTotals({ paths }); assert.equal(totals.count, 3); assert.equal(totals.bytes, "22".length + "1".length + "000".length); }); });