mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-12-03 20:32:55 +00:00
efde3f4ea6
This commit refactors a lot of packages. It refactors the handler package, removing the Synchronous field and replacing it the AddSyncHandler API, which allows each handler to control whether or not it should be ran synchronously independent of other handlers. This is useful for libraries that need to guarantee the incoming order of events. It also refactors the store interfaces to accept more interfaces. This is to make the API more consistent as well as reducing potential useless copies. The public-facing state API should still be the same, so this change will mostly concern users with their own store implementations. Several miscellaneous functions (such as a few in package gateway) were modified to be more suitable to other packages, but those functions should rarely ever be used, anyway. Several tests are also fixed within this commit, namely fixing state's intents bug.
79 lines
2.5 KiB
Go
79 lines
2.5 KiB
Go
package defaultstore
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
)
|
|
|
|
func populate12Store() *Message {
|
|
store := NewMessage(10)
|
|
|
|
// Insert a regular list of messages.
|
|
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)
|
|
|
|
// Try to insert newer messages after inserting new messages.
|
|
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)
|
|
|
|
// TThese messages should be discarded, due to age.
|
|
store.MessageSet(&discord.Message{ID: 1 << 23, ChannelID: 1}, false)
|
|
store.MessageSet(&discord.Message{ID: 1 << 22, ChannelID: 1}, false)
|
|
|
|
// These should be prepended.
|
|
store.MessageSet(&discord.Message{ID: 1 << 35, ChannelID: 1}, false)
|
|
store.MessageSet(&discord.Message{ID: 1 << 36, ChannelID: 1}, false)
|
|
|
|
return store
|
|
}
|
|
|
|
func TestMessageSet(t *testing.T) {
|
|
store := populate12Store()
|
|
|
|
messages, _ := store.Messages(1)
|
|
if len(messages) < store.MaxMessages() {
|
|
t.Errorf("store can store %d messages, but only returned %d", store.MaxMessages(),
|
|
len(messages))
|
|
}
|
|
|
|
maxShift := 36
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMessagesUpdate(t *testing.T) {
|
|
store := populate12Store()
|
|
|
|
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)
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|