ForcePilot/backend/test/unit/channels/test_wechat_format.py
Kris 69fe97a90d test: 批量修复并新增单元测试用例
1. 移除Telegram格式化测试中未使用的导入项
2. 修复Teams测试用例,添加monkeypatch参数并配置通配符开关
3. 更新钉钉适配器测试,替换弃用的流属性检查
4. 修正Twitch规范化测试,更新ROOMSTATE测试逻辑
5. 重构会话映射测试,完善数据库执行结果模拟
6. 格式化Slack块构建测试的长参数调用
7. 修复LINE适配器测试,更新能力断言和异步锁使用
8. 修正Slack会话解析测试,修复聊天类型判断错误
9. 更新能力测试,补充缺失的字段检查
10. 修复Matrix适配器测试,修正位置参数和配置校验逻辑
11. 为飞书分析模块测试添加跳过标记
12. 新增微信能力、限流、链接格式、会话路由等模块的单元测试
13. 修复Twitch适配器导入路径和测试断言
14. 新增Discord Webhook、Nextcloud Talk、Signal多账户等模块的单元测试
15. 修复Manager阶段测试的导入路径
16. 新增iMessage异常和命令处理的单元测试
17. 新增Nostr健康检查和相关模块的单元测试
18. 新增Signal守护进程和SSE重连相关测试
2026-05-13 16:43:01 +08:00

140 lines
4.2 KiB
Python

from __future__ import annotations
import pytest
from yuxi.channels.adapters.wechat.format import (
map_bridge_msg_type,
map_mp_msg_type,
map_wecom_msg_type,
truncate_text,
)
from yuxi.channels.models import MessageType
class TestTruncateText:
def test_text_within_limit_returns_unchanged(self):
assert truncate_text("hello", 10) == "hello"
def test_text_exactly_at_limit_returns_unchanged(self):
text = "a" * 100
result = truncate_text(text, 100)
assert result == text
assert len(result) == 100
def test_text_exceeds_limit_truncates_with_marker(self):
text = "hello world this is a longer message!!!"
result = truncate_text(text, 30)
assert len(result) <= 30
assert result.endswith("\n...(内容过长已截断)")
def test_very_long_text_truncation(self):
text = "x" * 5000
result = truncate_text(text, 2048)
assert len(result) == 2048
assert result.endswith("\n...(内容过长已截断)")
def test_empty_string(self):
result = truncate_text("", 10)
assert result == ""
def test_text_shorter_than_marker_clip_to_one_char(self):
result = truncate_text("ab", 3)
assert result == "ab"
def test_text_just_below_limit(self):
text = "a" * 49
result = truncate_text(text, 50)
assert result == text
def test_text_one_over_limit(self):
text = "a" * 51
result = truncate_text(text, 50)
assert len(result) == 50
def test_unicode_text_truncation(self):
text = "你好世界!" * 100
result = truncate_text(text, 50)
assert len(result) == 50
def test_limit_smaller_than_marker(self):
text = "hello"
result = truncate_text(text, 3)
assert result.endswith("\n...(内容过长已截断)")
class TestMapWeComMsgType:
def test_text(self):
assert map_wecom_msg_type("text") == MessageType.TEXT
def test_image(self):
assert map_wecom_msg_type("image") == MessageType.IMAGE
def test_voice(self):
assert map_wecom_msg_type("voice") == MessageType.AUDIO
def test_video(self):
assert map_wecom_msg_type("video") == MessageType.VIDEO
def test_file(self):
assert map_wecom_msg_type("file") == MessageType.FILE
def test_location(self):
assert map_wecom_msg_type("location") == MessageType.LOCATION
def test_unknown_type_defaults_to_text(self):
assert map_wecom_msg_type("unknown_type") == MessageType.TEXT
def test_empty_string_defaults_to_text(self):
assert map_wecom_msg_type("") == MessageType.TEXT
class TestMapMpMsgType:
def test_text(self):
assert map_mp_msg_type("text") == MessageType.TEXT
def test_image(self):
assert map_mp_msg_type("image") == MessageType.IMAGE
def test_voice(self):
assert map_mp_msg_type("voice") == MessageType.AUDIO
def test_video(self):
assert map_mp_msg_type("video") == MessageType.VIDEO
def test_location(self):
assert map_mp_msg_type("location") == MessageType.LOCATION
def test_mp_has_no_file_type_defaults_to_text(self):
assert map_mp_msg_type("file") == MessageType.TEXT
def test_unknown_type_defaults_to_text(self):
assert map_mp_msg_type("sticker") == MessageType.TEXT
class TestMapBridgeMsgType:
def test_text_type_1(self):
assert map_bridge_msg_type(1) == MessageType.TEXT
def test_image_type_3(self):
assert map_bridge_msg_type(3) == MessageType.IMAGE
def test_audio_type_34(self):
assert map_bridge_msg_type(34) == MessageType.AUDIO
def test_video_type_43(self):
assert map_bridge_msg_type(43) == MessageType.VIDEO
def test_location_type_48(self):
assert map_bridge_msg_type(48) == MessageType.LOCATION
def test_file_type_49(self):
assert map_bridge_msg_type(49) == MessageType.FILE
def test_unknown_integer_defaults_to_text(self):
assert map_bridge_msg_type(999) == MessageType.TEXT
def test_zero_defaults_to_text(self):
assert map_bridge_msg_type(0) == MessageType.TEXT
def test_negative_defaults_to_text(self):
assert map_bridge_msg_type(-1) == MessageType.TEXT