1- //go:build msc3030
2- // +build msc3030
1+ //go:build !dendrite_blacklist
2+ // +build !dendrite_blacklist
33
4- // This file contains tests for a jump to date API endpoint,
5- // currently experimental feature defined by MSC3030, which you can read here:
6- // https://github.com/matrix-org/matrix-doc/pull/3030
4+ // This file contains tests for the `/timestamp_to_event` client and federation API
5+ // endpoints (also known as *jump to date*). As defined by MSC3030, which you can read
6+ // here: https://github.com/matrix-org/matrix-doc/pull/3030
77
88package tests
99
@@ -118,7 +118,7 @@ func TestJumpToDateEndpoint(t *testing.T) {
118118 // Make the `/timestamp_to_event` request from Bob's perspective (non room member)
119119 timestamp := makeTimestampFromTime (timeBeforeRoomCreation )
120120 timestampString := strconv .FormatInt (timestamp , 10 )
121- timestampToEventRes := nonMemberUser .DoFunc (t , "GET" , []string {"_matrix" , "client" , "unstable" , "org.matrix.msc3030 " , "rooms" , roomID , "timestamp_to_event" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
121+ timestampToEventRes := nonMemberUser .DoFunc (t , "GET" , []string {"_matrix" , "client" , "v1 " , "rooms" , roomID , "timestamp_to_event" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
122122 "ts" : []string {timestampString },
123123 "dir" : []string {"f" },
124124 }))
@@ -146,7 +146,7 @@ func TestJumpToDateEndpoint(t *testing.T) {
146146 // Make the `/timestamp_to_event` request from Bob's perspective (non room member)
147147 timestamp := makeTimestampFromTime (timeBeforeRoomCreation )
148148 timestampString := strconv .FormatInt (timestamp , 10 )
149- timestampToEventRes := nonMemberUser .DoFunc (t , "GET" , []string {"_matrix" , "client" , "unstable" , "org.matrix.msc3030 " , "rooms" , roomID , "timestamp_to_event" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
149+ timestampToEventRes := nonMemberUser .DoFunc (t , "GET" , []string {"_matrix" , "client" , "v1 " , "rooms" , roomID , "timestamp_to_event" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
150150 "ts" : []string {timestampString },
151151 "dir" : []string {"f" },
152152 }))
@@ -236,6 +236,16 @@ type eventTime struct {
236236 AfterTimestamp time.Time
237237}
238238
239+ var txnCounter int = 0
240+
241+ func getTxnID (prefix string ) (txnID string ) {
242+ txnId := fmt .Sprintf ("%s-%d" , prefix , txnCounter )
243+
244+ txnCounter ++
245+
246+ return txnId
247+ }
248+
239249func createTestRoom (t * testing.T , c * client.CSAPI ) (roomID string , eventA , eventB * eventTime ) {
240250 t .Helper ()
241251
@@ -300,19 +310,20 @@ func mustCheckEventisReturnedForTime(t *testing.T, c *client.CSAPI, roomID strin
300310
301311 givenTimestamp := makeTimestampFromTime (givenTime )
302312 timestampString := strconv .FormatInt (givenTimestamp , 10 )
303- timestampToEventRes := c .DoFunc (t , "GET" , []string {"_matrix" , "client" , "unstable" , "org.matrix.msc3030 " , "rooms" , roomID , "timestamp_to_event" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
313+ timestampToEventRes := c .DoFunc (t , "GET" , []string {"_matrix" , "client" , "v1 " , "rooms" , roomID , "timestamp_to_event" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
304314 "ts" : []string {timestampString },
305315 "dir" : []string {direction },
306316 }))
307317 timestampToEventResBody := client .ParseJSON (t , timestampToEventRes )
308318
309- // Only allow a 200 response meaning we found an event or a 404 meaning we didn't.
310- // Other status codes will throw and assumed to be application errors.
319+ // Only allow a 200 response meaning we found an event or when no `expectedEventId` is provided, a
320+ // 404 meaning we didn't find anything. Other status codes will throw and are assumed to
321+ // be application errors.
311322 actualEventId := ""
312323 if timestampToEventRes .StatusCode == 200 {
313324 actualEventId = client .GetJSONFieldStr (t , timestampToEventResBody , "event_id" )
314- } else if timestampToEventRes .StatusCode != 404 {
315- t .Fatalf ("mustCheckEventisReturnedForTime: /timestamp_to_event request failed with status=%d" , timestampToEventRes .StatusCode )
325+ } else if timestampToEventRes .StatusCode != 404 || ( timestampToEventRes . StatusCode == 404 && expectedEventId != "" ) {
326+ t .Fatalf ("mustCheckEventisReturnedForTime: /timestamp_to_event request failed with status=%d body=%s " , timestampToEventRes .StatusCode , string ( timestampToEventResBody ) )
316327 }
317328
318329 if actualEventId != expectedEventId {
@@ -327,6 +338,42 @@ func mustCheckEventisReturnedForTime(t *testing.T, c *client.CSAPI, roomID strin
327338 }
328339}
329340
341+ func fetchUntilMessagesResponseHas (t * testing.T , c * client.CSAPI , roomID string , check func (gjson.Result ) bool ) {
342+ t .Helper ()
343+ start := time .Now ()
344+ checkCounter := 0
345+ for {
346+ if time .Since (start ) > c .SyncUntilTimeout {
347+ t .Fatalf ("fetchUntilMessagesResponseHas timed out. Called check function %d times" , checkCounter )
348+ }
349+
350+ messagesRes := c .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "v3" , "rooms" , roomID , "messages" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
351+ "dir" : []string {"b" },
352+ "limit" : []string {"100" },
353+ }))
354+ messsageResBody := client .ParseJSON (t , messagesRes )
355+ wantKey := "chunk"
356+ keyRes := gjson .GetBytes (messsageResBody , wantKey )
357+ if ! keyRes .Exists () {
358+ t .Fatalf ("missing key '%s'" , wantKey )
359+ }
360+ if ! keyRes .IsArray () {
361+ t .Fatalf ("key '%s' is not an array (was %s)" , wantKey , keyRes .Type )
362+ }
363+
364+ events := keyRes .Array ()
365+ for _ , ev := range events {
366+ if check (ev ) {
367+ return
368+ }
369+ }
370+
371+ checkCounter ++
372+ // Add a slight delay so we don't hammmer the messages endpoint
373+ time .Sleep (500 * time .Millisecond )
374+ }
375+ }
376+
330377func getDebugMessageListFromMessagesResponse (t * testing.T , c * client.CSAPI , roomID string , expectedEventId string , actualEventId string , givenTimestamp int64 ) string {
331378 t .Helper ()
332379
0 commit comments