test(discord): update tests for bot-to-bot fix (#3217)
The old test `test_on_message_ignores_bot_messages` asserted the previous (incorrect) contract that ALL bot-authored messages are dropped. With #3217 only self-loops are dropped, so this test was replaced with three more precise tests: - test_on_message_ignores_self_messages: verifies self-loop guard (author_id == _bot_user_id is dropped) - test_on_message_accepts_messages_from_other_bots: new test for the fix itself — other bots' messages flow through - test_on_message_stops_typing_on_handle_exception: preserves the typing cleanup assertion from the original test Net result: +1 behavior tested, same behaviors retained. Co-authored with Claude Opus 4.7
This commit is contained in:
committed by
Xubin Ren
parent
3fd24c72fd
commit
5d976d79ff
@@ -273,17 +273,41 @@ async def test_stop_is_safe_after_partial_start(monkeypatch) -> None:
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_on_message_ignores_bot_messages() -> None:
|
||||
# Incoming bot-authored messages must be ignored to prevent feedback loops.
|
||||
async def test_on_message_ignores_self_messages() -> None:
|
||||
# Self-loop guard: messages from this bot's own account must be dropped (#3217).
|
||||
channel = DiscordChannel(DiscordConfig(enabled=True, allow_from=["*"]), MessageBus())
|
||||
channel._bot_user_id = "999" # simulate bot identity populated in on_ready()
|
||||
handled: list[dict] = []
|
||||
channel._handle_message = lambda **kwargs: handled.append(kwargs) # type: ignore[method-assign]
|
||||
|
||||
await channel._on_message(_make_message(author_bot=True))
|
||||
await channel._on_message(_make_message(author_id=999, author_bot=True))
|
||||
|
||||
assert handled == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_on_message_accepts_messages_from_other_bots() -> None:
|
||||
# Multi-agent setups: messages from OTHER bots must be processed, not dropped (#3217).
|
||||
channel = DiscordChannel(DiscordConfig(enabled=True, allow_from=["*"]), MessageBus())
|
||||
channel._bot_user_id = "999"
|
||||
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, author_bot=True))
|
||||
|
||||
assert len(handled) == 1
|
||||
assert handled[0]["sender_id"] == "123"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_on_message_stops_typing_on_handle_exception() -> None:
|
||||
# If inbound handling raises, typing should be stopped for that channel.
|
||||
channel = DiscordChannel(DiscordConfig(enabled=True, allow_from=["*"]), MessageBus())
|
||||
|
||||
async def fail_handle(**kwargs) -> None:
|
||||
raise RuntimeError("boom")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user