2021-06-10 16:51:56 +01:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTypingTable(t *testing.T) {
|
2023-01-18 14:54:26 +00:00
|
|
|
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)
|
2021-07-23 11:07:03 +01:00
|
|
|
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
|
2021-08-03 17:43:41 +01:00
|
|
|
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
|
|
|
}
|