import { test } from "node:test"; import assert from "node:assert/strict"; import { lstat, mkdir, mkdtemp, readFile, rm, stat, symlink, utimes, 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 lib/storedText.test.ts // // STORED TEXT (lane text-compress): the names, the classifier, and the one // read path that accepts either form. Temp dirs, synthetic names. import { STORED_TEXT_NAMES, isStoredCaptionName, isStoredTextName, listingHas, logicalEntries, plainNameOf, storedFormsIn, storedNameOf, } from "./storedText"; import { decodeStoredBytes, encodeStoredBytes, frameContentSize, readStoredText, readdirLogical, statStoredText, storedTextExists, storedTextForms, storedTextLogicalSize, storedTextMtimeMs, StoredTextCorruptError, } from "./storedText-server"; import { classifyEntry, isScratchEntry, isTierable, LIVE_CHAT_STORED_FILENAME } from "./mediaTier"; import { LIVE_CHAT_FILENAME, isTranscriptVtt } from "./videoStatus"; const ROOT = await mkdtemp(path.join(tmpdir(), "stored-text-")); test.after(() => rm(ROOT, { recursive: true, force: true })); const CHAT = LIVE_CHAT_FILENAME; const BODY = '{"replayChatItemAction":{"actions":[]}}\n'.repeat(500); async function dir(name: string): Promise { const d = path.join(ROOT, name); await mkdir(d, { recursive: true }); return d; } test("the declared names: the live chat and every caption track, each with its stored form `.zst`", () => { assert.deepEqual([...STORED_TEXT_NAMES], [CHAT]); assert.equal(isStoredTextName(CHAT), true); // Every caption track (lane text-compress-captions), any language code. for (const c of ["transcript.en.vtt", "transcript.en-orig.vtt", "transcript.en-US.vtt", "transcript.es.vtt", "transcript.en-en-GB.vtt"]) { assert.equal(isStoredTextName(c), true, c); assert.equal(isStoredCaptionName(c), true, c); assert.equal(isTranscriptVtt(c), true, `${c}: the same pattern as videoStatus`); assert.equal(plainNameOf(`${c}.zst`), c); } // Not declared: the metadata, the whisper transcript, the cues, a // transcriber's temp, a `.part`, another track format. for (const n of ["metadata.info.json", "transcript.json", "transcript.cues.json", "transcript.tmp-4242.vtt", "transcript.en.vtt.part", "transcript.en.srv3", "live_chat.cues.json"]) { assert.equal(isStoredTextName(n), false, n); assert.equal(plainNameOf(`${n}.zst`), null, n); } assert.equal(isStoredCaptionName(CHAT), false); assert.equal(storedNameOf(CHAT), "transcript.live_chat.json.zst"); assert.equal(plainNameOf("transcript.live_chat.json.zst"), CHAT); assert.equal(plainNameOf(CHAT), null); assert.equal(LIVE_CHAT_STORED_FILENAME, storedNameOf(CHAT)); }); test("the classifier: a stored caption is TEXT, never tiered; its compressor's temps and set-aside are scratch", () => { assert.equal(classifyEntry("transcript.en.vtt.zst"), "text"); assert.equal(isTierable("transcript.en.vtt.zst"), false); assert.equal(isScratchEntry(".transcript.en.vtt.zst.compressing-4242-abc123"), true); assert.equal(isScratchEntry(".transcript.en-orig.vtt.retire-4242-abc123"), true); assert.equal(isScratchEntry(".transcript.es.vtt.restoring-4242-abc123"), true); assert.equal(isTierable(".transcript.en.vtt.zst.compressing-4242"), false); }); test("a logical listing reports the stored chat under its plain name, once", () => { assert.deepEqual( logicalEntries(["metadata.info.json", "transcript.live_chat.json.zst", CHAT, "x.zst"]), ["metadata.info.json", CHAT, "x.zst"], ); assert.equal(listingHas(["transcript.live_chat.json.zst"], CHAT), true); assert.equal(listingHas(["transcript.en.vtt.zst"], "transcript.en.vtt"), true); assert.equal(listingHas(["metadata.info.json.zst"], "metadata.info.json"), false); assert.deepEqual( logicalEntries(["transcript.en-orig.vtt.zst", "transcript.en.vtt", "transcript.en.vtt.zst", "metadata.info.json"]), ["transcript.en-orig.vtt", "transcript.en.vtt", "metadata.info.json"], ); assert.equal(storedFormsIn([CHAT], CHAT), "plain"); assert.equal(storedFormsIn(["transcript.live_chat.json.zst"], CHAT), "zst"); assert.equal(storedFormsIn([CHAT, "transcript.live_chat.json.zst"], CHAT), "both"); assert.equal(storedFormsIn([], CHAT), null); }); test("the classifier: the stored chat is media and tierable; the compressor's temps are scratch", () => { assert.equal(classifyEntry("transcript.live_chat.json.zst"), "media"); assert.equal(isTierable("transcript.live_chat.json.zst"), true); assert.equal(classifyEntry(".transcript.live_chat.json.zst.compressing-4242"), "scratch"); assert.equal(isScratchEntry(".transcript.live_chat.json.retire-4242"), true); assert.equal(isTierable(".transcript.live_chat.json.zst.compressing-4242"), false); // Not a declared stored form: text, as before. assert.equal(classifyEntry("transcript.en.vtt.zst"), "text"); }); test("a frame carries its content size; the reader decodes and checks it", async () => { const bytes = await encodeStoredBytes(Buffer.from(BODY)); assert.equal(frameContentSize(bytes.subarray(0, 18)), Buffer.byteLength(BODY)); assert.equal((await decodeStoredBytes(bytes)).toString(), BODY); assert.equal(frameContentSize(Buffer.from("not a frame at all")), null); }); test("a TRUNCATED or corrupt stored file throws — it never reads as shorter text", async () => { const bytes = await encodeStoredBytes(Buffer.from(BODY)); await assert.rejects(decodeStoredBytes(bytes.subarray(0, bytes.length - 12)), StoredTextCorruptError); for (const off of [10, Math.floor(bytes.length / 2), bytes.length - 2]) { const bad = Buffer.from(bytes); bad[off] ^= 0xff; await assert.rejects(decodeStoredBytes(bad), StoredTextCorruptError, `flipped byte ${off}`); } await assert.rejects(decodeStoredBytes(Buffer.from("plain text")), StoredTextCorruptError); }); test("readStoredText: plain first, else the stored form, else ENOENT", async () => { const d = await dir("read"); await assert.rejects(readStoredText(d, CHAT, "utf8"), { code: "ENOENT" }); await writeFile(path.join(d, storedNameOf(CHAT)), await encodeStoredBytes(Buffer.from(BODY))); assert.equal(await readStoredText(d, CHAT, "utf8"), BODY); assert.ok((await readStoredText(d, CHAT)).equals(Buffer.from(BODY))); // Both: the plain one is the truth. await writeFile(path.join(d, CHAT), "NEWER\n"); assert.equal(await readStoredText(d, CHAT, "utf8"), "NEWER\n"); // A name that is not declared never reads a `.zst`. await writeFile(path.join(d, "other.json.zst"), await encodeStoredBytes(Buffer.from("x"))); await assert.rejects(readStoredText(d, "other.json", "utf8"), { code: "ENOENT" }); }); test("a dangling plain link (an interrupted removal) reads the stored form", async () => { const d = await dir("dangling"); await symlink("../nowhere/transcript.live_chat.json", path.join(d, CHAT)); await writeFile(path.join(d, storedNameOf(CHAT)), await encodeStoredBytes(Buffer.from(BODY))); assert.equal(await readStoredText(d, CHAT, "utf8"), BODY); // A listing (lstat) still has the plain name; a stat falls through. assert.equal((await statStoredText(d, CHAT, { link: true })).form, "plain"); assert.equal((await statStoredText(d, CHAT)).form, "zst"); }); test("statStoredText answers the plain file's mtimeMs from the stored form, exactly", async () => { const d = await dir("mtime"); const plain = path.join(d, CHAT); await writeFile(plain, BODY); // A nanosecond-precision time, as ext4 keeps them. await utimes(plain, 1763700000.123456, 1763700000.987654); const before = (await lstat(plain)).mtimeMs; const { storeFixture } = await import("../testing/storedTextFixture"); await storeFixture(d, CHAT); assert.equal(await storedTextMtimeMs(d, CHAT, { link: true }), before); assert.equal(await storedTextMtimeMs(d, CHAT), before); assert.equal(await storedTextMtimeMs(d, "transcript.de.vtt"), null); }); test("existence, forms, logical size and the logical readdir", async () => { const d = await dir("exists"); assert.equal(await storedTextExists(d, CHAT), false); assert.equal(await storedTextForms(d, CHAT), null); await writeFile(path.join(d, storedNameOf(CHAT)), await encodeStoredBytes(Buffer.from(BODY))); await writeFile(path.join(d, "metadata.info.json"), "{}"); assert.equal(await storedTextExists(d, CHAT), true); assert.equal(await storedTextForms(d, CHAT), "zst"); assert.equal(await storedTextLogicalSize(d, CHAT), Buffer.byteLength(BODY)); assert.ok((await stat(path.join(d, storedNameOf(CHAT)))).size < Buffer.byteLength(BODY) / 5); assert.deepEqual((await readdirLogical(d)).sort(), ["metadata.info.json", CHAT]); await writeFile(path.join(d, CHAT), BODY); assert.equal(await storedTextForms(d, CHAT), "both"); assert.equal(await storedTextLogicalSize(d, CHAT), Buffer.byteLength(BODY)); assert.deepEqual(await readdirLogical(path.join(d, "missing")), []); assert.equal((await readFile(path.join(d, CHAT), "utf8")).length, BODY.length); });