From bd831c96925ea56151b40c427c5dd72ff2558bea Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Thu, 27 Oct 2022 17:57:31 +0200 Subject: [PATCH] Add presence test from people in 2 different rooms in incremental sync (#525) Co-authored-by: Jonathan de Jong Co-authored-by: David Robertson --- tests/csapi/sync_test.go | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tests/csapi/sync_test.go b/tests/csapi/sync_test.go index ab28a3f1..672d9b47 100644 --- a/tests/csapi/sync_test.go +++ b/tests/csapi/sync_test.go @@ -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++ {