from __future__ import annotations

import pytest

from core.agent.tools.asr_fun_asr import FunASRTool


def test_transcribe_uses_injected_dashscope_callable() -> None:
    def fake_transcribe(*, audio_bytes: bytes, filename: str) -> dict[str, str]:
        assert filename == "voice.wav"
        assert audio_bytes == b"audio"
        return {"text": "你好", "request_id": "req-1"}

    tool = FunASRTool(transcribe_callable=fake_transcribe)

    result = tool.transcribe(audio_bytes=b"audio", filename="voice.wav")

    assert result["text"] == "你好"
    assert result["request_id"] == "req-1"
    assert result["model"] == "fun-asr-realtime-2025-11-07"


def test_transcribe_raises_runtime_error_when_provider_fails() -> None:
    def fake_transcribe(*, audio_bytes: bytes, filename: str) -> dict[str, str]:
        raise RuntimeError("upstream timeout")

    tool = FunASRTool(transcribe_callable=fake_transcribe)

    with pytest.raises(RuntimeError):
        tool.transcribe(audio_bytes=b"audio", filename="voice.wav")
