# vim:ts=4:autoindent
$test = true

require 'test/unit'
require 'DeeDial'

ACCT = 900

class DeeDialTest < Test::Unit::TestCase
    T1 = "}\a-->. +^#6[T1=Remote:#900\r"

    T2 = "}}}-.Dark Realm^#5[T1=Latenightbbs^#6[T1=Ragnarok:900^#7<T1-Psychopathic Law\r"
    T3 = "}}}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"
    T4 = "}}}76-.Chasm^#7<T1-Destruction\r"

    def newDeeDial
        DeeDial.new( "x", "x", "900 000 000", false )
    end

    def test_online1
        dd = newDeeDial
        assert_kind_of Hash,                dd.stations
        assert_kind_of Hash,                dd.users
        assert_equal true,                  dd.stations.empty?
        assert_equal true,                  dd.users.empty?

        dd.handle_raw_line(T1) 

    end

    def test_online2
        dd = newDeeDial
        dd.handle_raw_line(T1)
        dd.handle_raw_line(T2)

        assert_equal( "Dark Realm",         dd.stations[""].name )
        assert_equal( false,                dd.stations[""].is_full )
        assert_equal( "Latenightbbs",       dd.users["5"].handle )
        assert_equal( nil,                  dd.users["1"] )
    end

    def test_online3
        dd = newDeeDial
        dd.handle_raw_line(T1)
        dd.handle_raw_line(T2)
        dd.handle_raw_line(T3)
        dd.handle_raw_line(T4)

        assert_equal "Savage Frontier",     dd.stations["7"].name
        assert_equal 7,                     dd.stations["7"].line

        assert_equal "Mouse",               dd.users["71"].handle
        assert_equal 59,                    dd.users["71"].acct
        assert_equal "71",                  dd.users["71"].line
        assert_equal 7,                     dd.users["71"].on_station.line
    end

    def test_login_logout1
        dd = newDeeDial
        l1 = "}7-->. -^#4[T1:Xodarap -=B-(:#042*\r"
        l2 = "}7-->. +^#5[T1:FlipperPA:#047*\r"

        [ T1, T2, T3, T4 ].each{ |l| dd.handle_raw_line(l) }
        assert_kind_of DeeDial::User,   dd.stations["7"].users[4]

        dd.handle_raw_line(l1)
        assert_nil                      dd.stations["7"].users[4]
        assert_nil                      dd.stations["7"].users[5]
        assert_nil                      dd.users["74"]
        assert_nil                      dd.users["75"]
        dd.handle_raw_line(l2)
        assert_not_nil                  dd.stations["7"].users[5]
        assert_not_nil                  dd.users["75"]
    end

    def test_wadm_login_logout1
        dd = newDeeDial

        a1 = "ACCEPT port 0 host 123.456.789.12"
        a2 = "ASSIGN port 0 to Subs 5"              # This will change with PORT_TO_LINK
                                                    # That's a major pain in the ass! XXX
        l1 = "}7-->. -^#4[T1:Xodarap -=B-(:#042*\r"
        l2 = "}7-->. +^#5[T1:FlipperPA:#047*\r"

        run_login = run_logout = false
        dd.on_event( self, :type => :login ) do |ev|
            u = ev[:user]
            assert_equal "75",          u.line
            assert_equal "FlipperPA",   u.handle
            assert_equal dd.ports[0],   u.on_port
            run_login = true
        end
        dd.on_event( self, :type => :logout ) do |ev|
            u = ev[:user]
            assert_equal "74",          u.line
            assert_equal "Xodarap -=B-(", u.handle
            assert_equal 42,            u.acct
            run_logout = true
        end

        [ T1, T2, T3, T4 ].each{ |l| dd.handle_raw_line(l) }
        assert_kind_of DeeDial::User,   dd.stations["7"].users[4]

        dd.handle_raw_line(l1)
        assert_nil                      dd.stations["7"].users[4]
        assert_nil                      dd.stations["7"].users[5]
        assert_nil                      dd.users["74"]
        assert_nil                      dd.users["75"]

        assert_nil                      dd.ports[0]
        dd.handle_raw_admin(a1)
        assert_kind_of Array,           dd.ports[0]
        assert_equal "123.456.789.12",  dd.ports[0][0]
        assert_equal nil,               dd.users["75"]

        dd.handle_raw_admin(a2)
        assert_equal dd.ports[0],       dd.users["75"].on_port

        dd.handle_raw_line(l2)
        assert_not_nil                  dd.stations["7"].users[5]
        assert_not_nil                  dd.users["75"]

        assert_equal true,              run_login
        assert_equal true,              run_logout
    end

    def test_changehat1
        dd = newDeeDial
        l1 = "7#4<T1:Xodarap -=B-() i have a hat now\r"

        run = false
        dd.on_event( self, :type => :chatter ) do |ev|
            u = ev[:user]
            assert_equal u,                 dd.users["74"]
            assert_equal "i have a hat now", ev[:message]
            run = true
        end

        [ T1, T2, T3, T4 ].each{ |l| dd.handle_raw_line(l) }
        assert_equal false,                 dd.users["74"].is_hat
        assert_equal true,                  dd.users["74"].is_validated
        assert_equal 1,                     dd.users["74"].channel
        dd.handle_raw_line(l1)
        assert_equal dd.users["74"],        dd.stations["7"].users[4]
        assert_equal true,                  dd.users["74"].is_hat
        assert_equal true,                  dd.users["74"].is_validated
        assert_equal true,                  run
    end

    def test_whoson1
        dd = newDeeDial

        a1 = "ACCEPT port 0 host 123.456.789.12"
        a2 = "ASSIGN port 0 to Main 5"

        [ T1, T2, T3, T4 ].each{ |l| dd.handle_raw_line(l) }
        [ a1, a2 ].each{ |l| dd.handle_admin(l) }

        dd.stations.each do |st1, st2|
            assert_kind_of String,              st1
            assert_kind_of DeeDial::Station,    st2
        end

        dd.ports.each_with_index do |host, p|
            assert_kind_of Array,               host
            assert_kind_of Fixnum,              p
        end
    end

    def test_privmsg1   ## happy vday - paved, this now works
        dd = newDeeDial

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

        run = false

        dd.on_event( self, :type => :privmsg, :priv_to => 1 ) do |ev|
            run = true
            assert_equal "70",          ev[:user].line
        end

        [ T1, T2, T3, T4, l1 ].each{ |l| dd.handle_raw_line(l) }
        assert_equal true,          run
    end

end