From 51cf63009eac6838ebe55627aaa6c85722a91bc6 Mon Sep 17 00:00:00 2001 From: Tatlatat Date: Tue, 2 Jun 2026 18:43:29 +0700 Subject: [PATCH] TTS: include mp3 files in cache stats MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TTSService._put_cache writes .mp3 for MP3 audio (ID3/MPEG-framed bytes) and .wav otherwise, and the rest of the class treats both as cache entries (_get_cache iterates (".mp3", ".wav"); eviction globs "*.*"). But get_stats() enumerated the cache with `glob("*.wav")` only, so both cache_entries and cache_size_mb undercounted — reporting 0 whenever the cache held MP3 files, which is the common case for most TTS providers. Glob both extensions so the reported stats match what's actually cached. tests/test_tts_cache_stats.py writes an MP3-headed blob via _put_cache and asserts get_stats() reports one entry with non-zero size. Fails before this change. --- services/tts/tts_service.py | 2 +- tests/test_tts_cache_stats.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/test_tts_cache_stats.py diff --git a/services/tts/tts_service.py b/services/tts/tts_service.py index a78c8a7..79820f2 100644 --- a/services/tts/tts_service.py +++ b/services/tts/tts_service.py @@ -188,7 +188,7 @@ class TTSService: provider = settings["tts_provider"] tts_enabled = settings.get("tts_enabled", True) - cache_files = list(self.cache_dir.glob("*.wav")) + cache_files = list(self.cache_dir.glob("*.wav")) + list(self.cache_dir.glob("*.mp3")) cache_size = sum(f.stat().st_size for f in cache_files) is_available = self.available and tts_enabled diff --git a/tests/test_tts_cache_stats.py b/tests/test_tts_cache_stats.py new file mode 100644 index 0000000..00d2fe1 --- /dev/null +++ b/tests/test_tts_cache_stats.py @@ -0,0 +1,12 @@ +from services.tts.tts_service import TTSService + + +def test_tts_cache_stats_counts_mp3(tmp_path): + service = TTSService(cache_dir=str(tmp_path)) + + # Put an MP3-headed blob (starts with b'ID3') into cache, with size > 1MB so cache_size_mb > 0 + service._put_cache("k", b"ID3" + b"x" * (1024 * 1024)) + + stats = service.get_stats() + assert stats["cache_entries"] == 1 + assert stats["cache_size_mb"] > 0