summaryrefslogtreecommitdiff
path: root/chat/chat_test.go
blob: 65a50dd3fd874d0b8ebffb69a597c01ebab0e228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package chat

import (
	"fmt"
	"testing"
)

func TestJoining(t *testing.T) {
	InitRooms(2)

	b1, r1, err := Join("test", "Foo")
	if err != nil {
		t.Fatalf("Could not join room: %s", err)
	}
	defer b1.Leave()

	if _, _, err = Join("test", "Foo"); err != NickAlreadyInUse {
		t.Fatalf("Got error \"%s\", expected \"%s\"", err, NickAlreadyInUse)
	}

	if _, _, err = Join("test", ""); err != EmptyNick {
		t.Fatalf("Got error \"%s\", expected \"%s\"", err, EmptyNick)
	}

	if _, _, err = Join("test", "abcdefghijklmnopqrstuvwxyz"); err != NickTooLong {
		t.Fatalf("Got error \"%s\", expected \"%s\"", err, NickTooLong)
	}

	b2, r2, err := Join("test", "Bar")
	if err != nil {
		t.Fatalf("Could not join room a second time: %s", err)
	}
	defer b2.Leave()

	// since we joined to the same room, r1 and r2 should be equal
	if r1 != r2 {
		t.Error("r1 and r2 are not equal")
	}

	buddies := r1.ListBuddies()
	seen := make(map[string]bool)
	for _, b := range buddies {
		seen[b] = true
	}
	if !seen["Foo"] || !seen["Bar"] {
		t.Error("Foo or Bar missing in buddy list")
	}

	if _, _, err = Join("test", "Baz"); err != RoomIsFull {
		t.Fatalf("Got error \"%s\", expected \"%s\"", err, RoomIsFull)
	}
}

func TestLeaving(t *testing.T) {
	InitRooms(10)

	b1, r, err := Join("test", "Foo")
	if err != nil {
		t.Fatalf("Could not join room: %s", err)
	}

	b2, _, err := Join("test", "Bar")

	b1.Leave()

	buddies := r.ListBuddies()
	for _, b := range buddies {
		if b == "Foo" {
			t.Error("Foo is still in buddy list, even after leaving")
		}
	}

	b2.Leave()

	// The room is now empty. It should no longer exist.
	if _, ok := rooms["test"]; ok {
		t.Error("Room test still exists, although no user is left")
	}
}

func checkMsg(t *testing.T, m Message, typ MsgType, user string, txt string) {
	fail := fmt.Sprintf("Expected a %s message from %s", typ, user)
	if txt != "" {
		fail += fmt.Sprintf(" with message '%s'.", txt)
	}

	failed := false

	if m.Type != typ {
		failed = true
		fail += fmt.Sprintf(" Type is wrong (%s)", m.Type)
	}
	if m.User != user {
		failed = true
		fail += fmt.Sprintf(" User is wrong (%s)", m.User)
	}
	if txt != "" && m.Text != txt {
		failed = true
		fail += fmt.Sprintf(" Text is wrong (%s)", m.Text)
	}

	if failed {
		t.Error(fail)
	}
}

func TestChatting(t *testing.T) {
	InitRooms(10)

	// In this test, we will ignore the errors of Join(), since we already tested that stuff in the tests above.

	b1, _, _ := Join("test", "Foo")
	b2, _, _ := Join("test", "Bar")

	checkMsg(t, <-b1.Receive, MsgJoin, "Bar", "")
	checkMsg(t, <-b2.Receive, MsgJoin, "Bar", "")

	b2.Say("Hello")
	checkMsg(t, <-b1.Receive, MsgChat, "Bar", "Hello")
	checkMsg(t, <-b2.Receive, MsgChat, "Bar", "Hello")

	b1.Say(":)")
	checkMsg(t, <-b1.Receive, MsgChat, "Foo", ":)")
	checkMsg(t, <-b2.Receive, MsgChat, "Foo", ":)")

	b3, _, _ := Join("test", "Baz")

	checkMsg(t, <-b1.Receive, MsgJoin, "Baz", "")
	checkMsg(t, <-b2.Receive, MsgJoin, "Baz", "")
	checkMsg(t, <-b3.Receive, MsgJoin, "Baz", "")

	b3.Say("!")

	checkMsg(t, <-b1.Receive, MsgChat, "Baz", "!")
	checkMsg(t, <-b2.Receive, MsgChat, "Baz", "!")
	checkMsg(t, <-b3.Receive, MsgChat, "Baz", "!")

	b2.Leave()

	checkMsg(t, <-b1.Receive, MsgLeave, "Bar", "")
	checkMsg(t, <-b3.Receive, MsgLeave, "Bar", "")
}