调整相关测试

This commit is contained in:
Sakura-RanChen 2026-11-19 17:11:57 +08:00
parent 39d4905137
commit aa524af1d3
2 changed files with 9 additions and 99 deletions

View file

@ -1,99 +0,0 @@
import asyncio
from types import SimpleNamespace
import pytest
@pytest.fixture
def handler_module(monkeypatch):
# 复用项目 conftest.py 提供的测试配置
from config import settings
monkeypatch.setattr(settings, "config_file_valid", True)
# 必须在配置检查之后导入业务模块
from core.handle.textHandler import listenMessageHandler
return listenMessageHandler
def create_connection():
return SimpleNamespace(
client_have_voice=False,
last_activity_time=0,
just_woken_up=False,
reset_audio_states=lambda: None,
config={
"wakeup_words": ["你好小智"],
"enable_greeting": True,
},
)
def test_wakeup_word_passes_filtered_text(handler_module, monkeypatch):
"""唤醒词清理后传给聊天和 ASR 上报。"""
calls = {}
async def fake_start_to_chat(conn, text):
calls["chat"] = text
def fake_enqueue_asr_report(conn, text, opus_data):
calls["report"] = text
monkeypatch.setattr(
handler_module, "startToChat", fake_start_to_chat
)
monkeypatch.setattr(
handler_module, "enqueue_asr_report", fake_enqueue_asr_report
)
conn = create_connection()
handler = handler_module.ListenTextMessageHandler()
original_text = "你好,小智!"
asyncio.run(
handler.handle(conn, {
"state": "detect",
"text": original_text
})
)
assert calls["chat"] == "你好小智"
assert calls["report"] == "你好小智"
def test_normal_text_keeps_original_text(handler_module, monkeypatch):
"""普通文本保留原文和标点。"""
calls = {}
async def fake_start_to_chat(conn, text):
calls["chat"] = text
def fake_enqueue_asr_report(conn, text, opus_data):
calls["report"] = text
monkeypatch.setattr(
handler_module, "startToChat", fake_start_to_chat
)
monkeypatch.setattr(
handler_module, "enqueue_asr_report", fake_enqueue_asr_report
)
conn = create_connection()
handler = handler_module.ListenTextMessageHandler()
original_text = "你好,小智,我有问题。"
asyncio.run(
handler.handle(conn, {
"state": "detect",
"text": original_text
})
)
assert calls["chat"] == original_text
assert calls["report"] == original_text

View file

@ -94,6 +94,14 @@ def test_tts_text_to_speak_writes_file() -> None:
"""
provider = _build_provider()
# 会话型流式 TTS(activate_session)依赖真实长连接,text_to_speak 只发
# 会话请求、由异步监听任务收流,不走“同步写文件”契约,本测试不适用。
if getattr(provider, "activate_session", None) is not None:
pytest.skip(
f"会话型流式 TTS '{type(provider).__name__}' 需要真实连接,"
"文件链路测试不适用"
)
# 参考 performance_tester_tts.py:部分 provider 内部访问 self.conn.sample_rate 等
if not hasattr(provider, "conn") or provider.conn is None:
provider.conn = SimpleNamespace(
@ -102,6 +110,7 @@ def test_tts_text_to_speak_writes_file() -> None:
stop_event=threading.Event(),
client_abort=False,
headers={},
sentence_id="test-sentence", # huoshan_double_stream 等会话型 TTS 需要
)
if not hasattr(provider, "opus_encoder") or provider.opus_encoder is None:
provider.opus_encoder = SimpleNamespace()