|
| 1 | +// +build msc3030 |
| 2 | + |
| 3 | +// This file contains tests for a jump to date API endpoint, |
| 4 | +// currently experimental feature defined by MSC3030, which you can read here: |
| 5 | +// https://github.com/matrix-org/matrix-doc/pull/3030 |
| 6 | + |
| 7 | +package tests |
| 8 | + |
| 9 | +import ( |
| 10 | + "net/url" |
| 11 | + "strconv" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/matrix-org/complement/internal/b" |
| 16 | + "github.com/matrix-org/complement/internal/client" |
| 17 | + "github.com/sirupsen/logrus" |
| 18 | + "github.com/tidwall/gjson" |
| 19 | +) |
| 20 | + |
| 21 | +func TestJumpToDateEndpoint(t *testing.T) { |
| 22 | + deployment := Deploy(t, b.BlueprintFederationTwoLocalOneRemote) |
| 23 | + defer deployment.Destroy(t) |
| 24 | + |
| 25 | + // Create the normal user which will send messages in the room |
| 26 | + userID := "@alice:hs1" |
| 27 | + alice := deployment.Client(t, "hs1", userID) |
| 28 | + |
| 29 | + roomID := alice.CreateRoom(t, map[string]interface{}{}) |
| 30 | + alice.JoinRoom(t, roomID, nil) |
| 31 | + |
| 32 | + timeBeforeEventA := time.Now() |
| 33 | + eventAID := alice.SendEventSynced(t, roomID, b.Event{ |
| 34 | + Type: "m.room.message", |
| 35 | + Content: map[string]interface{}{ |
| 36 | + "msgtype": "m.text", |
| 37 | + "body": "Message A", |
| 38 | + }, |
| 39 | + }) |
| 40 | + eventBID := alice.SendEventSynced(t, roomID, b.Event{ |
| 41 | + Type: "m.room.message", |
| 42 | + Content: map[string]interface{}{ |
| 43 | + "msgtype": "m.text", |
| 44 | + "body": "Message A", |
| 45 | + }, |
| 46 | + }) |
| 47 | + timeAfterEventB := time.Now() |
| 48 | + |
| 49 | + logrus.WithFields(logrus.Fields{ |
| 50 | + "eventAID": eventAID, |
| 51 | + "eventBID": eventBID, |
| 52 | + }).Error("see messages") |
| 53 | + |
| 54 | + t.Run("parallel", func(t *testing.T) { |
| 55 | + t.Run("should find event after given timestmap", func(t *testing.T) { |
| 56 | + checkEventisReturnedForTime(t, alice, roomID, timeBeforeEventA, eventAID) |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("should find event before given timestmap", func(t *testing.T) { |
| 60 | + checkEventisReturnedForTime(t, alice, roomID, timeAfterEventB, eventBID) |
| 61 | + }) |
| 62 | + |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +func makeTimestampFromTime(t time.Time) int64 { |
| 67 | + return t.UnixNano() / int64(time.Millisecond) |
| 68 | +} |
| 69 | + |
| 70 | +func checkEventisReturnedForTime(t *testing.T, c *client.CSAPI, roomID string, givenTime time.Time, expectedEventId string) { |
| 71 | + t.Helper() |
| 72 | + |
| 73 | + timestampString := strconv.FormatInt(makeTimestampFromTime(givenTime), 10) |
| 74 | + timestampToEventRes := c.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "timestamp_to_event"}, client.WithContentType("application/json"), client.WithQueries(url.Values{ |
| 75 | + "ts": []string{timestampString}, |
| 76 | + })) |
| 77 | + timestampToEventResBody := client.ParseJSON(t, timestampToEventRes) |
| 78 | + |
| 79 | + actualEventIdRes := gjson.GetBytes(timestampToEventResBody, "event_id") |
| 80 | + actualEventId := actualEventIdRes.Str |
| 81 | + |
| 82 | + if actualEventId != expectedEventId { |
| 83 | + actualEvent := c.GetEvent(t, roomID, actualEventId) |
| 84 | + t.Fatalf("Expected to see %s but received %s\n%+v", expectedEventId, actualEventId, actualEvent) |
| 85 | + } |
| 86 | +} |
0 commit comments