import { test } from "node:test"; import assert from "node:assert/strict"; import { lstat, mkdir, mkdtemp, readdir, rm, symlink, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test controller/storedText.delete.test.ts // // NO RESURRECTION (lane text-compress, slice 1). A plain file written again // beside an older stored copy, then deleted by a deleter that knows only the // plain name, would bring the older copy back as the file — silently and // wrongly. Every deleter of a declared name removes both forms // (`removeStoredText`), each through `removeMediaFile`. Temp dirs only. const ROOT = await mkdtemp(path.join(tmpdir(), "stored-delete-")); test.after(() => rm(ROOT, { recursive: true, force: true })); const { removeStoredText } = await import("../lib/storedText-write-server"); const { purgeSupersededAutoSubs } = await import("./purgeSupersededAutoSubs"); const { readStoredText } = await import("../lib/storedText-server"); const { storeFixture } = await import("../testing/storedTextFixture"); const CHAT = "transcript.live_chat.json"; async function videoDir(slug: string, id: string): Promise { const d = path.join(ROOT, "channels", slug, "data", id); await mkdir(d, { recursive: true }); return d; } const exists = (p: string) => lstat(p).then(() => true, () => false); test("removeStoredText: a plain chat re-written beside an older stored one — both go", async () => { const d = await videoDir("c", "v1"); await writeFile(path.join(d, CHAT), "OLD\n"); await storeFixture(d, CHAT); await writeFile(path.join(d, CHAT), "NEW\n"); assert.equal(await readStoredText(d, CHAT, "utf8"), "NEW\n"); await removeStoredText(d, CHAT); await assert.rejects(readStoredText(d, CHAT, "utf8"), { code: "ENOENT" }); assert.deepEqual(await readdir(d), []); }); test("removeStoredText through tier links: the bytes in media// go too, never orphaned", async () => { const d = await videoDir("t", "v2"); const media = path.join(ROOT, "channels", "t", "media", "v2"); await mkdir(media, { recursive: true }); await writeFile(path.join(media, CHAT), "CHAT\n"); await symlink(path.join("..", "..", "media", "v2", CHAT), path.join(d, CHAT)); await storeFixture(d, CHAT, { keepPlain: true }); assert.ok(await exists(path.join(media, `${CHAT}.zst`))); await removeStoredText(d, CHAT); assert.deepEqual(await readdir(d), []); assert.equal(await exists(path.join(media, CHAT)), false); assert.equal(await exists(path.join(media, `${CHAT}.zst`)), false); }); // YouTube's rolling-caption shape: an ASR track (inline word timing). const ASR_VTT = "WEBVTT\nKind: captions\nLanguage: en\n\n" + "00:00:00.000 --> 00:00:05.000 align:start position:0%\n" + "First<00:00:01.000> caption<00:00:02.000> line.\n\n" + "00:01:00.000 --> 00:01:50.000 align:start position:0%\n" + "Second<00:01:10.000> caption<00:01:20.000> line.\n"; test("the superseded-auto-caption purge removes a caption's stored sibling too", async () => { const d = await videoDir("p", "v3"); await writeFile(path.join(d, "transcript.json"), JSON.stringify({ transcription: [{ text: "x" }] })); await writeFile(path.join(d, "transcript.en.vtt"), ASR_VTT); // Both forms of the same track (a compression that stopped before the // original went): purged in both. await storeFixture(d, "transcript.en.vtt", { keepPlain: true }); const r = await purgeSupersededAutoSubs({ channelSlug: "p", paths: { channelsDir: path.join(ROOT, "channels") } as never, onLog: () => {}, }); assert.equal(r.removedFiles, 1); assert.deepEqual((await readdir(d)).sort(), ["transcript.json"]); }); test("the purge KEEPS both forms when they differ (or the stored one does not decode): it sniffed only the plain one", async () => { // (Changed in lane text-compress-captions: slice 1 removed a stored sibling // whatever it held. A stored copy with other content is another capture — // it may be the manual track — and nothing removes a stored caption // because a different one arrived.) for (const [id, stored] of [["v4", "differs"], ["v5", "corrupt"]] as const) { const d = await videoDir("p", id); await writeFile(path.join(d, "transcript.json"), JSON.stringify({ transcription: [{ text: "x" }] })); if (stored === "differs") { await writeFile(path.join(d, "transcript.en.vtt"), "WEBVTT\n\n00:00:00.000 --> 00:00:01.000\nA human track.\n"); await storeFixture(d, "transcript.en.vtt"); } else { await writeFile(path.join(d, "transcript.en.vtt.zst"), "not a zstd frame"); } await writeFile(path.join(d, "transcript.en.vtt"), ASR_VTT); const r = await purgeSupersededAutoSubs({ channelSlug: "p", ids: [id], paths: { channelsDir: path.join(ROOT, "channels") } as never, onLog: () => {}, }); assert.equal(r.removedFiles, 0, stored); assert.deepEqual((await readdir(d)).sort(), ["transcript.en.vtt", "transcript.en.vtt.zst", "transcript.json"], stored); } }); test("the purge finds a superseded ASR track stored ONLY compressed, and removes it", async () => { const d = await videoDir("p", "v6"); await writeFile(path.join(d, "transcript.json"), JSON.stringify({ transcription: [{ text: "x" }] })); await writeFile(path.join(d, "transcript.en.vtt"), ASR_VTT); await storeFixture(d, "transcript.en.vtt"); assert.deepEqual((await readdir(d)).sort(), ["transcript.en.vtt.zst", "transcript.json"]); const r = await purgeSupersededAutoSubs({ channelSlug: "p", ids: ["v6"], paths: { channelsDir: path.join(ROOT, "channels") } as never, onLog: () => {}, }); assert.equal(r.removedFiles, 1, "the decoded head sniffs as ASR"); assert.deepEqual(await readdir(d), ["transcript.json"]); }); // REVIEW-1 F5 / REVIEW-2 R2: THE FILES CARD'S DELETE OF ONE NAMED FORM // (`removeStoredTextForm`, what deleteVideoFileAction and `pnpm ops // delete-file` run). A person asked for ONE file: only that one goes, never the // other form — whether they hold the same content or not — and the answer // names the other and says which. (Before REVIEW-2: the same content took // both, so deleting a duplicate `.zst` beside its plain file left zero copies.) const { removeStoredTextForm } = await import("../lib/storedText-write-server"); test("delete one named form: only the named one goes, whatever the other holds", async () => { const ZST = `${CHAT}.zst`; // Same content, either name clicked: the other stays, and is read. for (const clicked of [CHAT, ZST]) { const d = await videoDir("f5same", clicked); await writeFile(path.join(d, CHAT), "SAME\n"); await storeFixture(d, CHAT, { keepPlain: true }); const out = await removeStoredTextForm(d, clicked); const other = clicked === CHAT ? ZST : CHAT; assert.deepEqual(out, { removed: [clicked], kept: { name: other, same: "same" } }); assert.deepEqual(await readdir(d), [other], `${clicked}: only it went`); assert.equal(await readStoredText(d, CHAT, "utf8"), "SAME\n", `${clicked}: readers still see the chat`); } // Different content: the plain one clicked — the stored one is kept, and read. { const d = await videoDir("f5diff", "plain"); await writeFile(path.join(d, CHAT), "OLD WHOLE CAPTURE\n"); await storeFixture(d, CHAT); await writeFile(path.join(d, CHAT), "NEWER, TRIMMED\n"); const out = await removeStoredTextForm(d, CHAT); assert.deepEqual(out, { removed: [CHAT], kept: { name: ZST, same: "differs" } }); assert.deepEqual(await readdir(d), [ZST]); assert.equal(await readStoredText(d, CHAT, "utf8"), "OLD WHOLE CAPTURE\n"); } // Different content: the stored one clicked — the plain one is kept. { const d = await videoDir("f5diff", "stored"); await writeFile(path.join(d, CHAT), "OLD\n"); await storeFixture(d, CHAT); await writeFile(path.join(d, CHAT), "NEW\n"); const out = await removeStoredTextForm(d, ZST); assert.deepEqual(out, { removed: [ZST], kept: { name: CHAT, same: "differs" } }); assert.deepEqual(await readdir(d), [CHAT]); } // One form only: it goes. { const d = await videoDir("f5one", "v"); await writeFile(path.join(d, CHAT), "ONLY\n"); await storeFixture(d, CHAT); assert.deepEqual(await removeStoredTextForm(d, ZST), { removed: [ZST] }); assert.deepEqual(await readdir(d), []); } }); test("delete one named form through tier links: the named form's bytes go, the other form's stay", async () => { const ZST = `${CHAT}.zst`; const d = await videoDir("f5t", "v5"); const media = path.join(ROOT, "channels", "f5t", "media", "v5"); await mkdir(media, { recursive: true }); await writeFile(path.join(media, CHAT), "TIERED\n"); await symlink(path.join("..", "..", "media", "v5", CHAT), path.join(d, CHAT)); await storeFixture(d, CHAT, { keepPlain: true }); assert.deepEqual(await removeStoredTextForm(d, ZST), { removed: [ZST], kept: { name: CHAT, same: "same" } }); assert.deepEqual(await readdir(d), [CHAT]); assert.equal(await exists(path.join(media, ZST)), false, "its bytes went with it"); assert.equal(await readStoredText(d, CHAT, "utf8"), "TIERED\n"); // The plain link's bytes gone (an interrupted removal): clicking the plain // name removes only that link; the stored copy stays, and is read. const e = await videoDir("f5t", "v6"); const media6 = path.join(ROOT, "channels", "f5t", "media", "v6"); await mkdir(media6, { recursive: true }); await writeFile(path.join(media6, CHAT), "ONLY COPY\n"); await symlink(path.join("..", "..", "media", "v6", CHAT), path.join(e, CHAT)); await storeFixture(e, CHAT); await symlink(path.join("..", "..", "media", "v6", CHAT), path.join(e, CHAT)); const out = await removeStoredTextForm(e, CHAT); assert.deepEqual(out, { removed: [CHAT], kept: { name: ZST, same: "unreadable" } }); assert.equal(await readStoredText(e, CHAT, "utf8"), "ONLY COPY\n"); }); // REVIEW-1 N1 (captions lane): "Set as transcript" over a stored `en` writes // the pick as a plain transcript.en.vtt beside it and pins it. Deleting the // plain pick must not leave the pin ranking YouTube's stored `en` first. test("the files card: the plain pick deleted beside a stored en of other content takes the pin with it", async () => { const d = await videoDir("pin", "v1"); await writeFile(path.join(d, "transcript.en.vtt"), "WEBVTT\n\n00:00:00.000 --> 00:00:01.000\nyoutube's en\n"); await storeFixture(d, "transcript.en.vtt"); await writeFile(path.join(d, "transcript.en.vtt"), "WEBVTT\n\n00:00:00.000 --> 00:00:01.000\nthe picked track\n"); await writeFile(path.join(d, "transcript-pin.json"), JSON.stringify({ from: "transcript.en-US.vtt", pinnedAt: "2026-11-21T00:00:00.000Z" })); const out = await removeStoredTextForm(d, "transcript.en.vtt"); assert.notEqual(out, "busy"); assert.equal((out as { unpinned?: boolean }).unpinned, true); assert.deepEqual((await readdir(d)).sort(), ["transcript.en.vtt.zst"]); // The same content beside it: nothing was picked over it — the pin stays. const e = await videoDir("pin", "v2"); await writeFile(path.join(e, "transcript.en.vtt"), "WEBVTT\n\nsame\n"); await storeFixture(e, "transcript.en.vtt", { keepPlain: true }); await writeFile(path.join(e, "transcript-pin.json"), JSON.stringify({ from: "transcript.en.vtt", pinnedAt: "x" })); const same = await removeStoredTextForm(e, "transcript.en.vtt"); assert.equal((same as { unpinned?: boolean }).unpinned, undefined); assert.ok((await readdir(e)).includes("transcript-pin.json")); });