import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import type { Paths } from "./paths"; import { dropSavedAlternate, loadSavedVideo, SavedAlternateRefusal, savedVideoPointerPath, } from "./savedVideo-server"; import { SAVED_VIDEOS_MARKER_FILENAME, SavedVideosStoreInTransitionError } from "./savedVideoStore"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/savedVideoDropAlternate.test.ts // // dropSavedAlternate: one listed alternate out of the store and out of the // pointer, nothing else — refused for the active copy, an unlisted name and a // path; dryRun changes nothing; a file already gone is cleaned from the // pointer; every other byte of the pointer is what it was. const ACTIVE = "source-media.mp4"; const ALT = "source-media.alt-1080p-20261029142639.mp4"; const ALT2 = "source-media.alt-360p-20261023172312.mp4"; type Fixture = { paths: Paths; videoDir: string; storeDir: string; pointer: Record }; async function withVideo(alts: string[], fn: (f: Fixture) => Promise): Promise { const root = await mkdtemp(path.join(tmpdir(), "drop-alternate-")); const paths = { transcriptsDir: root, channelsDir: path.join(root, "channels"), savedVideosDir: path.join(root, "saved-videos"), } as Paths; const videoDir = path.join(paths.channelsDir, "synth", "data", "v1"); const storeDir = path.join(paths.savedVideosDir, "synth", "v1"); await mkdir(videoDir, { recursive: true }); await mkdir(storeDir, { recursive: true }); await writeFile(path.join(storeDir, ACTIVE), "active-bytes"); for (const a of alts) await writeFile(path.join(storeDir, a), `bytes of ${a}`); // Key order and an unknown key on purpose: the rewrite must keep both. const pointer: Record = { storedAt: "2026-10-29T14:26:39.000Z", dir: storeDir, file: ACTIVE, bytes: 12, keepReason: "pin", origin: { requestedBy: "persist-videos" }, format: { preset: "original", height: 720, vcodec: "avc1" }, futureKey: { kept: [1, 2, 3] }, alternates: alts.map((a, i) => ({ file: a, bytes: 10 + i, storedAt: "2026-10-23T17:23:12.000Z", setAsideAt: "2026-10-29T14:26:39.000Z", height: 1080, someLaterField: "kept", })), probed: { seconds: 4696.5, height: 720 }, }; await writeFile(savedVideoPointerPath(videoDir), JSON.stringify(pointer, null, 2) + "\n"); try { await fn({ paths, videoDir, storeDir, pointer }); } finally { await rm(root, { recursive: true, force: true }); } } const pointerText = (videoDir: string) => readFile(savedVideoPointerPath(videoDir), "utf8"); test("drops a listed alternate: the file goes, its entry goes, every other byte of the pointer stays", async () => { await withVideo([ALT, ALT2], async ({ paths, videoDir, storeDir, pointer }) => { const r = await dropSavedAlternate(videoDir, ALT, { paths }); assert.equal(r.dryRun, false); assert.equal(r.existed, true); assert.equal(r.bytes, `bytes of ${ALT}`.length); assert.equal(r.remaining, 1); assert.equal(r.path, path.join(storeDir, ALT)); assert.equal(r.alternate.file, ALT); assert.deepEqual((await readdir(storeDir)).sort(), [ALT2, ACTIVE].sort()); const expected = { ...pointer, alternates: (pointer.alternates as { file: string }[]).filter((a) => a.file !== ALT), }; assert.equal(await pointerText(videoDir), JSON.stringify(expected, null, 2) + "\n"); }); }); test("the last alternate dropped removes the `alternates` key and nothing else", async () => { await withVideo([ALT], async ({ paths, videoDir, storeDir, pointer }) => { const r = await dropSavedAlternate(videoDir, ALT, { paths }); assert.equal(r.remaining, 0); assert.deepEqual(await readdir(storeDir), [ACTIVE]); const { alternates: _gone, ...rest } = pointer; assert.equal(await pointerText(videoDir), JSON.stringify(rest, null, 2) + "\n"); const loaded = await loadSavedVideo(videoDir); assert.equal(loaded?.file, ACTIVE); assert.equal(loaded?.alternates, undefined); }); }); test("refuses the active copy, an unlisted name and any path; nothing is touched", async () => { await withVideo([ALT], async ({ paths, videoDir, storeDir }) => { const before = await pointerText(videoDir); await assert.rejects(dropSavedAlternate(videoDir, ACTIVE, { paths }), (e: Error) => { assert.ok(e instanceof SavedAlternateRefusal); assert.match(e.message, /is the active copy/); return true; }); await assert.rejects(dropSavedAlternate(videoDir, "source-media.alt-9p-1.mp4", { paths }), (e: Error) => { assert.ok(e instanceof SavedAlternateRefusal); assert.match(e.message, /is not one of this video's listed alternates \(source-media\.alt-1080p/); return true; }); for (const bad of [`../v1/${ALT}`, `x/${ALT}`, path.join(storeDir, ALT), "..", ".", "", "a\\b"]) { await assert.rejects(dropSavedAlternate(videoDir, bad, { paths }), SavedAlternateRefusal, bad); } assert.equal(await pointerText(videoDir), before); assert.deepEqual((await readdir(storeDir)).sort(), [ALT, ACTIVE].sort()); }); }); test("no pointer is a refusal", async () => { await withVideo([ALT], async ({ paths, videoDir }) => { await rm(savedVideoPointerPath(videoDir)); await assert.rejects(dropSavedAlternate(videoDir, ALT, { paths }), /no saved-video pointer/); }); }); test("dryRun answers the same and changes nothing", async () => { await withVideo([ALT], async ({ paths, videoDir, storeDir }) => { const before = await pointerText(videoDir); const r = await dropSavedAlternate(videoDir, ALT, { dryRun: true, paths }); assert.equal(r.dryRun, true); assert.equal(r.existed, true); assert.equal(r.remaining, 0); assert.equal(await pointerText(videoDir), before); assert.deepEqual((await readdir(storeDir)).sort(), [ALT, ACTIVE].sort()); }); }); test("idempotent: a listed alternate already gone from disk is cleaned from the pointer; a second drop is refused", async () => { await withVideo([ALT], async ({ paths, videoDir, storeDir }) => { await rm(path.join(storeDir, ALT)); const r = await dropSavedAlternate(videoDir, ALT, { paths }); assert.equal(r.existed, false); assert.equal(r.bytes, 0); assert.equal((await loadSavedVideo(videoDir))?.alternates, undefined); assert.deepEqual(await readdir(storeDir), [ACTIVE]); await assert.rejects(dropSavedAlternate(videoDir, ALT, { paths }), /not one of this video's listed alternates \(it lists none\)/); }); }); test("the store's relocation marker holds a drop; a dry run is held too", async () => { await withVideo([ALT], async ({ paths, videoDir, storeDir }) => { await writeFile( path.join(paths.transcriptsDir, SAVED_VIDEOS_MARKER_FILENAME), JSON.stringify({ target: "/mnt/x/saved-videos", direction: "out", startedAt: "", phase: "copy" }), ); const before = await pointerText(videoDir); await assert.rejects(dropSavedAlternate(videoDir, ALT, { paths }), SavedVideosStoreInTransitionError); await assert.rejects(dropSavedAlternate(videoDir, ALT, { dryRun: true, paths }), SavedVideosStoreInTransitionError); assert.equal(await pointerText(videoDir), before); assert.deepEqual((await readdir(storeDir)).sort(), [ALT, ACTIVE].sort()); }); });