2023-08-11 14:49:58 +02:00
|
|
|
package syncv3_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-08-15 09:13:24 +02:00
|
|
|
"time"
|
2023-08-11 14:49:58 +02:00
|
|
|
|
2023-10-11 12:23:46 +01:00
|
|
|
"github.com/matrix-org/complement/b"
|
2023-08-11 14:49:58 +02:00
|
|
|
"github.com/matrix-org/sliding-sync/sync3"
|
|
|
|
"github.com/matrix-org/sliding-sync/testutils/m"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTimestamp(t *testing.T) {
|
|
|
|
alice := registerNewUser(t)
|
|
|
|
bob := registerNewUser(t)
|
2023-08-15 09:13:24 +02:00
|
|
|
charlie := registerNewUser(t)
|
2023-08-11 14:49:58 +02:00
|
|
|
|
2023-10-11 12:23:46 +01:00
|
|
|
roomID := alice.MustCreateRoom(t, map[string]interface{}{
|
2023-08-11 14:49:58 +02:00
|
|
|
"preset": "public_chat",
|
|
|
|
})
|
|
|
|
|
2023-08-15 09:13:24 +02:00
|
|
|
var gotTs, expectedTs uint64
|
|
|
|
|
|
|
|
lists := map[string]sync3.RequestList{
|
|
|
|
"myFirstList": {
|
|
|
|
Ranges: [][2]int64{{0, 1}},
|
|
|
|
RoomSubscription: sync3.RoomSubscription{
|
|
|
|
TimelineLimit: 10,
|
|
|
|
},
|
|
|
|
BumpEventTypes: []string{"m.room.message"}, // only messages bump the timestamp
|
|
|
|
},
|
|
|
|
"mySecondList": {
|
|
|
|
Ranges: [][2]int64{{0, 1}},
|
|
|
|
RoomSubscription: sync3.RoomSubscription{
|
|
|
|
TimelineLimit: 10,
|
|
|
|
},
|
|
|
|
BumpEventTypes: []string{"m.reaction"}, // only reactions bump the timestamp
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-08-11 14:49:58 +02:00
|
|
|
// Init sync to get the latest timestamp
|
2023-08-15 09:13:24 +02:00
|
|
|
resAlice := alice.SlidingSync(t, sync3.Request{
|
2023-08-11 14:49:58 +02:00
|
|
|
RoomSubscriptions: map[string]sync3.RoomSubscription{
|
|
|
|
roomID: {
|
|
|
|
TimelineLimit: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2023-08-15 09:13:24 +02:00
|
|
|
m.MatchResponse(t, resAlice, m.MatchRoomSubscription(roomID, m.MatchRoomInitial(true)))
|
|
|
|
timestampBeforeBobJoined := resAlice.Rooms[roomID].Timestamp
|
2023-08-11 14:49:58 +02:00
|
|
|
|
|
|
|
bob.JoinRoom(t, roomID, nil)
|
2023-08-15 09:13:24 +02:00
|
|
|
resAlice = alice.SlidingSyncUntilMembership(t, resAlice.Pos, roomID, bob, "join")
|
2023-08-11 14:49:58 +02:00
|
|
|
resBob := bob.SlidingSync(t, sync3.Request{
|
2023-08-15 09:13:24 +02:00
|
|
|
Lists: lists,
|
2023-08-11 14:49:58 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// Bob should see a different timestamp than alice, as he just joined
|
2023-08-15 09:13:24 +02:00
|
|
|
gotTs = resBob.Rooms[roomID].Timestamp
|
|
|
|
expectedTs = resAlice.Rooms[roomID].Timestamp
|
|
|
|
if gotTs != expectedTs {
|
|
|
|
t.Fatalf("expected timestamp to be equal, but got: %v vs %v", gotTs, expectedTs)
|
|
|
|
}
|
|
|
|
// ... the timestamp should still differ from what Alice received before the join
|
|
|
|
if gotTs == timestampBeforeBobJoined {
|
|
|
|
t.Fatalf("expected timestamp to differ, but got: %v vs %v", gotTs, timestampBeforeBobJoined)
|
2023-08-11 14:49:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send an event which should NOT bump Bobs timestamp, because it is not listed it
|
|
|
|
// any BumpEventTypes
|
|
|
|
emptyStateKey := ""
|
2023-10-11 12:23:46 +01:00
|
|
|
eventID := alice.SendEventSynced(t, roomID, b.Event{
|
2023-08-11 14:49:58 +02:00
|
|
|
Type: "m.room.topic",
|
|
|
|
StateKey: &emptyStateKey,
|
|
|
|
Content: map[string]interface{}{
|
|
|
|
"topic": "random topic",
|
|
|
|
},
|
|
|
|
})
|
2023-08-15 09:13:24 +02:00
|
|
|
time.Sleep(time.Millisecond)
|
2023-08-11 14:49:58 +02:00
|
|
|
|
|
|
|
resBob = bob.SlidingSyncUntilEventID(t, resBob.Pos, roomID, eventID)
|
2023-08-15 09:13:24 +02:00
|
|
|
gotTs = resBob.Rooms[roomID].Timestamp
|
|
|
|
expectedTs = resAlice.Rooms[roomID].Timestamp
|
|
|
|
if gotTs != expectedTs {
|
|
|
|
t.Fatalf("expected timestamps to be the same, but they aren't: %v vs %v", gotTs, expectedTs)
|
2023-08-11 14:49:58 +02:00
|
|
|
}
|
2023-08-15 09:13:24 +02:00
|
|
|
expectedTs = gotTs
|
2023-08-11 14:49:58 +02:00
|
|
|
|
|
|
|
// Now send a message which bumps the timestamp in myFirstList
|
2023-10-11 12:23:46 +01:00
|
|
|
eventID = alice.SendEventSynced(t, roomID, b.Event{
|
2023-08-11 14:49:58 +02:00
|
|
|
Type: "m.room.message",
|
|
|
|
Content: map[string]interface{}{
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "Hello, world!",
|
|
|
|
},
|
|
|
|
})
|
2023-08-15 09:13:24 +02:00
|
|
|
time.Sleep(time.Millisecond)
|
2023-08-11 14:49:58 +02:00
|
|
|
|
|
|
|
resBob = bob.SlidingSyncUntilEventID(t, resBob.Pos, roomID, eventID)
|
2023-08-15 09:13:24 +02:00
|
|
|
gotTs = resBob.Rooms[roomID].Timestamp
|
|
|
|
if expectedTs == gotTs {
|
|
|
|
t.Fatalf("expected timestamps to be different, but they aren't: %v vs %v", gotTs, expectedTs)
|
2023-08-11 14:49:58 +02:00
|
|
|
}
|
2023-08-15 09:13:24 +02:00
|
|
|
expectedTs = gotTs
|
2023-08-11 14:49:58 +02:00
|
|
|
|
|
|
|
// Now send a message which bumps the timestamp in mySecondList
|
2023-10-11 12:23:46 +01:00
|
|
|
eventID = alice.SendEventSynced(t, roomID, b.Event{
|
2023-08-11 14:49:58 +02:00
|
|
|
Type: "m.reaction",
|
|
|
|
Content: map[string]interface{}{
|
|
|
|
"m.relates.to": map[string]interface{}{
|
|
|
|
"event_id": eventID,
|
|
|
|
"key": "✅",
|
|
|
|
"rel_type": "m.annotation",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2023-08-15 09:13:24 +02:00
|
|
|
time.Sleep(time.Millisecond)
|
2023-08-11 14:49:58 +02:00
|
|
|
|
|
|
|
resBob = bob.SlidingSyncUntilEventID(t, resBob.Pos, roomID, eventID)
|
2023-08-15 09:13:24 +02:00
|
|
|
bobTimestampReaction := resBob.Rooms[roomID].Timestamp
|
|
|
|
if bobTimestampReaction == expectedTs {
|
|
|
|
t.Fatalf("expected timestamps to be different, but they aren't: %v vs %v", expectedTs, bobTimestampReaction)
|
2023-08-11 14:49:58 +02:00
|
|
|
}
|
2023-08-15 09:13:24 +02:00
|
|
|
expectedTs = bobTimestampReaction
|
2023-08-11 14:49:58 +02:00
|
|
|
|
|
|
|
// Send another event which should NOT bump Bobs timestamp
|
2023-10-11 12:23:46 +01:00
|
|
|
eventID = alice.SendEventSynced(t, roomID, b.Event{
|
2023-08-11 14:49:58 +02:00
|
|
|
Type: "m.room.name",
|
|
|
|
StateKey: &emptyStateKey,
|
|
|
|
Content: map[string]interface{}{
|
|
|
|
"name": "random name",
|
|
|
|
},
|
|
|
|
})
|
2023-08-15 09:13:24 +02:00
|
|
|
time.Sleep(time.Millisecond)
|
2023-08-11 14:49:58 +02:00
|
|
|
|
|
|
|
resBob = bob.SlidingSyncUntilEventID(t, resBob.Pos, roomID, eventID)
|
2023-08-15 09:13:24 +02:00
|
|
|
gotTs = resBob.Rooms[roomID].Timestamp
|
|
|
|
if gotTs != expectedTs {
|
|
|
|
t.Fatalf("expected timestamps to be the same, but they aren't: %v, expected %v", gotTs, expectedTs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bob makes an initial sync again, he should still see the m.reaction timestamp
|
|
|
|
resBob = bob.SlidingSync(t, sync3.Request{
|
|
|
|
Lists: lists,
|
|
|
|
})
|
|
|
|
|
|
|
|
gotTs = resBob.Rooms[roomID].Timestamp
|
|
|
|
expectedTs = bobTimestampReaction
|
|
|
|
if gotTs != expectedTs {
|
|
|
|
t.Fatalf("initial sync contains wrong timestamp: %d, expected %d", gotTs, expectedTs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Charlie joins the room
|
|
|
|
charlie.JoinRoom(t, roomID, nil)
|
|
|
|
resAlice = alice.SlidingSyncUntilMembership(t, resAlice.Pos, roomID, charlie, "join")
|
|
|
|
|
|
|
|
resCharlie := charlie.SlidingSync(t, sync3.Request{
|
|
|
|
Lists: lists,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Charlie just joined so should see the same timestamp as Alice, even if
|
|
|
|
// Charlie has the same bumpEvents as Bob, we don't leak those timestamps.
|
|
|
|
gotTs = resCharlie.Rooms[roomID].Timestamp
|
|
|
|
expectedTs = resAlice.Rooms[roomID].Timestamp
|
|
|
|
if gotTs != expectedTs {
|
|
|
|
t.Fatalf("Charlie should see the timestamp they joined, but didn't: %d, expected %d", gotTs, expectedTs)
|
2023-08-11 14:49:58 +02:00
|
|
|
}
|
2023-08-15 14:19:42 +02:00
|
|
|
|
|
|
|
// Initial sync without bump types should see the most recent timestamp
|
|
|
|
resAlice = alice.SlidingSync(t, sync3.Request{
|
|
|
|
RoomSubscriptions: map[string]sync3.RoomSubscription{
|
|
|
|
roomID: {
|
|
|
|
TimelineLimit: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// expected TS stays the same, so the join of Charlie
|
|
|
|
gotTs = resAlice.Rooms[roomID].Timestamp
|
|
|
|
if gotTs != expectedTs {
|
|
|
|
t.Fatalf("Alice should see the timestamp of Charlie joining, but didn't: %d, expected %d", gotTs, expectedTs)
|
|
|
|
}
|
2023-08-11 14:49:58 +02:00
|
|
|
}
|