import { test } from "node:test"; import assert from "node:assert/strict"; import os from "node:os"; import path from "node:path"; import { DEFAULT_ORPHEUS_TTS_SETTINGS, DEFAULT_TTS_SETTINGS, resolveTtsCacheDir, resolveTtsHome, sanitizeTts } from "./ttsSettings"; import { defaults } from "./settingsSchema"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/ttsSettings.test.ts test("the default block: kokoro, af_heart, natural pace, blank dirs", () => { assert.deepEqual(defaults().tts, { engine: "kokoro", voice: "af_heart", speed: 1, home: "", dir: "", orpheus: DEFAULT_ORPHEUS_TTS_SETTINGS, }); assert.deepEqual(sanitizeTts(undefined), DEFAULT_TTS_SETTINGS); assert.deepEqual(sanitizeTts("nonsense"), DEFAULT_TTS_SETTINGS); }); test("an unknown engine or a voice the engine lacks falls back; speed is clamped", () => { assert.equal(sanitizeTts({ engine: "piper" }).engine, "kokoro"); assert.deepEqual(sanitizeTts({ engine: "espeak-ng" }), { ...DEFAULT_TTS_SETTINGS, engine: "espeak-ng", voice: "en-us" }); assert.equal(sanitizeTts({ engine: "espeak-ng", voice: "en-gb" }).voice, "en-gb"); assert.equal(sanitizeTts({ voice: "bm_george" }).voice, "bm_george"); assert.equal(sanitizeTts({ voice: "someone_real" }).voice, "af_heart"); assert.equal(sanitizeTts({ speed: 0.1 }).speed, 0.5); assert.equal(sanitizeTts({ speed: 9 }).speed, 2); assert.equal(sanitizeTts({ speed: 0.904 }).speed, 0.9); assert.equal(sanitizeTts({ speed: "fast" }).speed, 1); }); test("home and dir: blank is the Paths default, ~/ expands, a relative path is dropped", () => { const paths = { ttsHome: "/d/home", ttsCacheDir: "/d/cache" }; assert.equal(resolveTtsHome(sanitizeTts({}), paths), "/d/home"); assert.equal(resolveTtsCacheDir(sanitizeTts({}), paths), "/d/cache"); assert.equal(resolveTtsHome(sanitizeTts({ home: "/opt/tts" }), paths), "/opt/tts"); assert.equal(resolveTtsCacheDir(sanitizeTts({ dir: "~/tts-cache" }), paths), path.join(os.homedir(), "tts-cache")); assert.equal(sanitizeTts({ dir: "relative/cache" }).dir, ""); assert.equal(sanitizeTts({ home: 7 }).home, ""); }); test("orpheus: zac by default, speed held at 1, its server block sanitized", () => { const o = sanitizeTts({ engine: "orpheus", speed: 0.8 }); assert.equal(o.voice, "zac"); assert.equal(o.speed, 1); assert.equal(sanitizeTts({ engine: "orpheus", voice: "leah" }).voice, "leah"); assert.deepEqual(DEFAULT_ORPHEUS_TTS_SETTINGS, { serverBin: "/usr/lib/ollama/llama-server", libraryPath: "/usr/lib/ollama", ggmlBackendPath: "/usr/lib/ollama/vulkan/libggml-vulkan.so", port: 8799, gpuLayers: 99, contextSize: 8192, startTimeoutSeconds: 150, idleStopSeconds: 5, heavyEstimateMB: 4000, takes: 3, tagCheck: false, }); const s = sanitizeTts({ orpheus: { serverBin: "relative/llama", libraryPath: "", port: 80, takes: 99, tagCheck: "yes", gpuLayers: 12.4 }, }).orpheus; assert.equal(s.serverBin, "/usr/lib/ollama/llama-server"); assert.equal(s.libraryPath, ""); assert.equal(s.port, 1024); assert.equal(s.takes, 6); assert.equal(s.tagCheck, false); assert.equal(s.gpuLayers, 12); });