// scripts/tts.mjs — the text-to-speech wrapper, run for real with espeak-ng // (the zero-install engine) and ffmpeg, and with a failing fake engine. // // Run with: node --test scripts/tts.test.mjs (part of `pnpm run test:scripts`) // // Kokoro is not run here: it needs `archilyzer tts setup`'s venv and ~350 MB // of model. Its command line is held by common/lib/ttsApps.test.ts. import { test, after } from "node:test"; import assert from "node:assert/strict"; import { execFileSync, spawnSync } from "node:child_process"; import { chmodSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; const WRAPPER = path.join(import.meta.dirname, "tts.mjs"); const TMP = mkdtempSync(path.join(os.tmpdir(), "tts-wrapper-")); after(() => rmSync(TMP, { recursive: true, force: true })); function has(bin, flag) { try { execFileSync(bin, [flag], { stdio: "ignore" }); return true; } catch { return false; } } const real = has("espeak-ng", "--version") && has("ffmpeg", "-version"); function run(args) { const r = spawnSync(process.execPath, [WRAPPER, ...args], { encoding: "utf8" }); return { code: r.status, stdout: r.stdout, stderr: r.stderr }; } test("espeak-ng: text in, a 48 kHz mono 16-bit WAV with a bare header out", { skip: !real && "espeak-ng or ffmpeg absent" }, () => { const dir = mkdtempSync(path.join(TMP, "ok-")); const text = path.join(dir, "in.txt"); writeFileSync(text, "Hello from the test suite."); const out = path.join(dir, "out.wav"); const r = run(["--engine-kind", "espeak-ng", "--voice", "en-gb", "--speed", "1.2", "--output", out, text]); assert.equal(r.code, 0, r.stderr); const res = JSON.parse(r.stdout.trim()); assert.equal(res.engine, "espeak-ng"); assert.equal(res.voice, "en-gb"); assert.equal(res.speed, 1.2); assert.equal(res.sampleRate, 48000); assert.ok(res.seconds > 0.5 && res.seconds < 10, String(res.seconds)); const wav = readFileSync(out); assert.equal(wav.toString("ascii", 0, 4), "RIFF"); assert.equal(wav.toString("ascii", 36, 40), "data", "a 44-byte header"); assert.equal(wav.readUInt16LE(22), 1, "mono"); assert.equal(wav.readUInt32LE(24), 48000); assert.equal(wav.readUInt16LE(34), 16); assert.equal(Math.round(((wav.length - 44) / 96000) * 1000) / 1000, res.seconds); // Nothing left beside it but the input. assert.deepEqual(readdirSync(dir).sort(), ["in.txt", "out.wav"]); }); test("--text in place of a file leaves no text file behind", { skip: !real && "espeak-ng or ffmpeg absent" }, () => { const dir = mkdtempSync(path.join(TMP, "inline-")); const out = path.join(dir, "out.wav"); const r = run(["--engine-kind", "espeak-ng", "--text", "Inline.", "--output", out]); assert.equal(r.code, 0, r.stderr); assert.deepEqual(readdirSync(dir), ["out.wav"]); }); test("refusals: no output, an unknown engine, a speed out of range, empty text", () => { const dir = mkdtempSync(path.join(TMP, "bad-")); const text = path.join(dir, "in.txt"); writeFileSync(text, "x"); const empty = path.join(dir, "empty.txt"); writeFileSync(empty, " \n"); const out = path.join(dir, "o.wav"); assert.match(run([text]).stderr, /missing --output/); assert.match(run(["--engine-kind", "piper", "--output", out, text]).stderr, /must be 'kokoro', 'espeak-ng' or 'orpheus'/); assert.match(run(["--engine-kind", "espeak-ng", "--speed", "3", "--output", out, text]).stderr, /between 0\.5 and 2/); assert.match(run(["--engine-kind", "espeak-ng", "--output", out, empty]).stderr, /the text is empty/); assert.match(run(["--engine-kind", "kokoro", "--output", out, text]).stderr, /missing --python/); assert.match(run(["--engine-kind", "orpheus", "--python", "/x/python", "--snac", "/x/s.onnx", "--output", out, text]).stderr, /missing --server/); assert.match( run(["--engine-kind", "orpheus", "--python", "/x/python", "--snac", "/x/s.onnx", "--server", "http://127.0.0.1:1", "--output", out, text]).stderr, /orpheus reads the editor's JSON of chunks, not plain text/, ); assert.deepEqual(readdirSync(dir).sort(), ["empty.txt", "in.txt"]); }); test("an engine that fails exits non-zero with its stderr, and leaves nothing", () => { const dir = mkdtempSync(path.join(TMP, "fail-")); const fake = path.join(dir, "fake-espeak"); writeFileSync(fake, "#!/bin/sh\necho 'voice not found' >&2\nexit 7\n"); chmodSync(fake, 0o755); const text = path.join(dir, "in.txt"); writeFileSync(text, "x"); const out = path.join(dir, "o.wav"); const r = run(["--engine-kind", "espeak-ng", "--espeak", fake, "--output", out, text]); assert.equal(r.code, 1); assert.match(r.stderr, /fake-espeak exited 7: voice not found/); assert.deepEqual(readdirSync(dir).sort(), ["fake-espeak", "in.txt"]); });