Files
nanobot/tests/test_telegram_suppress.py
code-server dafaa3bab4 feat: add suppression support to Telegram channel
Messages with metadata['suppressed']=True are logged but not sent
to Telegram API, enabling heartbeat to run without spamming user.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-28 23:40:39 +00:00

66 lines
1.6 KiB
Python

# tests/test_telegram_suppress.py
import pytest
from nanobot.bus.events import OutboundMessage
from nanobot.channels.telegram import TelegramChannel
from unittest.mock import AsyncMock, MagicMock
@pytest.mark.asyncio
async def test_suppressed_message_not_sent():
"""Test that messages with suppressed=True metadata are not sent to Telegram API."""
config = MagicMock()
config.token = "test-token"
bus = MagicMock()
channel = TelegramChannel(config, bus)
# Mock the internal _app and bot directly (skip start())
mock_app = MagicMock()
mock_bot = AsyncMock()
mock_app.bot = mock_bot
channel._app = mock_app
# Send a suppressed message
msg = OutboundMessage(
channel="telegram",
chat_id="12345",
content="[HIDDEN] This should not be sent",
metadata={"suppressed": True}
)
await channel.send(msg)
# Verify bot.send_message was NOT called
mock_bot.send_message.assert_not_called()
@pytest.mark.asyncio
async def test_normal_message_sent():
"""Test that normal messages are sent to Telegram API."""
config = MagicMock()
config.token = "test-token"
bus = MagicMock()
channel = TelegramChannel(config, bus)
# Mock the internal _app and bot directly (skip start())
mock_app = MagicMock()
mock_bot = AsyncMock()
mock_app.bot = mock_bot
channel._app = mock_app
# Send a normal message
msg = OutboundMessage(
channel="telegram",
chat_id="12345",
content="Normal message",
metadata={}
)
await channel.send(msg)
# Verify bot.send_message WAS called
mock_bot.send_message.assert_called_once()