30 lines
920 B
Python
30 lines
920 B
Python
|
|
"""百度 ASR:3301 时 WAV 重试。"""
|
|||
|
|
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import array
|
|||
|
|
from unittest.mock import MagicMock
|
|||
|
|
|
|||
|
|
from app.config import Settings
|
|||
|
|
from app.services.baidu_speech import BaiduSpeechService
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_asr_pcm_or_wav_fallback_retries_on_3301() -> None:
|
|||
|
|
ok = {"err_no": 0, "result": ["好"]}
|
|||
|
|
pcm = array.array("h", [100] * 800).tobytes()
|
|||
|
|
client = MagicMock()
|
|||
|
|
client.asr = MagicMock(side_effect=[{"err_no": 3301, "err_msg": "q"}, ok])
|
|||
|
|
svc = BaiduSpeechService(
|
|||
|
|
app_settings=Settings(
|
|||
|
|
BAIDU_APP_ID="1",
|
|||
|
|
BAIDU_API_KEY="k",
|
|||
|
|
BAIDU_SECRET_KEY="s",
|
|||
|
|
)
|
|||
|
|
)
|
|||
|
|
svc._client = client # type: ignore[attr-defined]
|
|||
|
|
r = svc.asr_16k_mono_pcm_or_wav_fallback(pcm)
|
|||
|
|
assert r == ok
|
|||
|
|
assert client.asr.call_count == 2
|
|||
|
|
assert client.asr.call_args_list[0][0][1] == "pcm"
|
|||
|
|
assert client.asr.call_args_list[1][0][1] == "wav"
|