This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
/
cmd_test.go
830 lines (690 loc) · 19.4 KB
/
cmd_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
package bot
import (
"errors"
"fmt"
"reflect"
"sort"
"strings"
"sync"
"testing"
"time"
)
var (
channel string
replies chan string
cmdError chan string
user *User
msgs []string
errs []string
protoParams interface{}
)
const (
expectedMsg = "msg"
cmd = "cmd"
cmdDescription = "Command description"
cmdExampleArgs = "arg1 arg2"
)
func waitMessages(t *testing.T, count int, errorCount int) {
for {
select {
case reply := <-replies:
msgs = append(msgs, reply)
case err := <-cmdError:
errs = append(errs, err)
case <-time.After(1 * time.Second):
t.Error("Timeout waiting for messages")
t.Errorf("msgs received: %v", msgs)
t.Errorf("errs received: %v", errs)
t.Fatal()
}
if len(msgs) == count && len(errs) == errorCount {
return
}
}
}
func responseHandler(target string, message string, sender *User) {
channel = target
user = sender
replies <- message
}
func responseHandlerV2(om OutgoingMessage) {
channel = om.Target
user = om.Sender
protoParams = om.ProtoParams
replies <- om.Message
}
func errorHandler(msg string, err error) {
cmdError <- fmt.Sprintf("%s: %s", msg, err)
}
func reset() {
channel = ""
user = &User{Nick: ""}
replies = make(chan string, 10)
cmdError = make(chan string, 10)
msgs = []string{}
errs = []string{}
protoParams = nil
commands = make(map[string]*customCommand)
periodicCommands = make(map[string]PeriodicConfig)
passiveCommands = make(map[string]*customCommand)
filterCommands = make(map[string]*customCommand)
}
func newBot() *Bot {
return New(&Handlers{
Response: responseHandler,
Errored: errorHandler,
},
&Config{
Protocol: "test",
Server: "test",
},
)
}
func newBotV2() *Bot {
return New(&Handlers{
Response: responseHandler,
ResponseV2: responseHandlerV2,
Errored: errorHandler,
},
&Config{
Protocol: "test",
Server: "test",
},
)
}
func registerValidCommand() {
RegisterCommand(cmd, cmdDescription, cmdExampleArgs,
func(c *Cmd) (string, error) {
return expectedMsg, nil
})
}
func TestPeriodicCommands(t *testing.T) {
reset()
RegisterPeriodicCommand("morning",
PeriodicConfig{
CronSpec: "0 08 * * mon-fri",
Channels: []string{"#channel"},
CmdFunc: func(channel string) (string, error) { return "ok " + channel, nil },
})
b := New(
&Handlers{Response: responseHandler},
&Config{Protocol: "test", Server: "test"},
)
defer b.Close()
entries := b.cron.Entries()
if len(entries) != 1 {
t.Fatal("Should have one cron job entry")
}
if entries[0].Next.Hour() != 8 {
t.Fatal("Cron job should be scheduled to 8am")
}
entries[0].Job.Run()
waitMessages(t, 1, 0)
if msgs[0] != "ok #channel" {
t.Fatal("Invalid reply")
}
}
func TestMultiplePeriodicCommands(t *testing.T) {
reset()
RegisterPeriodicCommand("morning",
PeriodicConfig{
CronSpec: "0 08 * * mon-fri",
Channels: []string{"#channel"},
CmdFunc: func(channel string) (string, error) { return "ok_morning " + channel, nil },
})
RegisterPeriodicCommand("afternoon",
PeriodicConfig{
CronSpec: "0 12 * * mon-fri",
Channels: []string{"#channel"},
CmdFunc: func(channel string) (string, error) { return "ok_afternoon " + channel, nil },
})
b := New(
&Handlers{Response: responseHandler},
&Config{Protocol: "test", Server: "test"},
)
defer b.Close()
entries := b.cron.Entries()
if len(entries) != 2 {
t.Fatal("Should have 2 cron job entries")
}
if entries[0].Next.Hour() != 8 {
t.Fatal("First cron job should be scheduled for 8am")
}
if entries[1].Next.Hour() != 12 {
t.Fatal("Second cron job should be schedule for 12am")
}
entries[0].Job.Run()
entries[1].Job.Run()
waitMessages(t, 2, 0)
if len(msgs) != 2 {
t.Fatal("Should have two replies in the channel")
}
sort.Strings(msgs)
if msgs[0] != "ok_afternoon #channel" {
t.Fatal("Invalid reply in afternoon cron job")
}
if msgs[1] != "ok_morning #channel" {
t.Fatalf("Invalid reply in morning cron job.")
}
}
func TestErroredPeriodicCommand(t *testing.T) {
reset()
RegisterPeriodicCommand("bugged",
PeriodicConfig{
CronSpec: "0 08 * * mon-fri",
Channels: []string{"#channel"},
CmdFunc: func(channel string) (string, error) { return "bug", errors.New("error") },
})
b := newBot()
defer b.Close()
entries := b.cron.Entries()
if len(entries) != 1 {
t.Fatal("Should have one cron job entry")
}
entries[0].Job.Run()
waitMessages(t, 0, 1)
if len(msgs) != 0 {
t.Error("Should not have a reply in the channel")
}
if len(errs) != 1 {
t.Error("Expected 1 error")
}
}
func TestPeriodicCommandsV2(t *testing.T) {
reset()
RegisterPeriodicCommandV2("morning",
PeriodicConfig{
CronSpec: "0 08 * * mon-fri",
CmdFuncV2: func() ([]CmdResult, error) {
ret := []CmdResult{
{Message: "message 1", Channel: "#channel1"},
{Message: "message 2", Channel: "#channel2"}}
return ret, nil
},
})
channels := make([]string, 0, 2)
b := New(&Handlers{Response: func(target string, message string, sender *User) {
channels = append(channels, target)
channel = target
user = sender
replies <- message
}},
&Config{Protocol: "test", Server: "test"})
defer b.Close()
entries := b.cron.Entries()
if len(entries) != 1 {
t.Fatal("Should have one cron job entry")
}
if entries[0].Next.Hour() != 8 {
t.Fatal("Cron job should be scheduled to 8am")
}
entries[0].Job.Run()
waitMessages(t, 2, 0)
if len(channels) != 2 {
t.Fatal("Should have 2 destinations channels", len(channels))
}
if msgs[0] != "message 1" {
t.Fatal("Invalid first reply")
}
if channels[0] != "#channel1" {
t.Fatal("Invalid channel for first message", channels[0])
}
if msgs[1] != "message 2" {
t.Fatal("Invalid second reply")
}
if channels[1] != "#channel2" {
t.Fatal("Invalid channel for second message", channels[1])
}
}
func TestErroredPeriodicCommandsV2(t *testing.T) {
reset()
RegisterPeriodicCommandV2("morning",
PeriodicConfig{
CronSpec: "0 08 * * mon-fri",
CmdFuncV2: func() ([]CmdResult, error) {
return nil, errors.New("error")
},
})
b := newBot()
defer b.Close()
entries := b.cron.Entries()
if len(entries) != 1 {
t.Fatal("Should have one cron job entry")
}
if entries[0].Next.Hour() != 8 {
t.Fatal("Cron job should be scheduled to 8am")
}
entries[0].Job.Run()
waitMessages(t, 0, 1)
if len(msgs) != 0 {
t.Error("Should not have a reply in the channel")
}
if len(errs) != 1 {
t.Error("Expected 1 error")
}
}
func TestDisabledCommands(t *testing.T) {
reset()
commands = make(map[string]*customCommand)
b := newBot()
RegisterCommand("cmd", "", "",
func(c *Cmd) (string, error) {
return "active", nil
})
RegisterPassiveCommand("passive",
func(cmd *PassiveCmd) (string, error) {
return "passive", nil
})
b.Disable([]string{"cmd"})
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!cmd"}, &User{Nick: "user"})
time.Sleep(100)
if len(msgs) != 0 {
t.Fatal("Should not execute disabled active commands")
}
b.Disable([]string{"passive"})
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "regular message"}, &User{Nick: "user"})
time.Sleep(100)
if len(msgs) != 0 {
t.Fatal("Should not execute disabled passive commands")
}
}
func TestCommandNotRegistered(t *testing.T) {
reset()
b := newBot()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!not_a_cmd"}, &User{})
time.Sleep(100)
if len(msgs) != 0 {
t.Fatal("Should not reply if a command is not found")
}
}
func TestInvalidCmdArgs(t *testing.T) {
reset()
registerValidCommand()
b := newBot()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!cmd \"invalid arg"}, &User{Nick: "user"})
waitMessages(t, 1, 0)
if channel != "#go-bot" {
t.Error("Should reply to #go-bot channel")
}
if !strings.HasPrefix(msgs[0], "Error parsing") {
t.Fatal("Should reply with an error message")
}
}
func TestErroredCmd(t *testing.T) {
reset()
cmdError := errors.New("error")
RegisterCommand("cmd", "", "",
func(c *Cmd) (string, error) {
return "", cmdError
})
b := newBot()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!cmd"}, &User{Nick: "user"})
waitMessages(t, 1, 1)
if channel != "#go-bot" {
t.Fatal("Invalid channel")
}
if msgs[0] != fmt.Sprintf(errorExecutingCommand, "cmd", cmdError.Error()) {
t.Fatal("Reply should contain the error message")
}
if len(errs) != 1 {
t.Error("Expected the command to return an error")
}
}
func TestValidCmdOnChannel(t *testing.T) {
reset()
registerValidCommand()
b := newBot()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!cmd"}, &User{Nick: "user"})
waitMessages(t, 1, 0)
if channel != "#go-bot" {
t.Fatal("Command called on channel should reply to channel")
}
if msgs[0] != expectedMsg {
t.Fatal("Invalid command reply")
}
}
func TestChannelData(t *testing.T) {
cd := ChannelData{
Protocol: "irc",
Server: "myserver",
Channel: "#mychan",
}
if cd.URI() != "irc://myserver/#mychan" {
t.Fatal("URI should return a valid IRC URI")
}
}
func TestHelpWithNoArgs(t *testing.T) {
reset()
registerValidCommand()
b := newBot()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!help"}, &User{Nick: "user"})
waitMessages(t, 2, 0)
expectedReply := []string{
fmt.Sprintf(helpAboutCommand, CmdPrefix),
fmt.Sprintf(availableCommands, "cmd"),
}
if !reflect.DeepEqual(msgs, expectedReply) {
t.Fatalf("Invalid reply. Expected %v got %v", expectedReply, msgs)
}
}
func TestDisableHelp(t *testing.T) {
reset()
registerValidCommand()
b := newBot()
b.Disable([]string{"help"})
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!help"}, &User{Nick: "user"})
time.Sleep(100)
if len(replies) > 0 {
t.Fatalf("Should not execute help after disabling it")
}
}
func TestHelpForACommand(t *testing.T) {
reset()
registerValidCommand()
b := newBot()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!help cmd"}, &User{Nick: "user"})
waitMessages(t, 2, 0)
expectedReply := []string{
fmt.Sprintf(helpDescripton, cmdDescription),
fmt.Sprintf(helpUsage, CmdPrefix, cmd, cmdExampleArgs),
}
if !reflect.DeepEqual(msgs, expectedReply) {
t.Fatalf("Invalid reply. Expected %v got %v", expectedReply, msgs)
}
}
func TestHelpWithNonExistingCommand(t *testing.T) {
reset()
registerValidCommand()
b := newBot()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!help not_a_cmd"}, &User{Nick: "user"})
expectedReply := []string{
fmt.Sprintf(helpAboutCommand, CmdPrefix),
fmt.Sprintf(availableCommands, "cmd"),
}
waitMessages(t, 2, 0)
if !reflect.DeepEqual(msgs, expectedReply) {
t.Fatalf("Invalid reply. Expected %v got %v", expectedReply, msgs)
}
}
func TestHelpWithInvalidArgs(t *testing.T) {
reset()
registerValidCommand()
b := newBot()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!help cmd \"invalid arg"}, &User{Nick: "user"})
waitMessages(t, 1, 0)
if !strings.HasPrefix(msgs[0], "Error parsing") {
t.Fatal("Should reply with an error message")
}
}
func TestCmdV2(t *testing.T) {
reset()
RegisterCommandV2("cmd", "", "",
func(c *Cmd) (CmdResult, error) {
return CmdResult{
Channel: "#channel",
Message: "message"}, nil
})
b := newBot()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!cmd"}, &User{Nick: "user"})
waitMessages(t, 1, 0)
if channel != "#channel" {
t.Error("Wrong channel")
}
if !reflect.DeepEqual([]string{"message"}, msgs) {
t.Error("Invalid reply")
}
}
func TestCmdV2WithProtoParams(t *testing.T) {
reset()
RegisterCommandV2("cmd", "", "",
func(c *Cmd) (CmdResult, error) {
return CmdResult{
Channel: "#channel",
Message: "message",
ProtoParams: &CmdResult{Message: "Nested!"},
}, nil
})
b := newBotV2()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!cmd"}, &User{Nick: "user"})
waitMessages(t, 1, 0)
if channel != "#channel" {
t.Error("Wrong channel")
}
if !reflect.DeepEqual([]string{"message"}, msgs) {
t.Error("Invalid reply")
}
if pa, ok := protoParams.(*CmdResult); ok {
if pa.Message != "Nested!" {
t.Error("Information lost in copying.")
}
} else {
t.Error("Failed to pass proto args through.")
}
}
func TestCmdV2WithoutSpecifyingChannel(t *testing.T) {
reset()
RegisterCommandV2("cmd", "", "",
func(c *Cmd) (CmdResult, error) {
return CmdResult{Message: "message"}, nil
})
b := newBot()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!cmd"}, &User{Nick: "user"})
waitMessages(t, 1, 0)
if channel != "#go-bot" {
t.Error("Should reply to original channel if no channel is returned")
}
}
func TestPassiveCommand(t *testing.T) {
reset()
passiveCommands = make(map[string]*customCommand)
echo := func(cmd *PassiveCmd) (string, error) { return cmd.Raw, nil }
ping := func(cmd *PassiveCmd) (string, error) { return "pong", nil }
errored := func(cmd *PassiveCmd) (string, error) { return "", errors.New("error") }
RegisterPassiveCommand("echo", echo)
RegisterPassiveCommand("ping", ping)
RegisterPassiveCommand("errored", errored)
b := newBot()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "test"}, &User{Nick: "user"})
waitMessages(t, 2, 1)
if channel != "#go-bot" {
t.Error("Invalid channel")
}
if len(msgs) != 2 {
t.Fatal("Invalid reply")
}
if len(errs) != 1 {
t.Error("Expected 1 error")
}
sort.Strings(msgs)
if msgs[0] != "pong" {
t.Error("ping command not executed")
}
if msgs[1] != "test" {
t.Error("echo command not executed")
}
}
func TestPassiveCommandV2(t *testing.T) {
reset()
result := CmdResultV3{
Channel: "#channel",
Message: make(chan string),
Done: make(chan bool)}
ping := func(cmd *PassiveCmd) (CmdResultV3, error) { return result, nil }
errored := func(cmd *PassiveCmd) (CmdResultV3, error) { return CmdResultV3{}, errors.New("error") }
RegisterPassiveCommandV2("ping", ping)
RegisterPassiveCommandV2("errored", errored)
b := newBot()
go b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "test"}, &User{Nick: "user"})
result.Message <- "pong"
result.Done <- true
waitMessages(t, 1, 1)
if channel != "#channel" {
t.Error("Invalid channel")
}
if len(msgs) != 1 {
t.Fatal("Invalid reply")
}
if msgs[0] != "pong" {
t.Error("ping command not executed")
}
}
func TestCmdV3(t *testing.T) {
reset()
result := CmdResultV3{
Channel: "#channel",
Message: make(chan string),
Done: make(chan bool)}
RegisterCommandV3("cmd", "", "",
func(c *Cmd) (CmdResultV3, error) {
return result, nil
})
b := newBot()
go b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!cmd"}, &User{Nick: "user"})
result.Message <- "message"
result.Done <- true
waitMessages(t, 1, 0)
if channel != "#channel" {
t.Error("Wrong channel")
}
if !reflect.DeepEqual([]string{"message"}, msgs) {
t.Error("Invalid reply")
}
}
func TestCmdV3WithoutSpecifyingChannel(t *testing.T) {
reset()
result := CmdResultV3{
Message: make(chan string),
Done: make(chan bool)}
RegisterCommandV3("cmd", "", "",
func(c *Cmd) (CmdResultV3, error) {
return result, nil
})
b := newBot()
go b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!cmd"}, &User{Nick: "user"})
result.Message <- "message"
result.Done <- true
waitMessages(t, 1, 0)
if channel != "#go-bot" {
t.Error("Should reply to original channel if no channel is returned")
}
if !reflect.DeepEqual([]string{"message"}, msgs) {
t.Error("Invalid reply")
}
}
func TestFilterCommand(t *testing.T) {
reset()
passiveCommands = make(map[string]*customCommand)
ping := func(cmd *PassiveCmd) (string, error) { return "pong", nil }
modified := func(cmd *FilterCmd) (string, error) { return "PONG!", nil }
errored := func(cmd *FilterCmd) (string, error) { return "", errors.New("error") }
RegisterPassiveCommand("ping", ping)
RegisterFilterCommand("modified", modified)
RegisterFilterCommand("errored", errored)
b := newBot()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "test"}, &User{Nick: "user"})
waitMessages(t, 1, 1)
if channel != "#go-bot" {
t.Error("Invalid channel")
}
if len(msgs) != 1 {
t.Fatal("Invalid reply")
}
if len(errs) != 1 {
t.Error("Expected 1 error")
}
sort.Strings(msgs)
if msgs[0] != "PONG!" {
t.Error("filter command not working")
}
}
func TestFilterCommandSilence(t *testing.T) {
reset()
passiveCommands = make(map[string]*customCommand)
ping := func(cmd *PassiveCmd) (string, error) { return "pong", nil }
silenced := func(cmd *FilterCmd) (string, error) { return "", nil }
errored := func(cmd *FilterCmd) (string, error) { return "Ignored", errors.New("error") }
RegisterPassiveCommand("ping", ping)
RegisterFilterCommand("silenced", silenced)
RegisterFilterCommand("errored", errored)
b := newBot()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "test"}, &User{Nick: "user"})
waitMessages(t, 0, 1)
if len(msgs) != 0 {
t.Fatal("Expected no messages!")
}
if len(errs) != 1 {
t.Error("Expected 1 error")
}
}
func TestFilterCommandSilenceSendV2(t *testing.T) {
reset()
passiveCommands = make(map[string]*customCommand)
ping := func(cmd *PassiveCmd) (string, error) { return "pong", nil }
silenced := func(cmd *FilterCmd) (string, error) { return "", nil }
errored := func(cmd *FilterCmd) (string, error) { return "Ignored", errors.New("error") }
RegisterPassiveCommand("ping", ping)
RegisterFilterCommand("silenced", silenced)
RegisterFilterCommand("errored", errored)
b := newBotV2()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "test"}, &User{Nick: "user"})
waitMessages(t, 0, 1)
if len(msgs) != 0 {
t.Fatal("Expected no messages!")
}
if len(errs) != 1 {
t.Error("Expected 1 error")
}
}
// how to test channels..
// https://www.hugopicado.com/2016/10/01/testing-over-golang-channels.html
func TestMessageStreams(t *testing.T) {
var mutex = &sync.Mutex{}
reset()
var msSender1 *MessageStream
var msSender2 *MessageStream
RegisterMessageStream("streamOne", func(ms1 *MessageStream) error {
mutex.Lock()
msSender1 = ms1
mutex.Unlock()
return nil
})
RegisterMessageStream("streamTwo", func(ms2 *MessageStream) error {
mutex.Lock()
msSender2 = ms2
mutex.Unlock()
return nil
})
b1 := New(&Handlers{Response: responseHandler, Errored: errorHandler}, &Config{Protocol: "protoA", Server: "test"})
b2 := New(&Handlers{Response: responseHandler, Errored: errorHandler}, &Config{Protocol: "protoB", Server: "test"})
msmB1 := MessageStreamMessage{
Message: "hello botOne",
ChannelData: &ChannelData{Server: b1.Server, Protocol: b1.Protocol, Channel: "#go-bot"},
}
msmB2 := MessageStreamMessage{
Message: "hello botTwo",
ChannelData: &ChannelData{Server: b2.Server, Protocol: b2.Protocol, Channel: "#go-bot"},
}
// give New() a second to make() the chans and setup the objects
time.Sleep(2 * time.Second)
// when you send a message destined for b1 #go-bot, even if you send it to b2, it should arrive at b1
mutex.Lock()
msSender1.Data <- msmB1
if "hello botOne" != <-replies {
t.Fatal("message not Recieved at Channel")
}
msSender2.Data <- msmB1
if "hello botOne" != <-replies {
t.Fatal("message not Recieved at Channel")
}
// and vice-versa
// when you send a message destined for b2 #go-bots, even if you send it to b1, it should arrive at b2
msSender1.Data <- msmB2
if "hello botTwo" != <-replies {
t.Fatal("message not Recieved at Channel")
}
msSender2.Data <- msmB2
if "hello botTwo" != <-replies {
t.Fatal("message not Recieved at Channel")
}
mutex.Unlock()
}