Open History to see the last commit
1import { generateSpeech } from '@tanstack/ai'
2import { createElevenLabsSpeech } from '@tanstack/ai-elevenlabs'
3import { test, expect } from './fixtures'
4import {
5 fillTextInput,
6 clickGenerate,
7 waitForGenerationComplete,
8 featureUrl,
9} from './helpers'
10import { providersFor } from './test-matrix'
11
12test.describe('elevenlabs -- tts formats', () => {
13 test('returns playable WAV from the PCM endpoint', async ({
14 page,
15 testId,
16 aimockPort,
17 }) => {
18 const result = await generateSpeech({
19 adapter: createElevenLabsSpeech('eleven_v3', 'e2e-dummy', {
20 baseURL: `http://127.0.0.1:${aimockPort}`,
21 defaultHeaders: { 'X-Test-Id': testId },
22 }),
23 text: '[tts] WAV format regression',
24 voice: 'test-voice',
25 format: 'wav',
26 })
27
28 expect(result.format).toBe('wav')
29 expect(result.contentType).toBe('audio/wav')
30 const wav = Buffer.from(result.audio, 'base64')
31 expect(wav.toString('ascii', 0, 4)).toBe('RIFF')
32 expect(wav.readUInt32LE(24)).toBe(44100)
33 expect(wav.readUInt32LE(40)).toBe(32)
34 expect(wav.subarray(44)).toEqual(Buffer.alloc(32))
35
36 const decoded = await page.evaluate(async (audio) => {
37 const bytes = Uint8Array.from(atob(audio), (char) => char.charCodeAt(0))
38 const context = new OfflineAudioContext(1, 16, 44100)
39 const buffer = await context.decodeAudioData(bytes.buffer)
40 return { channels: buffer.numberOfChannels, samples: buffer.length }
41 }, result.audio)
42 expect(decoded).toEqual({ channels: 1, samples: 16 })
43 })
44
45 for (const format of ['aac', 'flac'] as const) {
46 test(`rejects unsupported ${format}`, async ({ testId, aimockPort }) => {
47 await expect(
48 generateSpeech({
49 adapter: createElevenLabsSpeech('eleven_v3', 'e2e-dummy', {
50 baseURL: `http://127.0.0.1:${aimockPort}`,
51 defaultHeaders: { 'X-Test-Id': testId },
52 }),
53 text: '[tts] unsupported format regression',
54 voice: 'test-voice',
55 format,
56 }),
57 ).rejects.toThrow(
58 `ElevenLabs TTS does not support format '${format}'. Use mp3, pcm, opus, or wav.`,
59 )
60 })
61 }
62})
63
64for (const provider of providersFor('tts')) {
65 test.describe(`${provider} -- tts`, () => {
66 test('sse -- generates speech via SSE connection', async ({
67 page,
68 testId,
69 aimockPort,
70 }) => {
71 await page.goto(featureUrl(provider, 'tts', testId, aimockPort, 'sse'))
72 await fillTextInput(page, 'welcome to the guitar store')
73 await clickGenerate(page)
74 await waitForGenerationComplete(page)
75 const audio = page.getByTestId('generated-audio')
76 await expect(audio).toBeVisible()
77 })
78
79 test('http-stream -- generates speech via HTTP stream', async ({
80 page,
81 testId,
82 aimockPort,
83 }) => {
84 await page.goto(
85 featureUrl(provider, 'tts', testId, aimockPort, 'http-stream'),
86 )
87 await fillTextInput(page, 'welcome to the guitar store')
88 await clickGenerate(page)
89 await waitForGenerationComplete(page)
90 const audio = page.getByTestId('generated-audio')
91 await expect(audio).toBeVisible()
92 })
93
94 test('fetcher -- generates speech via server function', async ({
95 page,
96 testId,
97 aimockPort,
98 }) => {
99 await page.goto(
100 featureUrl(provider, 'tts', testId, aimockPort, 'fetcher'),
101 )
102 await fillTextInput(page, 'welcome to the guitar store')
103 await clickGenerate(page)
104 await waitForGenerationComplete(page)
105 const audio = page.getByTestId('generated-audio')
106 await expect(audio).toBeVisible()
107 })
108 })
109}
110
111// Dialogue and timestamps are adapter capabilities, not a feature every TTS
112// provider has. Seed Audio 1.0 is the one wired end to end here: its mount
113// validates the request the adapter builds (`references` + `@AudioN` markers)
114// and answers with the real mixed-unit subtitle shape, so this covers the core
115// `turns` / `timestamps` contract, not just the client plumbing.
116//
117// ElevenLabs has both capabilities but talks to `/v1/text-to-dialogue`, which
118// aimock does not serve — that adapter's four-way endpoint branch is covered
119// by unit tests instead.
120test.describe('byteplus -- tts dialogue', () => {
121 test('fetcher -- turns come back with per-turn segments and word alignment', async ({
122 page,
123 testId,
124 aimockPort,
125 }) => {
126 await page.goto(
127 featureUrl('byteplus', 'tts', testId, aimockPort, 'fetcher'),
128 )
129 await clickGenerate(page, 'generate-dialogue-button')
130 await waitForGenerationComplete(page)
131
132 await expect(page.getByTestId('generated-audio')).toBeVisible()
133 // Two turns in, two segments out.
134 await expect(page.getByTestId('segment-count')).toHaveText('2')
135 await expect(page.getByTestId('alignment-unit')).toHaveText('word')
136 // 11 words at 400ms = 4400ms, reported in ms by the provider and
137 // converted to seconds by the adapter.
138 await expect(page.getByTestId('alignment-end')).toHaveText('4.4')
139 })
140
141 test('sse -- turns survive the streaming transport', async ({
142 page,
143 testId,
144 aimockPort,
145 }) => {
146 await page.goto(featureUrl('byteplus', 'tts', testId, aimockPort, 'sse'))
147 await clickGenerate(page, 'generate-dialogue-button')
148 await waitForGenerationComplete(page)
149
150 await expect(page.getByTestId('segment-count')).toHaveText('2')
151 await expect(page.getByTestId('alignment-unit')).toHaveText('word')
152 })
153})