From a024ccd66e475a9827302cde7fa75b4b0f0ba89d Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Sat, 8 Aug 2026 11:56:21 -0700 Subject: [PATCH] test(gateway): regression tests for UTF-16 chunk limits at the Telegram boundary (#55844) --- tests/gateway/test_fence_chunker.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/gateway/test_fence_chunker.py b/tests/gateway/test_fence_chunker.py index 60f87dfb98059..5bd5ba41d16c7 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)