ForcePilot/backend/test/unit/channels/test_channels_imessage_sanitize.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

139 lines
4.3 KiB
Python

from __future__ import annotations
import pytest
from yuxi.channels.adapters.imessage.sanitize import (
media_placeholder,
sanitize_outbound_text,
sanitize_terminal_text,
)
class TestSanitizeTerminalText:
def test_empty_string(self):
assert sanitize_terminal_text("") == ""
def test_normal_text_unchanged(self):
text = "Hello, this is a normal message."
assert sanitize_terminal_text(text) == text
def test_ansi_escape_sequences_removed(self):
text = "\x1b[31mRed text\x1b[0m"
result = sanitize_terminal_text(text)
assert "\x1b" not in result
assert "Red text" in result
def test_complex_ansi_removed(self):
text = "\x1b[1;32mBold green\x1b[0m \x1b[4munderline\x1b[24m"
result = sanitize_terminal_text(text)
assert "\x1b" not in result
def test_control_characters_removed(self):
text = "Hello\x00\x01\x02\x1fWorld"
result = sanitize_terminal_text(text)
assert result == "HelloWorld"
def test_non_control_zero_width_chars_preserved(self):
text = "Hello\nWorld"
assert "\n" in sanitize_terminal_text(text)
def test_trim_to_max_length(self):
text = "A" * 300
result = sanitize_terminal_text(text)
assert len(result) <= 203
assert result.endswith("...")
def test_exact_max_length(self):
text = "A" * 200
result = sanitize_terminal_text(text)
assert len(result) == 200
def test_custom_max_length(self):
text = "Hello World, this is a long message"
result = sanitize_terminal_text(text, max_len=10)
assert len(result) <= 13
def test_none_input(self):
assert sanitize_terminal_text(None) == ""
class TestSanitizeOutboundText:
def test_normal_text(self):
text = "Hello world"
assert sanitize_outbound_text(text) == "Hello world"
def test_strips_carriage_return(self):
text = "Hello\r\nWorld\r\n"
result = sanitize_outbound_text(text)
assert "\r" not in result
assert "Hello\nWorld" in result
def test_removes_control_characters(self):
text = "Hello\x00World\x1f"
result = sanitize_outbound_text(text)
assert result == "HelloWorld"
def test_collapses_multiple_newlines(self):
text = "Line1\n\n\n\n\nLine2"
result = sanitize_outbound_text(text)
assert result == "Line1\n\nLine2"
def test_preserves_double_newline(self):
text = "Line1\n\nLine2"
result = sanitize_outbound_text(text)
assert result == "Line1\n\nLine2"
def test_preserves_single_newline(self):
text = "Line1\nLine2"
result = sanitize_outbound_text(text)
assert "Line1\nLine2" in result
def test_strips_length_prefix_exact(self):
text = "11\nHello world"
result = sanitize_outbound_text(text)
assert result == "Hello world"
def test_strips_length_prefix_near_match(self):
text = "11\nHello world"
result = sanitize_outbound_text(text)
assert result == "Hello world"
def test_keeps_length_prefix_bad_match(self):
text = "999\nHello world"
result = sanitize_outbound_text(text)
assert "999" in result
def test_no_length_prefix_for_mismatched_text(self):
text = "50\nshort"
result = sanitize_outbound_text(text)
assert result == "50\nshort"
def test_strips_leading_trailing_whitespace(self):
text = " Hello world "
result = sanitize_outbound_text(text)
assert result == "Hello world"
def test_handles_empty_string(self):
assert sanitize_outbound_text("") == ""
def test_unicode_preserved(self):
text = "你好,世界!😊"
result = sanitize_outbound_text(text)
assert result == "你好,世界!😊"
class TestMediaPlaceholder:
def test_image(self):
assert media_placeholder("image") == "<media:image>"
def test_video(self):
assert media_placeholder("video") == "<media:video>"
def test_audio(self):
assert media_placeholder("audio") == "<media:audio>"
def test_file(self):
assert media_placeholder("file") == "<media:file>"
def test_unknown_type(self):
assert media_placeholder("unknown") == "<media:file>"