import { test } from "node:test"; import assert from "node:assert/strict"; import { DEFAULT_VOICE_SETTINGS, resolveVoiceHome, sanitizeVoice } from "./voiceSettings"; import { NO_CLONING, VOICE_ENGINES, describeVoicePitch, listVoiceEngines, parseStockTarget, resolveVoiceParams, voiceEngineIds } from "./voiceApps"; // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/voiceSettings.test.ts test("settings.voice: defaults, and every field falls back rather than throws", () => { assert.deepEqual(sanitizeVoice(undefined), DEFAULT_VOICE_SETTINGS); assert.deepEqual(sanitizeVoice("nonsense"), DEFAULT_VOICE_SETTINGS); const s = sanitizeVoice({ home: "relative/dir", threads: 99, enhance: { engine: "seed-vc" }, convert: { engine: "rnnoise", target: "/home/me/voice.wav" }, heavyEstimateMB: { "seed-vc": 4200.4, openvoice: -5, nope: 7 }, }); assert.equal(s.home, ""); assert.equal(s.threads, 16); // An engine of the other op is not this op's engine. assert.equal(s.enhance.engine, "deepfilternet"); assert.equal(s.convert.engine, "openvoice"); // A recording is never a target, even in settings. assert.equal(s.convert.target, "orpheus:zac"); assert.equal(s.heavyEstimateMB["seed-vc"], 4200); assert.equal(s.heavyEstimateMB.openvoice, 0); assert.equal("nope" in s.heavyEstimateMB, false); // Every engine, and the f0 run of the one that can follow a pitch. assert.deepEqual(Object.keys(s.heavyEstimateMB).sort(), [...Object.keys(VOICE_ENGINES), "seed-vc-f0"].sort()); assert.equal(s.heavyEstimateMB["seed-vc-f0"], VOICE_ENGINES["seed-vc"].pitch!.defaultEstimateMB); assert.equal(sanitizeVoice({ heavyEstimateMB: { "seed-vc-f0": 5000 } }).heavyEstimateMB["seed-vc-f0"], 5000); // The pitch a conversion lands on: the target's own unless said, the semitones clamped. assert.deepEqual([s.convert.pitch, s.convert.semitones], ["target", 0]); const p = sanitizeVoice({ convert: { pitch: "contour", semitones: 40 } }).convert; assert.deepEqual([p.pitch, p.semitones], ["contour", 12]); const q = sanitizeVoice({ convert: { pitch: "higher", semitones: "2" } }).convert; assert.deepEqual([q.pitch, q.semitones], ["target", 0]); const ok = sanitizeVoice({ home: "~/voice", convert: { engine: "seed-vc", target: "kokoro:bm_george" }, enhance: { engine: "rnnoise" } }); assert.equal(ok.convert.engine, "seed-vc"); assert.equal(ok.convert.target, "kokoro:bm_george"); assert.equal(ok.enhance.engine, "rnnoise"); assert.match(resolveVoiceHome(ok, { voiceHome: "/x" }), /\/voice$/); assert.equal(resolveVoiceHome(DEFAULT_VOICE_SETTINGS, { voiceHome: "/x" }), "/x"); }); test("stock targets: our TTS engines' stock voices, nothing else", () => { const ok = parseStockTarget(" orpheus:leah "); assert.ok(ok.ok); if (ok.ok) assert.deepEqual(ok.value, { id: "orpheus:leah", ttsEngine: "orpheus", voice: "leah" }); // A bare stock voice one engine has is that engine's (umtool records a take's voice so). const bare = parseStockTarget("zac"); assert.ok(bare.ok); if (bare.ok) assert.equal(bare.value.id, "orpheus:zac"); const kb = parseStockTarget("am_michael"); assert.ok(kb.ok && kb.value.id === "kokoro:am_michael"); for (const bad of ["nobody", "orpheus:", ":zac", "piper:amy", "a.wav", "./zac", "/zac", "http://x/zac", 5, null]) { assert.equal(parseStockTarget(bad).ok, false, String(bad)); } assert.match(NO_CLONING, /no real person's\s+voice is ever a target/); }); test("the registry: two engines per op, every knob has a default in range", () => { assert.deepEqual(voiceEngineIds("enhance"), ["deepfilternet", "rnnoise"]); assert.deepEqual(voiceEngineIds("convert"), ["openvoice", "seed-vc"]); for (const e of Object.values(VOICE_ENGINES)) { const r = resolveVoiceParams(e, undefined); assert.ok(r.ok, e.id); for (const [k, spec] of Object.entries(e.params)) { assert.ok(spec.doc.length > 10, `${e.id}.${k} has a doc`); if (spec.kind === "number") assert.ok(spec.default >= spec.min && spec.default <= spec.max, `${e.id}.${k}`); } assert.ok(e.defaultEstimateMB > 0, e.id); if (e.setup.kind !== "bundled") for (const f of e.setup.files) assert.match(f.sha256, /^[0-9a-f]{64}$/, f.name); // An optional group: pinned by revision, size and sha256 like the rest, and no name of the required set. if (e.setup.kind === "python-venv") { const required = new Set(e.setup.files.map((f) => f.name)); for (const g of e.setup.optional ?? []) { for (const f of g.files) { assert.match(f.sha256, /^[0-9a-f]{64}$/, f.name); assert.match(f.url, /\/resolve\/[0-9a-f]{40}\//, f.name); assert.ok(f.bytes > 0 && !required.has(f.name), f.name); } } if (e.pitch) assert.ok(e.setup.optional?.some((g) => g.id === e.pitch!.group), e.id); } assert.equal(Boolean(e.pitch), e.id === "seed-vc", e.id); } assert.equal(describeVoicePitch({ pitch: "target", semitones: 0 }), ""); assert.equal(describeVoicePitch({ pitch: "source", semitones: 0 }), "source"); assert.equal(describeVoicePitch({ pitch: "contour", semitones: 2 }), "contour +2 st"); assert.equal(describeVoicePitch({ pitch: "source", semitones: -1.5 }), "source −1.5 st"); assert.equal(listVoiceEngines().length, 4); assert.equal(resolveVoiceParams(VOICE_ENGINES.rnnoise, []).ok, false); });