This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
cache_test.go
1633 lines (1359 loc) · 39.7 KB
/
cache_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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package disgord
import (
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/andersfylling/disgord/json"
)
func jsonbytes(format string, args ...interface{}) []byte {
return []byte(fmt.Sprintf(format, args...))
}
func deadlockTest(t *testing.T, c Cache, evt string, data []byte) {
// all locks should have been released
t.Run("deadlock", func(t *testing.T) {
done := make(chan struct{})
go func() {
if _, err := cacheDispatcher(c, evt, data); err != nil {
t.Error("failed to create channel from event data", err)
}
close(done)
}()
select {
case <-time.After(1 * time.Second):
t.Fatal("deadlock detected")
case <-done:
}
})
}
func deadlockGetTest(t *testing.T, cb func()) {
// all locks should have been released
t.Run("deadlock", func(t *testing.T) {
done := make(chan struct{})
go func() {
cb()
close(done)
}()
select {
case <-time.After(1 * time.Second):
t.Fatal("deadlock detected")
case <-done:
}
})
}
func TestBasicCache_Channels(t *testing.T) {
cache := NewBasicCache()
id := Snowflake(1)
topic := "test"
name := "anders"
t.Run("get", func(t *testing.T) {
t.Run("existing", func(t *testing.T) {
cache := NewBasicCache()
cache.Channels.Store[id] = &Channel{ID: id}
channel, err := cache.GetChannel(id)
if err != nil {
t.Error("cache has no channel")
}
if channel == nil {
t.Error("channel is nil")
}
})
t.Run("get unknown", func(t *testing.T) {
cache := NewBasicCache()
channel, err := cache.GetChannel(id)
if err == nil {
t.Error("should return error when channel is unknown")
}
if channel != nil {
t.Error("channel should be nil")
}
if !errors.Is(err, ErrCacheMiss) {
t.Error("expected error to be a cache miss err")
}
})
})
t.Run("create", func(t *testing.T) {
evt, err := cacheDispatcher(cache, EvtChannelCreate, jsonbytes(`{"id":%d,"topic":"%s"}`, id, topic))
if err != nil {
t.Fatal("failed to create channel from event data", err)
}
holder, ok := evt.(*ChannelCreate)
if !ok {
t.Fatal("unable to cast event to ChannelCreate type")
}
if holder == nil {
t.Fatal("holder is nil")
}
channel := holder.Channel
if channel == nil {
t.Fatal("channel is nil")
}
if channel.ID != id {
t.Errorf("channel id should be %d, got %d", id, channel.ID)
}
if channel.Topic != topic {
t.Errorf("channel topic should be %s, got %s", topic, channel.Topic)
}
})
deadlockTest(t, cache, EvtChannelCreate, jsonbytes(`{"id":%d,"topic":"%s"}`, id*2, "abdsa"))
t.Run("duplicate-create", func(t *testing.T) {
// should just use the latest event
// perhaps we were kicked from the guild and re-added. We might not have
// properly deleted the channel, so the latest event should be utilized.
currentChannel, err := cache.GetChannel(id)
if err != nil {
t.Fatal("cache has no channel")
}
// don't overwrite just given fields, completely delete old reference
evt, err := cacheDispatcher(cache, EvtChannelCreate, jsonbytes(`{"id":%d,"name":"%s"}`, id, name))
if err != nil {
t.Fatal("failed to update channel from event data", err)
}
holder, ok := evt.(*ChannelCreate)
if !ok {
t.Fatal("unable to cast event to ChannelCreate type")
}
if holder == nil {
t.Fatal("holder is nil")
}
channel := holder.Channel
if channel == nil {
t.Fatal("channel is nil")
}
if channel.Name != name {
t.Errorf("channel name is %s, expects %s", channel.Name, name)
}
// make sure old data does not exist
if channel.Topic != "" {
t.Errorf("topic should be empty, but got %s", channel.Topic)
}
// make sure the old reference was not updated
if currentChannel.Name == channel.Name {
t.Errorf("old reference was updated, cache is not read only!")
}
})
t.Run("update-existing", func(t *testing.T) {
// update a channel already in cache
oldChannel, err := cache.GetChannel(id)
if err != nil {
t.Fatal("cache has no channel")
}
updatedName := "test v2"
// should only update given fields
evt, err := cacheDispatcher(cache, EvtChannelUpdate, jsonbytes(`{"id":%d,"name":"%s"}`, id, updatedName))
if err != nil {
t.Fatal("failed to create channel from event data", err)
}
holder := evt.(*ChannelUpdate)
channel := holder.Channel
if channel.Topic != oldChannel.Topic {
t.Error("topic was overwritten")
}
if channel.Name != updatedName {
t.Errorf("name is %s, expected %s", channel.Name, updatedName)
}
if oldChannel.Name == updatedName {
t.Error("cache is not read only")
}
})
deadlockTest(t, cache, EvtChannelUpdate, jsonbytes(`{"id":%d,"topic":"%s"}`, id*2, "dsffddsfdf"))
t.Run("update-unknown-channel", func(t *testing.T) {
// if the channel does not exist, we should just create it
unknownID := id * 23
oldChannel, err := cache.GetChannel(unknownID)
if !errors.Is(err, ErrCacheMiss) {
t.Fatal("should have been a cache miss error")
}
if oldChannel != nil {
t.Fatal("returned object should be nil")
}
evt, err := cacheDispatcher(cache, EvtChannelUpdate, jsonbytes(`{"id":%d,"topic":"%s","name":"%s"}`, unknownID, topic, name))
if err != nil {
t.Fatal("failed to create channel from event data", err)
}
holder := evt.(*ChannelUpdate)
channel := holder.Channel
if channel.ID != unknownID {
t.Errorf("channel id should be %d, got %d", unknownID, channel.ID)
}
if channel.Topic != topic {
t.Errorf("channel topic should be %s, got %s", topic, channel.Topic)
}
if channel.Name != name {
t.Errorf("channel name should be %s, got %s", name, channel.Name)
}
})
t.Run("pin update", func(t *testing.T) {
channel, err := cache.GetChannel(id)
if err != nil {
t.Fatal("cache has no channel")
}
now := Time{
Time: time.Now(),
}
t.Run("new", func(t *testing.T) {
data, err := now.MarshalJSON()
if err != nil {
t.Fatal("unable to marshal pin timestamp")
}
evt, err := cacheDispatcher(cache, EvtChannelPinsUpdate, jsonbytes(`{"channel_id":%d,"last_pin_timestamp":%s}`, id, data))
if err != nil {
t.Fatal("failed to create event struct", err)
}
holder := evt.(*ChannelPinsUpdate)
if holder.LastPinTimestamp.Second() != now.Second() {
t.Errorf("incorrect time. Got %d, wants %d", holder.LastPinTimestamp.Second(), now.Second())
}
if !channel.LastPinTimestamp.IsZero() {
t.Error("cache is not read-only")
}
channelNow, _ := cache.GetChannel(id)
if channelNow.LastPinTimestamp.IsZero() {
t.Error("last ping timestamp was not updated")
}
})
t.Run("outdated", func(t *testing.T) {
now.Time = now.Add(-10 * time.Second)
data, err := now.MarshalJSON()
if err != nil {
t.Fatal("unable to marshal pin timestamp")
}
evt, err := cacheDispatcher(cache, EvtChannelPinsUpdate, jsonbytes(`{"channel_id":%d,"last_pin_timestamp":%s}`, id, data))
if err != nil {
t.Fatal("failed to create event struct", err)
}
holder := evt.(*ChannelPinsUpdate)
if holder.LastPinTimestamp.Second() != now.Second() {
t.Error("incorrect timestamp")
}
if !channel.LastPinTimestamp.IsZero() {
t.Error("cache is not read-only")
}
channelNow, _ := cache.GetChannel(id)
if channelNow.LastPinTimestamp.Second() == now.Second() {
t.Error("last ping timestamp was updated")
}
})
})
t.Run("delete", func(t *testing.T) {
channel, err := cache.GetChannel(id)
if err != nil {
t.Fatal("cache has no channel")
}
if channel == nil {
t.Fatal("returned channel should not be nil")
}
evt, err := cacheDispatcher(cache, EvtChannelDelete, jsonbytes(`{"id":%d}`, id))
if err != nil {
t.Fatal("failed to create event struct", err)
}
holder := evt.(*ChannelDelete)
if holder.Channel.ID != id {
t.Errorf("expected channel id to be %d, got %d", id, holder.Channel.ID)
}
channel, err = cache.GetChannel(id)
if !errors.Is(err, ErrCacheMiss) {
t.Fatal("should have been a cache miss error")
}
if channel != nil {
t.Fatal("returned object should be nil")
}
})
}
func TestBasicCache_TypingStart(t *testing.T) {
cache := NewBasicCache()
userID := Snowflake(123)
channelID := Snowflake(348765348)
evtData := jsonbytes(`{"user_id":%d,"channel_id":"%s"}`, userID, channelID)
t.Run("event", func(t *testing.T) {
evt, err := cacheDispatcher(cache, EvtTypingStart, evtData)
if err != nil {
t.Fatal("failed to create event struct", err)
}
typingStart := evt.(*TypingStart)
if typingStart.UserID != userID {
t.Errorf("incorrect user id. Got %d, wants %d", typingStart.UserID, userID)
}
if typingStart.ChannelID != channelID {
t.Errorf("incorrect channel id. Got %d, wants %d", typingStart.ChannelID, channelID)
}
})
deadlockTest(t, cache, EvtTypingStart, evtData)
}
func TestBasicCache_Ready(t *testing.T) {
cache := NewBasicCache()
guildIDsToGuilds := func(ids []Snowflake) (container []*GuildUnavailable) {
for _, id := range ids {
container = append(container, &GuildUnavailable{ID: id, Unavailable: true})
}
return
}
guilds := guildIDsToGuilds([]Snowflake{3, 4, 6, 7, 3})
guildsJson, err := json.Marshal(guilds)
if err != nil {
t.Fatal("unable to marshal unavail guilds")
}
evtData := jsonbytes(`{"v":8,"user":%s,"guilds":%s,"session_id":"gf7k4gfe78g"}`, `{"id":234}`, guildsJson)
t.Run("event", func(t *testing.T) {
evt, err := cacheDispatcher(cache, EvtReady, evtData)
if err != nil {
t.Fatal("failed to create event struct", err)
}
ready := evt.(*Ready)
if ready.User.ID != 234 {
t.Errorf("incorrect user id. Got %d, wants %d", ready.User.ID, 234)
}
if len(guilds) != len(ready.Guilds) {
t.Error("incorrect number of guilds")
}
if len(cache.Guilds.Store) != 0 {
t.Errorf("cache pre-allocated the guilds, but is it really needed?")
}
if cache.CurrentUser.ID != 234 {
t.Error("current user id was not updated")
}
})
deadlockTest(t, cache, EvtReady, evtData)
}
func TestBasicCache_Message(t *testing.T) {
cache := NewBasicCache()
evtData := jsonbytes(`{"id":1,"content":"testing","guild_id":2,"channel_id":3}`)
t.Run("create", func(t *testing.T) {
evt, err := cacheDispatcher(cache, EvtMessageCreate, evtData)
if err != nil {
t.Fatal("failed to create event struct", err)
}
msg := evt.(*MessageCreate).Message
if msg.ID != 1 {
t.Error("incorrect message id")
}
// should not create a DM channel
if len(cache.Channels.Store) > 0 {
t.Error("channel was created")
}
})
deadlockTest(t, cache, EvtMessageCreate, evtData)
t.Run("create DM", func(t *testing.T) {
// if guild id is not set, it's a DM message
// TODO: group DM
evt, err := cacheDispatcher(cache, EvtMessageCreate, jsonbytes(`{"id":1,"content":"testing","channel_id":3}`))
if err != nil {
t.Fatal("failed to create event struct", err)
}
msg := evt.(*MessageCreate).Message
if msg.ID != 1 {
t.Error("incorrect message id")
}
if len(cache.Channels.Store) == 0 {
t.Error("missing DM channel")
}
channel, err := cache.GetChannel(3)
if errors.Is(err, ErrCacheMiss) {
t.Fatal("DM channel was not created for message")
}
if channel.Type != ChannelTypeDM {
t.Errorf("channel was created with incorrect type. Got %d, wants %d", channel.Type, ChannelTypeDM)
}
})
}
func TestBasicCache_UserUpdate(t *testing.T) {
cache := NewBasicCache()
cache.CurrentUser = &User{ID: 1, Username: "anders", Bot: true}
evt, err := cacheDispatcher(cache, EvtUserUpdate, jsonbytes(`{"id":1,"username":"test"}`))
if err != nil {
t.Fatal("failed to create event struct", err)
}
usr := evt.(*UserUpdate)
if usr.ID != 1 {
t.Fatalf("incorrect user id. Got %d", usr.ID)
}
if usr.Username != "test" {
t.Error("username was not updated")
}
if !usr.Bot {
t.Error("bot value was overwritten")
}
if cache.CurrentUser.Username != "test" {
t.Error("current users username was not updated")
}
}
func TestBasicCache_Guilds(t *testing.T) {
t.Run("get", func(t *testing.T) {
t.Run("existing", func(t *testing.T) {
cache := NewBasicCache()
cache.Guilds.Store[1] = &guildCacheContainer{Guild: &Guild{ID: 1}}
guild, err := cache.GetGuild(1)
if err != nil {
t.Error("cache has no such guild")
}
if guild == nil {
t.Error("guild is nil")
}
})
t.Run("get unknown", func(t *testing.T) {
cache := NewBasicCache()
guild, err := cache.GetChannel(1)
if err == nil {
t.Error("should return error when guild is unknown")
}
if guild != nil {
t.Error("guild should be nil")
}
if !errors.Is(err, ErrCacheMiss) {
t.Error("expected error to be a cache miss err")
}
})
})
t.Run("get complex", func(t *testing.T) {
cache := NewBasicCache()
cache.Guilds.Store[1] = &guildCacheContainer{
Guild: &Guild{ID: 1},
ChannelIDs: []Snowflake{1, 4},
Members: map[Snowflake]*Member{
3: {UserID: 3, Nick: "andy"},
56: {UserID: 56},
34: {UserID: 34},
},
}
cache.Users.Store[3] = &User{ID: 3, Username: "anders"}
cache.Users.Store[56] = &User{ID: 56, Username: "test"}
cache.Users.Store[34] = &User{ID: 34, Username: "botlol"}
cache.Channels.Store[1] = &Channel{ID: 1, Name: "channel#1"}
cache.Channels.Store[4] = &Channel{ID: 4, Name: "fourth"}
guild, err := cache.GetGuild(1)
if err != nil {
t.Error("cache has no such guild")
}
if guild == nil {
t.Fatal("guild is nil")
}
if len(guild.Channels) != 2 {
t.Error("incorrect number of channels")
}
if len(guild.Members) != 3 {
t.Error("incorrect number of members")
}
if guild.Members[0].User == nil {
t.Error("member is missing user object")
}
})
t.Run("delete", func(t *testing.T) {
t.Run("kicked", func(t *testing.T) {
cache := NewBasicCache()
cache.Guilds.Store[1] = &guildCacheContainer{Guild: &Guild{ID: 1}}
evt, err := cacheDispatcher(cache, EvtGuildDelete, jsonbytes(`{"id":%d}`, 1))
if err != nil {
t.Fatal("failed to create event struct", err)
}
guildEvt := evt.(*GuildDelete).UnavailableGuild
if guildEvt.ID != 1 {
t.Error("incorrect guild id")
}
guild, err := cache.GetGuild(1)
if !errors.Is(err, ErrCacheMiss) {
t.Error("expected cache miss err")
}
if guild != nil {
t.Error("guild should be nil")
}
})
t.Run("deleted", func(t *testing.T) {
cache := NewBasicCache()
cache.Guilds.Store[1] = &guildCacheContainer{Guild: &Guild{ID: 1}}
evt, err := cacheDispatcher(cache, EvtGuildDelete, jsonbytes(`{"id":%d,"unavailable":true}`, 1))
if err != nil {
t.Fatal("failed to create event struct", err)
}
guildEvt := evt.(*GuildDelete).UnavailableGuild
if guildEvt.ID != 1 {
t.Error("incorrect guild id")
}
if !guildEvt.Unavailable {
t.Error("should have been unavail")
}
guild, err := cache.GetGuild(1)
if !errors.Is(err, ErrCacheMiss) {
t.Error("expected cache miss err")
}
if guild != nil {
t.Error("guild should be nil")
}
})
})
cache := NewBasicCache()
id := Snowflake(10)
name := "test guild"
memberID := Snowflake(1)
nick := "andy"
username := "anders"
channelID := Snowflake(352)
channelName := "sidgfs asd"
t.Run("create-with-members-and-channels", func(t *testing.T) {
memberData := jsonbytes(`{"nick":"%s","user":{"id":%d,"username":"%s"}}`, nick, memberID, username)
channelData := jsonbytes(`{"id":%d,"name":"%s"}`, channelID, channelName)
data := jsonbytes(`{"id":%d,"name":"%s","members":[%s],"channels":[%s]}`, id, name, memberData, channelData)
evt, err := cacheDispatcher(cache, EvtGuildCreate, data)
if err != nil {
t.Fatal("failed to create event", err)
}
holder, ok := evt.(*GuildCreate)
if !ok {
t.Fatal("unable to cast event to GuildCreate type")
}
if holder == nil {
t.Fatal("holder is nil")
}
guild := holder.Guild
if guild == nil {
t.Fatal("guild is nil")
}
if guild.ID != id {
t.Errorf("channel id should be %d, got %d", id, guild.ID)
}
if guild.Name != name {
t.Errorf("channel topic should be %s, got %s", name, guild.Name)
}
container, ok := cache.Guilds.Store[id]
if !ok || container.Guild == nil {
t.Error("guild was not cached")
}
if len(container.ChannelIDs) != 1 {
t.Fatal("missing channel id")
}
if cid := container.ChannelIDs[0]; cid.IsZero() {
t.Fatal("channel id was stored as 0")
} else {
channel, ok := cache.Channels.Store[cid]
if !ok {
t.Fatal("channel was not saved to cache")
}
if channel.ID != channelID {
t.Error("incorrect channel id")
}
if channel.Name != channelName {
t.Error("incorrect channel name")
}
}
if member, ok := container.Members[memberID]; !ok {
t.Error("member was not stored")
} else {
if member == nil {
t.Fatal("member is nil")
}
if member.UserID != memberID {
t.Error("member is missing user id")
}
if member.Nick != nick {
t.Error("incorrect nickname")
}
}
if user, ok := cache.Users.Store[memberID]; !ok {
t.Error("user was not stored")
} else {
if user == nil {
t.Fatal("user is nil")
}
if user.ID != memberID {
t.Error("user is missing user id")
}
if user.Username != username {
t.Error("incorrect username")
}
}
})
deadlockTest(t, cache, EvtGuildCreate, jsonbytes(`{"id":%d,"name":"%s"}`, id*2, "abdsa"))
t.Run("create-without-members-and-channels", func(t *testing.T) {
data := jsonbytes(`{"id":%d,"name":"%s","members":[],"channels":[]}`, id, name)
cache := NewBasicCache()
evt, err := cacheDispatcher(cache, EvtGuildCreate, data)
if err != nil {
t.Fatal("failed to create event", err)
}
guild := evt.(*GuildCreate).Guild
if guild.ID != id {
t.Errorf("channel id should be %d, got %d", id, guild.ID)
}
if guild.Name != name {
t.Errorf("channel topic should be %s, got %s", name, guild.Name)
}
container, ok := cache.Guilds.Store[id]
if !ok || container.Guild == nil {
t.Error("guild was not cached")
}
if container.Members == nil {
t.Error("members map was not initialized")
}
})
t.Run("update-without-members-and-channels", func(t *testing.T) {
data := jsonbytes(`{"id":%d,"name":"%s","members":[],"channels":[]}`, id, name)
cache := NewBasicCache()
evt, err := cacheDispatcher(cache, EvtGuildUpdate, data)
if err != nil {
t.Fatal("failed to create event", err)
}
guild := evt.(*GuildUpdate).Guild
if guild.ID != id {
t.Errorf("channel id should be %d, got %d", id, guild.ID)
}
if guild.Name != name {
t.Errorf("channel topic should be %s, got %s", name, guild.Name)
}
container, ok := cache.Guilds.Store[id]
if !ok || container.Guild == nil {
t.Error("guild was not cached")
}
if container.Members == nil {
t.Error("members map was not initialized")
}
})
t.Run("update-with-members-and-channels", func(t *testing.T) {
// these should not be stored as they will not be in a guild update
memberData := jsonbytes(`{"nick":"test","user":{"id":345,"username":"fdhhghgj"}}`)
channelData := jsonbytes(`{"id":345,"name":"fgdfhjlll"}`)
newGuildName := name + " v2"
data := jsonbytes(`{"id":%d,"name":"%s","members":[%s],"channels":[%s]}`, id, newGuildName, memberData, channelData)
evt, err := cacheDispatcher(cache, EvtGuildUpdate, data)
if err != nil {
t.Fatal("failed to create event", err)
}
holder, ok := evt.(*GuildUpdate)
if !ok {
t.Fatal("unable to cast event to GuildCreate type")
}
if holder == nil {
t.Fatal("holder is nil")
}
guild := holder.Guild
if guild == nil {
t.Fatal("guild is nil")
}
if guild.ID != id {
t.Errorf("channel id should be %d, got %d", id, guild.ID)
}
if guild.Name != newGuildName {
t.Errorf("channel topic should be %s, got %s", newGuildName, guild.Name)
}
// check that nothing else changed!
container, ok := cache.Guilds.Store[id]
if !ok || container.Guild == nil {
t.Error("guild was not cached")
}
if len(container.ChannelIDs) != 1 {
t.Fatal("missing channel id")
}
if cid := container.ChannelIDs[0]; cid.IsZero() {
t.Fatal("channel id was stored as 0")
} else {
channel, ok := cache.Channels.Store[cid]
if !ok {
t.Fatal("channel was not saved to cache")
}
if channel.ID != channelID {
t.Error("incorrect channel id")
}
if channel.Name != channelName {
t.Error("incorrect channel name")
}
}
if len(container.Members) != 1 {
t.Error("incorrect number of members")
}
if member, ok := container.Members[memberID]; !ok {
t.Error("member was not stored")
} else {
if member == nil {
t.Fatal("member is nil")
}
if member.UserID != memberID {
t.Error("member is missing user id")
}
if member.Nick != nick {
t.Error("incorrect nickname")
}
}
if user, ok := cache.Users.Store[memberID]; !ok {
t.Error("user was not stored")
} else {
if user == nil {
t.Fatal("user is nil")
}
if user.ID != memberID {
t.Error("user is missing user id")
}
if user.Username != username {
t.Error("incorrect username")
}
}
})
t.Run("update-on-unknown-guild", func(t *testing.T) {
cache := NewBasicCache()
memberData := jsonbytes(`{"nick":"%s","user":{"id":%d,"username":"%s"}}`, nick, memberID, username)
channelData := jsonbytes(`{"id":%d,"name":"%s"}`, channelID, channelName)
data := jsonbytes(`{"id":%d,"name":"%s","members":[%s],"channels":[%s]}`, id, name, memberData, channelData)
evt, err := cacheDispatcher(cache, EvtGuildUpdate, data)
if err != nil {
t.Fatal("failed to create event", err)
}
holder, ok := evt.(*GuildUpdate)
if !ok {
t.Fatal("unable to cast event to GuildCreate type")
}
if holder == nil {
t.Fatal("holder is nil")
}
guild := holder.Guild
if guild == nil {
t.Fatal("guild is nil")
}
if guild.ID != id {
t.Errorf("channel id should be %d, got %d", id, guild.ID)
}
if guild.Name != name {
t.Errorf("channel topic should be %s, got %s", name, guild.Name)
}
// check that nothing else changed!
container, ok := cache.Guilds.Store[id]
if !ok || container.Guild == nil {
t.Error("guild was not cached")
}
if len(container.ChannelIDs) != 1 {
t.Fatal("missing channel id")
}
if cid := container.ChannelIDs[0]; cid.IsZero() {
t.Fatal("channel id was stored as 0")
} else {
channel, ok := cache.Channels.Store[cid]
if !ok {
t.Fatal("channel was not saved to cache")
}
if channel.ID != channelID {
t.Error("incorrect channel id")
}
if channel.Name != channelName {
t.Error("incorrect channel name")
}
}
if len(container.Members) != 1 {
t.Error("incorrect number of members")
}
if member, ok := container.Members[memberID]; !ok {
t.Error("member was not stored")
} else {
if member == nil {
t.Fatal("member is nil")
}
if member.UserID != memberID {
t.Error("member is missing user id")
}
if member.Nick != nick {
t.Error("incorrect nickname")
}
}
if user, ok := cache.Users.Store[memberID]; !ok {
t.Error("user was not stored")
} else {
if user == nil {
t.Fatal("user is nil")
}
if user.ID != memberID {
t.Error("user is missing user id")
}
if user.Username != username {
t.Error("incorrect username")
}
}
})
t.Run("channel-create", func(t *testing.T) {
const guildID = 1
const channelID = 20
cache := NewBasicCache()
cache.Guilds.Store[guildID] = &guildCacheContainer{Guild: &Guild{ID: guildID}}
data := []byte(fmt.Sprintf(`{"id":%d,"guild_id":%d}`, channelID, guildID))
_, err := cacheDispatcher(cache, EvtChannelCreate, data)
if err != nil {
t.Fatal("failed to create event", err)
}
channelIDs := cache.Guilds.Store[guildID].ChannelIDs
if len(channelIDs) != 1 {
t.Fatal("channel was not linked to guild")
}
if channelIDs[0] != Snowflake(channelID) {
t.Error("wrong channel id")
}
})
t.Run("channel-update", func(t *testing.T) {
const guildID = 1
const channelID = 20
cache := NewBasicCache()
cache.Guilds.Store[guildID] = &guildCacheContainer{Guild: &Guild{ID: guildID}}
data := []byte(fmt.Sprintf(`{"id":%d,"guild_id":%d}`, channelID, guildID))
_, err := cacheDispatcher(cache, EvtChannelUpdate, data)
if err != nil {
t.Fatal("failed to create event", err)
}
channelIDs := cache.Guilds.Store[guildID].ChannelIDs
if len(channelIDs) != 1 {
t.Fatal("channel was not linked to guild")
}
if channelIDs[0] != Snowflake(channelID) {
t.Error("wrong channel id")
}
})
t.Run("channel-delete", func(t *testing.T) {
const guildID = 1
const channelID = 20
cache := NewBasicCache()
cache.Guilds.Store[guildID] = &guildCacheContainer{
Guild: &Guild{ID: guildID},
ChannelIDs: []Snowflake{channelID},
}
data := []byte(fmt.Sprintf(`{"id":%d,"guild_id":%d}`, channelID, guildID))
_, err := cacheDispatcher(cache, EvtChannelDelete, data)
if err != nil {
t.Fatal("failed to create event", err)
}
channelIDs := cache.Guilds.Store[guildID].ChannelIDs
if len(channelIDs) != 0 {
t.Error("there should be no channels")
}
})
}
func TestBasicCache_GuildMembers(t *testing.T) {
id := Snowflake(2523)
t.Run("members chunk", func(t *testing.T) {
cache := NewBasicCache()
cache.Guilds.Store[id] = &guildCacheContainer{
Guild: &Guild{ID: id},
Members: map[Snowflake]*Member{},
}
memberJson := func(id Snowflake, nick, username string) []byte {
return jsonbytes(`{"user":{"id":%d,"username":"%s"},"nick":"%s"}`, id, username, nick)
}