# -*- coding: utf-8 -*-
from irc3.testing import BotTestCase
import tempfile
import shutil
import glob
import os


class LoggerTestCase(BotTestCase):

    config = dict(includes=[
        'irc3.plugins.logger',
        'irc3.plugins.userlist',
    ])

    def setUp(self):
        super(LoggerTestCase, self).setUp()
        self.logdir = tempfile.mkdtemp(prefix='irc3log')
        self.addCleanup(shutil.rmtree, self.logdir)

    def test_logger(self):
        bot = self.callFTU(
            **{'irc3.plugins.logger': dict(
                handler='irc3.plugins.logger.file_handler',
                filename=os.path.join(
                    self.logdir,
                    '{host}-{channel}-{date:%Y-%m-%d}.log')
            )}
        )
        filenames = os.listdir(self.logdir)
        self.assertEqual(len(filenames), 0, filenames)

        bot.dispatch(':server 332 foo #foo :topic')
        filenames = glob.glob(os.path.join(self.logdir, '*.log'))
        self.assertEqual(len(filenames), 1, filenames)

        with open(filenames[0]) as fd:
            self.assertIn('server has set topic to: topic', fd.read())

        bot.dispatch(':bar!user@host JOIN #foo')
        with open(filenames[0]) as fd:
            self.assertIn('bar joined #foo', fd.read())

        bot.dispatch(':bar!user@host PRIVMSG #foo :!help')
        with open(filenames[0]) as fd:
            self.assertIn('<bar> !help', fd.read())

        bot.dispatch(':bar!user@host QUIT')
        with open(filenames[0]) as fd:
            self.assertIn('bar has quit', fd.read())

        bot.dispatch('PRIVMSG #foo :youhou!')
        with open(filenames[0]) as fd:
            self.assertIn('<irc3> youhou!', fd.read())

@@ -1570,32 +1570,26 @@
 bot.
-dispatch('PRIVMSG #foo :
+privmsg('#foo',  '
 youh

fix tests