1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
|
"""Tests for the SGLang-Omni TTS client (converter/clients/sglomni.py)."""
import base64
import io
import json
import tempfile
import unittest
import urllib.error
import wave
from pathlib import Path
from unittest.mock import MagicMock, patch
from converter import config
from converter.clients import SgOmniTTSClient
from converter.clients.base import NonRetryableTTSError
from converter.clients.sglomni import _data_url, _is_loopback
from converter.clients.speakers import QWEN3_TTS_SPEAKERS
class CatalogConsistencyTests(unittest.TestCase):
"""The backend catalog's vendored facts match the converter's."""
def test_customvoice_speakers_match_the_qwen_table(self):
from backends.sglomni.catalog import QWEN_CUSTOMVOICE_SPEAKERS
self.assertEqual(QWEN_CUSTOMVOICE_SPEAKERS, QWEN3_TTS_SPEAKERS)
_DUMMY_CHUNKS = Path(tempfile.gettempdir()) / "audiobook_sglomni_test_chunks"
def _make_wav() -> bytes:
"""A real minimal RIFF/WAVE file (what a server response looks like)."""
buffer = io.BytesIO()
with wave.open(buffer, "wb") as wav_file:
wav_file.setnchannels(1)
wav_file.setsampwidth(2)
wav_file.setframerate(24000)
wav_file.writeframes(b"\x01\x00" * 16)
return buffer.getvalue()
_WAV_BYTES = _make_wav()
_WAV_FRAMES = b"\x01\x00" * 16
class LoopbackTests(unittest.TestCase):
def test_loopback_hosts(self):
self.assertTrue(_is_loopback("http://127.0.0.1:8100"))
self.assertTrue(_is_loopback("http://localhost:8100"))
self.assertFalse(_is_loopback("http://10.20.30.40:8100"))
def test_data_url_carries_mime_and_bytes(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "ref.wav"
path.write_bytes(b"abc")
url = _data_url(path)
self.assertTrue(url.startswith("data:audio/wav;base64,"))
self.assertEqual(
base64.b64decode(url.partition(";base64,")[2]), b"abc")
class ConnectInputTests(unittest.TestCase):
"""Capability-driven validation before any HTTP is attempted."""
def setUp(self):
self._tmp = tempfile.TemporaryDirectory()
self.ref = Path(self._tmp.name) / "narrator.wav"
self.ref.write_bytes(b"abc")
self.addCleanup(self._tmp.cleanup)
def _client(self, model="higgs_audio_v3_tts", **kwargs):
# Bypass _connect (HTTP) — these tests cover the input checks.
with patch.object(SgOmniTTSClient, "_connect"):
return SgOmniTTSClient(_DUMMY_CHUNKS, model=model, **kwargs)
def test_unknown_model_raises(self):
with self.assertRaises(RuntimeError) as ctx:
self._client(model="nope")
self.assertIn("Unknown SGLang-Omni model", str(ctx.exception))
def test_design_model_requires_instructions(self):
with self.assertRaises(RuntimeError) as ctx:
self._client(model="qwen3_tts_1_7b_voicedesign")
self.assertIn("--instructions", str(ctx.exception))
def test_reference_required_model_refuses_to_connect_without_one(self):
with self.assertRaises(RuntimeError) as ctx:
self._client(model="qwen3_tts_1_7b_base")
self.assertIn("requires reference audio", str(ctx.exception))
def test_missing_reference_file_raises(self):
with self.assertRaises(RuntimeError) as ctx:
self._client(model="higgs_audio_v3_tts",
ref_audio=str(Path(self._tmp.name) / "gone.wav"))
self.assertIn("Reference audio not found", str(ctx.exception))
def test_clone_capable_model_allows_text_only(self):
client = self._client(model="higgs_audio_v3_tts")
self.assertIsNone(client.ref_audio)
def test_speaker_model_ignores_the_clone_reference(self):
client = self._client(model="qwen3_tts_0_6b_customvoice",
ref_audio=str(self.ref))
self.assertIsNone(client.ref_audio)
def test_seed_only_sent_for_models_that_accept_it(self):
with patch("converter.clients.sglomni.resolve_request_seed",
return_value=42):
client = self._client(model="qwen3_tts_1_7b_base",
ref_audio=str(self.ref))
self.assertEqual(client._seed, 42)
client = self._client(model="higgs_audio_v3_tts")
self.assertIsNone(client._seed)
def test_negative_seed_is_not_sent(self):
with patch("converter.clients.sglomni.resolve_request_seed",
return_value=-1):
client = self._client(model="qwen3_tts_1_7b_base",
ref_audio=str(self.ref))
self.assertIsNone(client._seed)
class ConnectHealthTests(unittest.TestCase):
"""_connect gates on /health and the hosted model."""
def _response(self, payload):
response = MagicMock()
response.__enter__.return_value = response
response.read.return_value = json.dumps(payload).encode("utf-8")
return response
def _connect(self, payloads, **kwargs):
# urlopen is called once per _get_json call, in order.
responses = [self._response(payload) for payload in payloads]
with patch("converter.clients.sglomni.urllib.request.urlopen",
side_effect=responses):
with patch.object(SgOmniTTSClient, "_resolve_reference_text"):
return SgOmniTTSClient(_DUMMY_CHUNKS,
model="higgs_audio_v3_tts", **kwargs)
def test_unreachable_server_raises_with_guidance(self):
import urllib.error
with patch("converter.clients.sglomni.urllib.request.urlopen",
side_effect=urllib.error.URLError("refused")):
with self.assertRaises(RuntimeError) as ctx:
SgOmniTTSClient(_DUMMY_CHUNKS, model="higgs_audio_v3_tts")
self.assertIn("not reachable", str(ctx.exception))
self.assertIn("sgl-omni", str(ctx.exception))
def test_booting_503_tells_the_user_to_wait(self):
# A booting sgl-omni answers /health with 503 + an "unhealthy"
# body (urlopen surfaces that as an HTTPError before any JSON
# could be inspected) — the message must say wait, not start.
with patch("converter.clients.sglomni.urllib.request.urlopen",
side_effect=_http_error(503, '{"status": "unhealthy"}')):
with self.assertRaises(RuntimeError) as ctx:
SgOmniTTSClient(_DUMMY_CHUNKS, model="higgs_audio_v3_tts")
message = str(ctx.exception)
self.assertIn("not healthy yet", message)
self.assertIn("HTTP 503", message)
self.assertIn("booting", message)
self.assertNotIn("not reachable", message)
def test_other_health_errors_name_the_code(self):
with patch("converter.clients.sglomni.urllib.request.urlopen",
side_effect=_http_error(404, "<html>nope</html>")):
with self.assertRaises(RuntimeError) as ctx:
SgOmniTTSClient(_DUMMY_CHUNKS, model="higgs_audio_v3_tts")
message = str(ctx.exception)
self.assertIn("HTTP 404", message)
self.assertIn("Is this an sgl-omni server?", message)
def test_booting_server_raises(self):
with self.assertRaises(RuntimeError) as ctx:
self._connect([{"status": "unhealthy"}])
self.assertIn("not healthy", str(ctx.exception))
def test_foreign_hosted_model_raises_with_both_names(self):
with self.assertRaises(RuntimeError) as ctx:
self._connect([
{"status": "healthy", "stages": []},
{"data": [{"id": "Zyphra/zonos2"}]},
])
message = str(ctx.exception)
self.assertIn("Zyphra/zonos2", message)
self.assertIn("bosonai/higgs-audio-v3-tts-4b", message)
def test_matching_model_connects(self):
client = self._connect([
{"status": "healthy", "stages": []},
{"data": [{"id": "bosonai/higgs-audio-v3-tts-4b"}]},
])
self.assertEqual(client.entry.repo, "bosonai/higgs-audio-v3-tts-4b")
class PayloadTests(unittest.TestCase):
"""The /v1/audio/speech request shape per voice capability."""
def setUp(self):
self._tmp = tempfile.TemporaryDirectory()
self.ref = Path(self._tmp.name) / "narrator.wav"
self.ref.write_bytes(b"abc")
self.addCleanup(self._tmp.cleanup)
def _make_client(self, model, **kwargs):
client = SgOmniTTSClient.__new__(SgOmniTTSClient)
from backends.sglomni.catalog import entry_by_key
client.entry = entry_by_key(model)
client.api_url = "http://127.0.0.1:8100"
client.voice = kwargs.get("voice")
if "ref_audio" in kwargs:
kwargs["ref_audio"] = str(self.ref)
client.ref_audio = kwargs.get("ref_audio")
client.ref_text = kwargs.get("ref_text", "")
client.instructions = kwargs.get("instructions", "")
client.language = "English"
client._seed = None
client._kv_fit = None
client._ref_audio_cached = None
return client
def test_speaker_payload_sends_the_preset_name(self):
client = self._make_client("qwen3_tts_0_6b_customvoice",
voice="Vivian")
payload = client._request_payload("Hello.")
self.assertEqual(payload["voice"], "Vivian")
self.assertEqual(payload["model"],
"Qwen/Qwen3-TTS-12Hz-0.6B-CustomVoice")
self.assertEqual(payload["response_format"], "wav")
self.assertNotIn("ref_audio", payload)
self.assertNotIn("task_type", payload)
def test_speaker_without_voice_uses_the_server_default(self):
client = self._make_client("voxtral_tts")
self.assertEqual(client._request_payload("Hello.")["voice"],
"default")
def test_design_payload_sends_task_type_and_instructions(self):
client = self._make_client("qwen3_tts_1_7b_voicedesign",
instructions="A warm narrator.")
payload = client._request_payload("Hello.")
self.assertEqual(payload["task_type"], "VoiceDesign")
self.assertEqual(payload["instructions"], "A warm narrator.")
def test_clone_payload_sends_reference_path_on_loopback(self):
client = self._make_client("higgs_audio_v3_tts",
ref_audio=str(self.ref),
ref_text="A transcript.")
payload = client._request_payload("Hello.")
self.assertEqual(payload["ref_audio"], str(self.ref.resolve()))
self.assertEqual(payload["ref_text"], "A transcript.")
def test_clone_payload_inlines_audio_for_remote_servers(self):
client = self._make_client("higgs_audio_v3_tts",
ref_audio=str(self.ref))
client.api_url = "http://10.20.30.40:8100"
payload = client._request_payload("Hello.")
self.assertTrue(payload["ref_audio"].startswith(
"data:audio/wav;base64,"))
self.assertEqual(
base64.b64decode(payload["ref_audio"].partition(";base64,")[2]),
b"abc")
self.assertNotIn("ref_text", payload)
def test_reference_audio_is_encoded_once_per_run(self):
# The clip cannot change mid-run: the data URL (or resolved path)
# is computed on the first sub-request and reused verbatim.
from converter.clients.sglomni import _data_url as real_data_url
client = self._make_client("higgs_audio_v3_tts",
ref_audio=str(self.ref))
client.api_url = "http://10.20.30.40:8100"
with patch("converter.clients.sglomni._data_url",
wraps=real_data_url) as encode:
first = client._request_payload("Hello.")
second = client._request_payload("Hello again.")
self.assertEqual(encode.call_count, 1)
self.assertEqual(first["ref_audio"], second["ref_audio"])
def test_clone_without_reference_sends_no_reference_fields(self):
client = self._make_client("higgs_audio_v3_tts")
payload = client._request_payload("Hello.")
self.assertNotIn("ref_audio", payload)
self.assertEqual(payload["voice"], "default")
def test_seed_included_when_resolved(self):
client = self._make_client("qwen3_tts_1_7b_base",
ref_audio="x.wav")
client._seed = 7
self.assertEqual(client._request_payload("Hello.")["seed"], 7)
def test_zonos2_payload_raises_the_generation_cap(self):
"""Zonos2's 1024-frame engine default caps a request at ~12 s."""
client = self._make_client("zonos2")
payload = client._request_payload("Hello.")
self.assertEqual(payload["max_new_tokens"], 12288)
def test_higgs_payload_raises_the_generation_cap(self):
"""Higgs's 2048-frame engine default caps a request at ~27 s
(75 fps); the catalog raises it to the most its admission window
allows (~40 s after the prompt tokens)."""
client = self._make_client("higgs_audio_v3_tts")
payload = client._request_payload("Hello.")
self.assertEqual(payload["max_new_tokens"], 3000)
def test_payload_keeps_a_learned_kv_fit(self):
"""A capacity learned from an admission rejection caps later
requests below the catalog value."""
client = self._make_client("higgs_audio_v3_tts")
client._kv_fit = 2500
self.assertEqual(client._request_payload("Hello.")["max_new_tokens"],
2500)
def test_models_without_a_cap_send_no_max_new_tokens(self):
client = self._make_client("moss_tts")
self.assertNotIn("max_new_tokens",
client._request_payload("Hello."))
class RequestErrorTests(unittest.TestCase):
"""OpenAI-style error envelopes decide retryability."""
def _make_client(self):
return SgOmniTTSClient.__new__(SgOmniTTSClient)
def test_bad_request_envelope_is_not_retryable(self):
client = self._make_client()
detail = json.dumps({"error": {
"message": "voice 'nope' not found",
"type": "BadRequestError", "code": 400}})
error = client._request_error(400, detail)
self.assertIsInstance(error, NonRetryableTTSError)
self.assertIn("voice 'nope' not found", str(error))
def test_server_error_is_retryable(self):
client = self._make_client()
error = client._request_error(503, "overloaded")
self.assertNotIsInstance(error, NonRetryableTTSError)
def test_non_json_4xx_is_not_retryable(self):
client = self._make_client()
error = client._request_error(422, "plain text rejection")
self.assertIsInstance(error, NonRetryableTTSError)
_KV_REJECTION_BODY = json.dumps({"error": {
"message": "Request requires more tokens than the thinker KV cache "
"can hold (input_tokens=684, max_new_tokens=12288, "
"required_tokens=12972, kv_capacity=4095). Current "
"mem_fraction_static is 0.800; try setting "
"--thinker-mem-fraction-static higher.",
"type": "InternalServerError", "code": 500}})
def _http_error(code: int, body: str) -> urllib.error.HTTPError:
return urllib.error.HTTPError(
"http://127.0.0.1:8100/v1/audio/speech", code, "error",
hdrs=None, fp=io.BytesIO(body.encode("utf-8")))
def _speech_response():
response = MagicMock()
response.__enter__.return_value = response
response.read.return_value = _WAV_BYTES
return response
class KvAdmissionTests(unittest.TestCase):
"""The KV-window admission rejection refits max_new_tokens once."""
def _client(self):
client = SgOmniTTSClient.__new__(SgOmniTTSClient)
from backends.sglomni.catalog import entry_by_key
client.entry = entry_by_key("higgs_audio_v3_tts")
client.api_url = "http://127.0.0.1:8100"
client.voice = None
client.ref_audio = None
client.ref_text = ""
client.instructions = ""
client.language = "English"
client._seed = None
client.chunk_size = None
client._kv_fit = None
return client
def test_fit_is_parsed_from_the_server_message(self):
fit = self._client()._kv_admission_fit(_KV_REJECTION_BODY)
# kv_capacity 4095 - input 684 - the 64-frame margin.
self.assertEqual(fit, 3347)
def test_fit_is_cached_for_later_requests(self):
client = self._client()
client._kv_admission_fit(_KV_REJECTION_BODY)
client._kv_admission_fit(_KV_REJECTION_BODY)
self.assertEqual(client._kv_fit, 3347)
def test_unrelated_errors_do_not_fit(self):
client = self._client()
self.assertIsNone(client._kv_admission_fit("CUDA out of memory"))
self.assertIsNone(client._kv_fit)
def test_a_window_below_the_floor_raises_with_guidance(self):
body = json.dumps({"error": {"message":
"Request requires more tokens than the thinker KV cache can "
"hold (input_tokens=4000, max_new_tokens=12288, "
"required_tokens=16288, kv_capacity=4095).", "code": 500}})
with self.assertRaises(NonRetryableTTSError) as ctx:
self._client()._kv_admission_fit(body)
self.assertIn("shorter reference clip", str(ctx.exception))
def test_request_wav_refits_and_resends_once(self):
client = self._client()
with patch(
"converter.clients.sglomni.urllib.request.urlopen",
side_effect=[_http_error(500, _KV_REJECTION_BODY),
_speech_response()]) as mock_open:
wav = client._request_wav("Hello.")
self.assertEqual(wav, _WAV_BYTES)
self.assertEqual(mock_open.call_count, 2)
refit = json.loads(mock_open.call_args[0][0].data)
self.assertEqual(refit["max_new_tokens"], 3347)
def test_request_wav_surfaces_a_refit_that_fails_again(self):
client = self._client()
with patch(
"converter.clients.sglomni.urllib.request.urlopen",
side_effect=[_http_error(500, _KV_REJECTION_BODY),
_http_error(500, _KV_REJECTION_BODY)]):
with self.assertRaises(RuntimeError) as ctx:
client._request_wav("Hello.")
message = str(ctx.exception)
self.assertIn("HTTP 500", message)
self.assertIn("thinker KV cache", message)
self.assertNotIsInstance(ctx.exception, NonRetryableTTSError)
def test_request_wav_does_not_refit_other_errors(self):
client = self._client()
with patch(
"converter.clients.sglomni.urllib.request.urlopen",
side_effect=[_http_error(500, "CUDA out of memory")]):
with self.assertRaises(RuntimeError) as ctx:
client._request_wav("Hello.")
self.assertIn("CUDA out of memory", str(ctx.exception))
self.assertIsNone(client._kv_fit)
def test_request_wav_keeps_the_refit_across_sub_requests(self):
# A tight window (a long reference clip): the fit binds below the
# 3000-frame catalog cap, and every later request carries it.
tight_body = json.dumps({"error": {"message":
"Request requires more tokens than the thinker KV cache can "
"hold (input_tokens=1500, max_new_tokens=3000, "
"required_tokens=4500, kv_capacity=4095).", "code": 500}})
client = self._client()
with patch(
"converter.clients.sglomni.urllib.request.urlopen",
side_effect=[_http_error(500, tight_body),
_speech_response(),
_speech_response()]) as mock_open:
client._request_wav("Hello.")
client._request_wav("Hello again.")
self.assertEqual(mock_open.call_count, 3)
second = json.loads(mock_open.call_args[0][0].data)
self.assertEqual(second["max_new_tokens"], 2531)
class ErrorClassificationTests(unittest.TestCase):
"""HTTP status → retry decision: every 4xx envelope is deterministic."""
def _request_error(self, status, detail):
client = SgOmniTTSClient.__new__(SgOmniTTSClient)
return client._request_error(status, detail)
def test_every_4xx_envelope_is_non_retryable(self):
# Including types outside the OpenAI-style names: the identical
# request fails identically on every attempt.
exception = self._request_error(
401, json.dumps({"error": {"message": "bad key",
"type": "AuthenticationError"}}))
self.assertIsInstance(exception, NonRetryableTTSError)
self.assertIn("bad key", str(exception))
def test_non_json_4xx_bodies_are_non_retryable(self):
exception = self._request_error(400, "plain text refusal")
self.assertIsInstance(exception, NonRetryableTTSError)
self.assertIn("plain text refusal", str(exception))
def test_5xx_stays_retryable(self):
exception = self._request_error(500, "CUDA out of memory")
self.assertNotIsInstance(exception, NonRetryableTTSError)
self.assertIn("CUDA out of memory", str(exception))
class GenerateChunkTests(unittest.TestCase):
"""Chunk generation: WAV output, sub-chunking, bookkeeping."""
def setUp(self):
self._tmp = tempfile.TemporaryDirectory()
self._sleep = patch("converter.clients.base.time.sleep")
self._sleep.start()
self.addCleanup(self._sleep.stop)
self.addCleanup(self._tmp.cleanup)
def _make_client(self):
client = SgOmniTTSClient.__new__(SgOmniTTSClient)
from backends.sglomni.catalog import entry_by_key
client.entry = entry_by_key("higgs_audio_v3_tts")
client.chunks_dir = Path(self._tmp.name)
client.api_url = "http://127.0.0.1:8100"
client.voice = None
client.ref_audio = None
client.ref_text = ""
client.instructions = ""
client.language = "English"
client._seed = None
client.chunk_size = None
client._kv_fit = None
return client
def _read_wav(self, path):
with wave.open(str(path), "rb") as wav_file:
return wav_file.readframes(wav_file.getnframes())
def test_generate_chunk_writes_the_wav_response(self):
client = self._make_client()
with patch.object(client, "_request_wav", return_value=_WAV_BYTES):
result = client.generate_chunk("Hello world.", 1)
self.assertIsNotNone(result)
path = Path(result)
self.assertEqual(path.name, "chunk_0001.wav")
self.assertEqual(self._read_wav(path), _WAV_FRAMES)
def test_long_text_is_subchunked_and_concatenated(self):
client = self._make_client()
text = " ".join(f"word{i}" for i in range(24))
responses = [_WAV_BYTES, _WAV_BYTES, _WAV_BYTES]
with patch.object(config, "CHUNK_SIZE", 10), \
patch.object(client, "_request_wav",
side_effect=responses) as mock_wav, \
patch("converter.clients.sglomni.concat_audio_files") as mock_concat:
result = client.generate_chunk(text, 1)
# 24 words at CHUNK_SIZE 10 -> three sub-requests (10/10/4).
self.assertEqual(mock_wav.call_count, 3)
self.assertIsNotNone(result)
mock_concat.assert_called_once()
args = mock_concat.call_args[0]
self.assertEqual(len(args[0]), 3)
self.assertEqual(args[1], Path(result))
def test_run_chunk_size_caps_the_sub_requests(self):
"""The pre-flight clamp (a chunk_words-capped model's popup
answer) overrides CHUNK_SIZE for this run."""
client = self._make_client()
client.chunk_size = 10
text = " ".join(f"word{i}" for i in range(24))
with patch.object(client, "_request_wav",
return_value=_WAV_BYTES) as mock_wav, \
patch("converter.clients.sglomni.concat_audio_files"):
client.generate_chunk(text, 1)
self.assertEqual(mock_wav.call_count, 3)
def test_single_subchunk_skips_concatenation(self):
client = self._make_client()
with patch.object(client, "_request_wav", return_value=_WAV_BYTES), \
patch("converter.clients.sglomni.concat_audio_files") as mock_concat:
client.generate_chunk("Hello.", 1)
mock_concat.assert_not_called()
def test_empty_text_fails_the_chunk(self):
client = self._make_client()
with patch.object(client, "_request_wav") as mock_wav:
self.assertIsNone(client.generate_chunk(" ", 1))
mock_wav.assert_not_called()
def test_request_failure_fails_the_chunk_attempt(self):
client = self._make_client()
with patch.object(client, "_request_wav",
side_effect=RuntimeError("down")) as mock_wav:
self.assertIsNone(client.generate_chunk("Hello.", 1))
self.assertEqual(mock_wav.call_count, 1)
def test_non_retryable_errors_propagate(self):
# Deterministic server errors must reach the retry loop directly
# (which skips its remaining attempts and re-raises with the
# actionable message), not come back as a generic failed attempt.
client = self._make_client()
with patch.object(client, "_request_wav",
side_effect=NonRetryableTTSError(
"unknown voice")):
with self.assertRaises(NonRetryableTTSError):
client.generate_chunk("Hello.", 1)
def test_retry_loop_skips_remaining_attempts(self):
client = self._make_client()
with patch.object(client, "_request_wav",
side_effect=NonRetryableTTSError(
"unknown voice")) as mock_wav:
with self.assertRaises(NonRetryableTTSError):
client.process_chunk_with_retry(1, "Hello.")
self.assertEqual(mock_wav.call_count, 1)
def test_a_non_wav_200_body_fails_the_request(self):
# A JSON error body served with HTTP 200 must not be written as
# chunk bytes (it would only fail later, confusingly, in the
# concat step).
client = self._make_client()
response = MagicMock()
response.__enter__.return_value = response
response.read.return_value = b'{"error": {"message": "nope"}}'
with patch("converter.clients.sglomni.urllib.request.urlopen",
return_value=response):
with self.assertRaises(RuntimeError) as ctx:
client._request_wav("Hello.")
self.assertIn("not a WAV file", str(ctx.exception))
def test_stale_chunk_files_are_removed(self):
stale = Path(self._tmp.name) / "chunk_0001.mp3"
stale.write_bytes(b"old")
client = self._make_client()
with patch.object(client, "_request_wav", return_value=_WAV_BYTES):
client.generate_chunk("Hello.", 1)
remaining = sorted(path.name for path in
Path(self._tmp.name).glob("chunk_0001.*"))
self.assertEqual(remaining, ["chunk_0001.wav"])
if __name__ == "__main__":
unittest.main()
|