diff --git a/csp_bot/bot.py b/csp_bot/bot.py index 031716a..2779c64 100644 --- a/csp_bot/bot.py +++ b/csp_bot/bot.py @@ -496,7 +496,11 @@ def extract_bot_commands(self, message: Message, channel: str, text: str, entiti raise except Exception: # Ignore - log.exception(f"Error processing message: {message}") + # NOTE: message itself may be malformed! + try: + log.exception(f"Error processing message: {message}") + except Exception: + log.exception("Error processing message (could not log message itself)") def run_bot_command(self, command_instance: BotCommand) -> Optional[Union[List[Message], List[BotCommand]]]: # grab the important bits of the command diff --git a/csp_bot/gateway/__init__.py b/csp_bot/gateway/__init__.py index 415843b..4ea1ecd 100644 --- a/csp_bot/gateway/__init__.py +++ b/csp_bot/gateway/__init__.py @@ -1 +1 @@ -from .gateway import CspBotGateway, Gateway, GatewayChannels, GatewayModule +from .gateway import * diff --git a/csp_bot/gateway/gateway.py b/csp_bot/gateway/gateway.py index bfe4f05..eba4d9a 100644 --- a/csp_bot/gateway/gateway.py +++ b/csp_bot/gateway/gateway.py @@ -18,6 +18,17 @@ log = getLogger(__name__) +__all__ = ( + "GatewayChannels", + "GatewayModule", + "GatewaySettings", + "CspBotGateway", + "Channels", + "Gateway", + "Module", + "Settings", +) + class GatewayChannels(GatewayChannelsBase): messages_in: ts[Message] = None @@ -72,5 +83,7 @@ def start(self, *args, **kwargs): super(CspBotGateway, self).start(*args, **kwargs) +Channels = GatewayChannels Gateway = CspBotGateway +Module = GatewayModule Settings = GatewaySettings diff --git a/csp_bot/tests/conftest.py b/csp_bot/tests/conftest.py index e69de29..c4d4294 100644 --- a/csp_bot/tests/conftest.py +++ b/csp_bot/tests/conftest.py @@ -0,0 +1,50 @@ +from unittest.mock import MagicMock, patch + +import pytest +from csp import ts +from csp.impl.wiring import Edge +from csp_adapter_discord import DiscordAdapterConfig +from csp_adapter_slack import SlackAdapterConfig + +from csp_bot import Bot, BotCommand, BotConfig, Channels, DiscordConfig, Message, SlackConfig + + +@pytest.fixture(scope="session") +def bot_config(): + return BotConfig( + discord_config=DiscordConfig( + bot_name="test_bot", + adapter_config=DiscordAdapterConfig( + token="1" * 72, + ), + ), + slack_config=SlackConfig( + bot_name="test_bot", + adapter_config=SlackAdapterConfig( + app_token="xapp-blerg", + bot_token="xoxb-blerg", + ), + ), + ) + + +@pytest.fixture(scope="session") +def bot(bot_config): + bot = Bot(config=bot_config) + channels_mock = MagicMock(spec=Channels) + + def side_effect(name): + if name == "commands": + return Edge(ts[BotCommand], None, 0) + raise Exception(name) + + channels_mock.get_channel.side_effect = side_effect + with ( + patch("csp.unroll", return_value=Edge(ts[Message], None, 0)), + patch("csp.flatten", return_value=Edge(ts[Message], None, 0)), + patch("csp.timer", return_value=Edge(ts[Message], None, 0)), + patch("csp_adapter_discord.adapter.DiscordAdapterManager.publish"), + patch("csp_adapter_slack.adapter.SlackAdapterManager.publish"), + ): + bot.connect(channels=channels_mock) + yield bot diff --git a/csp_bot/tests/test_bot.py b/csp_bot/tests/test_bot.py new file mode 100644 index 0000000..5e8c782 --- /dev/null +++ b/csp_bot/tests/test_bot.py @@ -0,0 +1,52 @@ +from csp_bot import Bot, Message + + +class TestBot: + def test_extract_bot_commands(self, bot: Bot): + # TODO + # commands = bot.extract_bot_commands( + # message=Message( + # user="user", + # msg="<@test_bot> /thanks <@user>", + # channel="test_channel", + # tags=["test_bot", "user"], + # backend="slack", + # ), + # channel="test_channel", + # text="<@test_bot> /thanks <@user>", + # entities=["test_bot", "user"], + # ) + # assert commands is not None + ... + + def test_extract_bot_commands_ignore_message(self, bot): + commands = bot.extract_bot_commands( + message=Message( + user="user", + msg="ignore", + channel="test_channel", + tags=[], + backend="slack", + ), + channel="test_channel", + text="ignore", + entities=[], + ) + assert commands is None + + def test_extract_bot_commands_bad_message(self, bot): + commands = bot.extract_bot_commands( + message=Message( + user="user", + msg="\U00001010", + channel="test_channel", + tags=[], + backend="slack", + ), + channel="test_channel", + text="\U00001010", + entities=[], + ) + assert commands is None + + def test_run_bot_command(self, bot): ...