-
Notifications
You must be signed in to change notification settings - Fork 69
Add MSC4222 /sync state_after test for initial sync lazy-loading room members
#842
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
Merged
MadLittleMods
merged 9 commits into
main
from
madlittlemods/msc4222-sync-state-after-initial-sync-lazy
Mar 3, 2026
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aa81ebc
Add MSC4222 test for initial sync lazy-loading room members
MadLittleMods 158656a
Explain test further
MadLittleMods 8e85ad2
Change to private room and invite
dbkr c373332
Have test for both public and private
MadLittleMods b3c058e
Better errors when it fails
MadLittleMods 7a77aa7
Deduplicate assertion logic (`testInitialSyncStateAfterIncludesTimeli…
MadLittleMods c4149a9
Remove stray new line
MadLittleMods 71c6925
Explain more detail
MadLittleMods 195fcd8
Explain sync to wait
MadLittleMods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package tests | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/matrix-org/complement" | ||
| ) | ||
|
|
||
| func TestMain(m *testing.M) { | ||
| complement.TestMain(m, "msc4222") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| package tests | ||
|
|
||
| import ( | ||
| "maps" | ||
| "slices" | ||
| "testing" | ||
|
|
||
| "github.com/matrix-org/complement" | ||
| "github.com/matrix-org/complement/client" | ||
| "github.com/matrix-org/complement/helpers" | ||
| "github.com/matrix-org/complement/must" | ||
| "github.com/tidwall/gjson" | ||
| ) | ||
|
|
||
| func TestSync(t *testing.T) { | ||
| deployment := complement.Deploy(t, 1) | ||
| defer deployment.Destroy(t) | ||
| alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{LocalpartSuffix: "alice"}) | ||
| bob := deployment.Register(t, "hs1", helpers.RegistrationOpts{LocalpartSuffix: "bob"}) | ||
|
|
||
| t.Run("parallel", func(t *testing.T) { | ||
| // When lazy-loading room members is enabled, for a public room, the `state_after` | ||
| // in an initial sync request should include membership from every `sender` in the | ||
| // `timeline` | ||
| // | ||
| // We're specifically testing the scenario where a new "DM" is created and the other person | ||
| // joins without speaking yet. | ||
| t.Run("Initial sync with lazy-loading room members -> public room `state_after` includes all members from timeline", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Alice creates a room | ||
| roomID := alice.MustCreateRoom(t, map[string]interface{}{"preset": "public_chat"}) | ||
| alice.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(alice.UserID, roomID)) | ||
|
|
||
| // Bob joins the room | ||
| bob.MustJoinRoom(t, roomID, nil) | ||
|
|
||
| // Make double sure that bob is joined to the room | ||
|
MadLittleMods marked this conversation as resolved.
Outdated
|
||
| alice.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(bob.UserID, roomID)) | ||
|
|
||
| // Now, Alice makes an initial sync request with lazy-loading members enabled | ||
| // | ||
| // The spec says `lazy_load_members` is valid field for both `timeline` and | ||
| // `state` but as far as I can tell, only makes sense for `state` and that's | ||
| // what Synapse keys off of. | ||
| aliceSyncFilter := `{ | ||
| "room": { | ||
| "timeline": { "limit": 20 }, | ||
| "state": { "lazy_load_members": true } | ||
| } | ||
| }` | ||
| res, _ := alice.MustSync(t, client.SyncReq{UseStateAfter: true, Filter: aliceSyncFilter}) | ||
| joinedRoomRes := res.Get("rooms.join." + client.GjsonEscape(roomID)) | ||
| if !joinedRoomRes.Exists() { | ||
| t.Fatalf("Unable to find roomID=%s in the join part of the sync response: %s", roomID, res) | ||
| } | ||
|
|
||
| // Collect the senders of all the time timeline events. | ||
| roomTimelineRes := joinedRoomRes.Get("timeline.events"); | ||
| if !roomTimelineRes.IsArray() { | ||
| t.Fatalf("Timeline events is not an array (found %s) %s", roomTimelineRes.Type.String(), res) | ||
| } | ||
| sendersFromTimeline := make(map[string]struct{}, 0) | ||
| for _, event := range roomTimelineRes.Array() { | ||
| sendersFromTimeline[event.Get("sender").Str] = struct{}{} | ||
| } | ||
| // We expect to see timeline events from alice and bob | ||
| must.ContainSubset(t, | ||
| slices.Collect(maps.Keys(sendersFromTimeline)), | ||
| []string{ alice.UserID, bob.UserID }, | ||
| ) | ||
|
|
||
| // Collect the `m.room.membership` from `state_after` | ||
| // | ||
| // Try looking up the stable variant `state_after` first, then fallback to the | ||
| // unstable version | ||
| roomStateAfterResStable := joinedRoomRes.Get("state_after.events"); | ||
| roomStateAfterResUnstable := joinedRoomRes.Get("org\\.matrix\\.msc4222\\.state_after.events"); | ||
| var roomStateAfterRes gjson.Result | ||
| if roomStateAfterResStable.Exists() { | ||
| roomStateAfterRes = roomStateAfterResStable | ||
| } else if roomStateAfterResUnstable.Exists() { | ||
| roomStateAfterRes = roomStateAfterResUnstable | ||
| } | ||
| // Sanity check syntax | ||
| if !roomStateAfterRes.IsArray() { | ||
| t.Fatalf("state_after events is not an array (found %s) %s", roomStateAfterRes.Type.String(), res) | ||
| } | ||
| membershipFromState := make(map[string]struct{}, 0) | ||
| for _, event := range roomStateAfterRes.Array() { | ||
| if event.Get("type").Str == "m.room.member" { | ||
| membershipFromState[event.Get("sender").Str] = struct{}{} | ||
| } | ||
| } | ||
| // We should see membership state from every `sender` in the `timeline` (alice | ||
| // and bob). | ||
| must.ContainSubset(t, | ||
| slices.Collect(maps.Keys(membershipFromState)), | ||
| slices.Collect(maps.Keys(sendersFromTimeline)), | ||
| ) | ||
|
|
||
| }) | ||
|
|
||
| // When lazy-loading room members is enabled, for a private room, the `state_after` | ||
| // in an initial sync request should include membership from every `sender` in the | ||
| // `timeline` | ||
| // | ||
| // We're specifically testing the scenario where a new "DM" is created and the other person | ||
| // joins without speaking yet. | ||
| t.Run("Initial sync with lazy-loading room members -> private room `state_after` includes all members from timeline", func(t *testing.T) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I've updated things so we have both tests (for public and private) |
||
| t.Parallel() | ||
|
|
||
| // Alice creates a room | ||
| roomID := alice.MustCreateRoom(t, map[string]interface{}{"preset": "private_chat"}) | ||
| alice.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(alice.UserID, roomID)) | ||
|
|
||
| // Alice invites Bob | ||
| alice.MustInviteRoom(t, roomID, bob.UserID) | ||
|
|
||
| // Bob must get the invite | ||
|
MadLittleMods marked this conversation as resolved.
Outdated
|
||
| bob.MustSyncUntil(t, client.SyncReq{}, client.SyncInvitedTo(bob.UserID, roomID)) | ||
|
|
||
| // Bob joins the room | ||
| bob.MustJoinRoom(t, roomID, nil) | ||
|
|
||
| // Make double sure that bob is joined to the room | ||
|
MadLittleMods marked this conversation as resolved.
Outdated
|
||
| alice.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(bob.UserID, roomID)) | ||
|
|
||
| // Now, Alice makes an initial sync request with lazy-loading members enabled | ||
| // | ||
| // The spec says `lazy_load_members` is valid field for both `timeline` and | ||
| // `state` but as far as I can tell, only makes sense for `state` and that's | ||
| // what Synapse keys off of. | ||
| aliceSyncFilter := `{ | ||
| "room": { | ||
| "timeline": { "limit": 20 }, | ||
| "state": { "lazy_load_members": true } | ||
| } | ||
| }` | ||
| res, _ := alice.MustSync(t, client.SyncReq{UseStateAfter: true, Filter: aliceSyncFilter}) | ||
| joinedRoomRes := res.Get("rooms.join." + client.GjsonEscape(roomID)) | ||
| if !joinedRoomRes.Exists() { | ||
| t.Fatalf("Unable to find roomID=%s in the join part of the sync response: %s", roomID, res) | ||
| } | ||
|
|
||
| // Collect the senders of all the time timeline events. | ||
| roomTimelineRes := joinedRoomRes.Get("timeline.events"); | ||
| if !roomTimelineRes.IsArray() { | ||
| t.Fatalf("Timeline events is not an array (found %s) %s", roomTimelineRes.Type.String(), res) | ||
| } | ||
| sendersFromTimeline := make(map[string]struct{}, 0) | ||
| for _, event := range roomTimelineRes.Array() { | ||
| sendersFromTimeline[event.Get("sender").Str] = struct{}{} | ||
| } | ||
| // We expect to see timeline events from alice and bob | ||
| must.ContainSubset(t, | ||
| slices.Collect(maps.Keys(sendersFromTimeline)), | ||
| []string{ alice.UserID, bob.UserID }, | ||
| ) | ||
|
|
||
| // Collect the `m.room.membership` from `state_after` | ||
| // | ||
| // Try looking up the stable variant `state_after` first, then fallback to the | ||
| // unstable version | ||
| roomStateAfterResStable := joinedRoomRes.Get("state_after.events"); | ||
| roomStateAfterResUnstable := joinedRoomRes.Get("org\\.matrix\\.msc4222\\.state_after.events"); | ||
| var roomStateAfterRes gjson.Result | ||
| if roomStateAfterResStable.Exists() { | ||
| roomStateAfterRes = roomStateAfterResStable | ||
| } else if roomStateAfterResUnstable.Exists() { | ||
| roomStateAfterRes = roomStateAfterResUnstable | ||
| } | ||
|
MadLittleMods marked this conversation as resolved.
Outdated
|
||
| // Sanity check syntax | ||
| if !roomStateAfterRes.IsArray() { | ||
| t.Fatalf("state_after events is not an array (found %s) %s", roomStateAfterRes.Type.String(), res) | ||
| } | ||
| membershipFromState := make(map[string]struct{}, 0) | ||
| for _, event := range roomStateAfterRes.Array() { | ||
| if event.Get("type").Str == "m.room.member" { | ||
| membershipFromState[event.Get("sender").Str] = struct{}{} | ||
| } | ||
| } | ||
| // We should see membership state from every `sender` in the `timeline` (alice | ||
| // and bob). | ||
| must.ContainSubset(t, | ||
| slices.Collect(maps.Keys(membershipFromState)), | ||
| slices.Collect(maps.Keys(sendersFromTimeline)), | ||
| ) | ||
|
|
||
| }) | ||
| }) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See other discussion #842 (comment)