File size: 1,102 Bytes
aefc853
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""TTS in-process LRU cache tests. Skipped when routes_training's deps (httpx)
aren't installed."""
import pytest

rt = pytest.importorskip("app.routes_training")


@pytest.fixture(autouse=True)
def _clear_cache():
    rt._tts_cache.clear()
    yield
    rt._tts_cache.clear()


def test_put_then_get_returns_bytes():
    key = rt._tts_cache_key("voiceA", "halo dunia")
    assert rt._tts_cache_get(key) is None
    rt._tts_cache_put(key, b"audio-bytes")
    assert rt._tts_cache_get(key) == b"audio-bytes"


def test_key_varies_by_voice_and_text():
    assert rt._tts_cache_key("v1", "x") != rt._tts_cache_key("v2", "x")
    assert rt._tts_cache_key("v1", "x") != rt._tts_cache_key("v1", "y")


def test_lru_eviction_bounds_size():
    for i in range(rt._TTS_CACHE_MAX + 10):
        rt._tts_cache_put(rt._tts_cache_key("v", str(i)), b"a")
    assert len(rt._tts_cache) == rt._TTS_CACHE_MAX
    # The oldest entries should have been evicted.
    assert rt._tts_cache_get(rt._tts_cache_key("v", "0")) is None
    assert rt._tts_cache_get(rt._tts_cache_key("v", str(rt._TTS_CACHE_MAX + 9))) is not None