The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
Voices: tests for saved voices, the fal routes, the two-step clone and the voice graphs
Claude8238a7f61eb0
diff --git a/tests/voice-design-comfy.test.ts b/tests/voice-design-comfy.test.tsnew file mode 100644index 00000000..26a9d27e--- /dev/null+++ b/tests/voice-design-comfy.test.ts@@ -0,0 +1,61 @@+import fs from "node:fs";+import os from "node:os";+import path from "node:path";+import { describe, expect, it } from "vitest";++/**+ * Design a voice on your GPU (nm10-voice-design): the Qwen3-TTS and VoxCPM2 graphs, their node packs pinned for the+ * GPU pre-flight, and what the ComfyUI provider binds (the voice in words, what a sample says, VoxCPM2's device).+ */++process.env.FUOBE_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "fuobe-voice-design-comfy-"));+process.env.COMFYUI_URL = "http://127.0.0.1:9";++const root = path.resolve(__dirname, "..");+const read = (p: string) => JSON.parse(fs.readFileSync(path.join(root, p), "utf8"));+const manifest = read("workflows/comfyui/manifest.json") as { workflows: { id: string; capability: string; customNodes?: { name: string }[]; validated: boolean; license?: string; autoDownloads?: string[]; bindings: Record<string, { node: string; input: string }[]> }[] };+const packs = read("workflows/comfyui/packs.json") as Record<string, { repo: string; commit: string }>;+const requirements = read("workflows/comfyui/requirements.json") as { workflows: Record<string, { packs: Record<string, string[]>; firstRun: string[] }> };+const subset = read("workflows/comfyui/object_info.subset.json") as Record<string, { python_module: string } | null>;++const GRAPHS = {+ "tts-qwen3-design": { capability: "voice-design", pack: "ComfyUI-QwenTTS", bindings: ["prompt", "voiceDescription", "seed", "filenamePrefix"] },+ "tts-qwen3-clone": { capability: "tts", pack: "ComfyUI-QwenTTS", bindings: ["prompt", "audio", "voiceTranscript", "seed", "filenamePrefix"] },+ "tts-voxcpm2": { capability: "voice-design", pack: "ComfyUI-VoxCPM2", bindings: ["prompt", "voiceDescription", "seed", "device", "filenamePrefix"] },+ "tts-voxcpm2-clone": { capability: "tts", pack: "ComfyUI-VoxCPM2", bindings: ["prompt", "audio", "voiceTranscript", "seed", "device", "filenamePrefix"] },+} as const;++describe("the voice graphs in the library", () => {+ it.each(Object.entries(GRAPHS))("%s: its pack pinned, its classes recorded, its licence and first-run download stated", (id, want) => {+ const w = manifest.workflows.find((x) => x.id === id)!;+ expect(w.capability).toBe(want.capability);+ expect(Object.keys(w.bindings).sort()).toEqual([...want.bindings].sort());+ expect(w.customNodes?.map((c) => c.name)).toContain(want.pack);+ // The packs aren't on the validation server: the graph waits for a GPU that has them.+ expect(w.validated).toBe(false);+ expect(w.license).toMatch(/Apache-2\.0/);+ expect(packs[want.pack].commit).toMatch(/^[0-9a-f]{40}$/);+ expect(requirements.workflows[id].packs[want.pack].length).toBe(1);+ expect(requirements.workflows[id].firstRun.join(" ")).toMatch(/downloads (Qwen\/Qwen3-TTS-12Hz-1\.7B|openbmb\/VoxCPM2)/);+ const graph = read(`workflows/comfyui/${id}.json`) as Record<string, { class_type: string }>;+ for (const n of Object.values(graph)) expect(subset[n.class_type], n.class_type).toBeTruthy();+ });+});++describe("what the ComfyUI provider binds", () => {+ it("puts the words into the voice design node, and the take's line and the GPU's device into the VoxCPM2 clone", async () => {+ const { prepareWorkflow } = await import("@/server/providers/comfyui");+ const ctx = (op: "voice-design" | "tts", device?: string) => ({ generationId: "g", op, upload: async () => "voice.wav", device: async () => device });+ const design = await prepareWorkflow("tts-qwen3-design", { prompt: "Hello there.", voiceDescription: " A warm older narrator. " }, ctx("voice-design"));+ expect(design.workflow["1"].inputs).toMatchObject({ text: "Hello there.", instruct: "A warm older narrator.", do_sample: true, model_size: "1.7B" });+ const vox = await prepareWorkflow("tts-voxcpm2", { prompt: "Hi.", voiceDescription: "A calm voice." }, ctx("voice-design", "mps"));+ expect(vox.workflow["1"].inputs).toMatchObject({ text: "Hi.", voice_description: "A calm voice.", device: "mps" });+ const clone = await prepareWorkflow("tts-voxcpm2-clone", { prompt: "New line.", voiceAudio: "data:audio/wav;base64,UklGRg==", voiceTranscript: "Hello there." }, ctx("tts", "cuda"));+ expect(clone.workflow["2"].inputs).toMatchObject({ text: "New line.", prompt_text: "Hello there.", device: "cuda" });+ // No transcript: the controllable clone (empty prompt_text); a GPU that can't say keeps the graph's cuda.+ const plain = await prepareWorkflow("tts-voxcpm2-clone", { prompt: "New line.", voiceAudio: "data:audio/wav;base64,UklGRg==" }, ctx("tts"));+ expect(plain.workflow["2"].inputs).toMatchObject({ prompt_text: "", device: "cuda" });+ const qclone = await prepareWorkflow("tts-qwen3-clone", { prompt: "New line.", voiceAudio: "data:audio/wav;base64,UklGRg==", voiceTranscript: "Hello there." }, ctx("tts"));+ expect(qclone.workflow["2"].inputs).toMatchObject({ target_text: "New line.", reference_text: "Hello there." });+ });+});diff --git a/tests/voice-design-server.test.ts b/tests/voice-design-server.test.tsnew file mode 100644index 00000000..bc381308--- /dev/null+++ b/tests/voice-design-server.test.ts@@ -0,0 +1,186 @@+import fs from "node:fs";+import os from "node:os";+import path from "node:path";+import { afterAll, beforeAll, describe, expect, it } from "vitest";++/**+ * Design a voice on the server (nm10-voice-design): an Element voice keeps the model, the words and the take's line; fal's+ * Qwen3-TTS routes send the right fields; a sample is cloned in two steps (clone-voice, then text-to-speech with the+ * embedding it returns), and a saved voice keeps that embedding so its next line skips the clone. No paid call is made:+ * fetch is stubbed and nothing reaches fal.+ */++const dir = fs.mkdtempSync(path.join(os.tmpdir(), "fuobe-voice-design-"));+process.env.FUOBE_DATA_DIR = dir;+process.env.COMFYUI_URL = "http://127.0.0.1:9";++const { setSetting } = await import("@/server/settings");+const { createElement, getElement, updateElement } = await import("@/server/elements");+const { saveFile } = await import("@/server/storage");+const { keepVoiceEmbedding, savedVoiceFor } = await import("@/server/voices");+const { runAsTenant } = await import("@/server/tenant");+const { buildPayload } = await import("@/lib/catalog/payload");+const { getModel } = await import("@/lib/catalog");+const { planGeneration } = await import("@/lib/catalog/plan");+const elementsRoute = await import("@/app/api/elements/route");+const { createGenerations, getGeneration } = await import("@/server/generations");++const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));+const wav = () => saveFile({ data: Buffer.from("RIFF0000WAVEfmt "), contentType: "audio/wav", name: "take.wav", source: "upload" }).url;+const qwen = getModel("qwen-3-tts")!;+const route = (op: string) => qwen.routes.find((r) => r.op === op)!;++beforeAll(() => {+ setSetting("demoMode", "off");+ setSetting("enhanceProvider", "none");+});+afterAll(() => {+ fs.rmSync(dir, { recursive: true, force: true });+});++describe("Element voices keep a designed voice", () => {+ it("stores the model, the words and the line; a new clip drops the old clip's embedding", () => {+ const clip = wav();+ const el = createElement({ name: "Old Narrator", kind: "voice", references: [clip], voice: { modelId: "oss-qwen3-tts", engine: "open", description: "A warm older male voice.", transcript: "Hello there." } });+ expect(getElement(el.id)!.voice).toEqual({ modelId: "oss-qwen3-tts", engine: "open", description: "A warm older male voice.", transcript: "Hello there." });+ updateElement(el.id, { voice: { ...el.voice!, embedding: "/api/files/uploads/x.bin" } });+ expect(getElement(el.id)!.voice!.embedding).toBe("/api/files/uploads/x.bin");+ // A rename keeps it; a new recording is a different voice: no embedding and no line of the old take.+ updateElement(el.id, { name: "Narrator" });+ expect(getElement(el.id)!.voice!.embedding).toBe("/api/files/uploads/x.bin");+ updateElement(el.id, { references: [wav()] });+ expect(getElement(el.id)!.voice).toEqual({ modelId: "oss-qwen3-tts", engine: "open", description: "A warm older male voice." });+ // Only voices carry one.+ expect(createElement({ name: "Prop", kind: "prop", references: [], description: "a mug", voice: { modelId: "oss-qwen3-tts", engine: "open" } }).voice).toBeUndefined();+ });++ it("POST /api/elements takes a take from a model that designs voices, never an embedding", async () => {+ const post = (body: unknown) => elementsRoute.POST(new Request("http://localhost/api/elements", { method: "POST", body: JSON.stringify(body) }));+ const clip = wav();+ const ok = await post({ name: "Bright Host", kind: "voice", references: [clip], voice: { modelId: "qwen-3-tts", description: "A bright young voice.", transcript: "Hi!", embedding: "https://evil.example/x" } });+ expect(ok.status).toBe(201);+ const el = (await ok.json()) as { voice: Record<string, unknown> };+ expect(el.voice).toEqual({ modelId: "qwen-3-tts", engine: "cloud", description: "A bright young voice.", transcript: "Hi!" });+ // A model that doesn't design voices, a voice with no take, or a voice on a character is refused.+ expect((await post({ name: "X", kind: "voice", references: [clip], voice: { modelId: "elevenlabs-v4" } })).status).toBe(400);+ expect((await post({ name: "X", kind: "voice", references: [], description: "words", voice: { modelId: "oss-voxcpm2" } })).status).toBe(400);+ expect((await post({ name: "X", kind: "character", references: [clip], voice: { modelId: "oss-voxcpm2" } })).status).toBe(400);+ });+});++describe("Qwen3-TTS on fal: the three routes' payloads", () => {+ it("voice design sends the line as text and the words as prompt, with room for a long line", () => {+ expect(route("voice-design").endpoint).toBe("fal-ai/qwen-3-tts/voice-design/1.7b");+ const p = buildPayload(route("voice-design").map!, { prompt: "Hello there.", voiceDescription: " A warm, gravelly narrator. " });+ expect(p).toEqual({ text: "Hello there.", prompt: "A warm, gravelly narrator.", max_new_tokens: 4096 });+ });++ it("text to speech speaks with a premade voice, or with a cloned voice's embedding and what its sample says", () => {+ const tts = route("tts");+ expect(tts.endpoint).toBe("fal-ai/qwen-3-tts/text-to-speech/1.7b");+ expect(buildPayload(tts.map!, { prompt: "Line.", voice: "Serena", language: "English" } as never)).toMatchObject({ text: "Line.", voice: "Serena", max_new_tokens: 4096 });+ const cloned = buildPayload(tts.map!, { prompt: "Line.", voiceAudio: "https://v3.fal.media/files/take.wav", voiceEmbedding: "https://v3.fal.media/files/e.safetensors", voiceTranscript: "Hello there." });+ expect(cloned).toEqual({ text: "Line.", speaker_voice_embedding_file_url: "https://v3.fal.media/files/e.safetensors", reference_text: "Hello there.", max_new_tokens: 4096 });+ // The sample itself is never sent to text to speech: it is cloned first.+ expect(JSON.stringify(cloned)).not.toContain("take.wav");+ expect(tts.map!.cloneFirst).toEqual({ endpoint: "fal-ai/qwen-3-tts/clone-voice/1.7b", audio: "audio_url", text: "reference_text", output: "speaker_embedding" });+ });++ it("is priced per 1,000 characters, and a sample is needed or a premade voice", () => {+ expect(qwen.pricing).toMatchObject({ unit: "request", usd: 0.09 });+ expect(qwen.options.voices?.map((v) => v.id)).toContain("Uncle_Fu");+ expect(() => planGeneration({ feature: "voice-design", modelId: "qwen-3-tts", op: "voice-design", prompt: "Hi" }, getModel, () => undefined)).toThrow(/Describe the voice/);+ expect(planGeneration({ feature: "voice-design", modelId: "qwen-3-tts", op: "voice-design", prompt: "Hi", params: { voiceDescription: "warm", numOutputs: 3 } }, getModel, () => undefined).jobs).toBe(3);+ });+});++/** A stand-in for fal's queue: records what was sent and answers like fal. */+function fakeFal() {+ const sent: { url: string; body?: unknown }[] = [];+ const real = globalThis.fetch;+ globalThis.fetch = (async (input: string | URL | Request, init?: RequestInit) => {+ const url = String(input instanceof Request ? input.url : input);+ sent.push({ url, body: init?.body ? JSON.parse(String(init.body)) : undefined });+ const json = (v: unknown) => new Response(JSON.stringify(v), { status: 200, headers: { "Content-Type": "application/json" } });+ if (url.endsWith("/fal-ai/qwen-3-tts/clone-voice/1.7b")) return json({ request_id: "c1", status_url: "https://queue.fal.run/fal-ai/qwen-3-tts/requests/c1/status", response_url: "https://queue.fal.run/fal-ai/qwen-3-tts/requests/c1", cancel_url: "https://queue.fal.run/x" });+ if (url.includes("/requests/c1/status")) return json({ status: "COMPLETED" });+ if (url.endsWith("/requests/c1")) return json({ speaker_embedding: { url: "https://v3.fal.media/files/voice.safetensors" } });+ return json({ request_id: "t1", status_url: "https://queue.fal.run/fal-ai/qwen-3-tts/requests/t1/status", response_url: "https://queue.fal.run/fal-ai/qwen-3-tts/requests/t1", cancel_url: "https://queue.fal.run/y" });+ }) as typeof fetch;+ return { sent, restore: () => void (globalThis.fetch = real) };+}++describe("a clone in two steps on fal", () => {+ it("clones the sample, then speaks with the embedding; the adapter says which embedding it made", async () => {+ setSetting("falKey", "fal_test_key_123456");+ const fal = fakeFal();+ const { falProvider } = await import("@/server/providers/fal");+ try {+ const res = await falProvider.submit({+ generationId: "g1",+ model: qwen,+ route: route("tts"),+ input: { prompt: "A new line.", voiceAudio: "https://v3.fal.media/files/take.wav", voiceTranscript: "Hello there." },+ });+ expect(fal.sent.map((s) => s.url.replace("https://queue.fal.run/", ""))).toEqual([+ "fal-ai/qwen-3-tts/clone-voice/1.7b",+ "fal-ai/qwen-3-tts/requests/c1/status",+ "fal-ai/qwen-3-tts/requests/c1",+ "fal-ai/qwen-3-tts/text-to-speech/1.7b",+ ]);+ expect(fal.sent[0].body).toEqual({ audio_url: "https://v3.fal.media/files/take.wav", reference_text: "Hello there." });+ expect(fal.sent[3].body).toEqual({ text: "A new line.", speaker_voice_embedding_file_url: "https://v3.fal.media/files/voice.safetensors", reference_text: "Hello there.", max_new_tokens: 4096 });+ expect(res.meta?.voiceEmbedding).toBe("https://v3.fal.media/files/voice.safetensors");+ // An embedding already kept: one call, straight to speech.+ fal.sent.length = 0;+ const again = await falProvider.submit({ generationId: "g2", model: qwen, route: route("tts"), input: { prompt: "More.", voiceAudio: "https://v3.fal.media/files/take.wav", voiceEmbedding: "https://v3.fal.media/files/kept.safetensors" } });+ expect(fal.sent.map((s) => s.url)).toEqual(["https://queue.fal.run/fal-ai/qwen-3-tts/text-to-speech/1.7b"]);+ expect(again.meta?.voiceEmbedding).toBeUndefined();+ } finally {+ fal.restore();+ }+ });++ it("a saved voice keeps the embedding fal made, from fal only, and never inside an account's work", async () => {+ const clip = wav();+ const el = createElement({ name: "Kept", kind: "voice", references: [clip], voice: { modelId: "qwen-3-tts", engine: "cloud", transcript: "Hi there." } });+ const file = async () => ({ data: Buffer.from("safetensors-bytes"), contentType: "application/octet-stream", url: "" });+ // Not fal's: nothing is fetched or kept.+ expect(await keepVoiceEmbedding(clip, "https://evil.example/voice.safetensors", file)).toBeNull();+ expect(await keepVoiceEmbedding(clip, "http://v3.fal.media/voice.safetensors", file)).toBeNull();+ // Inside an account's work the owner's voices aren't there at all.+ await runAsTenant({ kind: "account", id: "acct-1" }, async () => {+ expect(savedVoiceFor(clip)).toBeNull();+ expect(await keepVoiceEmbedding(clip, "https://v3.fal.media/files/voice.safetensors", file)).toBeNull();+ });+ const kept = await keepVoiceEmbedding(`${clip}?v=1`, "https://v3.fal.media/files/voice.safetensors", file);+ expect(kept).toMatch(/^\/api\/files\//);+ expect(getElement(el.id)!.voice!.embedding).toBe(kept);+ expect(fs.readFileSync(path.join(dir, "files", kept!.slice("/api/files/".length)), "utf8")).toBe("safetensors-bytes");+ // Kept once: a second clone doesn't replace it.+ expect(await keepVoiceEmbedding(clip, "https://v3.fal.media/files/other.safetensors", file)).toBeNull();+ });++ it("a run with a saved voice sends its kept embedding and its line, and makes no clone", async () => {+ setSetting("falKey", "fal_test_key_123456");+ const clip = wav();+ const emb = saveFile({ data: Buffer.from("emb"), contentType: "application/octet-stream", name: "e.safetensors", source: "derived" }).url;+ createElement({ name: "Ready", kind: "voice", references: [clip], voice: { modelId: "qwen-3-tts", engine: "cloud", transcript: "The take's line.", embedding: emb } });+ const fal = fakeFal();+ const { falProvider } = await import("@/server/providers/fal");+ const upload = falProvider.prepareFile;+ falProvider.prepareFile = async (_d, _t, name) => `https://v3.fal.media/files/${name}`;+ try {+ const [g] = createGenerations({ feature: "audio", modelId: "qwen-3-tts", engine: "cloud", op: "tts", prompt: "Say this.", inputs: { voiceAudio: clip } });+ const spoke = () => fal.sent.some((s) => s.url.endsWith("/text-to-speech/1.7b"));+ for (let i = 0; i < 200 && !spoke() && getGeneration(g.id)!.status !== "failed"; i++) await sleep(25);+ expect(getGeneration(g.id)!.error).toBeNull();+ const calls = fal.sent.filter((s) => s.url.startsWith("https://queue.fal.run/fal-ai/qwen-3-tts/"));+ expect(calls.map((s) => s.url)).toEqual(["https://queue.fal.run/fal-ai/qwen-3-tts/text-to-speech/1.7b"]);+ expect(calls[0].body).toMatchObject({ text: "Say this.", speaker_voice_embedding_file_url: expect.stringMatching(/^https:\/\/v3\.fal\.media\/files\//), reference_text: "The take's line." });+ } finally {+ falProvider.prepareFile = upload;+ fal.restore();+ }+ });+});