|
| 1 | +package csapi_tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/tidwall/gjson" |
| 8 | + |
| 9 | + "github.com/matrix-org/complement/internal/b" |
| 10 | + "github.com/matrix-org/complement/internal/client" |
| 11 | + "github.com/matrix-org/complement/internal/match" |
| 12 | + "github.com/matrix-org/complement/internal/must" |
| 13 | + "github.com/matrix-org/complement/runtime" |
| 14 | +) |
| 15 | + |
| 16 | +func TestRelations(t *testing.T) { |
| 17 | + runtime.SkipIf(t, runtime.Dendrite) // not supported |
| 18 | + deployment := Deploy(t, b.BlueprintAlice) |
| 19 | + defer deployment.Destroy(t) |
| 20 | + |
| 21 | + alice := deployment.Client(t, "hs1", "@alice:hs1") |
| 22 | + |
| 23 | + roomID := alice.CreateRoom(t, map[string]interface{}{"preset": "public_chat"}) |
| 24 | + |
| 25 | + const testMessage = "TestSendAndFetchMessage" |
| 26 | + |
| 27 | + _, token := alice.MustSync(t, client.SyncReq{}) |
| 28 | + |
| 29 | + res := alice.MustDoFunc(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.room.message", "txn-1"}, client.WithJSONBody(t, map[string]interface{}{ |
| 30 | + "msgtype": "m.text", |
| 31 | + "body": "root", |
| 32 | + })) |
| 33 | + rootEventID := client.GetJSONFieldStr(t, client.ParseJSON(t, res), "event_id") |
| 34 | + |
| 35 | + // Send a few related messages. |
| 36 | + res = alice.MustDoFunc(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.room.message", "txn-2"}, client.WithJSONBody(t, map[string]interface{}{ |
| 37 | + "msgtype": "m.text", |
| 38 | + "body": "reply", |
| 39 | + "m.relates_to": map[string]interface{}{ |
| 40 | + "event_id": rootEventID, |
| 41 | + "rel_type": "m.thread", |
| 42 | + }, |
| 43 | + })) |
| 44 | + threadEventID := client.GetJSONFieldStr(t, client.ParseJSON(t, res), "event_id") |
| 45 | + |
| 46 | + res = alice.MustDoFunc(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.dummy", "txn-3"}, client.WithJSONBody(t, map[string]interface{}{ |
| 47 | + "m.relates_to": map[string]interface{}{ |
| 48 | + "event_id": rootEventID, |
| 49 | + "rel_type": "m.thread", |
| 50 | + }, |
| 51 | + })) |
| 52 | + dummyEventID := client.GetJSONFieldStr(t, client.ParseJSON(t, res), "event_id") |
| 53 | + |
| 54 | + res = alice.MustDoFunc(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.room.message", "txn-4"}, client.WithJSONBody(t, map[string]interface{}{ |
| 55 | + "msgtype": "m.text", |
| 56 | + "body": "* edited root", |
| 57 | + "m.new_content": map[string]interface{}{ |
| 58 | + "msgtype": "m.text", |
| 59 | + "body": "edited root", |
| 60 | + }, |
| 61 | + "m.relates_to": map[string]interface{}{ |
| 62 | + "event_id": rootEventID, |
| 63 | + "rel_type": "m.replace", |
| 64 | + }, |
| 65 | + })) |
| 66 | + editEventID := client.GetJSONFieldStr(t, client.ParseJSON(t, res), "event_id") |
| 67 | + |
| 68 | + // sync until the server has processed it |
| 69 | + alice.MustSyncUntil(t, client.SyncReq{Since: token}, client.SyncTimelineHas(roomID, func(r gjson.Result) bool { |
| 70 | + return r.Get("event_id").Str == editEventID |
| 71 | + })) |
| 72 | + |
| 73 | + // Request the relations. |
| 74 | + res = alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "v1", "rooms", roomID, "relations", rootEventID}) |
| 75 | + must.MatchResponse(t, res, match.HTTPResponse{ |
| 76 | + StatusCode: http.StatusOK, |
| 77 | + JSON: []match.JSON{ |
| 78 | + match.JSONCheckOff("chunk", []interface{}{ |
| 79 | + threadEventID, dummyEventID, editEventID, |
| 80 | + }, func(r gjson.Result) interface{} { |
| 81 | + return r.Get("event_id").Str |
| 82 | + }, nil, |
| 83 | + ), |
| 84 | + }, |
| 85 | + }) |
| 86 | + |
| 87 | + // Also test filtering by the relation type. |
| 88 | + res = alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "v1", "rooms", roomID, "relations", rootEventID, "m.thread"}) |
| 89 | + must.MatchResponse(t, res, match.HTTPResponse{ |
| 90 | + StatusCode: http.StatusOK, |
| 91 | + JSON: []match.JSON{ |
| 92 | + match.JSONCheckOff("chunk", []interface{}{ |
| 93 | + threadEventID, dummyEventID, |
| 94 | + }, func(r gjson.Result) interface{} { |
| 95 | + return r.Get("event_id").Str |
| 96 | + }, nil, |
| 97 | + ), |
| 98 | + }, |
| 99 | + }) |
| 100 | + |
| 101 | + // And test filtering by relation type + event type. |
| 102 | + res = alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "v1", "rooms", roomID, "relations", rootEventID, "m.thread", "m.room.message"}) |
| 103 | + must.MatchResponse(t, res, match.HTTPResponse{ |
| 104 | + StatusCode: http.StatusOK, |
| 105 | + JSON: []match.JSON{ |
| 106 | + match.JSONCheckOff("chunk", []interface{}{ |
| 107 | + threadEventID, |
| 108 | + }, func(r gjson.Result) interface{} { |
| 109 | + return r.Get("event_id").Str |
| 110 | + }, nil, |
| 111 | + ), |
| 112 | + }, |
| 113 | + }) |
| 114 | +} |
0 commit comments