import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/voice-convert/route.test.ts" // // The two voice adapters' surface: the token, the body's keys (umtool's // contract), and the controller's refusals answered as 400s before any job // exists — a recording as a convert target first. The jobs themselves are // common/controller/voiceFile.test.ts's. In a temp corpus and voice home. const ROOT = await mkdtemp(path.join(os.tmpdir(), "voice-route-")); const CORPUS = path.join(ROOT, "transcripts"); await mkdir(CORPUS, { recursive: true }); const SETTINGS_FILE = path.join(ROOT, "settings.json"); await writeFile(SETTINGS_FILE, JSON.stringify({ workers: [], voice: { home: path.join(ROOT, "voice") } })); process.env.WORKER_TOKEN = "test-token"; process.env.TRANSCRIPTS_DIR = CORPUS; process.env.SETTINGS_FILE = SETTINGS_FILE; const convert = (await import("./route")).POST; const enhance = (await import("../voice-enhance/route")).POST; test.after(() => rm(ROOT, { recursive: true, force: true })); async function post( route: typeof convert, body: Record, token = "test-token", ): Promise<{ status: number; json: { ok?: boolean; error?: string; jobId?: string } }> { const res = await route( new Request("http://localhost/api/ops/voice", { method: "POST", headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, body: JSON.stringify(body), }), ); return { status: res.status, json: await res.json() }; } test("a wrong token is a 401", async () => { assert.equal((await post(enhance, { file: "/a.wav", out: "/b.wav" }, "wrong")).status, 401); assert.equal((await post(convert, { file: "/a.wav", out: "/b.wav" }, "wrong")).status, 401); }); test("the body keys are umtool's contract; enhance takes no voice", async () => { const e = await post(enhance, { file: "/a.wav", out: "/b.wav", voice: "orpheus:zac" }); assert.equal(e.status, 400); assert.match(e.json.error!, /unknown key\(s\): voice — this route accepts file, path, out, engine, params, parts/); const c = await post(convert, { file: "/a.wav", out: "/b.wav", reference: "/me.wav" }); assert.equal(c.status, 400); assert.match(c.json.error!, /unknown key\(s\): reference — this route accepts file, path, out, engine, params, parts, voice, target/); }); test("a recording as a target is a 400 that says why, before any job", async () => { const take = path.join(ROOT, "take.wav"); await writeFile(take, "x"); for (const voice of [take, "my-voice.wav", "https://example.com/v.mp3"]) { const r = await post(convert, { file: take, out: path.join(ROOT, "o.wav"), voice }); assert.equal(r.status, 400, voice); assert.match(r.json.error!, /STOCK synthetic voice.* no real person's voice is ever a target/); } }); test("an engine not installed is a 400 umtool reads as not installed", async () => { const take = path.join(ROOT, "t.wav"); const { execFileSync } = await import("node:child_process"); try { execFileSync("ffmpeg", ["-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", "sine=d=0.5", take]); } catch { return; // no ffmpeg here: the controller test covers the refusal } const r = await post(enhance, { file: take, out: path.join(ROOT, "o.wav"), engine: "deepfilternet" }); assert.equal(r.status, 400); assert.match(r.json.error!, /DeepFilterNet 3 is not installed .* run `archilyzer voice setup --engine deepfilternet`/); });