2026-05-12 00:56:47 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
import pytest
|
2026-05-12 00:56:47 +08:00
|
|
|
|
|
|
|
|
from yuxi.channels.adapters.wechat.format import (
|
|
|
|
|
map_bridge_msg_type,
|
2026-05-13 16:43:01 +08:00
|
|
|
map_mp_msg_type,
|
|
|
|
|
map_wecom_msg_type,
|
2026-05-12 00:56:47 +08:00
|
|
|
truncate_text,
|
|
|
|
|
)
|
|
|
|
|
from yuxi.channels.models import MessageType
|
|
|
|
|
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
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):
|
2026-05-12 00:56:47 +08:00
|
|
|
assert map_wecom_msg_type("text") == MessageType.TEXT
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_image(self):
|
2026-05-12 00:56:47 +08:00
|
|
|
assert map_wecom_msg_type("image") == MessageType.IMAGE
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_voice(self):
|
2026-05-12 00:56:47 +08:00
|
|
|
assert map_wecom_msg_type("voice") == MessageType.AUDIO
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_video(self):
|
2026-05-12 00:56:47 +08:00
|
|
|
assert map_wecom_msg_type("video") == MessageType.VIDEO
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_file(self):
|
2026-05-12 00:56:47 +08:00
|
|
|
assert map_wecom_msg_type("file") == MessageType.FILE
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_location(self):
|
2026-05-12 00:56:47 +08:00
|
|
|
assert map_wecom_msg_type("location") == MessageType.LOCATION
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
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
|
|
|
|
|
|
2026-05-12 00:56:47 +08:00
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
class TestMapMpMsgType:
|
|
|
|
|
def test_text(self):
|
2026-05-12 00:56:47 +08:00
|
|
|
assert map_mp_msg_type("text") == MessageType.TEXT
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_image(self):
|
2026-05-12 00:56:47 +08:00
|
|
|
assert map_mp_msg_type("image") == MessageType.IMAGE
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_voice(self):
|
2026-05-12 00:56:47 +08:00
|
|
|
assert map_mp_msg_type("voice") == MessageType.AUDIO
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
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):
|
2026-05-12 00:56:47 +08:00
|
|
|
assert map_bridge_msg_type(1) == MessageType.TEXT
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_image_type_3(self):
|
2026-05-12 00:56:47 +08:00
|
|
|
assert map_bridge_msg_type(3) == MessageType.IMAGE
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_audio_type_34(self):
|
2026-05-12 00:56:47 +08:00
|
|
|
assert map_bridge_msg_type(34) == MessageType.AUDIO
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_video_type_43(self):
|
2026-05-12 00:56:47 +08:00
|
|
|
assert map_bridge_msg_type(43) == MessageType.VIDEO
|
|
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_location_type_48(self):
|
|
|
|
|
assert map_bridge_msg_type(48) == MessageType.LOCATION
|
2026-05-12 00:56:47 +08:00
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_file_type_49(self):
|
|
|
|
|
assert map_bridge_msg_type(49) == MessageType.FILE
|
2026-05-12 00:56:47 +08:00
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_unknown_integer_defaults_to_text(self):
|
|
|
|
|
assert map_bridge_msg_type(999) == MessageType.TEXT
|
2026-05-12 00:56:47 +08:00
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_zero_defaults_to_text(self):
|
|
|
|
|
assert map_bridge_msg_type(0) == MessageType.TEXT
|
2026-05-12 00:56:47 +08:00
|
|
|
|
2026-05-13 16:43:01 +08:00
|
|
|
def test_negative_defaults_to_text(self):
|
|
|
|
|
assert map_bridge_msg_type(-1) == MessageType.TEXT
|