-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain_test.go
223 lines (207 loc) · 5.75 KB
/
main_test.go
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package karmabot
import (
"testing"
"time"
"github.com/kamaln7/karmabot/database"
"github.com/nlopes/slack"
)
func TestNew(t *testing.T) {
cfg := &Config{}
b := New(cfg)
if b == nil {
t.Fatalf("New(cfg) returned nil; wanted *Bot")
}
if b.Config != cfg {
t.Errorf("New(cfg): returned Bot with incorrect config")
}
}
func newBot(cfg *Config) (*Bot, *TestChatService, *TestDatabase) {
cs := &TestChatService{
IncomingEvents: make(chan slack.RTMEvent),
}
db := &TestDatabase{}
db.InsertPoints(&database.Points{
From: "point_giver",
To: "onehundred_points",
Points: 100,
Reason: "for being a swell guy",
})
cfg.Slack = cs
cfg.DB = db
return New(cfg), cs, db
}
func TestListen(t *testing.T) {
b, cs, _ := newBot(&Config{})
hasExited := false
hasStarted := make(chan int)
go func() {
close(hasStarted)
b.Listen()
hasExited = true
}()
<-hasStarted
time.Sleep(1 * time.Millisecond)
if hasExited {
t.Errorf("Listen: exited immediately")
}
close(cs.IncomingEvents)
time.Sleep(1 * time.Millisecond)
if !hasExited {
t.Errorf("Listen: did not exit after closing incoming events channel")
}
// TODO: To properly test Listen, it needs to be decoupled further from what it actually does.
}
func TestHandleSlackEvent(t *testing.T) {
tt := []struct {
Name string
ReacjiDisabled bool
ReactionAddedEvent *slack.ReactionAddedEvent
ReactionRemovedEvent *slack.ReactionRemovedEvent
MessageEvent *slack.MessageEvent
ExpectMessage string
ShouldHavePoints int
}{
{
Name: "+1 added with reacji disabled",
ReacjiDisabled: true,
ReactionAddedEvent: &slack.ReactionAddedEvent{
Type: "reaction_added",
User: "user",
ItemUser: "onehundred_points",
Reaction: "+1",
},
ShouldHavePoints: 100,
},
{
Name: "+1 added with reacji enabled",
ReactionAddedEvent: &slack.ReactionAddedEvent{
Type: "reaction_added",
User: "user",
ItemUser: "onehundred_points",
Reaction: "+1",
},
ExpectMessage: "onehundred_points == 101 (+1 for user added a :+1: reactji)",
ShouldHavePoints: 101,
},
{
Name: "-1 added with reacji enabled",
ReactionAddedEvent: &slack.ReactionAddedEvent{
Type: "reaction_added",
User: "user",
ItemUser: "onehundred_points",
Reaction: "-1",
},
ExpectMessage: "onehundred_points == 99 (-1 for user added a :-1: reactji)",
ShouldHavePoints: 99,
},
{
Name: "cat added with reacji enabled",
ReactionAddedEvent: &slack.ReactionAddedEvent{
Type: "reaction_added",
User: "user",
ItemUser: "onehundred_points",
Reaction: "cat",
},
ShouldHavePoints: 100,
},
{
Name: "+1 removed with reacji disabled",
ReacjiDisabled: true,
ReactionRemovedEvent: &slack.ReactionRemovedEvent{
Type: "reaction_removed",
User: "user",
ItemUser: "onehundred_points",
Reaction: "+1",
},
ShouldHavePoints: 100,
},
{
Name: "+1 removed with reacji enabled",
ReactionRemovedEvent: &slack.ReactionRemovedEvent{
Type: "reaction_removed",
User: "user",
ItemUser: "onehundred_points",
Reaction: "+1",
},
ExpectMessage: "onehundred_points == 99 (-1 for user removed a :+1: reactji)",
ShouldHavePoints: 99,
},
{
Name: "-1 removed with reacji enabled",
ReactionRemovedEvent: &slack.ReactionRemovedEvent{
Type: "reaction_removed",
User: "user",
ItemUser: "onehundred_points",
Reaction: "-1",
},
ExpectMessage: "onehundred_points == 101 (+1 for user removed a :-1: reactji)",
ShouldHavePoints: 101,
},
{
Name: "cat removed with reacji enabled",
ReactionRemovedEvent: &slack.ReactionRemovedEvent{
Type: "reaction_removed",
User: "user",
ItemUser: "onehundred_points",
Reaction: "cat",
},
ShouldHavePoints: 100,
},
{
Name: "should tell user about their sick karma events from the past",
MessageEvent: &slack.MessageEvent{
Msg: slack.Msg{
Type: "message",
Text: "karmabot throwback",
Channel: "user",
User: "onehundred_points",
},
},
ExpectMessage: "önehundred_points received 100 points from ρoint_giver now for for being a swell guy",
ShouldHavePoints: 100,
},
}
for _, tc := range tt {
upvote, downvote := make(StringList, 1), make(StringList, 1)
upvote.Set("+1")
downvote.Set("-1")
b, cs, db := newBot(&Config{
Reactji: &ReactjiConfig{
Enabled: !tc.ReacjiDisabled,
Upvote: upvote,
Downvote: downvote,
},
})
if tc.ReactionAddedEvent != nil {
b.handleReactionAddedEvent(tc.ReactionAddedEvent)
}
if tc.ReactionRemovedEvent != nil {
b.handleReactionRemovedEvent(tc.ReactionRemovedEvent)
}
if tc.MessageEvent != nil {
b.handleMessageEvent(tc.MessageEvent)
}
if len(cs.SentMessages) != 0 && tc.ExpectMessage == "" {
t.Errorf("%s: sent unexpected message %#v", tc.Name, cs.SentMessages[0])
} else if len(cs.SentMessages) == 0 && tc.ExpectMessage != "" {
t.Errorf("%s: did not send expected message %v", tc.Name, tc.ExpectMessage)
} else if len(cs.SentMessages) > 1 {
t.Errorf("%s: sent too many messages: %v", tc.Name, cs.SentMessages)
} else if tc.ExpectMessage != "" {
msg := cs.SentMessages[0]
if msg.Text != tc.ExpectMessage {
t.Errorf("%s: sent message %q; want %q", tc.Name, msg.Text, tc.ExpectMessage)
}
if msg.Channel != "user" {
t.Errorf("%s: sent message to %q; want to send message to %q", tc.Name, msg.Channel, "user")
}
}
u, err := db.GetUser("onehundred_points")
if err != nil {
t.Fatalf("%s: db.GetUser: %v", tc.Name, err)
}
if u.Points != tc.ShouldHavePoints {
t.Errorf("%s: user %v has %v points; want %v", tc.Name, "onehundred_points", u.Points, tc.ShouldHavePoints)
}
}
}