sliding-sync/state/typing_table_test.go

53 lines
1.3 KiB
Go
Raw Normal View History

2021-06-10 16:51:56 +01:00
package state
import (
"reflect"
"testing"
)
func TestTypingTable(t *testing.T) {
db, close := connectToDB(t)
defer close()
2021-06-10 16:51:56 +01:00
userIDs := []string{
"@alice:localhost",
"@bob:localhost",
}
roomID := "!foo:localhost"
table := NewTypingTable(db)
lastStreamID := int64(-1)
2021-06-10 16:51:56 +01:00
setAndCheck := func() {
2021-06-10 17:08:06 +01:00
streamID, err := table.SetTyping(roomID, userIDs)
2021-06-10 16:51:56 +01:00
if err != nil {
t.Fatalf("failed to SetTyping: %s", err)
}
2021-06-10 17:08:06 +01:00
if streamID == 0 {
t.Errorf("SetTyping: streamID was not returned")
}
if lastStreamID >= streamID {
t.Errorf("SetTyping: streamID returned should always be increasing but it wasn't, got %d, last %d", streamID, lastStreamID)
}
lastStreamID = streamID
gotUserIDs, _, err := table.Typing(roomID, streamID-1, lastStreamID)
2021-06-10 16:51:56 +01:00
if err != nil {
t.Fatalf("failed to Typing: %s", err)
}
if !reflect.DeepEqual(gotUserIDs, userIDs) {
t.Errorf("got typing users %v want %v", gotUserIDs, userIDs)
}
}
setAndCheck()
userIDs = userIDs[1:]
userIDs = append(userIDs, "@charlie:localhost")
setAndCheck()
userIDs = []string{}
setAndCheck()
2021-07-23 14:06:18 +01:00
highest, err := table.SelectHighestID()
if err != nil {
t.Fatalf("SelectHighestID: %s", err)
}
if highest != lastStreamID {
t.Fatalf("SelectHighestID: got %d want %d", highest, lastStreamID)
}
2021-06-10 16:51:56 +01:00
}