2021-02-26 07:57:45 +00:00
|
|
|
package defaultstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
2021-02-26 07:57:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func populate12Store() *Message {
|
|
|
|
store := NewMessage(10)
|
|
|
|
|
|
|
|
// Insert a regular list of messages.
|
2021-11-03 22:16:02 +00:00
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 29, ChannelID: 1}, false)
|
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 28, ChannelID: 1}, false)
|
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 27, ChannelID: 1}, false)
|
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 26, ChannelID: 1}, false)
|
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 25, ChannelID: 1}, false)
|
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 24, ChannelID: 1}, false)
|
2021-02-26 07:57:45 +00:00
|
|
|
|
|
|
|
// Try to insert newer messages after inserting new messages.
|
2021-11-03 22:16:02 +00:00
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 30, ChannelID: 1}, false)
|
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 31, ChannelID: 1}, false)
|
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 32, ChannelID: 1}, false)
|
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 33, ChannelID: 1}, false)
|
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 34, ChannelID: 1}, false)
|
2021-02-26 07:57:45 +00:00
|
|
|
|
2021-06-03 19:39:49 +00:00
|
|
|
// TThese messages should be discarded, due to age.
|
2021-11-03 22:16:02 +00:00
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 23, ChannelID: 1}, false)
|
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 22, ChannelID: 1}, false)
|
2021-06-03 19:39:49 +00:00
|
|
|
|
|
|
|
// These should be prepended.
|
2021-11-03 22:16:02 +00:00
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 35, ChannelID: 1}, false)
|
|
|
|
store.MessageSet(&discord.Message{ID: 1 << 36, ChannelID: 1}, false)
|
2021-02-26 07:57:45 +00:00
|
|
|
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessageSet(t *testing.T) {
|
|
|
|
store := populate12Store()
|
|
|
|
|
|
|
|
messages, _ := store.Messages(1)
|
2021-06-03 19:39:49 +00:00
|
|
|
if len(messages) < store.MaxMessages() {
|
|
|
|
t.Errorf("store can store %d messages, but only returned %d", store.MaxMessages(),
|
|
|
|
len(messages))
|
|
|
|
}
|
2021-02-26 07:57:45 +00:00
|
|
|
|
2021-06-03 19:39:49 +00:00
|
|
|
maxShift := 36
|
2021-02-26 07:57:45 +00:00
|
|
|
|
2021-06-03 19:39:49 +00:00
|
|
|
for i, actual := range messages {
|
|
|
|
expectID := discord.MessageID(1) << (maxShift - i)
|
|
|
|
if actual.ID != expectID {
|
|
|
|
t.Errorf("message at %d has mismatch ID %d, expecting %d", i, actual.ID, expectID)
|
2021-02-26 07:57:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessagesUpdate(t *testing.T) {
|
|
|
|
store := populate12Store()
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
store.MessageSet(&discord.Message{ID: 5, ChannelID: 1, Content: "edited 1"}, true)
|
|
|
|
store.MessageSet(&discord.Message{ID: 6, ChannelID: 1, Content: "edited 2"}, true)
|
|
|
|
store.MessageSet(&discord.Message{ID: 5, ChannelID: 1, Content: "edited 3"}, true)
|
2021-02-26 07:57:45 +00:00
|
|
|
|
|
|
|
expect := map[discord.MessageID]string{
|
|
|
|
5: "edited 3",
|
|
|
|
6: "edited 2",
|
|
|
|
}
|
|
|
|
|
|
|
|
messages, _ := store.Messages(1)
|
|
|
|
|
|
|
|
for i := 0; i < store.MaxMessages(); i++ {
|
|
|
|
msg := messages[i]
|
|
|
|
content, ok := expect[msg.ID]
|
|
|
|
if ok && msg.Content != content {
|
|
|
|
t.Errorf("id %d expected %q, got %q", i, content, msg.Content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|