// The failed copies of a gone recording (release 29 review): appended one per // URL, the newest kept, read back for find-copy. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/copyAttempts.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { readFile, writeFile } from "node:fs/promises"; import { COPY_ATTEMPTS_FILENAME, COPY_ATTEMPT_RETRY_DAYS, coerceCopyAttempts, isSourceSideCopyFailure, latestFetchedCopy, loadCopyAttempts, readFailedCopies, recentTerminalAttempt, recordCopyAttempt, recordFailedCopy, } from "./copyAttempts-server"; test("recordFailedCopy appends one per URL, newest last; readFailedCopies reads them", async () => { const dir = await mkdtemp(path.join(os.tmpdir(), "copy-attempts-")); try { assert.deepEqual(await readFailedCopies(dir), []); await recordFailedCopy(dir, { from: "wayback", url: "https://web.archive.org/web/x", at: "1", error: "404" }); await recordFailedCopy(dir, { from: "archive.org", url: "https://archive.org/download/a/b.mp4", at: "2", error: "403" }); await recordFailedCopy(dir, { from: "wayback", url: "https://web.archive.org/web/x", at: "3", error: "410" }); assert.deepEqual( (await readFailedCopies(dir)).map((f) => [f.from, f.at, f.error]), [ ["archive.org", "2", "403"], ["wayback", "3", "410"], ], ); assert.equal(COPY_ATTEMPTS_FILENAME, "copy-attempts.json"); } finally { await rm(dir, { recursive: true, force: true }); } }); test("isSourceSideCopyFailure: the copy's 404/410/403 or unreachability, never our selector, ffmpeg or the unplaceable", () => { assert.equal(isSourceSideCopyFailure("unknown", "ERROR: [generic] x: Requested format is not available. Use --list-formats"), false); assert.equal(isSourceSideCopyFailure("network", "ERROR: [generic] x: Requested format is not available"), false); assert.equal(isSourceSideCopyFailure("unknown", "ERROR: [generic] Unable to download webpage: HTTP Error 404: Not Found"), true); assert.equal(isSourceSideCopyFailure("unknown", "ERROR: HTTP Error 410: Gone"), true); assert.equal(isSourceSideCopyFailure("network", "ERROR: HTTP Error 403: Forbidden"), true); assert.equal(isSourceSideCopyFailure("per_video", "Video unavailable"), true); assert.equal(isSourceSideCopyFailure("unknown", "ERROR: [web.archive:youtube] x: The requested video is not archived or indexed"), true); assert.equal(isSourceSideCopyFailure("unknown", "ERROR: ffmpeg exited with code 1"), false); assert.equal(isSourceSideCopyFailure("unknown", "No space left on device"), false); }); // Release 31 (lane C): the whole-copy attempts beside the failed copies. test("recordCopyAttempt and recordFailedCopy keep each other's entries; a file of the older shape still reads", async () => { const dir = await mkdtemp(path.join(os.tmpdir(), "copy-attempts-")); try { await writeFile(path.join(dir, COPY_ATTEMPTS_FILENAME), JSON.stringify({ failed: [{ from: "wayback", url: "https://web.archive.org/web/1/x", at: "1", error: "404" }] })); assert.equal((await loadCopyAttempts(dir))?.attempts, undefined, "release 29's file: no attempts"); await recordCopyAttempt(dir, { at: "2026-10-01T00:00:00.000Z", outcome: "none-found", detail: "wayback none; archive.org none" }); await recordFailedCopy(dir, { from: "archive.org", url: "https://archive.org/download/a/b.mp4", at: "3", error: "403" }); await recordCopyAttempt(dir, { at: "2026-10-02T00:00:00.000Z", outcome: "fetched", source: "archive.org", url: "https://archive.org/download/a/c.mp4", identifier: "a", file: "c.mp4", audioFile: "audio.mp3", }); const c = await loadCopyAttempts(dir); assert.deepEqual(c?.failed.map((f) => f.at), ["1", "3"]); assert.deepEqual(c?.attempts?.map((a) => a.outcome), ["none-found", "fetched"]); assert.equal(latestFetchedCopy(c)?.identifier, "a"); assert.equal(recentTerminalAttempt(c), null, "the newest attempt fetched: not terminal"); const raw = JSON.parse(await readFile(path.join(dir, COPY_ATTEMPTS_FILENAME), "utf8")); assert.equal(raw.attempts.length, 2); } finally { await rm(dir, { recursive: true, force: true }); } }); test("recentTerminalAttempt: the newest attempt when none-found or failed and younger than the retry window", () => { const now = Date.parse("2026-10-16T00:00:00.000Z"); const day = 86_400_000; const at = (daysAgo: number) => new Date(now - daysAgo * day).toISOString(); const c = (outcome: "fetched" | "none-found" | "failed", daysAgo: number) => coerceCopyAttempts({ attempts: [{ at: at(daysAgo), outcome }] }); assert.equal(recentTerminalAttempt(c("none-found", 3), now)?.outcome, "none-found"); assert.equal(recentTerminalAttempt(c("failed", COPY_ATTEMPT_RETRY_DAYS - 1), now)?.outcome, "failed"); assert.equal(recentTerminalAttempt(c("none-found", COPY_ATTEMPT_RETRY_DAYS + 1), now), null, "old enough to ask again"); assert.equal(recentTerminalAttempt(c("fetched", 1), now), null); assert.equal(recentTerminalAttempt(null, now), null); assert.equal(coerceCopyAttempts({ attempts: [{ at: "x", outcome: "maybe" }] })?.attempts?.length, 0, "an unknown outcome is dropped"); assert.equal(coerceCopyAttempts({ nothing: true }), null); });