Skip to content

Commit

Permalink
Add presence test from people in 2 different rooms in incremental sync (
Browse files Browse the repository at this point in the history
#525)

Co-authored-by: Jonathan de Jong <[email protected]>
Co-authored-by: David Robertson <[email protected]>
  • Loading branch information
3 people authored Oct 27, 2022
1 parent b0c66b2 commit bd831c9
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tests/csapi/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,68 @@ func TestSync(t *testing.T) {
})
}

// Test presence from people in 2 different rooms in incremental sync
func TestPresenceSyncDifferentRooms(t *testing.T) {
deployment := Deploy(t, b.BlueprintOneToOneRoom)
defer deployment.Destroy(t)

alice := deployment.Client(t, "hs1", "@alice:hs1")
bob := deployment.Client(t, "hs1", "@bob:hs1")

deployment.RegisterUser(t, "hs1", "charlie", "charliepassword", false)
charlie := deployment.Client(t, "hs1", "@charlie:hs1")

// Alice creates two rooms: one with her and Bob, and a second with her and Charlie.
bobRoomID := alice.CreateRoom(t, struct{}{})
charlieRoomID := alice.CreateRoom(t, struct{}{})
nextBatch := alice.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(alice.UserID, bobRoomID), client.SyncJoinedTo(alice.UserID, charlieRoomID))

alice.InviteRoom(t, bobRoomID, bob.UserID)
alice.InviteRoom(t, charlieRoomID, charlie.UserID)
bob.JoinRoom(t, bobRoomID, nil)
charlie.JoinRoom(t, charlieRoomID, nil)

nextBatch = alice.MustSyncUntil(t,
client.SyncReq{Since: nextBatch},
client.SyncJoinedTo(bob.UserID, bobRoomID),
client.SyncJoinedTo(charlie.UserID, charlieRoomID),
)

// Bob and Charlie mark themselves as online.
reqBody := client.WithJSONBody(t, map[string]interface{}{
"presence": "online",
})
bob.DoFunc(t, "PUT", []string{"_matrix", "client", "v3", "presence", "@bob:hs1", "status"}, reqBody)
charlie.DoFunc(t, "PUT", []string{"_matrix", "client", "v3", "presence", "@charlie:hs1", "status"}, reqBody)

// Alice should see that Bob and Charlie are online. She may see this happen
// simultaneously in one /sync response, or separately in two /sync
// responses.
seenBobOnline, seenCharlieOnline := false, false

alice.MustSyncUntil(t, client.SyncReq{Since: nextBatch}, func(clientUserID string, sync gjson.Result) error {
presenceArray := sync.Get("presence").Get("events").Array()
if len(presenceArray) == 0 {
return fmt.Errorf("presence.events is empty")
}
for _, x := range presenceArray {
if x.Get("content").Get("presence").Str != "online" {
continue
}
if x.Get("sender").Str == bob.UserID {
seenBobOnline = true
}
if x.Get("sender").Str == charlie.UserID {
seenCharlieOnline = true
}
if seenBobOnline && seenCharlieOnline {
return nil
}
}
return fmt.Errorf("all users not present yet, bob %t charlie %t", seenBobOnline, seenCharlieOnline)
})
}

func sendMessages(t *testing.T, client *client.CSAPI, roomID string, prefix string, count int) {
t.Helper()
for i := 0; i < count; i++ {
Expand Down

0 comments on commit bd831c9

Please sign in to comment.