From 459a4d7311d663cca7a57a7ecf0b32df12ee741d Mon Sep 17 00:00:00 2001 From: Xubin Ren Date: Thu, 16 Apr 2026 18:05:52 +0000 Subject: [PATCH] test(discord): cover allow_channels filtering in _should_accept_inbound Locks in the two key boundaries of the new channel-based filter: 1. When an incoming channel id is in allow_channels, messages are forwarded. 2. When an incoming channel id is not in allow_channels, messages are silently dropped. The empty-list backward-compatible path is already covered by every existing test that omits allow_channels (default_factory=list). Made-with: Cursor --- tests/channels/test_discord_channel.py | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/channels/test_discord_channel.py b/tests/channels/test_discord_channel.py index 7a39bff2b..82ef6c51b 100644 --- a/tests/channels/test_discord_channel.py +++ b/tests/channels/test_discord_channel.py @@ -313,6 +313,45 @@ async def test_on_message_accepts_allowlisted_dm() -> None: assert handled[0]["metadata"] == {"message_id": "789", "guild_id": None, "reply_to": None} +@pytest.mark.asyncio +async def test_on_message_accepts_when_channel_in_allow_channels() -> None: + # When allow_channels is set, messages from listed channels should be forwarded. + channel = DiscordChannel( + DiscordConfig(enabled=True, allow_from=["*"], allow_channels=["456"]), + MessageBus(), + ) + handled: list[dict] = [] + + async def capture_handle(**kwargs) -> None: + handled.append(kwargs) + + channel._handle_message = capture_handle # type: ignore[method-assign] + + await channel._on_message(_make_message(author_id=123, channel_id=456)) + + assert len(handled) == 1 + assert handled[0]["chat_id"] == "456" + + +@pytest.mark.asyncio +async def test_on_message_drops_when_channel_not_in_allow_channels() -> None: + # When allow_channels is set and incoming channel is not listed, drop silently. + channel = DiscordChannel( + DiscordConfig(enabled=True, allow_from=["*"], allow_channels=["999"]), + MessageBus(), + ) + handled: list[dict] = [] + + async def capture_handle(**kwargs) -> None: + handled.append(kwargs) + + channel._handle_message = capture_handle # type: ignore[method-assign] + + await channel._on_message(_make_message(author_id=123, channel_id=456)) + + assert handled == [] + + @pytest.mark.asyncio async def test_on_message_ignores_unmentioned_guild_message() -> None: # With mention-only group policy, guild messages without a bot mention are dropped.