|
1 | 1 | package csapi_tests |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/tidwall/gjson" |
5 | 9 |
|
6 | 10 | "github.com/matrix-org/complement/internal/b" |
7 | 11 | "github.com/matrix-org/complement/internal/client" |
@@ -41,3 +45,108 @@ func TestPushRuleCacheHealth(t *testing.T) { |
41 | 45 | }, |
42 | 46 | }) |
43 | 47 | } |
| 48 | + |
| 49 | +func TestPushSync(t *testing.T) { |
| 50 | + deployment := Deploy(t, b.BlueprintAlice) |
| 51 | + defer deployment.Destroy(t) |
| 52 | + |
| 53 | + alice := deployment.Client(t, "hs1", "@alice:hs1") |
| 54 | + |
| 55 | + var syncResp gjson.Result |
| 56 | + var nextBatch string |
| 57 | + |
| 58 | + // sytest: Push rules come down in an initial /sync |
| 59 | + t.Run("Push rules come down in an initial /sync", func(t *testing.T) { |
| 60 | + syncResp, nextBatch = alice.MustSync(t, client.SyncReq{}) |
| 61 | + // get the first result where the type is m.push_rules |
| 62 | + pushrules := syncResp.Get(`account_data.events.#(type=="m.push_rules").content.global`) |
| 63 | + if !pushrules.Exists() { |
| 64 | + t.Fatalf("no pushrules found in sync response: %s", syncResp.Raw) |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + // sytest: Adding a push rule wakes up an incremental /sync |
| 69 | + t.Run("Adding a push rule wakes up an incremental /sync", func(t *testing.T) { |
| 70 | + nextBatch = checkWokenUp(t, alice, client.SyncReq{Since: nextBatch, TimeoutMillis: "10000"}, func() { |
| 71 | + body := client.WithJSONBody(t, map[string]interface{}{ |
| 72 | + "actions": []string{"notify"}, |
| 73 | + }) |
| 74 | + |
| 75 | + alice.MustDoFunc(t, "PUT", []string{"_matrix", "client", "v3", "pushrules", "global", "room", "!foo:example.com"}, body) |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + // sytest: Disabling a push rule wakes up an incremental /sync |
| 80 | + t.Run("Disabling a push rule wakes up an incremental /sync", func(t *testing.T) { |
| 81 | + nextBatch = checkWokenUp(t, alice, client.SyncReq{Since: nextBatch, TimeoutMillis: "10000"}, func() { |
| 82 | + body := client.WithJSONBody(t, map[string]interface{}{ |
| 83 | + "enabled": false, |
| 84 | + }) |
| 85 | + |
| 86 | + alice.MustDoFunc(t, "PUT", []string{"_matrix", "client", "v3", "pushrules", "global", "room", "!foo:example.com", "enabled"}, body) |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + // sytest: Enabling a push rule wakes up an incremental /sync |
| 91 | + t.Run("Enabling a push rule wakes up an incremental /sync", func(t *testing.T) { |
| 92 | + nextBatch = checkWokenUp(t, alice, client.SyncReq{Since: nextBatch, TimeoutMillis: "10000"}, func() { |
| 93 | + body := client.WithJSONBody(t, map[string]interface{}{ |
| 94 | + "enabled": true, |
| 95 | + }) |
| 96 | + |
| 97 | + alice.MustDoFunc(t, "PUT", []string{"_matrix", "client", "v3", "pushrules", "global", "room", "!foo:example.com", "enabled"}, body) |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + // sytest: Setting actions for a push rule wakes up an incremental /sync |
| 102 | + t.Run("Setting actions for a push rule wakes up an incremental /sync", func(t *testing.T) { |
| 103 | + checkWokenUp(t, alice, client.SyncReq{Since: nextBatch, TimeoutMillis: "10000"}, func() { |
| 104 | + body := client.WithJSONBody(t, map[string]interface{}{ |
| 105 | + "actions": []string{"dont_notify"}, |
| 106 | + }) |
| 107 | + |
| 108 | + alice.MustDoFunc(t, "PUT", []string{"_matrix", "client", "v3", "pushrules", "global", "room", "!foo:example.com", "actions"}, body) |
| 109 | + }) |
| 110 | + }) |
| 111 | +} |
| 112 | + |
| 113 | +func checkWokenUp(t *testing.T, csapi *client.CSAPI, syncReq client.SyncReq, fn func()) (nextBatch string) { |
| 114 | + t.Helper() |
| 115 | + errChan := make(chan error, 1) |
| 116 | + syncStarted := make(chan struct{}) |
| 117 | + go func() { |
| 118 | + var syncResp gjson.Result |
| 119 | + syncStarted <- struct{}{} |
| 120 | + syncResp, nextBatch = csapi.MustSync(t, syncReq) |
| 121 | + // get the first result where the type is m.push_rules |
| 122 | + pushrules := syncResp.Get(`account_data.events.#(type=="m.push_rules").content.global`) |
| 123 | + if !pushrules.Exists() { |
| 124 | + errChan <- fmt.Errorf("no pushrules found in sync response: %s", syncResp.Raw) |
| 125 | + return |
| 126 | + } |
| 127 | + errChan <- nil |
| 128 | + }() |
| 129 | + // Try to wait for the sync to actually start, so that we test wakeups |
| 130 | + select { |
| 131 | + case <-syncStarted: |
| 132 | + fn() |
| 133 | + case <-time.After(time.Second * 5): |
| 134 | + // even though this should mostly be impossible, make sure we have a timeout |
| 135 | + t.Fatalf("goroutine didn't start") |
| 136 | + } |
| 137 | + |
| 138 | + // Try to wait for the sync to return or timeout after 15 seconds, |
| 139 | + // as the above tests are using a timeout of 10 seconds |
| 140 | + select { |
| 141 | + case err := <-errChan: |
| 142 | + if err != nil { |
| 143 | + t.Fatal(err) |
| 144 | + } |
| 145 | + case <-time.After(time.Second * 15): |
| 146 | + t.Errorf("sync failed to return") |
| 147 | + } |
| 148 | + |
| 149 | + close(errChan) |
| 150 | + close(syncStarted) |
| 151 | + return nextBatch |
| 152 | +} |
0 commit comments