"""
ASR 语音识别测试
使用 TTS 生成的音频文件测试识别
"""

from pathlib import Path
from src.Module.asr.asr import ASR, recognize, recognize_text


def test_recognize_wav():
    """测试识别 WAV 文件"""
    print("=" * 60)
    print("     ASR 语音识别测试")
    print("=" * 60)

    # 使用 TTS 测试生成的音频文件
    audio_file = Path(__file__).parent.parent / 'tts' / 'output' / 'stream_test.wav'

    if not audio_file.exists():
        print(f"[跳过] 音频文件不存在: {audio_file}")
        print("请先运行 TTS 测试生成音频文件")
        return False

    print(f"\n音频文件: {audio_file}")
    print(f"文件大小: {audio_file.stat().st_size / 1024:.1f} KB")

    # 创建 ASR 实例
    asr = ASR(
        model='qwen3-asr-flash',
        language='zh',
    )

    print("\n开始识别...")
    result = asr.recognize(str(audio_file))

    print(f"\n识别结果:")
    print(f"  成功: {result.success}")
    print(f"  文本: {result.text}")
    if result.error:
        print(f"  错误: {result.error}")
    if result.request_id:
        print(f"  请求ID: {result.request_id}")

    return result.success


def test_convenient_function():
    """测试便捷函数"""
    print("\n" + "=" * 60)
    print("便捷函数测试")
    print("=" * 60)

    audio_file = Path(__file__).parent.parent / 'tts' / \
        'output' / 'bidirectional_test.wav'

    if not audio_file.exists():
        print(f"[跳过] 音频文件不存在: {audio_file}")
        return False

    print(f"\n音频文件: {audio_file.name}")

    # 使用便捷函数
    text = recognize_text(str(audio_file), language='zh')

    print(f"识别文本: {text}")

    return len(text) > 0


if __name__ == '__main__':
    results = []

    success1 = test_recognize_wav()
    results.append(("WAV 文件识别", success1))

    success2 = test_convenient_function()
    results.append(("便捷函数", success2))

    print("\n" + "=" * 60)
    print("测试结果:")
    for name, success in results:
        status = "✓ 通过" if success else "✗ 失败/跳过"
        print(f"  {name}: {status}")
