ForcePilot/backend/test/unit/channels/test_channels_imessage_sanitize.py

139 lines
4.3 KiB
Python
Raw Normal View History

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>"