import { test } from "node:test"; import assert from "node:assert/strict"; import { createHash } from "node:crypto"; import { mkdir, mkdtemp, readFile, readdir, rm, stat, writeFile } from "node:fs/promises"; import { hostname, tmpdir } from "node:os"; import path from "node:path"; import type { Paths } from "./paths"; import { compactedLine, compactedOriginalOf, parseSavedVideoPointer, savedAlternatesLine, } from "./savedVideo"; import { writeCompactingMarker } from "./compactingMarker-server"; import { COMPACTING_MARKER_FILENAME, CompactionInProgressError, SavedAlternateRefusal, dropSavedAlternate, dropSavedVideo, loadSavedVideo, persistSourceVideo, recordOriginalReview, savedVideoPointerPath, unpersistSavedVideo, updateSavedVideoChecksum, } from "./savedVideo-server"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/savedVideoOriginal.test.ts // // A COMPACTED VIDEO'S POINTER (controller/compactSavedVideos.ts): the // `compacted` block and the original kept as `keptAs: "original"` parse back, // say "re-encode" wherever they are listed, and the original is deleted by // nothing but a confirmed drop — identified by its sha256, with the copy read // back from disk inside the drop, and for a video not freshly on its platform // a review of THIS copy. const ACTIVE = "source-media.compact-av1-20261114031209.mp4"; const ORIGINAL = "source-media.mp4"; const ACTIVE_BYTES = "av1!!"; const ORIGINAL_BYTES = "o".repeat(1200); const sha = (s: string) => createHash("sha256").update(s).digest("hex"); const SHA_ACTIVE = sha(ACTIVE_BYTES); const SHA_ORIGINAL = sha(ORIGINAL_BYTES); const READ_BACK = { probe: async () => ({ durationSec: 100, hasVideo: true }) }; const ON = { onPlatform: true, why: "checked public" }; const GONE = { onPlatform: false, why: "no availability record" }; function compactedBlock(extra: Record = {}) { return { at: "2026-11-14T03:12:09.000Z", tool: "archilyzer compact-saved-videos 1", encoder: "ffmpeg version n9.0.2", settings: { codec: "av1", encoder: "libsvtav1", preset: 8, crf: 36, gopSeconds: 5, pixFmt: "yuv420p", fps: 30, audio: "copy", chunkSeconds: 600 }, source: { file: ORIGINAL, bytes: 1200, sha256: SHA_ORIGINAL, vcodec: "h264", height: 1080, fps: 60, seconds: 100 }, result: { file: ACTIVE, bytes: ACTIVE_BYTES.length, sha256: SHA_ACTIVE, vcodec: "av1", height: 1080, fps: 30, seconds: 100 }, checks: { passed: true, decoded: true, seconds: [100, 100], secondsTolerance: 0.034, frames: [6000, 3000, 3000], audio: "identical", audioHash: "c".repeat(64), quality: { metric: "vmaf", mean: 94.2, min: 91.9, samples: 12, minMean: 90, minSample: 85 }, cuts: [{ at: 10, ssim: 0.97 }], cutSsimMin: 0.9, clock: "none", wholeFile: { fps: 2, width: 64, frames: 200, mean: 0.99, min: 0.97, worstAt: 12, minFloor: 0.9, meanFloor: 0.97, maxGapSeconds: 0.033 }, }, chunks: 1, ...extra, }; } type Fixture = { paths: Paths; videoDir: string; storeDir: string }; async function withCompacted(fn: (f: Fixture) => Promise): Promise { const root = await mkdtemp(path.join(tmpdir(), "compacted-original-")); const paths = { transcriptsDir: root, channelsDir: path.join(root, "channels"), savedVideosDir: path.join(root, "saved-videos"), } as Paths; const videoDir = path.join(paths.channelsDir, "kick", "data", "v1"); const storeDir = path.join(paths.savedVideosDir, "kick", "v1"); await mkdir(videoDir, { recursive: true }); await mkdir(storeDir, { recursive: true }); await writeFile(path.join(storeDir, ACTIVE), ACTIVE_BYTES); await writeFile(path.join(storeDir, ORIGINAL), ORIGINAL_BYTES); const pointer = { storedAt: "2026-11-01T00:00:00.000Z", dir: storeDir, file: ACTIVE, bytes: ACTIVE_BYTES.length, sha256: SHA_ACTIVE, keepReason: "pin", format: { preset: "original", height: 1080, vcodec: "av1" }, compacted: compactedBlock(), alternates: [ { file: ORIGINAL, bytes: 1200, storedAt: "2026-11-01T00:00:00.000Z", setAsideAt: "2026-11-14T03:12:09.000Z", height: 1080, vcodec: "h264", keptAs: "original", sha256: SHA_ORIGINAL, }, ], }; await writeFile(savedVideoPointerPath(videoDir), JSON.stringify(pointer, null, 2) + "\n"); try { await fn({ paths, videoDir, storeDir }); } finally { await rm(root, { recursive: true, force: true }); } } const readRaw = async (videoDir: string) => JSON.parse(await readFile(savedVideoPointerPath(videoDir), "utf8")); const writeRaw = (videoDir: string, raw: unknown) => writeFile(savedVideoPointerPath(videoDir), JSON.stringify(raw)); const CONFIRM = { confirmOriginal: true, readBack: READ_BACK } as const; const liveMarker = (storeDir: string) => writeCompactingMarker(storeDir); test("a pointer without `compacted` parses exactly as before", () => { const raw = { storedAt: "x", dir: "/s", file: "source-media.mp4", bytes: 3, keepReason: "pin" }; assert.deepEqual(parseSavedVideoPointer(raw), raw); assert.equal(compactedLine(parseSavedVideoPointer(raw)!), null); }); test("the compacted block and the original parse back, and every line says re-encode", () => { const p = parseSavedVideoPointer({ storedAt: "x", dir: "/s", file: ACTIVE, bytes: 4_900_000_000, compacted: compactedBlock({ source: { file: ORIGINAL, bytes: 12_300_000_000, sha256: SHA_ORIGINAL, vcodec: "h264", height: 1080, fps: 60 } }), alternates: [{ file: ORIGINAL, bytes: 12_300_000_000, storedAt: "", setAsideAt: "", keptAs: "original", sha256: SHA_ORIGINAL }], })!; assert.equal(p.compacted?.checks.passed, true); assert.equal(p.compacted?.checks.wholeFile?.min, 0.97); assert.equal(compactedOriginalOf(p)?.file, ORIGINAL); const line = compactedLine(p)!; assert.match(line, /^Re-encoded copy \(AV1 crf 36, 1080p30 from 60 fps, audio copied\) of a 11\.46 GB H\.264 original — 4\.56 GB, 2026-11-14\./); assert.match(line, /The original is kept beside it \(source-media\.mp4\)\./); const alts = savedAlternatesLine(p); assert.match(alts, /active .*a re-encode \(AV1 crf 36\)/); assert.match(alts, /the original the active copy was re-encoded from, kept until it is dropped/); }); test("the original is found by its sha256, never by position", () => { const p = parseSavedVideoPointer({ storedAt: "x", dir: "/s", file: ACTIVE, bytes: 3, compacted: compactedBlock(), alternates: [ { file: "source-media.alt-1080p-1.mp4", bytes: 1, storedAt: "", setAsideAt: "", keptAs: "original", sha256: "f".repeat(64) }, { file: ORIGINAL, bytes: 1200, storedAt: "", setAsideAt: "", keptAs: "original", sha256: SHA_ORIGINAL }, ], })!; assert.equal(compactedOriginalOf(p)?.file, ORIGINAL); }); test("a damaged compacted block still reads as a re-encode, its checks as not passed, its audio as unknown", () => { const p = parseSavedVideoPointer({ storedAt: "x", dir: "/s", file: ACTIVE, bytes: 3, compacted: { at: 7, checks: "?" } })!; assert.ok(p.compacted); assert.equal(p.compacted.checks.passed, false); assert.equal(p.compacted.checks.audio, "unknown"); assert.match(compactedLine(p)!, /^Re-encoded copy/); }); test("an original is not dropped without confirmOriginal", async () => { await withCompacted(async ({ videoDir, storeDir }) => { await assert.rejects( dropSavedAlternate(videoDir, ORIGINAL, { presence: ON, readBack: READ_BACK }), (e: Error) => e instanceof SavedAlternateRefusal && /ORIGINAL .*confirmOriginal/.test(e.message), ); await stat(path.join(storeDir, ORIGINAL)); }); }); test("the drop reads the compacted copy back itself: a copy whose bytes changed keeps the original", async () => { await withCompacted(async ({ videoDir, storeDir }) => { // Same size, other bytes on disk: the default hasher reads the disk. await writeFile(path.join(storeDir, ACTIVE), "av1??"); await assert.rejects(dropSavedAlternate(videoDir, ORIGINAL, { ...CONFIRM, presence: ON }), /did not read back from disk .*sha256/); await stat(path.join(storeDir, ORIGINAL)); // A hasher that disagrees, a container that does not open: kept too. await writeFile(path.join(storeDir, ACTIVE), ACTIVE_BYTES); await assert.rejects( dropSavedAlternate(videoDir, ORIGINAL, { confirmOriginal: true, presence: ON, readBack: { ...READ_BACK, hasher: async () => "0".repeat(64) } }), /did not read back/, ); await assert.rejects( dropSavedAlternate(videoDir, ORIGINAL, { confirmOriginal: true, presence: ON, readBack: { probe: async () => null } }), /does not open as a video/, ); await stat(path.join(storeDir, ORIGINAL)); // A dry run answers without reading back. const dry = await dropSavedAlternate(videoDir, ORIGINAL, { confirmOriginal: true, presence: ON, dryRun: true }); assert.equal(dry.dryRun, true); }); }); test("an alternate labelled original whose sha256 is not the compacted copy's source is never dropped", async () => { await withCompacted(async ({ videoDir, storeDir }) => { const raw = await readRaw(videoDir); raw.alternates[0].sha256 = "e".repeat(64); await writeRaw(videoDir, raw); await assert.rejects(dropSavedAlternate(videoDir, ORIGINAL, { ...CONFIRM, presence: ON }), /is not the original the compacted copy was made from/); await stat(path.join(storeDir, ORIGINAL)); }); await withCompacted(async ({ videoDir }) => { const raw = await readRaw(videoDir); delete raw.compacted; await writeRaw(videoDir, raw); await assert.rejects(dropSavedAlternate(videoDir, ORIGINAL, { ...CONFIRM, presence: ON }), /active copy is not a compacted re-encode/); }); }); test("a video not known to be on its platform keeps its original until a review of THIS copy is recorded", async () => { await withCompacted(async ({ videoDir, storeDir }) => { await assert.rejects( dropSavedAlternate(videoDir, ORIGINAL, { ...CONFIRM, presence: GONE }), /not known to be on its platform \(no availability record\).*Review the copy that stays first/, ); // With no availability record on disk at all, the default reads it as gone. await assert.rejects(dropSavedAlternate(videoDir, ORIGINAL, CONFIRM), /not known to be on its platform \(no availability record/); await stat(path.join(storeDir, ORIGINAL)); const reviewed = await recordOriginalReview(videoDir, { at: "2026-11-20T10:00:00.000Z", by: "editor", note: "watched it" }); assert.equal(reviewed.review?.forSha256, SHA_ACTIVE); const r = await dropSavedAlternate(videoDir, ORIGINAL, { ...CONFIRM, presence: GONE, by: "test" }); assert.equal(r.existed, true); await assert.rejects(stat(path.join(storeDir, ORIGINAL))); const raw = await readRaw(videoDir); assert.equal(raw.alternates, undefined); assert.equal(raw.compacted.originalDropped.by, "test"); assert.equal(raw.compacted.originalDropped.sha256, SHA_ORIGINAL); assert.equal(raw.compacted.originalDropped.bytes, 1200); assert.equal(raw.compacted.originalDropped.fileRemoved, true); assert.match(compactedLine((await loadSavedVideo(videoDir))!)!, /The original was dropped on 20\d\d-\d\d-\d\d \(sha256 [0-9a-f]{12}…, 1200 bytes\)/); }); }); test("a fresh, first-hand check is what counts as on the platform: a backfilled or old public is unknown", async () => { await withCompacted(async ({ videoDir }) => { const write = (rec: unknown) => writeFile(path.join(videoDir, "availability.json"), JSON.stringify(rec)); const now = new Date().toISOString(); await write({ checkedAt: now, availability: "public", checkedBy: "backfill" }); await assert.rejects(dropSavedAlternate(videoDir, ORIGINAL, CONFIRM), /was not read by a check of the platform \(backfill\)/); await write({ checkedAt: now, availability: "public" }); await assert.rejects(dropSavedAlternate(videoDir, ORIGINAL, CONFIRM), /was not read by a check of the platform/); await write({ checkedAt: "2026-01-01T00:00:00.000Z", availability: "public", checkedBy: "check" }); await assert.rejects(dropSavedAlternate(videoDir, ORIGINAL, CONFIRM), /older than 7 day\(s\).*Check availability/); await write({ checkedAt: now, availability: "needs_auth", checkedBy: "check" }); await assert.rejects(dropSavedAlternate(videoDir, ORIGINAL, CONFIRM), /its check says needs_auth/); await write({ checkedAt: now, availability: "public", checkedBy: "check" }); await dropSavedAlternate(videoDir, ORIGINAL, CONFIRM); }); }); test("a review of another copy counts for nothing", async () => { await withCompacted(async ({ videoDir }) => { await recordOriginalReview(videoDir, { at: "2026-11-20T10:00:00.000Z", by: "editor" }); const raw = await readRaw(videoDir); raw.alternates[0].review.forSha256 = "1".repeat(64); await writeRaw(videoDir, raw); await assert.rejects(dropSavedAlternate(videoDir, ORIGINAL, { ...CONFIRM, presence: GONE }), /review is for another copy/); }); }); test("recordOriginalReview refuses a pointer with no compacted block", async () => { await withCompacted(async ({ videoDir }) => { const raw = await readRaw(videoDir); delete raw.compacted; await writeRaw(videoDir, raw); await assert.rejects(recordOriginalReview(videoDir, { at: "x", by: "editor" }), /not a compacted re-encode/); }); }); test("a video on its platform drops its original with confirm and the read-back, no review needed", async () => { await withCompacted(async ({ videoDir, storeDir }) => { await dropSavedAlternate(videoDir, ORIGINAL, { ...CONFIRM, presence: ON }); await assert.rejects(stat(path.join(storeDir, ORIGINAL))); await stat(path.join(storeDir, ACTIVE)); }); }); test("no drop while a download or persist of the video runs, nor while it is being compacted", async () => { await withCompacted(async ({ videoDir, storeDir }) => { await assert.rejects( dropSavedAlternate(videoDir, ORIGINAL, { ...CONFIRM, presence: ON, busy: () => "a persist-videos job (running, j1) may write this video's saved copy" }), /is kept: a persist-videos job/, ); await liveMarker(storeDir); await assert.rejects(dropSavedAlternate(videoDir, ORIGINAL, { ...CONFIRM, presence: ON }), (e: Error) => e instanceof CompactionInProgressError); await stat(path.join(storeDir, ORIGINAL)); }); }); test("the drop writes the pointer first; an interrupted drop is seen and finished when asked again", async () => { await withCompacted(async ({ videoDir, storeDir }) => { // The state a crash between the pointer write and the rm leaves. const raw = await readRaw(videoDir); delete raw.alternates; raw.compacted.originalDropped = { at: "2026-11-20T00:00:00.000Z", by: "x", file: ORIGINAL, bytes: 1200, sha256: SHA_ORIGINAL, fileRemoved: false }; raw.pendingDrops = [{ file: ORIGINAL, at: "2026-11-20T00:00:00.000Z", keptAs: "original", sha256: SHA_ORIGINAL }]; await writeRaw(videoDir, raw); await assert.rejects(dropSavedAlternate(videoDir, ORIGINAL, { presence: ON }), /drop was interrupted: finish it with "confirmOriginal": true/); const r = await dropSavedAlternate(videoDir, ORIGINAL, { confirmOriginal: true }); assert.equal(r.existed, true); await assert.rejects(stat(path.join(storeDir, ORIGINAL))); assert.equal((await readRaw(videoDir)).compacted.originalDropped.fileRemoved, true); }); }); test("an original whose compacted copy's checks did not pass is never dropped, nor reviewed", async () => { await withCompacted(async ({ videoDir }) => { const raw = await readRaw(videoDir); raw.compacted.checks.passed = false; await writeRaw(videoDir, raw); await assert.rejects(dropSavedAlternate(videoDir, ORIGINAL, { ...CONFIRM, presence: ON }), /checks did not pass/); await assert.rejects(recordOriginalReview(videoDir, { at: "x", by: "editor" }), /checks did not pass/); }); }); test("dropSavedVideo (the prune's delete) refuses a video that keeps an original", async () => { await withCompacted(async ({ videoDir, storeDir }) => { await assert.rejects(dropSavedVideo(videoDir), (e: Error) => e instanceof SavedAlternateRefusal); await stat(path.join(storeDir, ORIGINAL)); await stat(path.join(storeDir, ACTIVE)); assert.ok(await loadSavedVideo(videoDir)); }); }); test("unpersist brings every copy back with the original as the source; refused once the original is gone", async () => { await withCompacted(async ({ videoDir }) => { assert.equal(await unpersistSavedVideo(videoDir), true); assert.equal(await readFile(path.join(videoDir, ORIGINAL), "utf8"), ORIGINAL_BYTES); await stat(path.join(videoDir, ACTIVE)); }); await withCompacted(async ({ videoDir, storeDir }) => { const raw = await readRaw(videoDir); delete raw.alternates; await writeRaw(videoDir, raw); await rm(path.join(storeDir, ORIGINAL)); await assert.rejects(unpersistSavedVideo(videoDir), /re-encode .*original was dropped/); assert.ok(await loadSavedVideo(videoDir)); await stat(path.join(storeDir, ACTIVE)); }); }); test("a persist over a compacted copy moves the original aside, re-labels it, and nothing can drop it automatically", async () => { await withCompacted(async ({ videoDir, storeDir }) => { await writeFile(path.join(videoDir, ORIGINAL), "a new download"); const p = await persistSourceVideo({ videoDir, sourceFilename: ORIGINAL, storeDir, keepReason: "override" }); assert.equal(p.file, ORIGINAL); assert.equal(p.compacted, undefined); assert.equal(await readFile(path.join(storeDir, ORIGINAL), "utf8"), "a new download"); const kept = p.alternates?.find((a) => a.sha256 === SHA_ORIGINAL); assert.ok(kept && kept.file !== ORIGINAL && /^source-media\.alt-1080p-\d{14}\.mp4$/.test(kept.file), kept?.file); // No longer "the original the active copy was re-encoded from". assert.equal(kept.keptAs, "former-original"); assert.match(savedAlternatesLine(p), /an original of an earlier compacted copy .*kept until a person drops it/); assert.equal(compactedOriginalOf(p), null); assert.equal(await readFile(path.join(storeDir, kept.file), "utf8"), ORIGINAL_BYTES); assert.ok((await readdir(storeDir)).includes(ACTIVE)); // Never by the prune, never without a person's confirm. await assert.rejects(dropSavedVideo(videoDir), /keeps an original/); await assert.rejects(dropSavedAlternate(videoDir, kept.file, { presence: ON }), /ORIGINAL of an earlier compacted copy/); }); }); test("a persist of a video being compacted is refused, its container left where it was; a stale marker is not", async () => { await withCompacted(async ({ videoDir, storeDir }) => { await liveMarker(storeDir); await writeFile(path.join(videoDir, "source-media.webm"), "dl"); await assert.rejects( persistSourceVideo({ videoDir, sourceFilename: "source-media.webm", storeDir }), (e: Error) => e instanceof CompactionInProgressError && /being compacted/.test(e.message), ); await stat(path.join(videoDir, "source-media.webm")); // A marker whose process is gone is stale. await writeFile(path.join(storeDir, COMPACTING_MARKER_FILENAME), JSON.stringify({ pid: 2 ** 22 + 7, host: hostname(), startedAt: "x" })); await persistSourceVideo({ videoDir, sourceFilename: "source-media.webm", storeDir }); }); }); test("a checksum is written only while the pointer names the file that was hashed", async () => { await withCompacted(async ({ videoDir }) => { await updateSavedVideoChecksum(videoDir, "d".repeat(64), ORIGINAL); assert.equal((await readRaw(videoDir)).sha256, SHA_ACTIVE); await updateSavedVideoChecksum(videoDir, "e".repeat(64), ACTIVE); const raw = await readRaw(videoDir); assert.equal(raw.sha256, "e".repeat(64)); assert.ok(raw.compacted, "the rest of the pointer is kept"); }); }); // --- review round 2 ------------------------------------------------------------ async function makeFormer(videoDir: string, storeDir: string): Promise { // A download of another container extension lands: no rename, the original relabelled. await writeFile(path.join(videoDir, "source-media.mkv"), "a new download"); const p = await persistSourceVideo({ videoDir, sourceFilename: "source-media.mkv", storeDir, keepReason: "pin" }); const former = p.alternates!.find((a) => a.sha256 === SHA_ORIGINAL)!; assert.equal(former.keptAs, "former-original"); assert.equal(former.file, ORIGINAL); return former.file; } test("F1: the after-verify drop names exactly what it means; a re-labelled original is not dropped", async () => { await withCompacted(async ({ videoDir, storeDir }) => { const captured = compactedOriginalOf((await loadSavedVideo(videoDir))!)!; await makeFormer(videoDir, storeDir); await assert.rejects( dropSavedAlternate(videoDir, captured.file, { confirmOriginal: true, expect: { keptAs: "original", sha256: SHA_ORIGINAL, file: captured.file }, presence: ON, busy: () => null, readBack: READ_BACK, }), /not what this drop expected \(original, .*it is former-original, .*nothing dropped/, ); await stat(path.join(storeDir, ORIGINAL)); // Even without `expect`, confirmOriginal alone never drops a former original. await assert.rejects(dropSavedAlternate(videoDir, captured.file, { ...CONFIRM, presence: ON }), /pass "confirmFormerOriginal": true/); await stat(path.join(storeDir, ORIGINAL)); }); }); test("F1: a former original is dropped only with confirmFormerOriginal and the same gone/review rule", async () => { await withCompacted(async ({ videoDir, storeDir }) => { const file = await makeFormer(videoDir, storeDir); await assert.rejects( dropSavedAlternate(videoDir, file, { confirmFormerOriginal: true, presence: GONE }), /not known to be on its platform .*Review the copy that stays first/, ); // The review is of the active copy that replaced it: it needs that copy's sha256. await assert.rejects(recordOriginalReview(videoDir, { at: "x", by: "editor" }, { formerOriginal: file }), /no recorded sha256/); await updateSavedVideoChecksum(videoDir, sha("a new download"), "source-media.mkv"); const r = await recordOriginalReview(videoDir, { at: "2026-11-20T00:00:00.000Z", by: "editor" }, { formerOriginal: file }); assert.equal(r.review?.forSha256, sha("a new download")); await dropSavedAlternate(videoDir, file, { confirmFormerOriginal: true, presence: GONE }); await assert.rejects(stat(path.join(storeDir, ORIGINAL))); }); }); test("F1: no automatic step passes confirmFormerOriginal (a scan of every caller)", async () => { const roots = ["controller", "lib", "jobs", "ytdlp", "bin"].map((d) => path.join(import.meta.dirname, "..", d)); const editorApp = path.join(import.meta.dirname, "..", "..", "editor", "app"); // The flag's definition, and the person-driven ways in: the drop button, the // ops route, the server action that both call. const allowed = new Set([ path.join(import.meta.dirname, "savedVideo-server.ts"), path.join(editorApp, "api", "ops", "drop-saved-alternate", "route.ts"), path.join(editorApp, "channels", "[slug]", "videos", "[id]", "videoActions.ts"), path.join(editorApp, "channels", "[slug]", "videos", "[id]", "components", "cards", "SourceVideoSection.tsx"), ]); const found: string[] = []; const walk = async (d: string): Promise => { for (const e of await readdir(d, { withFileTypes: true }).catch(() => [])) { const p = path.join(d, e.name); if (e.isDirectory()) { if (e.name !== "node_modules" && !e.name.startsWith(".")) await walk(p); } else if (/\.(ts|tsx|mjs)$/.test(e.name) && !/\.test\./.test(e.name)) { if ((await readFile(p, "utf8")).includes("confirmFormerOriginal")) found.push(p); } } }; for (const r of [...roots, editorApp]) await walk(r); assert.deepEqual(found.filter((f) => !allowed.has(f)), [], "an unexpected caller names confirmFormerOriginal"); const compactor = await readFile(path.join(import.meta.dirname, "..", "controller", "compactSavedVideos.ts"), "utf8"); assert.ok(!compactor.includes("confirmFormerOriginal")); assert.match(compactor, /expect: \{ keptAs: "original", sha256: pointer\.compacted!\.source\.sha256, file: original\.file \}/); }); test("an interrupted drop of an ordinary alternate is listed and finished by asking again", async () => { await withCompacted(async ({ videoDir, storeDir }) => { await writeFile(path.join(storeDir, "source-media.alt-360p-1.mp4"), "alt"); const raw = await readRaw(videoDir); // The state a crash between the pointer write and the rm leaves. raw.pendingDrops = [{ file: "source-media.alt-360p-1.mp4", at: "2026-11-20T00:00:00.000Z" }]; await writeRaw(videoDir, raw); assert.equal((await loadSavedVideo(videoDir))!.pendingDrops?.[0].file, "source-media.alt-360p-1.mp4"); const r = await dropSavedAlternate(videoDir, "source-media.alt-360p-1.mp4"); assert.equal(r.existed, true); await assert.rejects(stat(path.join(storeDir, "source-media.alt-360p-1.mp4"))); assert.equal((await readRaw(videoDir)).pendingDrops, undefined); }); });