Skip to content

The agent that grows with you

test(gateway): regression tests for UTF-16 chunk limits at the Telegram boundary (#55844)

Tekniuma024ccd66e47
diff --git a/tests/gateway/test_fence_chunker.py b/tests/gateway/test_fence_chunker.py
index 60f87dfb980..5bd5ba41d16 100644
--- a/tests/gateway/test_fence_chunker.py
+++ b/tests/gateway/test_fence_chunker.py
@@ -145,3 +145,32 @@ def test_greedy_pack_overflow_callback():
# ── canonical table-row splitter delegation ──────────────────────────────────
+
+
+# ── UTF-16 length units at the Telegram limit (#55844) ──────────────────────
+
+
+def test_emoji_heavy_split_respects_utf16_limit():
+ """Telegram's 4096 limit is UTF-16 code units — emoji count as 2.
+
+ Regression for #55844: the fallback splitter must measure chunks with
+ the platform len_fn, not Python codepoints, or emoji-heavy messages
+ ship oversized chunks that Telegram rejects with MESSAGE_TOO_LONG.
+ """
+ limit = 3996 # gateway's safe limit: 4096 - 100 headroom
+ text = "🚀" * 3000 # 3000 codepoints == 6000 UTF-16 units
+ chunks = split_text_fence_aware(
+ text, limit, utf16_len, prefer_paragraphs=False, balance_fences=True
+ )
+ assert len(chunks) > 1
+ assert all(utf16_len(c) <= limit for c in chunks)
+ assert "".join(chunks) == text
+
+
+def test_mixed_emoji_newline_text_utf16_limit():
+ text = "emoji 🎉🎊 line with some more words attached\n" * 400
+ chunks = split_text_fence_aware(
+ text, 500, utf16_len, prefer_paragraphs=False, balance_fences=True
+ )
+ assert all(utf16_len(c) <= 500 for c in chunks)
+ assert all(c for c in chunks)