2021-10-08 10:59:56 +01:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUnreadTable(t *testing.T) {
|
2023-01-18 14:54:26 +00:00
|
|
|
db, close := connectToDB(t)
|
|
|
|
defer close()
|
2021-10-08 10:59:56 +01:00
|
|
|
table := NewUnreadTable(db)
|
|
|
|
userID := "@alice:localhost"
|
|
|
|
roomA := "!TestUnreadTableA:localhost"
|
|
|
|
roomB := "!TestUnreadTableB:localhost"
|
|
|
|
roomC := "!TestUnreadTableC:localhost"
|
|
|
|
|
|
|
|
two := 2
|
2021-10-08 13:10:14 +01:00
|
|
|
one := 1
|
2021-10-08 10:59:56 +01:00
|
|
|
zero := 0
|
|
|
|
|
|
|
|
// try all kinds of insertions
|
2021-10-08 13:10:14 +01:00
|
|
|
assertNoError(t, table.UpdateUnreadCounters(userID, roomA, &two, &one)) // both
|
2021-10-08 10:59:56 +01:00
|
|
|
assertNoError(t, table.UpdateUnreadCounters(userID, roomB, &two, nil)) // one
|
|
|
|
assertNoError(t, table.UpdateUnreadCounters(userID, roomC, nil, &two)) // one
|
2021-10-08 13:10:14 +01:00
|
|
|
assertUnread(t, table, userID, roomA, 2, 1)
|
2021-10-08 10:59:56 +01:00
|
|
|
assertUnread(t, table, userID, roomB, 2, 0)
|
|
|
|
assertUnread(t, table, userID, roomC, 0, 2)
|
|
|
|
|
|
|
|
// try all kinds of updates
|
2021-10-08 13:31:30 +01:00
|
|
|
assertNoError(t, table.UpdateUnreadCounters(userID, roomA, &zero, nil)) // one
|
|
|
|
assertNoError(t, table.UpdateUnreadCounters(userID, roomB, nil, &two)) // one
|
|
|
|
assertNoError(t, table.UpdateUnreadCounters(userID, roomC, &zero, &zero)) // both
|
2021-10-08 13:10:14 +01:00
|
|
|
assertUnread(t, table, userID, roomA, 0, 1)
|
2021-10-08 10:59:56 +01:00
|
|
|
assertUnread(t, table, userID, roomB, 2, 2)
|
2021-10-08 13:31:30 +01:00
|
|
|
assertUnread(t, table, userID, roomC, 0, 0)
|
|
|
|
|
|
|
|
wantHighlights := map[string]int{
|
|
|
|
roomB: 2,
|
|
|
|
}
|
|
|
|
wantNotifs := map[string]int{
|
|
|
|
roomA: 1,
|
|
|
|
roomB: 2,
|
|
|
|
}
|
2021-10-11 16:22:41 +01:00
|
|
|
assertNoError(t, table.SelectAllNonZeroCountsForUser(userID, func(gotRoomID string, gotHighlight int, gotNotif int) {
|
2021-10-08 13:31:30 +01:00
|
|
|
wantHighlight := wantHighlights[gotRoomID]
|
|
|
|
if wantHighlight != gotHighlight {
|
2021-10-11 16:22:41 +01:00
|
|
|
t.Errorf("SelectAllNonZeroCountsForUser for %v got %d highlights, want %d", gotRoomID, gotHighlight, wantHighlight)
|
2021-10-08 13:31:30 +01:00
|
|
|
}
|
|
|
|
wantNotif := wantNotifs[gotRoomID]
|
|
|
|
if wantNotif != gotNotif {
|
2021-10-11 16:22:41 +01:00
|
|
|
t.Errorf("SelectAllNonZeroCountsForUser for %v got %d notifs, want %d", gotRoomID, gotNotif, wantNotif)
|
2021-10-08 13:31:30 +01:00
|
|
|
}
|
|
|
|
delete(wantHighlights, gotRoomID)
|
|
|
|
delete(wantNotifs, gotRoomID)
|
|
|
|
}))
|
|
|
|
if len(wantHighlights) != 0 {
|
2021-10-11 16:22:41 +01:00
|
|
|
t.Errorf("SelectAllNonZeroCountsForUser missed highlight rooms: %+v", wantHighlights)
|
2021-10-08 13:31:30 +01:00
|
|
|
}
|
|
|
|
if len(wantNotifs) != 0 {
|
2021-10-11 16:22:41 +01:00
|
|
|
t.Errorf("SelectAllNonZeroCountsForUser missed notif rooms: %+v", wantNotifs)
|
2021-10-08 13:31:30 +01:00
|
|
|
}
|
2021-10-08 10:59:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func assertUnread(t *testing.T, table *UnreadTable, userID, roomID string, wantHighight, wantNotif int) {
|
|
|
|
t.Helper()
|
|
|
|
gotHighlight, gotNotif, err := table.SelectUnreadCounters(userID, roomID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("SelectUnreadCounters %s %s: %s", userID, roomID, err)
|
|
|
|
}
|
|
|
|
if gotHighlight != wantHighight {
|
|
|
|
t.Errorf("SelectUnreadCounters: got %d highlights, want %d", gotHighlight, wantHighight)
|
|
|
|
}
|
|
|
|
if gotNotif != wantNotif {
|
|
|
|
t.Errorf("SelectUnreadCounters: got %d notifs, want %d", gotNotif, wantNotif)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertNoError(t *testing.T, err error) {
|
|
|
|
t.Helper()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got error: %s", err)
|
|
|
|
}
|
|
|
|
}
|