Skip to content

Commit

Permalink
Add basic tests for /relations.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Sep 20, 2022
1 parent 50b86f6 commit 2914a96
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions tests/csapi/room_relations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package csapi_tests

import (
"net/http"
"testing"

"github.com/tidwall/gjson"

"github.com/matrix-org/complement/internal/b"
"github.com/matrix-org/complement/internal/client"
"github.com/matrix-org/complement/internal/match"
"github.com/matrix-org/complement/internal/must"
"github.com/matrix-org/complement/runtime"
)

func TestRelations(t *testing.T) {
runtime.SkipIf(t, runtime.Dendrite) // not supported
deployment := Deploy(t, b.BlueprintAlice)
defer deployment.Destroy(t)

alice := deployment.Client(t, "hs1", "@alice:hs1")

roomID := alice.CreateRoom(t, map[string]interface{}{"preset": "public_chat"})

const testMessage = "TestSendAndFetchMessage"

_, token := alice.MustSync(t, client.SyncReq{})

res := alice.MustDoFunc(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.room.message", "txn-1"}, client.WithJSONBody(t, map[string]interface{}{
"msgtype": "m.text",
"body": "root",
}))
rootEventID := client.GetJSONFieldStr(t, client.ParseJSON(t, res), "event_id")

// Send a few related messages.
res = alice.MustDoFunc(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.room.message", "txn-2"}, client.WithJSONBody(t, map[string]interface{}{
"msgtype": "m.text",
"body": "reply",
"m.relates_to": map[string]interface{}{
"event_id": rootEventID,
"rel_type": "m.thread",
},
}))
threadEventID := client.GetJSONFieldStr(t, client.ParseJSON(t, res), "event_id")

res = alice.MustDoFunc(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.dummy", "txn-3"}, client.WithJSONBody(t, map[string]interface{}{
"m.relates_to": map[string]interface{}{
"event_id": rootEventID,
"rel_type": "m.thread",
},
}))
dummyEventID := client.GetJSONFieldStr(t, client.ParseJSON(t, res), "event_id")

res = alice.MustDoFunc(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.room.message", "txn-4"}, client.WithJSONBody(t, map[string]interface{}{
"msgtype": "m.text",
"body": "* edited root",
"m.new_content": map[string]interface{}{
"msgtype": "m.text",
"body": "edited root",
},
"m.relates_to": map[string]interface{}{
"event_id": rootEventID,
"rel_type": "m.replace",
},
}))
editEventID := client.GetJSONFieldStr(t, client.ParseJSON(t, res), "event_id")

// sync until the server has processed it
alice.MustSyncUntil(t, client.SyncReq{Since: token}, client.SyncTimelineHas(roomID, func(r gjson.Result) bool {
return r.Get("event_id").Str == editEventID
}))

// Request the relations.
res = alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "v1", "rooms", roomID, "relations", rootEventID})
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: http.StatusOK,
JSON: []match.JSON{
match.JSONCheckOff("chunk", []interface{}{
threadEventID, dummyEventID, editEventID,
}, func(r gjson.Result) interface{} {
return r.Get("event_id").Str
}, nil,
),
},
})

res = alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "v1", "rooms", roomID, "relations", rootEventID, "m.thread"})
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: http.StatusOK,
JSON: []match.JSON{
match.JSONCheckOff("chunk", []interface{}{
threadEventID, dummyEventID,
}, func(r gjson.Result) interface{} {
return r.Get("event_id").Str
}, nil,
),
},
})

res = alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "v1", "rooms", roomID, "relations", rootEventID, "m.thread", "m.room.message"})
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: http.StatusOK,
JSON: []match.JSON{
match.JSONCheckOff("chunk", []interface{}{
threadEventID,
}, func(r gjson.Result) interface{} {
return r.Get("event_id").Str
}, nil,
),
},
})
}

0 comments on commit 2914a96

Please sign in to comment.