import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, 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-read.test.ts // // THE PLAIN-JS TWIN (lib/storedText-read.mjs, for umtool's bare-node scripts) // AND THE TS READ PATH (lib/storedText.ts, lib/storedText-server.ts) GIVE THE // SAME ANSWERS: which names are declared, the logical listing, the decoded // bytes and head, the logical size, and a truncated frame refused. Change one, // change the other. Temp dirs, synthetic names. import * as ts from "./storedText"; import * as tsServer from "./storedText-server"; import * as mjs from "./storedText-read.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "stored-read-twin-")); test.after(() => rm(ROOT, { recursive: true, force: true })); const NAMES = [ "transcript.live_chat.json", "transcript.en.vtt", "transcript.en-orig.vtt", "transcript.es.vtt", "transcript.tmp-77.vtt", "transcript.json", "transcript.cues.json", "metadata.info.json", "transcript.en.vtt.part", "transcript.en.srv3", ]; test("the declared names and the logical listing are the same in both", () => { for (const n of NAMES) { assert.equal(mjs.isStoredTextName(n), ts.isStoredTextName(n), n); assert.equal(mjs.plainNameOf(`${n}.zst`), ts.plainNameOf(`${n}.zst`), n); } const listing = [...NAMES, ...NAMES.map((n) => `${n}.zst`), "x.zst"]; assert.deepEqual(mjs.logicalEntries(listing), ts.logicalEntries(listing)); }); test("the read, the head, the stat and the logical size are the same in both", async () => { const dir = path.join(ROOT, "v1"); await mkdir(dir, { recursive: true }); const body = "WEBVTT\n\n" + "00:00:00.000 --> 00:00:01.000\nline é\n\n".repeat(400); await writeFile(path.join(dir, "transcript.en.vtt.zst"), await tsServer.encodeStoredBytes(Buffer.from(body))); await writeFile(path.join(dir, "transcript.es.vtt"), body); for (const n of ["transcript.en.vtt", "transcript.es.vtt"]) { assert.equal(await mjs.readStoredText(dir, n, "utf8"), await tsServer.readStoredText(dir, n, "utf8"), n); assert.deepEqual(await mjs.readStoredTextHead(dir, n, 2048), await tsServer.readStoredTextHead(dir, n, 2048), n); assert.equal(await mjs.storedTextLogicalSize(dir, n), await tsServer.storedTextLogicalSize(dir, n), n); assert.equal((await mjs.statStoredText(dir, n)).stats.mtimeMs, (await tsServer.statStoredText(dir, n)).stats.mtimeMs, n); } assert.equal(await mjs.readStoredText(dir, "transcript.en.vtt", "utf8"), body); assert.deepEqual(await mjs.readdirLogical(dir), await tsServer.readdirLogical(dir)); await assert.rejects(mjs.readStoredText(dir, "transcript.de.vtt"), { code: "ENOENT" }); }); test("a truncated stored frame is refused by both, never read short", async () => { const dir = path.join(ROOT, "v2"); await mkdir(dir, { recursive: true }); const whole = await tsServer.encodeStoredBytes(Buffer.from("WEBVTT\n\n".repeat(5000))); await writeFile(path.join(dir, "transcript.en.vtt.zst"), whole.subarray(0, Math.floor(whole.length / 2))); await assert.rejects(mjs.readStoredText(dir, "transcript.en.vtt"), /not a whole stored-text frame/); await assert.rejects(tsServer.readStoredText(dir, "transcript.en.vtt"), /not a whole stored-text frame/); });