41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
from meshbot.messages import trim_to_bytes
|
|
|
|
|
|
def test_short_ascii_passthrough():
|
|
assert trim_to_bytes("hello", 184) == "hello"
|
|
|
|
|
|
def test_exact_fit_passthrough():
|
|
s = "a" * 184
|
|
assert trim_to_bytes(s, 184) == s
|
|
|
|
|
|
def test_long_ascii_clean_cut():
|
|
s = "x" * 200
|
|
out = trim_to_bytes(s, 184)
|
|
assert len(out.encode("utf-8")) == 184
|
|
assert out == "x" * 184
|
|
|
|
|
|
def test_emoji_does_not_split():
|
|
# Each 🎉 is 4 UTF-8 bytes. Limit of 5 must keep just one emoji (4 bytes), not 5.
|
|
out = trim_to_bytes("🎉🎉", 5)
|
|
assert out == "🎉"
|
|
assert len(out.encode("utf-8")) == 4
|
|
|
|
|
|
def test_multibyte_at_boundary():
|
|
# "ä" is 2 bytes in UTF-8. With a 3-byte budget for "aä" (3 bytes total), we keep both.
|
|
assert trim_to_bytes("aä", 3) == "aä"
|
|
# With a 2-byte budget we can only keep the leading "a".
|
|
assert trim_to_bytes("aä", 2) == "a"
|
|
|
|
|
|
def test_zero_or_negative_max_bytes():
|
|
assert trim_to_bytes("anything", 0) == ""
|
|
assert trim_to_bytes("anything", -1) == ""
|
|
|
|
|
|
def test_empty_input():
|
|
assert trim_to_bytes("", 184) == ""
|