Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion csp_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion csp_bot/gateway/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .gateway import CspBotGateway, Gateway, GatewayChannels, GatewayModule
from .gateway import *
13 changes: 13 additions & 0 deletions csp_bot/gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@

log = getLogger(__name__)

__all__ = (
"GatewayChannels",
"GatewayModule",
"GatewaySettings",
"CspBotGateway",
"Channels",
"Gateway",
"Module",
"Settings",
)


class GatewayChannels(GatewayChannelsBase):
messages_in: ts[Message] = None
Expand Down Expand Up @@ -72,5 +83,7 @@ def start(self, *args, **kwargs):
super(CspBotGateway, self).start(*args, **kwargs)


Channels = GatewayChannels
Gateway = CspBotGateway
Module = GatewayModule
Settings = GatewaySettings
50 changes: 50 additions & 0 deletions csp_bot/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions csp_bot/tests/test_bot.py
Original file line number Diff line number Diff line change
@@ -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): ...