"""Tests for vumi.demos.memo."""

from twisted.trial import unittest
from twisted.internet.defer import inlineCallbacks, returnValue

from vumi.tests.utils import get_stubbed_worker
from vumi.demos.memo import MemoWorker
from vumi.message import TransportUserMessage


class TestMemoWorker(unittest.TestCase):
    @inlineCallbacks
    def setUp(self):
        self.transport_name = 'test_transport'
        self.worker = get_stubbed_worker(MemoWorker, {
                'transport_name': self.transport_name})
        self.broker = self.worker._amqp_client.broker
        yield self.worker.startWorker()

    @inlineCallbacks
    def tearDown(self):
        yield self.worker.stopWorker()

    @inlineCallbacks
    def send(self, content, channel=None):
        transport_metadata = {}
        if channel is not None:
            transport_metadata['irc_channel'] = channel
        msg = TransportUserMessage(content=content,
                                   from_addr='testnick', to_addr='+134567',
                                   transport_name='test',
                                   transport_type='fake',
                                   transport_metadata=transport_metadata)
        self.broker.publish_message('vumi', '%s.inbound' % self.transport_name,
                                    msg)
        yield self.broker.kick_delivery()

    @inlineCallbacks
    def recv(self, n=0):
        msgs = yield self.broker.wait_messages('vumi', '%s.outbound'
                                                % self.transport_name, n)

        def reply_code(msg):
            if msg['session_event'] == TransportUserMessage.SESSION_CLOSE:
                return 'end'
            return 'reply'

        returnValue([(reply_code(msg), msg['content']) for msg in msgs])

    @inlineCallbacks
    def test_no_memos(self):
        yield self.send("Message from someone with no messages.")
        replies = yield self.recv()
        self.assertEquals([], replies)

    @inlineCallbacks
    def test_leave_memo(self):
        yield self.send('bot: tell memoed hey there', channel='#test')
        self.assertEquals(self.worker.memos, {
            ('#test', 'memoed'): [('testnick', 'hey there')]
            })
        replies = yield self.recv()
        self.assertEqual(replies, [
            ('reply', 'Sure thing, boss.'),
            ])

@@ -731,16 +731,38 @@
 content,
+ from_addr='testnick',
  channel
@@ -765,24 +765,24 @@
 nnel=None):%0A
-
         tran
@@ -986,27 +986,61 @@
 om_addr=
-'testnick',
+from_addr,%0A                                  
  to_addr
@@ -1045,15 +1045,17 @@
 dr='
-+134567
+some-addr
 ',%0A 
@@ -1403,24 +1403,135 @@
 delivery()%0A%0A
+    def clear_messages(self):%0A        self.broker.clear_messages('vumi', '%25s.outbound' %25 self.transport_name)%0A%0A
     @inlineC
@@ -2453,16 +2453,16 @@
 lies, %5B%0A
-
         
@@ -2512,8 +2512,996 @@
      %5D)%0A
+%0A    @inlineCallbacks%0A    def test_send_memos(self):%0A        yield self.send('bot: tell testmemo this is memo 1', channel='#test')%0A        yield self.send('bot: tell testmemo this is memo 2', channel='#test')%0A        yield self.send('bot: tell testmemo this is a different channel',%0A                        channel='#another')%0A%0A        # replies to setting memos%0A        replies = yield self.recv(3)%0A        self.clear_messages()%0A%0A        yield self.send('ping', channel='#test', from_addr='testmemo')%0A        replies = yield self.recv(2)%0A        self.assertEqual(replies, %5B%0A            ('reply', 'message from testnick: this is memo 1'),%0A            ('reply', 'message from testnick: this is memo 2'),%0A            %5D)%0A        self.clear_messages()%0A%0A        yield self.send('ping', channel='#another', from_addr='testmemo')%0A        replies = yield self.recv(1)%0A        self.assertEqual(replies, %5B%0A            ('reply', 'message from testnick: this is a different channel'),%0A            %5D)%0A

Add test for messages being sent to different channels and check that only messages for the correct channel are sent.