# vim:ts=4:autoindent

require 'test/unit'
require 'DeeDialServ'

class DeeDialServTest < Test::Unit::TestCase

    # Test some logins and logouts

    def newDeeDialServ
        DeeDialServ.new( "x", "x", "x", "x" )
    end

    def test_parse_login1
        dd = newDeeDialServ
        run = false

        l = "}\a-->. +^#1[T2=Remote:900\r"

        dd.on_event( :type => :loginout ){ |ed|
            run = true
            assert_equal true,      ed[:login]
            assert_nil              ed[:link]
            assert_equal false,     ed[:station_full]
            assert_equal false,     ed[:station_locked]
            assert_equal 1,         ed[:line]
            assert_equal true,      ed[:validated]
            assert_equal false,     ed[:hat]
            assert_equal 2,         ed[:channel]
            assert_equal true,      ed[:remote]
            assert_equal "Remote",  ed[:handle]
            assert_equal 900,       ed[:acct]
            assert_equal false,     ed[:acctstar]
            assert_equal "1",       ed.linkline
        }
        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_login2
        dd = newDeeDialServ
        run = false

        l = "}}-->, -^#6(T1:\r"

        dd.on_event( :type => :loginout ){ |ed|
            run = true
            assert_equal false,     ed[:login]
            assert_nil              ed[:link]
            assert_equal false,     ed[:station_full]
            assert_equal true,      ed[:station_locked]
            assert_equal 6,         ed[:line]
            assert_equal false,     ed[:validated]
            assert_equal false,     ed[:hat]
            assert_equal 1,         ed[:channel]
            assert_equal false,     ed[:remote]
            assert_equal "",        ed[:handle]
            assert_equal nil,       ed[:acct]
            assert_equal nil,       ed[:acctstar]
            assert_equal "6",       ed.linkline
        }
        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_login3
        dd = newDeeDialServ
        run = false

        l = "}7-->: +^#3[T1:Mouse /mu!@:#059*\r"

        dd.on_event( :type => :loginout ){ |ed|
            run = true
            assert_equal true,      ed[:login]
            assert_equal 7,         ed[:link]
            assert_equal true,      ed[:station_full]
            assert_equal false,     ed[:station_locked]
            assert_equal 3,         ed[:line]
            assert_equal true,      ed[:validated]
            assert_equal false,     ed[:hat]
            assert_equal 1,         ed[:channel]
            assert_equal false,     ed[:remote]
            assert_equal "Mouse /mu!@", ed[:handle]
            assert_equal 59,        ed[:acct]
            assert_equal true,      ed[:acctstar]
            assert_equal "73",      ed.linkline
        }
        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_login4   # added after Pete's ddial kicked us
        dd = newDeeDialServ
        run = false

        l = "}}5-->. +^#5\r"

        dd.on_event( :type => :loginout ){ |ed|
            run = true
            assert_equal true,      ed[:login]
            assert_equal 5,         ed[:link]
            assert_equal false,     ed[:station_full]
            assert_equal false,     ed[:station_locked]
            assert_equal 5,         ed[:line]
            assert_equal false,     ed[:validated]
            assert_equal false,     ed[:hat]
            assert_equal 1,         ed[:channel]
            assert_equal false,     ed[:remote]
            assert_equal "?",       ed[:handle]
            assert_equal nil,       ed[:acct]
            assert_equal nil,       ed[:acctstar]
            assert_equal "55",      ed.linkline
        }
        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_sp1
        dd = newDeeDialServ
        run = false

        l = "}}}-.Savage Frontier^#1[T1=Remote^#7[T1=MooBot:900\r"

        dd.on_event( :type => :sp ){ |ed|
            run = true
            assert_nil              ed[:link]
            assert_equal false,     ed[:station_full]
            assert_equal false,     ed[:station_locked]
            assert_equal "Savage Frontier", ed[:station_name]
            assert_equal "",        ed.linkline

            u = ed[:users][1]
            assert_equal 1,         u[:line]
            assert_equal "Remote",  u[:handle]
            assert_equal nil,       u[:acct]
            assert_equal true,      u[:remote]
            assert_equal "1",       u.linkline

            u = ed[:users][7]
            assert_equal "MooBot",  u[:handle]
            assert_equal 900,       u[:acct]
            assert_equal false,     u[:acctstar]
            assert_equal "7",       u.linkline
        }
        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_sp2
        dd = newDeeDialServ
        run = false

        l = "}}}7-.Savage Frontier^#1[T1:Mouse:059*^#2[T1:Pirate:011*^#3[T1:Accidental Angel:034*^#4[T1:Xodarap -=B-(:042*^#6<T1-Chasm^#7<T1-Dark Realm\r"

        dd.on_event( :type => :sp ){ |ed|
            run = true
            assert_equal 7,         ed[:link]
            assert_equal 59,        ed[:users][1][:acct]
            assert_equal false,     ed[:users][2][:remote]
            assert_equal true,      ed[:users][3][:acctstar]
            assert_equal nil,       ed[:users][5]
            assert_equal true,      ed[:users][6][:remote]
            assert_equal "71",      ed[:users][1].linkline
        }
        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_priv1
        dd = newDeeDialServ
        run = false

        l = "/PS #1[T1:Mouse) hello remote\r"

        dd.on_event( :type => :privmsg ){ |ed|
            run = true
            assert_equal :station,  ed[:priv_to]
            assert_equal nil,       ed[:link]
            assert_equal 1,         ed[:line]
            assert_equal "Mouse",   ed[:handle]
            assert_equal false,     ed[:hat]
            assert_equal "hello remote", ed[:message]
        }
        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_priv2
        dd = newDeeDialServ
        run = false

        l = "/P123 714#5<T3:]-[3r]<) suckit\r"

        dd.on_event( :type => :privmsg ){ |ed|
            run = true
            assert_equal 123,       ed[:priv_to]
            assert_equal 714,       ed[:link]
            assert_equal 5,         ed[:line]
            assert_equal 3,         ed[:channel]
            assert_equal "]-[3r]<", ed[:handle]
            assert_equal true,      ed[:hat]
            assert_equal "suckit",  ed[:message]
        }
        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_chatter1
        dd = newDeeDialServ
        run = false

        l = "243#2(T1:?!%ma) thinkthin\r"

        dd.on_event( :type => :chatter ){ |ed|
            run = true
            assert_equal nil,       ed[:priv_to]
            assert_equal 243,       ed[:link]
            assert_equal 2,         ed[:line]
            assert_equal 1,         ed[:channel]
            assert_equal "?!%ma",   ed[:handle]
            assert_equal false,     ed[:hat]
            assert_equal false,     ed[:validated]
            assert_equal "thinkthin", ed[:message]
        }
        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_chatter2
        dd = newDeeDialServ
        run = false

        l = "#7[T4:Kingpin) yoRm0m\r"

        dd.on_event( :type => :chatter ){ |ed|
            run = true
            assert_equal nil,       ed[:priv_to]
            assert_equal nil,       ed[:link]
            assert_equal 7,         ed[:line]
            assert_equal 4,         ed[:channel]
            assert_equal "Kingpin", ed[:handle]
            assert_equal false,     ed[:hat]
            assert_equal true,      ed[:validated]
            assert_equal "yoRm0m", ed[:message]
        }
        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_priv1
        dd = newDeeDialServ
        run = false

        l = "/P1 7#2[T1:Mouse) join\r"

        dd.on_event( :type => :privmsg ){ |ed|
            run = true
            assert_equal 1,         ed[:priv_to]
            assert_equal 7,         ed[:link]
            assert_equal 2,         ed[:line]
            assert_equal 1,         ed[:channel]
            assert_equal "Mouse",   ed[:handle]
            assert_equal false,     ed[:hat]
            assert_equal true,      ed[:validated]
            assert_equal "join",    ed[:message]
        }
        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_priv2
        dd = newDeeDialServ
        run = false

        l = "/P1 7#0<T4:Mouse) join\r"

        dd.on_event( :type => :privmsg ){ |ed|
            run = true
            assert_equal 1,         ed[:priv_to]
            assert_equal 7,         ed[:link]
            assert_equal 0,         ed[:line]
            assert_equal 4,         ed[:channel]
            assert_equal "Mouse",   ed[:handle]
            assert_equal true,      ed[:hat]
            assert_equal true,      ed[:validated]
            assert_equal "join",    ed[:message]
        }
        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_regex1
        dd = newDeeDialServ
        run = false

        l = "/PS 7#1[T1:Mouse) set notify on\r"

        dd.on_event( :type => :privmsg, :message => /^set / ){ |ev|
            run = true
            assert_equal "set notify on", ev[:message]
        }

        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_regex2
        dd = newDeeDialServ
        run = false

        l = "/PS 7#1[T1:Mouse) hOwAbOuTcAsEiNsEnSiTiVe\r"

        dd.on_event( :type => :privmsg, :message => /^howaboutcase/i ){ |ev|
            run = true
            assert_equal "hOwAbOuTcAsEiNsEnSiTiVe", ev[:message]
        }

        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_email1
        dd = newDeeDialServ
        run = false

        l = "/E~A059"

        dd.on_event( :type => :email ){ |ev|
            run = true
            assert_equal 59,        ev[:acct]
            assert_equal 1,         ev[:line]
            assert_equal nil,       ev[:to]
        }

        dd.handle_raw_line(l)
        assert_equal true,          run
    end

    def test_parse_email2
        dd = newDeeDialServ
        run = false

        l = "/E~C048043liz, you smell bad."

        dd.on_event( :type => :email ){ |ev|
            run = true
            assert_equal 48,        ev[:acct]
            assert_equal 3,         ev[:line]
            assert_equal 43,        ev[:to]
            assert_equal "liz, you smell bad.", ev[:message]
        }

        dd.handle_raw_line(l)
        assert_equal true,          run
    end
end