|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +from unittest.mock import AsyncMock, MagicMock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from astrbot.core.provider.sources.typecast_tts_source import ProviderTypecastTTS |
| 8 | + |
| 9 | + |
| 10 | +def _make_provider(**overrides) -> ProviderTypecastTTS: |
| 11 | + config = { |
| 12 | + "id": "test-typecast", |
| 13 | + "type": "typecast_tts", |
| 14 | + "api_key": "test-api-key", |
| 15 | + "typecast-voice-id": "tc_60e5426de8b95f1d3000d7b5", |
| 16 | + "model": "ssfm-v30", |
| 17 | + "language": "kor", |
| 18 | + "typecast-emotion-preset": "normal", |
| 19 | + "typecast-emotion-intensity": 1.0, |
| 20 | + "typecast-volume": 100, |
| 21 | + "typecast-pitch": 0, |
| 22 | + "typecast-tempo": 1.0, |
| 23 | + "timeout": 30, |
| 24 | + } |
| 25 | + config.update(overrides) |
| 26 | + return ProviderTypecastTTS(provider_config=config, provider_settings={}) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.asyncio |
| 30 | +async def test_get_audio_success(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): |
| 31 | + """Successful API call saves WAV and returns path.""" |
| 32 | + provider = _make_provider() |
| 33 | + |
| 34 | + monkeypatch.setattr( |
| 35 | + "astrbot.core.provider.sources.typecast_tts_source.get_astrbot_temp_path", |
| 36 | + lambda: str(tmp_path), |
| 37 | + ) |
| 38 | + |
| 39 | + fake_response = AsyncMock() |
| 40 | + fake_response.status_code = 200 |
| 41 | + fake_response.headers = {"content-type": "audio/wav"} |
| 42 | + fake_response.aiter_bytes = lambda: _async_iter([b"RIFF", b"fake_wav_data"]) |
| 43 | + |
| 44 | + fake_client = AsyncMock() |
| 45 | + fake_client.__aenter__ = AsyncMock(return_value=fake_client) |
| 46 | + fake_client.__aexit__ = AsyncMock(return_value=False) |
| 47 | + fake_client.stream = MagicMock(return_value=_async_context_manager(fake_response)) |
| 48 | + |
| 49 | + monkeypatch.setattr( |
| 50 | + "astrbot.core.provider.sources.typecast_tts_source.AsyncClient", |
| 51 | + lambda **kwargs: fake_client, |
| 52 | + ) |
| 53 | + |
| 54 | + path = await provider.get_audio("Hello world") |
| 55 | + |
| 56 | + assert path.endswith(".wav") |
| 57 | + assert os.path.exists(path) |
| 58 | + with open(path, "rb") as f: |
| 59 | + assert f.read() == b"RIFFfake_wav_data" |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.asyncio |
| 63 | +async def test_get_audio_api_error(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): |
| 64 | + """API error raises RuntimeError with detail message.""" |
| 65 | + provider = _make_provider() |
| 66 | + |
| 67 | + monkeypatch.setattr( |
| 68 | + "astrbot.core.provider.sources.typecast_tts_source.get_astrbot_temp_path", |
| 69 | + lambda: str(tmp_path), |
| 70 | + ) |
| 71 | + |
| 72 | + fake_response = AsyncMock() |
| 73 | + fake_response.status_code = 401 |
| 74 | + fake_response.headers = {"content-type": "application/json"} |
| 75 | + fake_response.aread = AsyncMock(return_value=b'{"detail": "Invalid API key"}') |
| 76 | + |
| 77 | + fake_client = AsyncMock() |
| 78 | + fake_client.__aenter__ = AsyncMock(return_value=fake_client) |
| 79 | + fake_client.__aexit__ = AsyncMock(return_value=False) |
| 80 | + fake_client.stream = MagicMock(return_value=_async_context_manager(fake_response)) |
| 81 | + |
| 82 | + monkeypatch.setattr( |
| 83 | + "astrbot.core.provider.sources.typecast_tts_source.AsyncClient", |
| 84 | + lambda **kwargs: fake_client, |
| 85 | + ) |
| 86 | + |
| 87 | + with pytest.raises(RuntimeError, match="Invalid API key"): |
| 88 | + await provider.get_audio("Hello world") |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.asyncio |
| 92 | +async def test_get_audio_request_body(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): |
| 93 | + """Verify the request body sent to Typecast API.""" |
| 94 | + provider = _make_provider( |
| 95 | + **{ |
| 96 | + "typecast-emotion-preset": "happy", |
| 97 | + "typecast-emotion-intensity": 1.5, |
| 98 | + "typecast-pitch": 3, |
| 99 | + "typecast-tempo": 1.2, |
| 100 | + } |
| 101 | + ) |
| 102 | + |
| 103 | + monkeypatch.setattr( |
| 104 | + "astrbot.core.provider.sources.typecast_tts_source.get_astrbot_temp_path", |
| 105 | + lambda: str(tmp_path), |
| 106 | + ) |
| 107 | + |
| 108 | + captured_kwargs = {} |
| 109 | + |
| 110 | + fake_response = AsyncMock() |
| 111 | + fake_response.status_code = 200 |
| 112 | + fake_response.headers = {"content-type": "audio/wav"} |
| 113 | + fake_response.aiter_bytes = lambda: _async_iter([b"fake_wav"]) |
| 114 | + |
| 115 | + fake_client = AsyncMock() |
| 116 | + fake_client.__aenter__ = AsyncMock(return_value=fake_client) |
| 117 | + fake_client.__aexit__ = AsyncMock(return_value=False) |
| 118 | + |
| 119 | + def capture_stream(method, url, **kwargs): |
| 120 | + captured_kwargs.update(kwargs) |
| 121 | + return _async_context_manager(fake_response) |
| 122 | + |
| 123 | + fake_client.stream = capture_stream |
| 124 | + |
| 125 | + monkeypatch.setattr( |
| 126 | + "astrbot.core.provider.sources.typecast_tts_source.AsyncClient", |
| 127 | + lambda **kwargs: fake_client, |
| 128 | + ) |
| 129 | + |
| 130 | + await provider.get_audio("Test text") |
| 131 | + |
| 132 | + body = captured_kwargs["json"] |
| 133 | + assert body["voice_id"] == "tc_60e5426de8b95f1d3000d7b5" |
| 134 | + assert body["text"] == "Test text" |
| 135 | + assert body["model"] == "ssfm-v30" |
| 136 | + assert body["language"] == "kor" |
| 137 | + assert body["prompt"]["emotion_type"] == "preset" |
| 138 | + assert body["prompt"]["emotion_preset"] == "happy" |
| 139 | + assert body["prompt"]["emotion_intensity"] == 1.5 |
| 140 | + assert body["output"]["audio_pitch"] == 3 |
| 141 | + assert body["output"]["audio_tempo"] == 1.2 |
| 142 | + assert body["output"]["audio_format"] == "wav" |
| 143 | + assert body["output"]["volume"] == 100 |
| 144 | + |
| 145 | + |
| 146 | +def test_provider_config_defaults(): |
| 147 | + """Default config values are applied correctly.""" |
| 148 | + provider = ProviderTypecastTTS( |
| 149 | + provider_config={ |
| 150 | + "id": "test-typecast", |
| 151 | + "type": "typecast_tts", |
| 152 | + "api_key": "test-api-key", |
| 153 | + "typecast-voice-id": "tc_60e5426de8b95f1d3000d7b5", |
| 154 | + }, |
| 155 | + provider_settings={}, |
| 156 | + ) |
| 157 | + assert provider.voice_id == "tc_60e5426de8b95f1d3000d7b5" |
| 158 | + assert provider.model_name == "ssfm-v30" |
| 159 | + assert provider.language == "kor" |
| 160 | + assert provider.emotion_preset == "normal" |
| 161 | + assert provider.emotion_intensity == 1.0 |
| 162 | + assert provider.volume == 100 |
| 163 | + assert provider.pitch == 0 |
| 164 | + assert provider.tempo == 1.0 |
| 165 | + assert provider.timeout == 30 |
| 166 | + |
| 167 | + |
| 168 | +def test_provider_config_invalid_numbers_use_defaults(): |
| 169 | + """Invalid numeric config values fall back to defaults.""" |
| 170 | + provider = ProviderTypecastTTS( |
| 171 | + provider_config={ |
| 172 | + "id": "test-typecast", |
| 173 | + "type": "typecast_tts", |
| 174 | + "api_key": "test-api-key", |
| 175 | + "typecast-voice-id": "tc_60e5426de8b95f1d3000d7b5", |
| 176 | + "typecast-emotion-intensity": "not-a-number", |
| 177 | + "typecast-volume": "not-a-number", |
| 178 | + "typecast-pitch": "not-a-number", |
| 179 | + "typecast-tempo": "not-a-number", |
| 180 | + "timeout": "not-a-number", |
| 181 | + }, |
| 182 | + provider_settings={}, |
| 183 | + ) |
| 184 | + assert provider.emotion_intensity == 1.0 |
| 185 | + assert provider.volume == 100 |
| 186 | + assert provider.pitch == 0 |
| 187 | + assert provider.tempo == 1.0 |
| 188 | + assert provider.timeout == 30 |
| 189 | + |
| 190 | + |
| 191 | +# --- Test helpers --- |
| 192 | + |
| 193 | +async def _async_iter(items): |
| 194 | + for item in items: |
| 195 | + yield item |
| 196 | + |
| 197 | + |
| 198 | +class _async_context_manager: |
| 199 | + def __init__(self, response): |
| 200 | + self.response = response |
| 201 | + |
| 202 | + async def __aenter__(self): |
| 203 | + return self.response |
| 204 | + |
| 205 | + async def __aexit__(self, *args): |
| 206 | + pass |
0 commit comments