import { test, after } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, mkdir, open, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { main, voiceSetupPlan } from "./voice-setup"; import { SEEDVC_F0_FILES, SEEDVC_FILES, VOICE_ENGINES } from "../lib/voiceApps"; import { VOICE_CODE_MARKER, voiceGroupState, voiceInstallState } from "../lib/voiceInstall"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test bin/voice-setup.test.ts // // `archilyzer voice setup`'s plan (what it says before it fetches), --dry-run // (which fetches nothing), --check over an empty home, and the engine id being // required. The pieces it shares with tts setup (verify, download, --from) are // tts-setup.test.ts's; the real installs are run by hand. const ROOT = await mkdtemp(path.join(os.tmpdir(), "voice-setup-")); after(() => rm(ROOT, { recursive: true, force: true })); const SETTINGS = path.join(ROOT, "settings.json"); await writeFile(SETTINGS, JSON.stringify({ workers: [] })); process.env.SETTINGS_FILE = SETTINGS; process.env.TRANSCRIPTS_DIR = path.join(ROOT, "transcripts"); await mkdir(process.env.TRANSCRIPTS_DIR, { recursive: true }); function capture() { const lines: string[] = []; return { lines, log: (l: string) => lines.push(l) }; } test("the plan names every file, its size and its source, and the total", () => { const plan = voiceSetupPlan(VOICE_ENGINES["seed-vc"]); assert.ok(plan.some((l) => /a python 3\.12 venv \(uv\), with: torch==2\.4\.0\+cpu/.test(l))); assert.ok(plan.some((l) => /the code: https:\/\/github\.com\/Plachtaa\/seed-vc at 51383efd/.test(l))); assert.ok(plan.some((l) => /whisper-small\/model\.safetensors — 967\.0 MB from https:\/\/huggingface\.co\/openai\/whisper-small\/resolve\/973afd24/.test(l))); // The optional f0 group is listed after the total, not fetched or counted… const total = plan.findIndex((l) => /^ {2}files: /.test(l)); assert.match(plan[total], /files: 1\.88 GB/); assert.match(plan[total + 1], /optional, not fetched without `--with f0` — the f0-conditioned 44\.1 kHz model/); assert.ok(plan.slice(total).some((l) => /rmvpe\.pt — 181\.2 MB from https:\/\/huggingface\.co\/lj1995\/VoiceConversionWebUI\/resolve\/e6d0c1a1/.test(l))); assert.match(plan.at(-1)!, /1\.49 GB more/); // …and with --with f0 it is part of what is fetched. const withF0 = voiceSetupPlan(VOICE_ENGINES["seed-vc"], ["f0"]); assert.ok(withF0.some((l) => /bigvgan-44k\/bigvgan_generator\.pt — 489\.0 MB from https:\/\/huggingface\.co\/nvidia\/bigvgan_v2_44khz_128band_512x\/resolve\/95a9d1dc/.test(l))); assert.match(withF0.at(-1)!, /files: 3\.38 GB/); assert.match(voiceSetupPlan(VOICE_ENGINES.deepfilternet).at(-1)!, /files: 36\.4 MB$/); assert.match(voiceSetupPlan(VOICE_ENGINES.rnnoise)[0], /nothing to download/); }); test("--dry-run says what it would fetch and fetches nothing", async () => { const c = capture(); const fetchNever = (async () => { throw new Error("fetched"); }) as unknown as typeof fetch; const code = await main({ engine: "openvoice", dryRun: true, home: path.join(ROOT, "home"), log: c.log, fetch: fetchNever, run: async () => { throw new Error("ran"); } }); assert.equal(code, 0); assert.match(c.lines[0], /^Would install OpenVoice v2 \(tone-colour converter\) \(MIT\) at .*home\/openvoice:$/); assert.ok(c.lines.some((l) => /converter\/checkpoint\.pth — 131\.3 MB/.test(l))); }); test("--check over an empty home lists what is missing and exits 1", async () => { const c = capture(); const code = await main({ engine: "deepfilternet", check: true, home: path.join(ROOT, "empty"), log: c.log }); assert.equal(code, 1); assert.match(c.lines.join("\n"), /DeepFilterNet 3 at .*empty\/deepfilternet — missing:\n {2}- deep-filter\nRun `archilyzer voice setup --engine deepfilternet`/); }); async function sparse(dir: string, files: readonly { name: string; bytes: number }[]) { for (const f of files) { const p = path.join(dir, f.name); await mkdir(path.dirname(p), { recursive: true }); const fh = await open(p, "w"); await fh.truncate(f.bytes); await fh.close(); } } test("seed-vc's f0 files are optional: an install without them checks out, --with f0 counts them", async () => { const home = path.join(ROOT, "seed-home"); const dir = path.join(home, "seed-vc"); const seed = VOICE_ENGINES["seed-vc"]; assert.ok(seed.setup.kind === "python-venv"); if (seed.setup.kind !== "python-venv") return; await mkdir(path.join(dir, "venv", "bin"), { recursive: true }); await writeFile(path.join(dir, "venv", "bin", "python"), ""); await mkdir(path.join(dir, "code"), { recursive: true }); await writeFile(path.join(dir, "code", VOICE_CODE_MARKER), `${seed.setup.code.commit}\n`); await sparse(dir, SEEDVC_FILES); // The install made before the group existed is installed; the group is said. const state = voiceInstallState(seed, { home, voiceModelsDir: ROOT }); assert.equal(state.installed, true); assert.deepEqual(state.optional.map((g) => [g.id, g.installed, g.missing.length]), [["f0", false, SEEDVC_F0_FILES.length]]); // --check (the venv here is a stub, so the import fails: read the lines about the group). const plain = capture(); await main({ engine: "seed-vc", check: true, home, log: plain.log }); assert.ok(plain.lines.some((l) => /optional f0 \(.*follows the input's pitch.*\): not installed — `archilyzer voice setup --engine seed-vc --with f0` fetches it/.test(l)), plain.lines.join("\n")); assert.equal(plain.lines.some((l) => /- rmvpe\.pt/.test(l)), false); const asked = capture(); assert.equal(await main({ engine: "seed-vc", check: true, with: ["f0"], home, log: asked.log }), 1); assert.ok(asked.lines.some((l) => /^ {2}- rmvpe\.pt$/.test(l))); assert.match(asked.lines.at(-1)!, /Run `archilyzer voice setup --engine seed-vc --with f0`/); // There: said as installed. await sparse(dir, SEEDVC_F0_FILES); assert.equal(voiceGroupState(seed, "f0", { home })!.installed, true); const there = capture(); await main({ engine: "seed-vc", check: true, with: ["f0"], home, log: there.log }); assert.ok(there.lines.some((l) => /with f0 \(.*\): installed/.test(l))); assert.equal(there.lines.some((l) => /rmvpe/.test(l)), false); // A group the engine does not have. const no = capture(); assert.equal(await main({ engine: "seed-vc", with: ["f1"], dryRun: true, home, log: no.log }), 2); assert.match(no.lines[0], /seed-vc has no optional group "f1" — --with is one of f0/); assert.equal(await main({ engine: "openvoice", with: ["f0"], dryRun: true, home, log: no.log }), 2); // --dry-run --with f0 lists them as fetched. const dry = capture(); assert.equal(await main({ engine: "seed-vc", with: ["f0"], dryRun: true, home, log: dry.log }), 0); assert.ok(dry.lines.some((l) => /with f0 — the f0-conditioned/.test(l))); }); test("an engine is required, by its id", async () => { const c = capture(); assert.equal(await main({ log: c.log }), 2); assert.match(c.lines[0], /--engine is one of deepfilternet, rnnoise, openvoice, seed-vc/); assert.equal(await main({ engine: "kokoro", log: c.log }), 2); });