From 61dd5ac13ad3de2ad04261732540123a710d0e5e Mon Sep 17 00:00:00 2001 From: Xubin Ren Date: Wed, 8 Apr 2026 16:21:18 +0000 Subject: [PATCH] test(discord): cover streamed reply overflow Lock the Discord streaming path with a regression test for final chunk splitting so oversized replies stay safe to merge and ship. Made-with: Cursor --- tests/channels/test_discord_channel.py | 29 +++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/channels/test_discord_channel.py b/tests/channels/test_discord_channel.py index f588334ba..09b80740f 100644 --- a/tests/channels/test_discord_channel.py +++ b/tests/channels/test_discord_channel.py @@ -9,7 +9,7 @@ discord = pytest.importorskip("discord") from nanobot.bus.events import OutboundMessage from nanobot.bus.queue import MessageBus -from nanobot.channels.discord import DiscordBotClient, DiscordChannel, DiscordConfig +from nanobot.channels.discord import MAX_MESSAGE_LEN, DiscordBotClient, DiscordChannel, DiscordConfig from nanobot.command.builtin import build_help_text @@ -471,6 +471,33 @@ async def test_send_delta_streams_by_editing_message(monkeypatch) -> None: assert owner._stream_bufs == {} +@pytest.mark.asyncio +async def test_send_delta_stream_end_splits_oversized_reply(monkeypatch) -> None: + owner = DiscordChannel(DiscordConfig(enabled=True, allow_from=["*"]), MessageBus()) + client = _FakeDiscordClient(owner, intents=None) + owner._client = client + owner._running = True + target = _FakeChannel(channel_id=123) + client.channels[123] = target + + prefix = "a" * (MAX_MESSAGE_LEN - 100) + suffix = "b" * 150 + full_text = prefix + suffix + chunks = DiscordBotClient._build_chunks(full_text, [], False) + assert len(chunks) == 2 + + times = iter([1.0, 3.0]) + monkeypatch.setattr("nanobot.channels.discord.time.monotonic", lambda: next(times, 3.0)) + + await owner.send_delta("123", prefix, {"_stream_delta": True, "_stream_id": "s1"}) + await owner.send_delta("123", suffix, {"_stream_delta": True, "_stream_id": "s1"}) + await owner.send_delta("123", "", {"_stream_end": True, "_stream_id": "s1"}) + + assert target.sent_payloads == [{"content": prefix}, {"content": chunks[1]}] + assert target.sent_messages[0].edits == [{"content": chunks[0]}, {"content": chunks[0]}] + assert owner._stream_bufs == {} + + @pytest.mark.asyncio async def test_slash_new_forwards_when_user_is_allowlisted() -> None: channel = DiscordChannel(DiscordConfig(enabled=True, allow_from=["123"]), MessageBus())