|
| 1 | +/** |
| 2 | + * Tests for ElevenLabs language mapping fix (issue #51) |
| 3 | + * |
| 4 | + * Problem: |
| 5 | + * 1. labels.accent ("american", "british") is not a BCP-47 code |
| 6 | + * 2. Multilingual voices only got one language code instead of all supported languages |
| 7 | + * |
| 8 | + * Fix: |
| 9 | + * - Fetch /v1/models to get language lists per model |
| 10 | + * - Map voice.high_quality_base_model_ids → union of languages |
| 11 | + * - Use language_id ("en", "es") as bcp47, language name as display |
| 12 | + */ |
| 13 | + |
| 14 | +import { ElevenLabsTTSClient } from "../src/engines/elevenlabs"; |
| 15 | + |
| 16 | +const MOCK_VOICES = [ |
| 17 | + { |
| 18 | + voice_id: "v1", |
| 19 | + name: "Rachel", |
| 20 | + labels: { gender: "female", accent: "american" }, |
| 21 | + high_quality_base_model_ids: ["eleven_multilingual_v2", "eleven_flash_v2_5"], |
| 22 | + }, |
| 23 | + { |
| 24 | + voice_id: "v2", |
| 25 | + name: "Bella", |
| 26 | + labels: { gender: "female", accent: "british" }, |
| 27 | + high_quality_base_model_ids: ["eleven_multilingual_v2"], |
| 28 | + }, |
| 29 | + { |
| 30 | + voice_id: "v3", |
| 31 | + name: "OldVoice", |
| 32 | + labels: {}, |
| 33 | + high_quality_base_model_ids: [], // no models |
| 34 | + }, |
| 35 | +]; |
| 36 | + |
| 37 | +const MOCK_MODELS = [ |
| 38 | + { |
| 39 | + model_id: "eleven_multilingual_v2", |
| 40 | + can_do_text_to_speech: true, |
| 41 | + languages: [ |
| 42 | + { language_id: "en", name: "English" }, |
| 43 | + { language_id: "es", name: "Spanish" }, |
| 44 | + { language_id: "fr", name: "French" }, |
| 45 | + { language_id: "de", name: "German" }, |
| 46 | + ], |
| 47 | + }, |
| 48 | + { |
| 49 | + model_id: "eleven_flash_v2_5", |
| 50 | + can_do_text_to_speech: true, |
| 51 | + languages: [ |
| 52 | + { language_id: "en", name: "English" }, |
| 53 | + { language_id: "es", name: "Spanish" }, |
| 54 | + { language_id: "ja", name: "Japanese" }, |
| 55 | + ], |
| 56 | + }, |
| 57 | + { |
| 58 | + model_id: "eleven_tts_v1", |
| 59 | + can_do_text_to_speech: false, // not a TTS model — should be ignored |
| 60 | + languages: [{ language_id: "en", name: "English" }], |
| 61 | + }, |
| 62 | +]; |
| 63 | + |
| 64 | +function mockFetch(voicesPayload: object, modelsPayload: object[]) { |
| 65 | + return jest.fn().mockImplementation((url: string) => { |
| 66 | + if (url.includes("/models")) { |
| 67 | + return Promise.resolve({ |
| 68 | + ok: true, |
| 69 | + json: () => Promise.resolve(modelsPayload), |
| 70 | + }); |
| 71 | + } |
| 72 | + return Promise.resolve({ |
| 73 | + ok: true, |
| 74 | + json: () => Promise.resolve(voicesPayload), |
| 75 | + }); |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +describe("ElevenLabs _mapVoicesToUnified — language mapping from models", () => { |
| 80 | + let client: any; |
| 81 | + |
| 82 | + beforeEach(() => { |
| 83 | + client = new ElevenLabsTTSClient({ apiKey: "fake" }); |
| 84 | + }); |
| 85 | + |
| 86 | + it("maps a multilingual voice to all languages from its models (deduped)", async () => { |
| 87 | + // Rachel supports eleven_multilingual_v2 (en, es, fr, de) + eleven_flash_v2_5 (en, es, ja) |
| 88 | + // → union = en, es, fr, de, ja (en and es deduped) |
| 89 | + const rawVoices = await client._getVoicesWithModels(MOCK_VOICES, MOCK_MODELS); |
| 90 | + const voices = await client._mapVoicesToUnified(rawVoices); |
| 91 | + const rachel = voices.find((v: any) => v.id === "v1"); |
| 92 | + |
| 93 | + const bcp47s = rachel.languageCodes.map((lc: any) => lc.bcp47); |
| 94 | + expect(bcp47s).toContain("en"); |
| 95 | + expect(bcp47s).toContain("es"); |
| 96 | + expect(bcp47s).toContain("fr"); |
| 97 | + expect(bcp47s).toContain("de"); |
| 98 | + expect(bcp47s).toContain("ja"); |
| 99 | + expect(new Set(bcp47s).size).toBe(bcp47s.length); // no duplicates |
| 100 | + }); |
| 101 | + |
| 102 | + it("uses human-readable language name as display", async () => { |
| 103 | + const rawVoices = await client._getVoicesWithModels(MOCK_VOICES, MOCK_MODELS); |
| 104 | + const voices = await client._mapVoicesToUnified(rawVoices); |
| 105 | + const rachel = voices.find((v: any) => v.id === "v1"); |
| 106 | + const en = rachel.languageCodes.find((lc: any) => lc.bcp47 === "en"); |
| 107 | + |
| 108 | + expect(en.display).toBe("English"); |
| 109 | + }); |
| 110 | + |
| 111 | + it("falls back to English when voice has no model ids", async () => { |
| 112 | + const rawVoices = await client._getVoicesWithModels(MOCK_VOICES, MOCK_MODELS); |
| 113 | + const voices = await client._mapVoicesToUnified(rawVoices); |
| 114 | + const old = voices.find((v: any) => v.id === "v3"); |
| 115 | + |
| 116 | + expect(old.languageCodes).toHaveLength(1); |
| 117 | + expect(old.languageCodes[0].bcp47).toBe("en"); |
| 118 | + }); |
| 119 | + |
| 120 | + it("ignores models where can_do_text_to_speech is false", async () => { |
| 121 | + const rawVoices = await client._getVoicesWithModels(MOCK_VOICES, MOCK_MODELS); |
| 122 | + const voices = await client._mapVoicesToUnified(rawVoices); |
| 123 | + // No voice uses eleven_tts_v1 — but confirm it wasn't added to language map |
| 124 | + const rachel = voices.find((v: any) => v.id === "v1"); |
| 125 | + const bcp47s = rachel.languageCodes.map((lc: any) => lc.bcp47); |
| 126 | + // eleven_tts_v1 only had "en" — already present, so count shouldn't change due to it |
| 127 | + expect(bcp47s).toContain("en"); |
| 128 | + }); |
| 129 | +}); |
0 commit comments