Skip to content

Commit c86bf66

Browse files
Add and use SyncStateAfterHas(...) for TestDelayedEvents (#874)
Follow-up to #869 (comment)
1 parent db8fc0f commit c86bf66

2 files changed

Lines changed: 30 additions & 15 deletions

File tree

client/sync.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,31 @@ func SyncStateHas(roomID string, check func(gjson.Result) bool) SyncCheckOpt {
232232
}
233233
}
234234

235+
// Check that the `state_after` section for `roomID` has an event which passes the check function.
236+
//
237+
// Note that the `state_after` section of a sync response will not contain the entire
238+
// state of the room for incremental or `lazy_load_members` syncs.
239+
func SyncStateAfterHas(roomID string, check func(gjson.Result) bool) SyncCheckOpt {
240+
return func(clientUserID string, topLevelSyncJSON gjson.Result) error {
241+
// Check the stable field
242+
errStable := checkArrayElements(
243+
topLevelSyncJSON, "rooms.join."+GjsonEscape(roomID)+".state_after.events", check,
244+
)
245+
// Check the unstable field
246+
//
247+
// FIXME: Some implementations haven't stabilized yet (Synapse) so we'll keep this
248+
// here until then.
249+
errUnstable := checkArrayElements(
250+
topLevelSyncJSON, "rooms.join."+GjsonEscape(roomID)+"."+GjsonEscape("org.matrix.msc4222.state_after")+".events", check,
251+
)
252+
// Valid to find it in either place
253+
if errStable == nil || errUnstable == nil {
254+
return nil
255+
}
256+
return fmt.Errorf("SyncStateAfterHas(%s): Tried to check in the stable field: %s - and unstable field: %s", roomID, errStable, errUnstable)
257+
}
258+
}
259+
235260
func SyncEphemeralHas(roomID string, check func(gjson.Result) bool) SyncCheckOpt {
236261
return func(clientUserID string, topLevelSyncJSON gjson.Result) error {
237262
err := checkArrayElements(

tests/msc4140/delayed_event_test.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ const (
3030
DelayedEventActionSend = "send"
3131
)
3232

33-
// A filter for `/sync` that excludes timeline events.
34-
//
35-
// This is useful if you want to see `state` in the `/sync` response without the pesky
36-
// de-duplication with `timeline` that traditional `/sync` does.
37-
const NoTimelineSyncFilter = `{
38-
"room": {
39-
"timeline": { "limit": 0 }
40-
}
41-
}`
42-
4333
// TODO: Test pagination of `GET /_matrix/client/v1/delayed_events` once
4434
// it is implemented in a homeserver.
4535

@@ -195,7 +185,7 @@ func TestDelayedEvents(t *testing.T) {
195185

196186
// Check for the state change from the delayed state event (using `MustSyncUntil` to
197187
// account for any processing or worker replication delays)
198-
user.MustSyncUntil(t, client.SyncReq{Filter: NoTimelineSyncFilter}, client.SyncStateHas(roomID, func(ev gjson.Result) bool {
188+
user.MustSyncUntil(t, client.SyncReq{UseStateAfter: true}, client.SyncStateAfterHas(roomID, func(ev gjson.Result) bool {
199189
return ev.Get("type").Str == eventType && ev.Get("state_key").Str == stateKey
200190
}))
201191
// Make sure the state looks as expected after
@@ -347,7 +337,7 @@ func TestDelayedEvents(t *testing.T) {
347337

348338
// Check for the state change from the delayed state event (using `MustSyncUntil` to
349339
// account for any processing or worker replication delays)
350-
user.MustSyncUntil(t, client.SyncReq{Filter: NoTimelineSyncFilter}, client.SyncStateHas(roomID, func(ev gjson.Result) bool {
340+
user.MustSyncUntil(t, client.SyncReq{UseStateAfter: true}, client.SyncStateAfterHas(roomID, func(ev gjson.Result) bool {
351341
return ev.Get("type").Str == eventType && ev.Get("state_key").Str == stateKey
352342
}))
353343
// Make sure the state looks as expected after
@@ -417,7 +407,7 @@ func TestDelayedEvents(t *testing.T) {
417407

418408
// Check for the state change from the delayed state event (using `MustSyncUntil` to
419409
// account for any processing or worker replication delays)
420-
user.MustSyncUntil(t, client.SyncReq{Filter: NoTimelineSyncFilter}, client.SyncStateHas(roomID, func(ev gjson.Result) bool {
410+
user.MustSyncUntil(t, client.SyncReq{UseStateAfter: true}, client.SyncStateAfterHas(roomID, func(ev gjson.Result) bool {
421411
return ev.Get("type").Str == eventType && ev.Get("state_key").Str == stateKey
422412
}))
423413
// Make sure the state looks as expected after
@@ -512,7 +502,7 @@ func TestDelayedEvents(t *testing.T) {
512502
// Sanity check that the room state was updated correctly with the delayed events
513503
// that were sent. (using `MustSyncUntil` to account for any processing or worker
514504
// replication delays)
515-
user.MustSyncUntil(t, client.SyncReq{Filter: NoTimelineSyncFilter}, client.SyncStateHas(roomID, func(ev gjson.Result) bool {
505+
user.MustSyncUntil(t, client.SyncReq{UseStateAfter: true}, client.SyncStateAfterHas(roomID, func(ev gjson.Result) bool {
516506
return ev.Get("type").Str == eventType && ev.Get("state_key").Str == stateKey1
517507
}))
518508

@@ -528,7 +518,7 @@ func TestDelayedEvents(t *testing.T) {
528518
// FIXME: Ideally, we'd check specifically for the last one that was sent but it
529519
// will be a bit of a juggle and fiddly to get this right so for now we just check
530520
// one.
531-
user.MustSyncUntil(t, client.SyncReq{Filter: NoTimelineSyncFilter}, client.SyncStateHas(roomID, func(ev gjson.Result) bool {
521+
user.MustSyncUntil(t, client.SyncReq{UseStateAfter: true}, client.SyncStateAfterHas(roomID, func(ev gjson.Result) bool {
532522
return ev.Get("type").Str == eventType && ev.Get("state_key").Str == stateKey2
533523
}))
534524
})

0 commit comments

Comments
 (0)