Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import {
3 insertMissingModels,
4 modelIdsFromRequestSchema,
5 ttsModelIdsFromCatalog,
6} from './elevenlabs'
7import type { ElevenLabsCatalogModel } from './elevenlabs'
8
9const SOURCE = `export const ELEVENLABS_TTS_MODELS = [
10 'eleven_v3',
11 'eleven_flash_v2',
12 // Deprecated by ElevenLabs — use \`eleven_flash_v2\`.
13 'eleven_turbo_v2',
14] as const
15`
16
17describe('ttsModelIdsFromCatalog', () => {
18 const models: Array<ElevenLabsCatalogModel> = [
19 {
20 rawId: 'eleven_v3',
21 firstSeenAt: 10,
22 deprecatedAt: null,
23 capabilities: { canDoTextToSpeech: true },
24 },
25 {
26 rawId: 'eleven_v4_turbo',
27 firstSeenAt: 30,
28 deprecatedAt: null,
29 capabilities: { canDoTextToSpeech: true },
30 },
31 {
32 rawId: 'eleven_v4',
33 firstSeenAt: 30,
34 deprecatedAt: null,
35 capabilities: { canDoTextToSpeech: true },
36 },
37 {
38 rawId: 'eleven_english_sts_v2',
39 firstSeenAt: 40,
40 deprecatedAt: null,
41 capabilities: { canDoTextToSpeech: false },
42 },
43 {
44 rawId: 'eleven_old',
45 firstSeenAt: 50,
46 deprecatedAt: 60,
47 capabilities: { canDoTextToSpeech: true },
48 },
49 {
50 rawId: "eleven_v4'; alert(1)",
51 firstSeenAt: 90,
52 capabilities: { canDoTextToSpeech: true },
53 },
54 ]
55
56 it('keeps text-to-speech ids, newest first, and drops the rest', () => {
57 expect(ttsModelIdsFromCatalog(models)).toEqual([
58 'eleven_v4',
59 'eleven_v4_turbo',
60 'eleven_v3',
61 ])
62 })
63})
64
65describe('insertMissingModels', () => {
66 it('prepends missing ids and leaves existing rows in place', () => {
67 const next = insertMissingModels(SOURCE, 'ELEVENLABS_TTS_MODELS', [
68 'eleven_v4',
69 'eleven_v4_turbo',
70 'eleven_v3',
71 ])
72 expect(next).toContain(`export const ELEVENLABS_TTS_MODELS = [
73 'eleven_v4',
74 'eleven_v4_turbo',
75 'eleven_v3',
76 'eleven_flash_v2',`)
77 expect(next).toContain("'eleven_turbo_v2'")
78 expect(
79 insertMissingModels(next, 'ELEVENLABS_TTS_MODELS', [
80 'eleven_v4',
81 'eleven_v3',
82 ]),
83 ).toBe(next)
84 })
85
86 it('prepends into the audio array and leaves the speech array alone', () => {
87 const both = `${SOURCE}
88export const ELEVENLABS_AUDIO_MODELS = [
89 'music_v2',
90 'eleven_text_to_sound_v2',
91] as const satisfies ReadonlyArray<string>
92`
93 const next = insertMissingModels(both, 'ELEVENLABS_AUDIO_MODELS', [
94 'music_v2_5',
95 'music_v2',
96 ])
97 expect(next).toContain(`export const ELEVENLABS_AUDIO_MODELS = [
98 'music_v2_5',
99 'music_v2',`)
100 expect(next).toContain(`export const ELEVENLABS_TTS_MODELS = [
101 'eleven_v3',`)
102 expect(
103 insertMissingModels(next, 'ELEVENLABS_AUDIO_MODELS', ['music_v2_5']),
104 ).toBe(next)
105 })
106
107 it('refuses an id that is not a safe token', () => {
108 expect(() =>
109 insertMissingModels(SOURCE, 'ELEVENLABS_TTS_MODELS', ['eleven_v4;']),
110 ).toThrow(/Refusing/)
111 })
112
113 it('throws when the array marker is missing', () => {
114 expect(() =>
115 insertMissingModels(
116 'export const NOPE = []',
117 'ELEVENLABS_TTS_MODELS',
118 [],
119 ),
120 ).toThrow(/not found/)
121 })
122})
123
124describe('modelIdsFromRequestSchema', () => {
125 it('drops deprecated $ref ids and lists the newest enum value first', () => {
126 expect(
127 modelIdsFromRequestSchema({
128 $defs: {
129 MusicModelID: {
130 enum: ['music_v1', 'music_v2', 'music_v2_5'],
131 'x-fern-enum': { music_v1: { deprecated: true } },
132 },
133 },
134 properties: { model_id: { $ref: '#/$defs/MusicModelID' } },
135 }),
136 ).toEqual(['music_v2_5', 'music_v2'])
137 })
138
139 it('reads an inline voice-design enum newest first', () => {
140 expect(
141 modelIdsFromRequestSchema({
142 properties: {
143 model_id: {
144 enum: ['eleven_multilingual_ttv_v2', 'eleven_ttv_v3'],
145 },
146 },
147 }),
148 ).toEqual(['eleven_ttv_v3', 'eleven_multilingual_ttv_v2'])
149 })
150
151 it('throws when model_id is missing or unsafe', () => {
152 expect(() => modelIdsFromRequestSchema({ properties: {} })).toThrow(
153 /model_id/,
154 )
155 expect(() =>
156 modelIdsFromRequestSchema({
157 properties: { model_id: { enum: ['music_v2;'] } },
158 }),
159 ).toThrow(/Refusing/)
160 })
161})