test_funasr_chat_message_push.py
6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
AIfeng/2025-01-21 15:53:20
FunASR聊天消息推送功能测试
测试新增的直接推送chat_message功能
"""
import sys
import os
import time
import asyncio
from pathlib import Path
# 添加项目根目录到Python路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
import util
from funasr_asr_sync import FunASRClient
def test_chat_message_push():
"""测试FunASR聊天消息推送功能"""
util.log(1, "=== FunASR聊天消息推送功能测试 ===")
try:
# 创建FunASR客户端实例
username = "test_user_001"
funasr_client = FunASRClient(username=username)
util.log(1, f"创建FunASR客户端: {username}")
# 测试1: 使用便捷方法发送聊天消息
util.log(1, "\n--- 测试1: 便捷方法发送聊天消息 ---")
test_messages = [
{
"content": "这是一条测试消息",
"sender": "FunASR测试",
"model_info": "Funasr-Test-v1.0"
},
{
"content": "语音识别结果:你好,世界!",
"sender": "回音",
"model_info": "Funasr"
},
{
"content": "多语言识别测试:Hello World",
"sender": "多语言助手",
"model_info": "Funasr-Multilang"
}
]
for i, msg in enumerate(test_messages, 1):
util.log(1, f"发送测试消息 {i}: {msg['content'][:20]}...")
success = funasr_client.send_chat_message(
content=msg["content"],
sender=msg["sender"],
model_info=msg["model_info"]
)
if success:
util.log(1, f"✅ 消息 {i} 发送成功")
else:
util.log(2, f"❌ 消息 {i} 发送失败")
time.sleep(0.5) # 避免发送过快
# 测试2: 直接使用_send_chat_message_direct方法
util.log(1, "\n--- 测试2: 直接推送方法测试 ---")
try:
from core.wsa_websocket_service import get_web_instance
web_instance = get_web_instance()
custom_message = {
"type": "chat_message",
"sender": "直接推送测试",
"content": "这是通过_send_chat_message_direct方法发送的消息",
"Username": username,
"model_info": "Direct-Push-Test",
"timestamp": int(time.time()),
"extra_data": {
"test_mode": True,
"push_method": "direct"
}
}
util.log(1, "使用直接推送方法发送自定义消息")
funasr_client._send_chat_message_direct(web_instance, custom_message)
util.log(1, "✅ 直接推送测试完成")
except RuntimeError as e:
util.log(2, f"WSA服务未初始化,跳过直接推送测试: {e}")
except Exception as e:
util.log(3, f"直接推送测试失败: {e}")
# 测试3: 模拟识别结果推送
util.log(1, "\n--- 测试3: 模拟识别结果推送 ---")
# 模拟设置识别结果
funasr_client.finalResults = "这是模拟的语音识别结果文本"
# 模拟识别完成后的推送逻辑
try:
from core.wsa_websocket_service import get_web_instance
web_instance = get_web_instance()
if web_instance and web_instance.is_connected(username):
chat_message = {
"type": "chat_message",
"sender": "回音",
"content": funasr_client.finalResults,
"Username": username,
"model_info": "Funasr"
}
util.log(1, "模拟识别结果推送")
funasr_client._send_chat_message_direct(web_instance, chat_message)
util.log(1, "✅ 识别结果推送测试完成")
else:
util.log(2, f"用户{username}未连接到Web客户端,跳过识别结果推送测试")
except RuntimeError as e:
util.log(2, f"WSA服务未初始化,跳过识别结果推送测试: {e}")
except Exception as e:
util.log(3, f"识别结果推送测试失败: {e}")
util.log(1, "\n=== FunASR聊天消息推送功能测试完成 ===")
except Exception as e:
util.log(3, f"测试过程中出现错误: {e}")
import traceback
util.log(3, f"错误详情: {traceback.format_exc()}")
def test_async_compatibility():
"""测试异步兼容性"""
util.log(1, "\n=== 异步兼容性测试 ===")
async def async_test():
"""异步环境测试"""
try:
username = "async_test_user"
funasr_client = FunASRClient(username=username)
util.log(1, "在异步环境中测试聊天消息推送")
success = funasr_client.send_chat_message(
content="异步环境测试消息",
sender="异步测试助手",
model_info="Async-Test"
)
if success:
util.log(1, "✅ 异步环境消息发送成功")
else:
util.log(2, "❌ 异步环境消息发送失败")
except Exception as e:
util.log(3, f"异步测试失败: {e}")
# 运行异步测试
try:
asyncio.run(async_test())
util.log(1, "✅ 异步兼容性测试完成")
except Exception as e:
util.log(3, f"异步兼容性测试失败: {e}")
if __name__ == "__main__":
# 设置日志级别
util.log(1, "启动FunASR聊天消息推送功能测试")
# 基础功能测试
test_chat_message_push()
# 异步兼容性测试
test_async_compatibility()
util.log(1, "所有测试完成")