Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add presence test from people in 2 different rooms in incremental sync #525

Merged
merged 9 commits into from
Oct 27, 2022
Merged
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{}{})
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
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{}{
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
"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