|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/matrix-org/complement" |
| 10 | + "github.com/matrix-org/complement/b" |
| 11 | + "github.com/matrix-org/complement/client" |
| 12 | + "github.com/matrix-org/complement/federation" |
| 13 | + "github.com/matrix-org/complement/helpers" |
| 14 | + "github.com/matrix-org/complement/match" |
| 15 | + "github.com/matrix-org/complement/must" |
| 16 | + "github.com/matrix-org/complement/runtime" |
| 17 | + "github.com/tidwall/gjson" |
| 18 | +) |
| 19 | + |
| 20 | +// Tests https://github.com/element-hq/synapse/issues/16928 |
| 21 | +// To set up: |
| 22 | +// - Alice and Bob join the same room, sends E1. |
| 23 | +// - Alice sends event E3. |
| 24 | +// - Bob forks the graph at E1 and sends S2. |
| 25 | +// - Alice sends event E4 on the same fork as E3. |
| 26 | +// - Alice sends event E5 merging the forks. |
| 27 | +// - Alice sync with timeline_limit=1 and a filter that skips E5 |
| 28 | +func TestSyncOmitsStateChangeOnFilteredEvents(t *testing.T) { |
| 29 | + runtime.SkipIf(t, runtime.Dendrite) // S2 is put in the timeline, not state. |
| 30 | + deployment := complement.Deploy(t, 1) |
| 31 | + defer deployment.Destroy(t) |
| 32 | + srv := federation.NewServer(t, deployment, |
| 33 | + federation.HandleKeyRequests(), |
| 34 | + federation.HandleMakeSendJoinRequests(), |
| 35 | + federation.HandleTransactionRequests(nil, nil), |
| 36 | + ) |
| 37 | + srv.UnexpectedRequestsAreErrors = false |
| 38 | + cancel := srv.Listen() |
| 39 | + defer cancel() |
| 40 | + |
| 41 | + alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{}) |
| 42 | + bob := srv.UserID("bob") |
| 43 | + ver := alice.GetDefaultRoomVersion(t) |
| 44 | + serverRoom := srv.MustMakeRoom(t, ver, federation.InitialRoomEvents(ver, bob)) |
| 45 | + |
| 46 | + // Join Alice to the new room on the federation server and send E1. |
| 47 | + alice.MustJoinRoom(t, serverRoom.RoomID, []string{srv.ServerName()}) |
| 48 | + e1 := alice.SendEventSynced(t, serverRoom.RoomID, b.Event{ |
| 49 | + Type: "m.room.message", |
| 50 | + Content: map[string]interface{}{ |
| 51 | + "msgtype": "m.text", |
| 52 | + "body": "E1", |
| 53 | + }, |
| 54 | + }) |
| 55 | + |
| 56 | + // wait until bob's server sees e1 |
| 57 | + serverRoom.WaiterForEvent(e1).Waitf(t, 5*time.Second, "failed to see e1 (%s) on complement server", e1) |
| 58 | + |
| 59 | + // create S2 but don't send it yet, prev_events will be set to [e1] |
| 60 | + roomName := "I am the room name, S2" |
| 61 | + s2 := srv.MustCreateEvent(t, serverRoom, federation.Event{ |
| 62 | + Type: "m.room.name", |
| 63 | + StateKey: b.Ptr(""), |
| 64 | + Sender: bob, |
| 65 | + Content: map[string]interface{}{ |
| 66 | + "name": roomName, |
| 67 | + }, |
| 68 | + }) |
| 69 | + |
| 70 | + // Alice sends E3 & E4 |
| 71 | + alice.SendEventSynced(t, serverRoom.RoomID, b.Event{ |
| 72 | + Type: "m.room.message", |
| 73 | + Content: map[string]interface{}{ |
| 74 | + "msgtype": "m.text", |
| 75 | + "body": "E3", |
| 76 | + }, |
| 77 | + }) |
| 78 | + alice.SendEventSynced(t, serverRoom.RoomID, b.Event{ |
| 79 | + Type: "m.room.message", |
| 80 | + Content: map[string]interface{}{ |
| 81 | + "msgtype": "m.text", |
| 82 | + "body": "E4", |
| 83 | + }, |
| 84 | + }) |
| 85 | + |
| 86 | + // fork the dag earlier at e1 and send s2 |
| 87 | + srv.MustSendTransaction(t, deployment, "hs1", []json.RawMessage{s2.JSON()}, nil) |
| 88 | + |
| 89 | + // wait until we see S2 to ensure the server has processed this. |
| 90 | + alice.MustSyncUntil(t, client.SyncReq{}, client.SyncTimelineHasEventID(serverRoom.RoomID, s2.EventID())) |
| 91 | + |
| 92 | + // now send E5, merging the forks |
| 93 | + alice.SendEventSynced(t, serverRoom.RoomID, b.Event{ |
| 94 | + Type: "please_filter_me", |
| 95 | + Content: map[string]interface{}{ |
| 96 | + "msgtype": "m.text", |
| 97 | + "body": "E5", |
| 98 | + }, |
| 99 | + }) |
| 100 | + |
| 101 | + // now do a sync request which filters events of type 'please_filter_me', and ensure we still see the |
| 102 | + // correct room name. Note we don't need to SyncUntil here as we have all the data in the right places |
| 103 | + // already. |
| 104 | + res, _ := alice.MustSync(t, client.SyncReq{ |
| 105 | + Filter: `{ |
| 106 | + "room": { |
| 107 | + "timeline": { |
| 108 | + "not_types": ["please_filter_me"], |
| 109 | + "limit": 1 |
| 110 | + } |
| 111 | + } |
| 112 | + }`, |
| 113 | + }) |
| 114 | + must.MatchGJSON(t, res, match.JSONCheckOff( |
| 115 | + // look in this array |
| 116 | + fmt.Sprintf("rooms.join.%s.state.events", client.GjsonEscape(serverRoom.RoomID)), |
| 117 | + // for these items |
| 118 | + []interface{}{s2.EventID()}, |
| 119 | + // and map them first into this format |
| 120 | + match.CheckOffMapper(func(r gjson.Result) interface{} { |
| 121 | + return r.Get("event_id").Str |
| 122 | + }), match.CheckOffAllowUnwanted(), |
| 123 | + )) |
| 124 | +} |
0 commit comments