11package csapi_tests
22
33import (
4+ "fmt"
45 "net/http"
6+ "net/url"
57 "testing"
68
79 "github.com/tidwall/gjson"
@@ -19,11 +21,7 @@ func TestRelations(t *testing.T) {
1921 defer deployment .Destroy (t )
2022
2123 alice := deployment .Client (t , "hs1" , "@alice:hs1" )
22-
2324 roomID := alice .CreateRoom (t , map [string ]interface {}{"preset" : "public_chat" })
24-
25- const testMessage = "TestSendAndFetchMessage"
26-
2725 _ , token := alice .MustSync (t , client.SyncReq {})
2826
2927 res := alice .MustDoFunc (t , "PUT" , []string {"_matrix" , "client" , "v3" , "rooms" , roomID , "send" , "m.room.message" , "txn-1" }, client .WithJSONBody (t , map [string ]interface {}{
@@ -112,3 +110,101 @@ func TestRelations(t *testing.T) {
112110 },
113111 })
114112}
113+
114+ func TestRelationsPagination (t * testing.T ) {
115+ runtime .SkipIf (t , runtime .Dendrite ) // not supported
116+ deployment := Deploy (t , b .BlueprintAlice )
117+ defer deployment .Destroy (t )
118+
119+ alice := deployment .Client (t , "hs1" , "@alice:hs1" )
120+ roomID := alice .CreateRoom (t , map [string ]interface {}{"preset" : "public_chat" })
121+ _ , token := alice .MustSync (t , client.SyncReq {})
122+
123+ res := alice .MustDoFunc (t , "PUT" , []string {"_matrix" , "client" , "v3" , "rooms" , roomID , "send" , "m.room.message" , "txn-1" }, client .WithJSONBody (t , map [string ]interface {}{
124+ "msgtype" : "m.text" ,
125+ "body" : "root" ,
126+ }))
127+ rootEventID := client .GetJSONFieldStr (t , client .ParseJSON (t , res ), "event_id" )
128+
129+ // Create some related events.
130+ event_ids := [10 ]string {}
131+ for i := 0 ; i < 10 ; i ++ {
132+ res = alice .MustDoFunc (t , "PUT" , []string {"_matrix" , "client" , "v3" , "rooms" , roomID , "send" , "m.room.message" , fmt .Sprintf ("txn-%d" , 2 + i )}, client .WithJSONBody (t , map [string ]interface {}{
133+ "msgtype" : "m.text" ,
134+ "body" : fmt .Sprintf ("reply %d" , i ),
135+ "m.relates_to" : map [string ]interface {}{
136+ "event_id" : rootEventID ,
137+ "rel_type" : "m.thread" ,
138+ },
139+ }))
140+ event_ids [i ] = client .GetJSONFieldStr (t , client .ParseJSON (t , res ), "event_id" )
141+ }
142+
143+ // sync until the server has processed it
144+ alice .MustSyncUntil (t , client.SyncReq {Since : token }, client .SyncTimelineHas (roomID , func (r gjson.Result ) bool {
145+ return r .Get ("event_id" ).Str == event_ids [9 ]
146+ }))
147+
148+ // Fetch the first page.
149+ queryParams := url.Values {}
150+ queryParams .Set ("limit" , "3" )
151+ res = alice .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "v1" , "rooms" , roomID , "relations" , rootEventID }, client .WithQueries (queryParams ))
152+ body := must .MatchResponse (t , res , match.HTTPResponse {
153+ StatusCode : http .StatusOK ,
154+ JSON : []match.JSON {
155+ match .JSONCheckOff ("chunk" , []interface {}{
156+ event_ids [9 ], event_ids [8 ], event_ids [7 ],
157+ }, func (r gjson.Result ) interface {} {
158+ return r .Get ("event_id" ).Str
159+ }, nil ,
160+ ),
161+ },
162+ })
163+
164+ // Fetch the next page.
165+ queryParams .Set ("from" , client .GetJSONFieldStr (t , body , "next_batch" ))
166+ res = alice .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "v1" , "rooms" , roomID , "relations" , rootEventID }, client .WithQueries (queryParams ))
167+ must .MatchResponse (t , res , match.HTTPResponse {
168+ StatusCode : http .StatusOK ,
169+ JSON : []match.JSON {
170+ match .JSONCheckOff ("chunk" , []interface {}{
171+ event_ids [6 ], event_ids [5 ], event_ids [4 ],
172+ }, func (r gjson.Result ) interface {} {
173+ return r .Get ("event_id" ).Str
174+ }, nil ,
175+ ),
176+ },
177+ })
178+
179+ // Fetch the first page in the forward direction.
180+ queryParams = url.Values {}
181+ queryParams .Set ("limit" , "3" )
182+ queryParams .Set ("dir" , "f" )
183+ res = alice .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "v1" , "rooms" , roomID , "relations" , rootEventID }, client .WithQueries (queryParams ))
184+ body = must .MatchResponse (t , res , match.HTTPResponse {
185+ StatusCode : http .StatusOK ,
186+ JSON : []match.JSON {
187+ match .JSONCheckOff ("chunk" , []interface {}{
188+ event_ids [0 ], event_ids [1 ], event_ids [2 ],
189+ }, func (r gjson.Result ) interface {} {
190+ return r .Get ("event_id" ).Str
191+ }, nil ,
192+ ),
193+ },
194+ })
195+
196+ // Fetch the next page in the forward direction.
197+ queryParams .Set ("from" , client .GetJSONFieldStr (t , body , "next_batch" ))
198+ res = alice .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "v1" , "rooms" , roomID , "relations" , rootEventID }, client .WithQueries (queryParams ))
199+ must .MatchResponse (t , res , match.HTTPResponse {
200+ StatusCode : http .StatusOK ,
201+ JSON : []match.JSON {
202+ match .JSONCheckOff ("chunk" , []interface {}{
203+ event_ids [3 ], event_ids [4 ], event_ids [5 ],
204+ }, func (r gjson.Result ) interface {} {
205+ return r .Get ("event_id" ).Str
206+ }, nil ,
207+ ),
208+ },
209+ })
210+ }
0 commit comments