Skip to content

Commit 87e6b65

Browse files
authored
feat: add tests for testing all wss model backends (#53)
* test: add integration tests for WebSocket streaming with different models * test: simplify TTS model testing by removing exception handling * style: format * test: add delay to async client teardown to prevent API rate limits * test: add delays in WebSocket streaming tests to prevent SSL errors
1 parent d34e449 commit 87e6b65

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

tests/integration/conftest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ def client(api_key):
4444
@pytest.fixture
4545
async def async_client(api_key):
4646
"""Async Fish Audio client."""
47+
import asyncio
48+
4749
client = AsyncFishAudio(api_key=api_key)
4850
yield client
4951
await client.close()
52+
# Brief delay to avoid API rate limits on WebSocket connections
53+
await asyncio.sleep(0.3)
5054

5155

5256
@pytest.fixture

tests/integration/test_tts_integration.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,11 @@ def test_tts_with_different_models(self, client, save_audio):
5050
models = get_args(Model)
5151

5252
for model in models:
53-
try:
54-
audio = client.tts.convert(text=f"Testing model {model}", model=model)
55-
assert len(audio) > 0, f"Failed for model: {model}"
56-
57-
# Write to output directory
58-
save_audio(audio, f"test_model_{model}.mp3")
59-
except Exception as e:
60-
# Some models might not be available
61-
pytest.skip(f"Model {model} not available: {e}")
53+
audio = client.tts.convert(text=f"Testing model {model}", model=model)
54+
assert len(audio) > 0, f"Failed for model: {model}"
55+
56+
# Write to output directory
57+
save_audio(audio, f"test_model_{model}.mp3")
6258

6359
def test_tts_longer_text(self, client, save_audio):
6460
"""Test TTS with longer text."""

tests/integration/test_tts_websocket_integration.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
"""Integration tests for TTS WebSocket streaming functionality."""
22

3+
from typing import get_args
4+
35
import pytest
46

57
from fishaudio import WebSocketOptions
68
from fishaudio.types import Prosody, TTSConfig, TextEvent, FlushEvent
9+
from fishaudio.types.shared import Model
710
from .conftest import TEST_REFERENCE_ID
811

912

@@ -31,6 +34,26 @@ def text_stream():
3134
# Save the audio
3235
save_audio(audio_chunks, "test_websocket_streaming.mp3")
3336

37+
def test_websocket_streaming_with_different_models(self, client, save_audio):
38+
"""Test WebSocket streaming with different models."""
39+
import time
40+
41+
models = get_args(Model)
42+
43+
for model in models:
44+
45+
def text_stream():
46+
yield f"Testing model {model} via WebSocket."
47+
48+
audio_chunks = list(client.tts.stream_websocket(text_stream(), model=model))
49+
assert len(audio_chunks) > 0, f"Failed for model: {model}"
50+
51+
# Write to output directory
52+
save_audio(audio_chunks, f"test_websocket_model_{model}.mp3")
53+
54+
# Brief delay to avoid SSL errors when opening next WebSocket connection
55+
time.sleep(0.3)
56+
3457
def test_websocket_streaming_with_wav_format(self, client, save_audio):
3558
"""Test WebSocket streaming with WAV format."""
3659
config = TTSConfig(format="wav", chunk_length=200)
@@ -195,6 +218,34 @@ async def text_stream():
195218

196219
save_audio(audio_chunks, "test_async_websocket_streaming.mp3")
197220

221+
@pytest.mark.asyncio
222+
async def test_async_websocket_streaming_with_different_models(
223+
self, async_client, save_audio
224+
):
225+
"""Test async WebSocket streaming with different models."""
226+
import asyncio
227+
228+
models = get_args(Model)
229+
230+
for model in models:
231+
232+
async def text_stream():
233+
yield f"Testing model {model} via async WebSocket."
234+
235+
audio_chunks = []
236+
async for chunk in async_client.tts.stream_websocket(
237+
text_stream(), model=model
238+
):
239+
audio_chunks.append(chunk)
240+
241+
assert len(audio_chunks) > 0, f"Failed for model: {model}"
242+
243+
# Write to output directory
244+
save_audio(audio_chunks, f"test_async_websocket_model_{model}.mp3")
245+
246+
# Brief delay to avoid SSL errors when opening next WebSocket connection
247+
await asyncio.sleep(0.3)
248+
198249
@pytest.mark.asyncio
199250
async def test_async_websocket_streaming_with_format(
200251
self, async_client, save_audio
@@ -285,6 +336,8 @@ async def test_async_websocket_streaming_multiple_calls(
285336
self, async_client, save_audio
286337
):
287338
"""Test multiple async WebSocket streaming calls in sequence."""
339+
import asyncio
340+
288341
for i in range(3):
289342

290343
async def text_stream():
@@ -297,6 +350,9 @@ async def text_stream():
297350
assert len(audio_chunks) > 0, f"Call {i + 1} should return audio"
298351
save_audio(audio_chunks, f"test_async_websocket_call_{i + 1}.mp3")
299352

353+
# Brief delay to avoid SSL errors when opening next WebSocket connection
354+
await asyncio.sleep(0.3)
355+
300356
@pytest.mark.asyncio
301357
async def test_async_websocket_streaming_empty_text(self, async_client, save_audio):
302358
"""Test async WebSocket streaming with empty text stream raises error."""

0 commit comments

Comments
 (0)