Compare commits

...

2 Commits

3 changed files with 113 additions and 1 deletions

View File

@ -171,7 +171,18 @@ class DrReactor(irc.client.Reactor):
else: else:
all_nicks = connection.get_nickname() all_nicks = connection.get_nickname()
addressed_pattern = r'^(({nicks})[:,]|@({nicks}))\s+(?P<addressed_msg>.*)'.format(nicks=all_nicks) addressed_pattern = r'^(({nicks})[:,]|@({nicks}))\s+(?P<addressed_msg>.*)'.format(nicks=all_nicks)
match = re.match(addressed_pattern, what, re.IGNORECASE)
# ignore the first word, a nick, if the speaker is the bridge
try:
channel = IrcChannel.objects.get(name=sent_location)
if sender_nick == channel.discord_bridge:
short_what = ' '.join(what.split(' ')[1:])
match = re.match(addressed_pattern, short_what, re.IGNORECASE)
else:
match = re.match(addressed_pattern, what, re.IGNORECASE)
except IrcChannel.DoesNotExist:
match = re.match(addressed_pattern, what, re.IGNORECASE)
if match: if match:
event.addressed = True event.addressed = True
event.addressed_msg = match.group('addressed_msg') event.addressed_msg = match.group('addressed_msg')

84
tests/test_ircbot_bot.py Normal file
View File

@ -0,0 +1,84 @@
"""Test core methods of the IRC bot."""
from unittest import mock
from django.test import TestCase
from ircbot.bot import DrReactor
from ircbot.models import IrcServer
class DrReactorTestCase(TestCase):
"""Test the bot innards."""
fixtures = ['tests/fixtures/irc_server_fixture.json']
def setUp(self):
"""Create common objects."""
self.bot = DrReactor()
self.mock_connection = mock.MagicMock()
self.mock_connection.get_nickname.return_value = 'test_bot'
self.mock_connection.server_config = IrcServer.objects.get(pk=1)
def test_handle_event_not_addressed(self):
"""Test that the core identifies not being addressed."""
mock_event = mock.MagicMock()
mock_event.arguments = ['someone: hello this is a test message']
mock_event.type = 'pubmsg'
mock_event.target = '#test'
self.bot._handle_event(self.mock_connection, mock_event)
self.assertFalse(mock_event.addressed)
def test_handle_event_addressed(self):
"""Test that the core identifies being addressed IRC style."""
mock_event = mock.MagicMock()
mock_event.arguments = ['test_bot: hello this is a test message']
mock_event.type = 'pubmsg'
mock_event.target = '#test'
self.bot._handle_event(self.mock_connection, mock_event)
self.assertTrue(mock_event.addressed)
def test_handle_event_addressed_alternate(self):
"""Test that the core identifies being addressed in the alternate (e.g. discord) style."""
mock_event = mock.MagicMock()
mock_event.arguments = ['@test_bot hello this is a test message']
mock_event.type = 'pubmsg'
mock_event.target = '#test'
self.bot._handle_event(self.mock_connection, mock_event)
self.assertTrue(mock_event.addressed)
def test_handle_event_bridge_addressed(self):
"""Test that the core identifies being addressed IRC style through a discord bridge."""
mock_event = mock.MagicMock()
mock_event.arguments = ['<someone> test_bot: hello this is a test message']
mock_event.type = 'pubmsg'
mock_event.target = '#test'
mock_event.source = 'bridge!bridge@localhost'
self.bot._handle_event(self.mock_connection, mock_event)
self.assertTrue(mock_event.addressed)
def test_handle_event_bridge_addressed_alternate(self):
"""Test that the core identifies being addressed in the alternate style through a discord bridge."""
mock_event = mock.MagicMock()
mock_event.arguments = ['<someone> @test_bot hello this is a test message']
mock_event.type = 'pubmsg'
mock_event.target = '#test'
mock_event.source = 'bridge!bridge@localhost'
self.bot._handle_event(self.mock_connection, mock_event)
self.assertTrue(mock_event.addressed)
def test_handle_event_not_addressed_not_bridge(self):
"""Test that the core identifies not to chop the first word from a paste not from the bridge user."""
mock_event = mock.MagicMock()
mock_event.arguments = ['<somebody> test_bot: hello this is a test message']
mock_event.type = 'pubmsg'
mock_event.target = '#test'
mock_event.source = 'not-bridge!not-bridge@localhost'
self.bot._handle_event(self.mock_connection, mock_event)
self.assertFalse(mock_event.addressed)

View File

@ -73,6 +73,23 @@ class MarkovTestCase(TestCase):
self.assertEqual(mock_learn_line.call_args.args[0], 'hello this is a test message') self.assertEqual(mock_learn_line.call_args.args[0], 'hello this is a test message')
def test_learn_bridge_no_edit(self):
"""Test that if a message is from the bridge, we learn what looks like a speaker's nick.
This is primarily for if someone on IRC is pasting a log --- the "<somebody> whatever" should
be retained in full, rather than learned as "whatever".
"""
mock_event = mock.MagicMock()
mock_event.arguments = ['<tester> hello this is a test message']
mock_event.target = '#test'
mock_event.recursing = False
mock_event.source = 'not-bridge!not-bridge@localhost'
with mock.patch('markov.lib.learn_line') as mock_learn_line:
self.plugin.handle_chatter(self.mock_connection, mock_event)
self.assertEqual(mock_learn_line.call_args.args[0], '<tester> hello this is a test message')
def test_learn_bridge_and_self_edit(self): def test_learn_bridge_and_self_edit(self):
"""Test that we don't learn our own name when learning something addressed to us, discord style.""" """Test that we don't learn our own name when learning something addressed to us, discord style."""
mock_event = mock.MagicMock() mock_event = mock.MagicMock()