| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- # 端到端冒烟测试脚本
- # 验证:录音 → ASR → LLM → TTS → 播放 完整流程
- import sys
- import time
- from config import Config
- def print_header(text):
- print('\n' + '=' * 60)
- print(f' {text}')
- print('=' * 60)
- def print_result(success, message):
- icon = '✅' if success else '❌'
- print(f'{icon} {message}')
- def test_config():
- """测试 1: 配置验证"""
- print_header('测试 1: 配置验证')
- try:
- Config.validate()
- print_result(True, f'配置 OK')
- print(f' TTS: {Config.TTS_BASE_URL}')
- print(f' LLM: {Config.LLM_PROVIDER}')
- print(f' ASR: {Config.ASR_PROVIDER}')
- return True
- except ValueError as e:
- print_result(False, str(e))
- return False
- def test_tts_server():
- """测试 2: TTS 服务连通性"""
- print_header('测试 2: TTS 服务连通性')
- try:
- import requests
- # 测试 /tts/voices 是否可达
- url = f'{Config.TTS_BASE_URL}/tts/voices'
- response = requests.get(url, timeout=5)
- if response.status_code == 200:
- data = response.json()
- voices = data.get('data', {}).get('voices', [])
- print_result(True, f'TTS 服务可达,{len(voices)} 个音色')
- return True
- else:
- print_result(False, f'服务返回 {response.status_code}')
- return False
- except Exception as e:
- print_result(False, f'连接失败: {e}')
- print(f' 请确认后端服务在 {Config.TTS_BASE_URL} 运行')
- return False
- def test_tts_sync():
- """测试 3: 同步合成端点"""
- print_header('测试 3: 同步合成端点 (/tts/synthesize)')
- try:
- from tts_client import synthesize
- text = '冒烟测试,同步合成端点是否正常'
- print(f' 测试文本: "{text}"')
- start = time.time()
- audio_url = synthesize(text, voice_id=Config.TTS_VOICE_ID)
- elapsed = time.time() - start
- print_result(True, f'同步合成成功 ({elapsed:.2f}s)')
- print(f' 音频 URL: {audio_url}')
- if elapsed > 3:
- print_result(False, f'延迟偏高({elapsed:.2f}s),理想 < 1s')
- return True
- except Exception as e:
- print_result(False, f'同步合成失败: {e}')
- return False
- def test_tts_download():
- """测试 4: 音频下载"""
- print_header('测试 4: 音频下载')
- try:
- from tts_client import synthesize, download_audio
- audio_url = synthesize('测试下载', voice_id=Config.TTS_VOICE_ID)
- local_path = download_audio(audio_url)
- import os
- size = os.path.getsize(local_path)
- print_result(True, f'下载成功 ({size / 1024:.1f} KB)')
- print(f' 本地路径: {local_path}')
- # 清理
- os.unlink(local_path)
- return True
- except Exception as e:
- print_result(False, f'下载失败: {e}')
- return False
- def test_asr():
- """测试 5: ASR(可选,需要 API Key)"""
- print_header('测试 5: ASR(可选)')
- if not Config.OPENAI_API_KEY:
- print_result(False, '跳过(未配置 OPENAI_API_KEY)')
- return True
- try:
- # 简单的 ASR 测试(需要预录音频)
- print(' 如需测试 ASR,请录制一个音频文件 test_audio.wav')
- print(' 然后运行: python asr.py')
- return True
- except Exception as e:
- print_result(False, f'ASR 测试失败: {e}')
- return False
- def test_end_to_end():
- """测试 6: 端到端流程(可选)"""
- print_header('测试 6: 端到端流程(可选)')
- print(' 完整端到端测试需要:')
- print(' 1. OPENAI_API_KEY(ASR)')
- print(' 2. LLM_API_KEY(对话)')
- print(' 3. 麦克风权限')
- print()
- print(' 满足条件后运行: python main.py')
- return True
- def main():
- print_header('智能音箱电脑模拟版 - 冒烟测试')
- results = []
- results.append(('配置验证', test_config()))
- results.append(('TTS 服务连通', test_tts_server()))
- # 后续测试依赖前面通过
- if results[-1][1]:
- results.append(('同步合成', test_tts_sync()))
- if results[-1][1]:
- results.append(('音频下载', test_tts_download()))
- # 可选测试
- test_asr()
- test_end_to_end()
- # 汇总
- print_header('测试结果汇总')
- for name, success in results:
- status = '✅ 通过' if success else '❌ 失败'
- print(f' {name}: {status}')
- passed = sum(1 for _, s in results if s)
- total = len(results)
- print(f'\n 通过率: {passed}/{total}')
- if passed == total:
- print('\n🎉 所有测试通过!可以运行 python main.py 体验完整流程')
- else:
- print('\n⚠️ 有测试失败,请检查:')
- print(' 1. 后端服务是否启动 (npm run dev)')
- print(' 2. .env 配置是否正确')
- print(' 3. 网络是否可达')
- if __name__ == '__main__':
- main()
|