dispatch/pkg/irc/client_test.go

175 lines
3.8 KiB
Go
Raw Permalink Normal View History

2015-06-10 03:48:54 +00:00
package irc
import (
"net"
"testing"
2016-03-01 00:51:26 +00:00
"github.com/stretchr/testify/assert"
2015-06-10 03:48:54 +00:00
)
2016-01-13 19:05:14 +00:00
func testClientSend() (*Client, chan string) {
c := NewClient(&Config{})
2016-01-13 19:05:14 +00:00
conn := &mockConn{hook: make(chan string, 16)}
2015-06-11 00:04:51 +00:00
c.conn = conn
2016-01-15 07:26:06 +00:00
c.sendRecv.Add(1)
2015-06-11 00:04:51 +00:00
go c.send()
2016-01-13 19:05:14 +00:00
return c, conn.hook
2015-06-11 00:04:51 +00:00
}
2015-06-10 03:48:54 +00:00
type mockConn struct {
hook chan string
net.Conn
}
func (c *mockConn) Write(b []byte) (int, error) {
c.hook <- string(b)
return len(b), nil
}
2015-06-11 00:04:51 +00:00
func (c *mockConn) Close() error {
return nil
2015-06-10 03:48:54 +00:00
}
func TestPass(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-11 02:57:52 +00:00
c.writePass("pass")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "PASS pass\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
func TestNick(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-10 03:48:54 +00:00
c.Nick("test2")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "NICK test2\r\n", <-out)
2015-06-11 02:57:52 +00:00
c.writeNick("nick")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "NICK nick\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
func TestUser(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-11 02:57:52 +00:00
c.writeUser("user", "rn")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "USER user 0 * :rn\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
func TestOper(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-10 03:48:54 +00:00
c.Oper("name", "pass")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "OPER name pass\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
func TestMode(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-10 03:48:54 +00:00
c.Mode("#chan", "+o", "user")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "MODE #chan +o user\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
func TestQuit(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-10 03:48:54 +00:00
c.connected = true
c.Quit()
2016-01-13 19:05:14 +00:00
assert.Equal(t, "QUIT\r\n", <-out)
2015-06-10 03:48:54 +00:00
_, ok := <-c.quit
assert.Equal(t, false, ok)
}
func TestJoin(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-10 03:48:54 +00:00
c.Join("#a")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "JOIN #a\r\n", <-out)
2015-06-10 03:48:54 +00:00
c.Join("#b", "#c")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "JOIN #b,#c\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
func TestPart(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-10 03:48:54 +00:00
c.Part("#a")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "PART #a\r\n", <-out)
2015-06-10 03:48:54 +00:00
c.Part("#b", "#c")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "PART #b,#c\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
func TestTopic(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-10 03:48:54 +00:00
c.Topic("#chan")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "TOPIC #chan\r\n", <-out)
c.Topic("#chan", "apple pie")
assert.Equal(t, "TOPIC #chan :apple pie\r\n", <-out)
c.Topic("#chan", "")
assert.Equal(t, "TOPIC #chan :\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
func TestInvite(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-10 03:48:54 +00:00
c.Invite("user", "#chan")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "INVITE user #chan\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
func TestKick(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-10 03:48:54 +00:00
c.Kick("#chan", "user")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "KICK #chan user\r\n", <-out)
2015-06-10 03:48:54 +00:00
c.Kick("#chan", "a", "b")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "KICK #chan a,b\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
func TestPrivmsg(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-10 03:48:54 +00:00
c.Privmsg("user", "the message")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "PRIVMSG user :the message\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
func TestNotice(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-10 03:48:54 +00:00
c.Notice("user", "the message")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "NOTICE user :the message\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
2020-05-20 02:19:40 +00:00
func TestReplyCTCP(t *testing.T) {
c, out := testClientSend()
c.ReplyCTCP("user", "PING", "PONG")
assert.Equal(t, "NOTICE user :\x01PING PONG\x01\r\n", <-out)
}
2015-06-10 03:48:54 +00:00
func TestWhois(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-10 03:48:54 +00:00
c.Whois("user")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "WHOIS user\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
func TestAway(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2015-06-10 03:48:54 +00:00
c.Away("not here")
2016-01-13 19:05:14 +00:00
assert.Equal(t, "AWAY :not here\r\n", <-out)
2015-06-10 03:48:54 +00:00
}
2015-06-11 02:57:52 +00:00
func TestRegister(t *testing.T) {
2016-01-13 19:05:14 +00:00
c, out := testClientSend()
2020-05-23 07:42:20 +00:00
c.Config.Nick = "nick"
c.Config.Username = "user"
c.Config.Realname = "rn"
2015-06-11 02:57:52 +00:00
c.register()
2020-05-23 06:05:37 +00:00
assert.Equal(t, "CAP LS 302\r\n", <-out)
2016-01-13 19:05:14 +00:00
assert.Equal(t, "NICK nick\r\n", <-out)
assert.Equal(t, "USER user 0 * :rn\r\n", <-out)
2015-06-11 02:57:52 +00:00
2020-06-04 00:28:41 +00:00
c.Config.ServerPassword = "pass"
2015-06-11 02:57:52 +00:00
c.register()
2020-05-23 06:05:37 +00:00
assert.Equal(t, "CAP LS 302\r\n", <-out)
2016-01-13 19:05:14 +00:00
assert.Equal(t, "PASS pass\r\n", <-out)
assert.Equal(t, "NICK nick\r\n", <-out)
assert.Equal(t, "USER user 0 * :rn\r\n", <-out)
2015-06-11 02:57:52 +00:00
}
2016-02-03 20:12:32 +00:00
func TestFlushChannels(t *testing.T) {
c, out := testClientSend()
c.addChannel("#chan1")
c.flushChannels()
assert.Equal(t, <-out, "JOIN #chan1\r\n")
c.addChannel("#chan2")
c.addChannel("#chan4")
c.removeChannels("#chan4")
2016-02-03 20:12:32 +00:00
c.addChannel("#chan3")
c.flushChannels()
assert.Equal(t, <-out, "JOIN #chan2,#chan3\r\n")
}