Files
meshcore-bot/tests/test_bridge_disabled_stop.py
agessaman 56a366ff7c fix(bridges): stop cleanly when the bridge disabled itself at startup
TelegramBridgeService returned from __init__ as soon as it found no api_token (or no HTTP library), before setting message_queues, send_times, http_session and _queue_processor_task. start() then returned early as intended, but stop() read _queue_processor_task and raised AttributeError, so every shutdown logged "Failed to stop service 'telegram_bridge'". DiscordBridgeService had the same gap on its no-HTTP-library path. Both now set that state before any early return.

New tests start and stop each self-disabled bridge and check that only the reason it disabled itself is logged as an error; all three fail on dev.
2026-10-03 14:42:16 -07:00

54 lines
2.1 KiB
Python

"""A bridge that disabled itself in __init__ still starts and stops without errors."""
from configparser import ConfigParser
from unittest.mock import MagicMock, patch
import pytest
from modules.service_plugins.discord_bridge_service import DiscordBridgeService
from modules.service_plugins.telegram_bridge_service import TelegramBridgeService
@pytest.fixture
def bot(mock_logger):
bot = MagicMock()
bot.logger = mock_logger
bot.config = ConfigParser()
bot.config.add_section("DiscordBridge")
bot.config.add_section("TelegramBridge")
bot.channel_sent_listeners = []
return bot
async def _start_and_stop(service, bot):
await service.start()
await service.stop()
bot.logger.error.assert_called_once() # only the reason it disabled itself
assert bot.channel_sent_listeners == []
@pytest.mark.asyncio
class TestDisabledBridgesStop:
async def test_telegram_without_api_token(self, bot, monkeypatch):
monkeypatch.delenv("TELEGRAM_BOT_TOKEN", raising=False)
service = TelegramBridgeService(bot)
assert service.enabled is False
await _start_and_stop(service, bot)
bot.logger.info.assert_any_call("Telegram bridge service stopped")
async def test_telegram_without_http_library(self, bot):
with patch("modules.service_plugins.telegram_bridge_service.AIOHTTP_AVAILABLE", False), \
patch("modules.service_plugins.telegram_bridge_service.REQUESTS_AVAILABLE", False):
service = TelegramBridgeService(bot)
assert service.enabled is False
await _start_and_stop(service, bot)
bot.logger.info.assert_any_call("Telegram bridge service stopped")
async def test_discord_without_http_library(self, bot):
with patch("modules.service_plugins.discord_bridge_service.AIOHTTP_AVAILABLE", False), \
patch("modules.service_plugins.discord_bridge_service.REQUESTS_AVAILABLE", False):
service = DiscordBridgeService(bot)
assert service.enabled is False
await _start_and_stop(service, bot)
bot.logger.info.assert_any_call("Discord bridge service stopped")