test_smoke.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # 端到端冒烟测试脚本
  2. # 验证:录音 → ASR → LLM → TTS → 播放 完整流程
  3. import sys
  4. import time
  5. from config import Config
  6. def print_header(text):
  7. print('\n' + '=' * 60)
  8. print(f' {text}')
  9. print('=' * 60)
  10. def print_result(success, message):
  11. icon = '✅' if success else '❌'
  12. print(f'{icon} {message}')
  13. def test_config():
  14. """测试 1: 配置验证"""
  15. print_header('测试 1: 配置验证')
  16. try:
  17. Config.validate()
  18. print_result(True, f'配置 OK')
  19. print(f' TTS: {Config.TTS_BASE_URL}')
  20. print(f' LLM: {Config.LLM_PROVIDER}')
  21. print(f' ASR: {Config.ASR_PROVIDER}')
  22. return True
  23. except ValueError as e:
  24. print_result(False, str(e))
  25. return False
  26. def test_tts_server():
  27. """测试 2: TTS 服务连通性"""
  28. print_header('测试 2: TTS 服务连通性')
  29. try:
  30. import requests
  31. # 测试 /tts/voices 是否可达
  32. url = f'{Config.TTS_BASE_URL}/tts/voices'
  33. response = requests.get(url, timeout=5)
  34. if response.status_code == 200:
  35. data = response.json()
  36. voices = data.get('data', {}).get('voices', [])
  37. print_result(True, f'TTS 服务可达,{len(voices)} 个音色')
  38. return True
  39. else:
  40. print_result(False, f'服务返回 {response.status_code}')
  41. return False
  42. except Exception as e:
  43. print_result(False, f'连接失败: {e}')
  44. print(f' 请确认后端服务在 {Config.TTS_BASE_URL} 运行')
  45. return False
  46. def test_tts_sync():
  47. """测试 3: 同步合成端点"""
  48. print_header('测试 3: 同步合成端点 (/tts/synthesize)')
  49. try:
  50. from tts_client import synthesize
  51. text = '冒烟测试,同步合成端点是否正常'
  52. print(f' 测试文本: "{text}"')
  53. start = time.time()
  54. audio_url = synthesize(text, voice_id=Config.TTS_VOICE_ID)
  55. elapsed = time.time() - start
  56. print_result(True, f'同步合成成功 ({elapsed:.2f}s)')
  57. print(f' 音频 URL: {audio_url}')
  58. if elapsed > 3:
  59. print_result(False, f'延迟偏高({elapsed:.2f}s),理想 < 1s')
  60. return True
  61. except Exception as e:
  62. print_result(False, f'同步合成失败: {e}')
  63. return False
  64. def test_tts_download():
  65. """测试 4: 音频下载"""
  66. print_header('测试 4: 音频下载')
  67. try:
  68. from tts_client import synthesize, download_audio
  69. audio_url = synthesize('测试下载', voice_id=Config.TTS_VOICE_ID)
  70. local_path = download_audio(audio_url)
  71. import os
  72. size = os.path.getsize(local_path)
  73. print_result(True, f'下载成功 ({size / 1024:.1f} KB)')
  74. print(f' 本地路径: {local_path}')
  75. # 清理
  76. os.unlink(local_path)
  77. return True
  78. except Exception as e:
  79. print_result(False, f'下载失败: {e}')
  80. return False
  81. def test_asr():
  82. """测试 5: ASR(可选,需要 API Key)"""
  83. print_header('测试 5: ASR(可选)')
  84. if not Config.OPENAI_API_KEY:
  85. print_result(False, '跳过(未配置 OPENAI_API_KEY)')
  86. return True
  87. try:
  88. # 简单的 ASR 测试(需要预录音频)
  89. print(' 如需测试 ASR,请录制一个音频文件 test_audio.wav')
  90. print(' 然后运行: python asr.py')
  91. return True
  92. except Exception as e:
  93. print_result(False, f'ASR 测试失败: {e}')
  94. return False
  95. def test_end_to_end():
  96. """测试 6: 端到端流程(可选)"""
  97. print_header('测试 6: 端到端流程(可选)')
  98. print(' 完整端到端测试需要:')
  99. print(' 1. OPENAI_API_KEY(ASR)')
  100. print(' 2. LLM_API_KEY(对话)')
  101. print(' 3. 麦克风权限')
  102. print()
  103. print(' 满足条件后运行: python main.py')
  104. return True
  105. def main():
  106. print_header('智能音箱电脑模拟版 - 冒烟测试')
  107. results = []
  108. results.append(('配置验证', test_config()))
  109. results.append(('TTS 服务连通', test_tts_server()))
  110. # 后续测试依赖前面通过
  111. if results[-1][1]:
  112. results.append(('同步合成', test_tts_sync()))
  113. if results[-1][1]:
  114. results.append(('音频下载', test_tts_download()))
  115. # 可选测试
  116. test_asr()
  117. test_end_to_end()
  118. # 汇总
  119. print_header('测试结果汇总')
  120. for name, success in results:
  121. status = '✅ 通过' if success else '❌ 失败'
  122. print(f' {name}: {status}')
  123. passed = sum(1 for _, s in results if s)
  124. total = len(results)
  125. print(f'\n 通过率: {passed}/{total}')
  126. if passed == total:
  127. print('\n🎉 所有测试通过!可以运行 python main.py 体验完整流程')
  128. else:
  129. print('\n⚠️ 有测试失败,请检查:')
  130. print(' 1. 后端服务是否启动 (npm run dev)')
  131. print(' 2. .env 配置是否正确')
  132. print(' 3. 网络是否可达')
  133. if __name__ == '__main__':
  134. main()