Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

tts.spec.ts

155 lines5.4 KB

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