aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_audio.py67
-rw-r--r--tests/test_chunking.py70
-rw-r--r--tests/test_tts.py155
3 files changed, 280 insertions, 12 deletions
diff --git a/tests/test_audio.py b/tests/test_audio.py
index fb448f8..c043766 100644
--- a/tests/test_audio.py
+++ b/tests/test_audio.py
@@ -1,9 +1,11 @@
"""Tests for audio helpers: speed parameters, chunk cleanup, encoding,
-command construction, and duration verification."""
+command construction, duration verification, and audio concatenation."""
import tempfile
import unittest
+import wave
from pathlib import Path
+from unittest.mock import patch
from converter import audio
from converter import config
@@ -17,6 +19,7 @@ from converter.audio import (
build_ffmetadata,
build_m4b_chapters_command,
cleanup_chunks,
+ concat_audio_files,
speed_export_params,
verify_output_duration,
)
@@ -422,5 +425,67 @@ class BuildM4bChaptersCommandMetaTests(unittest.TestCase):
self.assertNotIn("-metadata", cmd)
+class ConcatAudioFilesTests(unittest.TestCase):
+ """Concatenation of sub-request audio into one chunk file."""
+
+ @staticmethod
+ def _write_wav(path: Path, frames: bytes, framerate: int = 24000) -> Path:
+ with wave.open(str(path), "wb") as wav_file:
+ wav_file.setnchannels(1)
+ wav_file.setsampwidth(2)
+ wav_file.setframerate(framerate)
+ wav_file.writeframes(frames)
+ return path
+
+ def test_wav_files_are_merged_in_order(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ first = self._write_wav(Path(tmp) / "a.wav", b"\x01\x00" * 10)
+ second = self._write_wav(Path(tmp) / "b.wav", b"\x02\x00" * 20)
+ destination = Path(tmp) / "out.wav"
+ concat_audio_files([first, second], destination)
+ with wave.open(str(destination), "rb") as wav_file:
+ self.assertEqual(wav_file.getframerate(), 24000)
+ self.assertEqual(wav_file.getnchannels(), 1)
+ self.assertEqual(wav_file.getsampwidth(), 2)
+ frames = wav_file.readframes(wav_file.getnframes())
+ self.assertEqual(frames, b"\x01\x00" * 10 + b"\x02\x00" * 20)
+
+ def test_single_wav_file_is_copied(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ source = self._write_wav(Path(tmp) / "a.wav", b"\x03\x00" * 15)
+ destination = Path(tmp) / "out.wav"
+ concat_audio_files([source], destination)
+ with wave.open(str(destination), "rb") as wav_file:
+ self.assertEqual(wav_file.readframes(wav_file.getnframes()),
+ b"\x03\x00" * 15)
+
+ def test_empty_source_list_raises(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ with self.assertRaises(ValueError):
+ concat_audio_files([], Path(tmp) / "out.wav")
+
+ def test_mismatched_wav_parameters_fall_back_to_ffmpeg(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ first = self._write_wav(Path(tmp) / "a.wav", b"\x01\x00" * 10, framerate=24000)
+ second = self._write_wav(Path(tmp) / "b.wav", b"\x02\x00" * 10, framerate=16000)
+ destination = Path(tmp) / "out.wav"
+ with patch("converter.audio.shutil.which", return_value=None), \
+ self.assertRaises(RuntimeError) as ctx:
+ concat_audio_files([first, second], destination)
+ self.assertIn("ffmpeg", str(ctx.exception))
+ # The wave-module path must not have written a partial output.
+ self.assertFalse(destination.exists())
+
+ def test_non_wav_input_falls_back_to_ffmpeg(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ source = Path(tmp) / "part.mp3"
+ source.write_bytes(b"not a wav file")
+ destination = Path(tmp) / "out.wav"
+ with patch("converter.audio.shutil.which", return_value=None), \
+ self.assertRaises(RuntimeError) as ctx:
+ concat_audio_files([source], destination)
+ self.assertIn("ffmpeg", str(ctx.exception))
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/tests/test_chunking.py b/tests/test_chunking.py
index d1d95bd..2062b4e 100644
--- a/tests/test_chunking.py
+++ b/tests/test_chunking.py
@@ -1,18 +1,45 @@
"""Tests for text chunking."""
import unittest
+from unittest.mock import patch
from converter import config
from converter.chunking import split_into_chunks
class ChunkSizeDefaultTests(unittest.TestCase):
- """Guard the default chunk size: each API call is one model generation,
- and long single generations lose prosody, can turn garbled, and are
- truncated at the model's token limit (text past it is never spoken)."""
+ """Guard the request-size settings: each API call is one model
+ generation, and the servers silently truncate audio past their caps
+ (~2.5 min faster backend, ~11 min Gradio demo), so both the default
+ chunk size and the hard ceiling must stay well inside that budget."""
- def test_default_chunk_size_within_single_generation_budget(self):
- self.assertLessEqual(config.CHUNK_SIZE_WORDS, 60)
+ def test_default_chunk_size_within_request_ceiling(self):
+ self.assertLessEqual(config.CHUNK_SIZE_WORDS, config.MAX_REQUEST_WORDS)
+
+ def test_request_ceiling_within_single_generation_budget(self):
+ self.assertLessEqual(config.MAX_REQUEST_WORDS, 300)
+
+ def test_sizes_are_positive(self):
+ self.assertGreaterEqual(config.CHUNK_SIZE_WORDS, 1)
+ self.assertGreaterEqual(config.MAX_REQUEST_WORDS, 1)
+
+
+class RequestCeilingClampTests(unittest.TestCase):
+ def test_oversized_chunk_size_is_clamped_with_warning(self):
+ text = " ".join(f"word{i}" for i in range(30)) + "."
+ with patch.object(config, "MAX_REQUEST_WORDS", 10), \
+ self.assertLogs("converter.chunking", level="WARNING") as logs:
+ chunks = split_into_chunks(text, max_words=5000)
+ self.assertTrue(all(len(chunk.split()) <= 10 for chunk in chunks))
+ self.assertIn("clamped", " ".join(logs.output))
+
+ def test_default_ceiling_clamps_realistic_configuration(self):
+ sentences = " ".join(
+ f"S{i} " + " ".join(["word"] * 8) + "." for i in range(60))
+ chunks = split_into_chunks(sentences, max_words=5000)
+ self.assertGreater(len(chunks), 1)
+ self.assertTrue(all(len(chunk.split()) <= config.MAX_REQUEST_WORDS
+ for chunk in chunks))
class SplitIntoChunksTests(unittest.TestCase):
@@ -54,16 +81,39 @@ class SplitIntoChunksTests(unittest.TestCase):
self.assertNotIn("12: 30", joined)
def test_clause_split_requires_whitespace_after_punctuation(self):
- # Run-on clauses without spaces after commas have no split point and
- # must stay byte-identical rather than being re-joined with spaces.
+ # Run-on clauses without spaces after commas have no clause split
+ # point, so the last-resort word-boundary split fires instead.
+ # Tokens themselves (and numbers like "1,000,000") stay intact.
sentence = ",".join([" ".join(["w"] * 5) for _ in range(10)]) + "."
chunks = split_into_chunks(sentence, max_words=12)
- self.assertEqual(chunks, [sentence])
+ self.assertGreater(len(chunks), 1)
+ self.assertTrue(all(len(chunk.split()) <= 12 for chunk in chunks))
+ tokens = sentence.replace(",", " , ").split()
+ rejoined = " ".join(chunks).replace(",", " , ").split()
+ self.assertEqual(rejoined, tokens)
- def test_single_oversized_sentence_stays_intact(self):
+ def test_single_oversized_sentence_is_word_split(self):
+ # A punctuation-free sentence longer than the limit is split at word
+ # boundaries: the request-size ceiling is a hard limit because the
+ # TTS servers silently truncate oversized generations.
sentence = " ".join(["word"] * 30) + "."
chunks = split_into_chunks(sentence, max_words=10)
- self.assertEqual(chunks, [sentence])
+ self.assertGreater(len(chunks), 1)
+ self.assertTrue(all(len(chunk.split()) <= 10 for chunk in chunks))
+ self.assertEqual(sum(len(chunk.split()) for chunk in chunks), 30)
+
+ def test_word_split_never_breaks_number_tokens(self):
+ # Numbers and other punctuation-bearing tokens are single words and
+ # must never be broken apart by the last-resort word split.
+ sentence = ("There were exactly 1,000,000 soldiers marching at 12:30 "
+ "and " + "they kept marching onward " * 20) + "endlessly."
+ chunks = split_into_chunks(sentence, max_words=10)
+ self.assertGreater(len(chunks), 1)
+ joined = " ".join(chunks)
+ self.assertIn("1,000,000", joined)
+ self.assertIn("12:30", joined)
+ self.assertNotIn("1, 000", joined)
+ self.assertNotIn("12: 30", joined)
if __name__ == "__main__":
diff --git a/tests/test_tts.py b/tests/test_tts.py
index 47a8309..26f663e 100644
--- a/tests/test_tts.py
+++ b/tests/test_tts.py
@@ -241,13 +241,25 @@ class FasterTTSClientGenerateTests(unittest.TestCase):
sentences = [" ".join(f"word{i}" for i in range(6)) + "." for _ in range(3)]
text = " ".join(sentences)
pcm_parts = [b"\x01\x00" * 10, b"\x02\x00" * 20, b"\x03\x00" * 30]
- with patch.object(config, "CHUNK_SIZE_WORDS", 10), \
+ with patch.object(config, "MAX_REQUEST_WORDS", 10), \
patch.object(client, "_request_pcm", side_effect=pcm_parts) as mock_pcm:
result = client.generate_chunk(text, 1)
self.assertEqual(mock_pcm.call_count, 3)
_, _, _, frames = self._read_wav(Path(result))
self.assertEqual(frames, b"".join(pcm_parts))
+ def test_subchunk_size_is_clamped_to_request_ceiling(self):
+ client = self._make_client()
+ text = " ".join(f"word{i}" for i in range(8))
+ pcm = b"\x01\x00" * 10
+ with patch.object(config, "CHUNK_SIZE_WORDS", 4), \
+ patch.object(client, "_request_pcm", return_value=pcm) as mock_pcm:
+ result = client.generate_chunk(text, 1)
+ # CHUNK_SIZE_WORDS no longer drives request size: the hard ceiling
+ # does, so the whole (8-word) text is one request here.
+ self.assertEqual(mock_pcm.call_count, 1)
+ self.assertIsNotNone(result)
+
def test_stale_chunk_files_are_removed(self):
stale = Path(self._tmp.name) / "chunk_0001.mp3"
stale.write_bytes(b"old")
@@ -316,6 +328,147 @@ class FasterTTSClientGenerateTests(unittest.TestCase):
self.assertEqual(payload["response_format"], "pcm")
+class TruncationDetectionTests(unittest.TestCase):
+ """check_for_truncation: servers cut audio silently past their caps, so
+ grossly short audio must fail the request (then retry / fail visibly)."""
+
+ def test_duration_below_ratio_raises(self):
+ with self.assertRaises(RuntimeError) as ctx:
+ tts.check_for_truncation(" ".join(["w"] * 150), 25.0, "Chunk 1")
+ self.assertIn("truncated", str(ctx.exception))
+
+ def test_duration_at_ratio_passes(self):
+ tts.check_for_truncation(" ".join(["w"] * 150), 30.0, "Chunk 1")
+
+ def test_unknown_duration_skips_check(self):
+ tts.check_for_truncation(" ".join(["w"] * 150), None, "Chunk 1")
+
+ def test_short_requests_skip_check(self):
+ tts.check_for_truncation(" ".join(["w"] * 9), 0.0, "Chunk 1")
+
+ def test_zero_duration_fails_checked_requests(self):
+ with self.assertRaises(RuntimeError):
+ tts.check_for_truncation(" ".join(["w"] * 150), 0.0, "Chunk 1")
+
+
+class FasterTTSClientTruncationTests(unittest.TestCase):
+ """Audio far shorter than its text implies fails the faster request."""
+
+ def setUp(self):
+ self._tmp = tempfile.TemporaryDirectory()
+ self._chunks = patch.object(tts, "CHUNKS_FOLDER", Path(self._tmp.name))
+ self._chunks.start()
+
+ def tearDown(self):
+ self._chunks.stop()
+ self._tmp.cleanup()
+
+ def _make_client(self):
+ client = FasterTTSClient.__new__(FasterTTSClient)
+ client.voice = "default"
+ client.api_url = "http://127.0.0.1:8000"
+ return client
+
+ def test_truncated_pcm_fails_the_chunk(self):
+ client = self._make_client()
+ text = " ".join(f"word{i}" for i in range(12))
+ with patch.object(client, "_request_pcm", return_value=b"\x01\x00" * 24), \
+ self.assertLogs("converter.tts", level="ERROR") as logs:
+ result = client.generate_chunk(text, 1)
+ self.assertIsNone(result)
+ self.assertTrue(any("truncated" in line for line in logs.output))
+
+ def test_full_length_pcm_passes(self):
+ client = self._make_client()
+ text = " ".join(f"word{i}" for i in range(12))
+ # 12 words -> expected 4.8s, half is 2.4s -> 2.5s of audio passes.
+ pcm = b"\x01\x00" * int(2.5 * tts.SAMPLE_RATE)
+ with patch.object(client, "_request_pcm", return_value=pcm):
+ result = client.generate_chunk(text, 1)
+ self.assertIsNotNone(result)
+
+
+class QwenTTSClientGenerateTests(unittest.TestCase):
+ """Qwen chunk generation: sub-request splitting and concatenation."""
+
+ def setUp(self):
+ self._tmp = tempfile.TemporaryDirectory()
+ self._chunks = patch.object(tts, "CHUNKS_FOLDER", Path(self._tmp.name))
+ self._chunks.start()
+
+ def tearDown(self):
+ self._chunks.stop()
+ self._tmp.cleanup()
+
+ def _make_client(self):
+ client = QwenTTSClient.__new__(QwenTTSClient)
+ client.voice_mode = tts.VOICE_MODE_CUSTOM
+ return client
+
+ @staticmethod
+ def _write_wav(path: Path, frames: bytes) -> Path:
+ with wave.open(str(path), "wb") as wav_file:
+ wav_file.setnchannels(1)
+ wav_file.setsampwidth(2)
+ wav_file.setframerate(tts.SAMPLE_RATE)
+ wav_file.writeframes(frames)
+ return path
+
+ def _read_wav_frames(self, path: Path) -> bytes:
+ with wave.open(str(path), "rb") as wav_file:
+ return wav_file.readframes(wav_file.getnframes())
+
+ def test_single_request_copies_audio(self):
+ client = self._make_client()
+ source = self._write_wav(Path(self._tmp.name) / "server.wav", b"\x01\x00" * 50)
+ with patch.object(client, "_generate_custom_voice",
+ return_value=(str(source),)) as mock_generate:
+ result = client.generate_chunk("Hello world.", 1)
+ mock_generate.assert_called_once_with("Hello world.")
+ path = Path(result)
+ self.assertEqual(path.name, "chunk_0001.wav")
+ self.assertEqual(self._read_wav_frames(path), b"\x01\x00" * 50)
+
+ def test_oversized_input_is_split_and_concatenated_in_order(self):
+ client = self._make_client()
+ first = self._write_wav(Path(self._tmp.name) / "one.wav", b"\x01\x00" * 10)
+ second = self._write_wav(Path(self._tmp.name) / "two.wav", b"\x02\x00" * 20)
+ text = " ".join(f"word{i}" for i in range(12))
+ with patch.object(config, "MAX_REQUEST_WORDS", 5), \
+ patch.object(client, "_generate_custom_voice",
+ side_effect=[(str(first),), (str(second),),
+ (str(first),)]) as mock_generate:
+ result = client.generate_chunk(text, 1)
+ self.assertEqual(mock_generate.call_count, 3)
+ path = Path(result)
+ self.assertEqual(path.name, "chunk_0001.wav")
+ self.assertEqual(self._read_wav_frames(path),
+ b"\x01\x00" * 10 + b"\x02\x00" * 20 + b"\x01\x00" * 10)
+ for call in mock_generate.call_args_list:
+ self.assertLessEqual(len(call[0][0].split()), 5)
+
+ def test_truncated_sub_request_fails_the_chunk(self):
+ client = self._make_client()
+ # 0.1s of audio for 12 words (expected >= 2.4s).
+ source = self._write_wav(Path(self._tmp.name) / "short.wav",
+ b"\x01\x00" * int(0.1 * tts.SAMPLE_RATE))
+ text = " ".join(f"word{i}" for i in range(12))
+ with patch.object(client, "_generate_custom_voice",
+ return_value=(str(source),)) as mock_generate, \
+ self.assertLogs("converter.tts", level="ERROR") as logs:
+ result = client.generate_chunk(text, 1)
+ self.assertIsNone(result)
+ self.assertEqual(mock_generate.call_count, 1)
+ self.assertTrue(any("truncated" in line for line in logs.output))
+
+ def test_empty_text_fails_the_chunk(self):
+ client = self._make_client()
+ with patch.object(client, "_generate_custom_voice") as mock_generate:
+ result = client.generate_chunk(" ", 1)
+ self.assertIsNone(result)
+ mock_generate.assert_not_called()
+
+
class FasterModeWiringTests(unittest.TestCase):
"""AudiobookConverter wiring for the --faster backend."""