import { test, after } from "node:test"; import assert from "node:assert/strict"; import { createHash } from "node:crypto"; import { mkdtemp, mkdir, readdir, rm, stat, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { adoptModelFile, downloadModelFile, main, verifyModelFile } from "./tts-setup"; import type { TtsModelFile } from "../lib/ttsApps"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test bin/tts-setup.test.ts // // `archilyzer tts setup`'s pieces over a tiny synthetic "model": verification // by size and sha256, the one-stream download (a fake fetch — nothing here // touches the network), `--from` adoption, and `--check` over an empty home. // The real install (a venv, kokoro-onnx, 350 MB of model) is run by hand. const ROOT = await mkdtemp(path.join(os.tmpdir(), "tts-setup-")); after(() => rm(ROOT, { recursive: true, force: true })); const BYTES = Buffer.from("not a real model, ".repeat(64)); const SPEC: TtsModelFile = { name: "tiny.onnx", url: "https://example.invalid/tiny.onnx", bytes: BYTES.length, sha256: createHash("sha256").update(BYTES).digest("hex"), }; function fakeFetch(body: Buffer, status = 200): typeof fetch { const calls: string[] = []; const f = (async (url: string | URL | Request) => { calls.push(String(url)); return new Response(status === 200 ? new Uint8Array(body) : null, { status }); }) as unknown as typeof fetch; return Object.assign(f, { calls }); } test("a file verifies by size, then sha256", async () => { const dir = await mkdtemp(path.join(ROOT, "verify-")); const f = path.join(dir, SPEC.name); assert.equal(await verifyModelFile(f, SPEC), "absent"); await writeFile(f, BYTES.subarray(1)); assert.match(await verifyModelFile(f, SPEC), /bytes, expected/); const bad = Buffer.from(BYTES); bad[0] ^= 1; await writeFile(f, bad); assert.match(await verifyModelFile(f, SPEC), /^sha256 .* expected/); await writeFile(f, BYTES); assert.equal(await verifyModelFile(f, SPEC), "ok"); }); test("a download is one stream, verified before it is moved into place", async () => { const dir = await mkdtemp(path.join(ROOT, "dl-")); const target = path.join(dir, SPEC.name); const lines: string[] = []; const ok = fakeFetch(BYTES); await downloadModelFile(SPEC, target, (l) => lines.push(l), ok); assert.equal(await verifyModelFile(target, SPEC), "ok"); assert.deepEqual((ok as unknown as { calls: string[] }).calls, [SPEC.url]); assert.deepEqual(await readdir(dir), [SPEC.name]); assert.ok(lines.some((l) => /100%/.test(l)), lines.join("\n")); // A body that does not verify is not kept, and is not retried. const corrupt = Buffer.from(BYTES); corrupt[3] ^= 1; const other = path.join(dir, "other.onnx"); const bad = fakeFetch(corrupt); await assert.rejects(downloadModelFile(SPEC, other, () => {}, bad), /does not verify/); assert.equal((bad as unknown as { calls: string[] }).calls.length, 1); assert.deepEqual(await readdir(dir), [SPEC.name]); // A server error is an error, once. await assert.rejects(downloadModelFile(SPEC, other, () => {}, fakeFetch(BYTES, 503)), /answered 503/); }); test("--from adopts a verified file (a hard link), and refuses one that is not", async () => { const src = await mkdtemp(path.join(ROOT, "from-")); const dst = await mkdtemp(path.join(ROOT, "to-")); await writeFile(path.join(src, SPEC.name), BYTES); const lines: string[] = []; assert.equal(await adoptModelFile(path.join(src, SPEC.name), path.join(dst, SPEC.name), SPEC, (l) => lines.push(l)), true); const a = await stat(path.join(src, SPEC.name)); const b = await stat(path.join(dst, SPEC.name)); assert.equal(a.ino, b.ino, "a hard link on the same filesystem"); assert.match(lines.join("\n"), /linked tiny\.onnx/); await writeFile(path.join(src, "short.onnx"), BYTES.subarray(2)); assert.equal( await adoptModelFile(path.join(src, "short.onnx"), path.join(dst, "short.onnx"), SPEC, () => {}), false, ); assert.deepEqual(await readdir(dst), [SPEC.name]); }); test("--check over an empty home lists what is missing, installs nothing, exits 1", async () => { const home = path.join(ROOT, "empty-home"); await mkdir(home); const lines: string[] = []; const code = await main({ check: true, home, log: (l) => lines.push(l) }); assert.equal(code, 1); const text = lines.join("\n"); assert.match(text, /Kokoro-82M at .*empty-home\/kokoro — missing:/); assert.match(text, /- the python venv/); assert.match(text, /- kokoro-v1\.0\.onnx/); assert.match(text, /- voices-v1\.0\.bin/); assert.match(text, /Run `archilyzer tts setup`/); assert.deepEqual(await readdir(home), []); }); test("an unknown engine is refused; espeak-ng has nothing to install", async () => { const lines: string[] = []; assert.equal(await main({ engine: "piper", log: (l) => lines.push(l) }), 2); assert.match(lines[0], /no engine "piper" — known: kokoro, espeak-ng/); const missing: string[] = []; assert.equal(await main({ engine: "espeak-ng", envPath: path.join(ROOT, "no-bin"), log: (l) => missing.push(l) }), 1); assert.match(missing[0], /espeak-ng is not on PATH — install espeak-ng/); }); test("--check for orpheus names its files and the engine in its hint", async () => { const home = path.join(ROOT, "empty-orpheus"); await mkdir(home); const lines: string[] = []; const code = await main({ engine: "orpheus", check: true, home, log: (l) => lines.push(l), envPath: path.join(ROOT, "no-bin") }); assert.equal(code, 1); const text = lines.join("\n"); assert.match(text, /Orpheus 3B at .*empty-orpheus\/orpheus — missing:/); assert.match(text, /- orpheus-3b-0\.1-ft-Q8_0\.gguf/); assert.match(text, /- snac_decoder\.onnx/); assert.match(text, /Run `archilyzer tts setup --engine orpheus`/); assert.deepEqual(await readdir(home), []); });