-
Notifications
You must be signed in to change notification settings - Fork 59
MSC3030 Jump to date API endpoint #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1889d0a
Add tests to ensure MSC3030 functionality
MadLittleMods 55385ec
Add color coding debug string when the test fails
MadLittleMods 2a360a0
Remove unused GetEvent and GetJSONFieldStringMap functions
MadLittleMods 352b8e9
Merge branch 'master' into madlittlemods/msc3030-jump-to-date
MadLittleMods 5990efe
Fix timestamp insertion in debug string
MadLittleMods f8b88a9
Show debug message list in scrollback order how you would see it in a…
MadLittleMods 42af51a
Update to use direction paremeter
MadLittleMods 5233383
Add tests for finding nothing before and after the event timeline
MadLittleMods 4348803
Add some comments
MadLittleMods 83f993c
Make tests parallel and add federation tests
MadLittleMods 7d3a691
Add experimental feature flag and use unstable endpoint
MadLittleMods 97a287f
Add potential history visibility test
MadLittleMods d96622b
Remove superfluous history visibility test
MadLittleMods e8787c1
Add actual/expected text labels so it's usable with ascii colors
MadLittleMods f71f88f
Remove commented out code
MadLittleMods 94957f4
Add test to make sure we're not leaking events from private rooms
MadLittleMods 39cb82c
Refactor language to want/got
MadLittleMods 2486df7
Merge remote-tracking branch 'origin/main' into madlittlemods/msc3030…
MadLittleMods 5678986
Merge branch 'main' into madlittlemods/msc3030-jump-to-date
MadLittleMods 148387e
Always set preset since Synapse/Dendrite disagree on what the default is
MadLittleMods ccb51a9
Fix message A vs B typo
MadLittleMods 3b914c2
Merge branch 'main' into madlittlemods/msc3030-jump-to-date
MadLittleMods a383a08
Also add test to make sure we don't leak from public room either
MadLittleMods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// +build msc3030 | ||
|
||
// This file contains tests for a jump to date API endpoint, | ||
// currently experimental feature defined by MSC3030, which you can read here: | ||
// https://github.com/matrix-org/matrix-doc/pull/3030 | ||
|
||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/matrix-org/complement/internal/b" | ||
"github.com/matrix-org/complement/internal/client" | ||
"github.com/tidwall/gjson" | ||
) | ||
|
||
func TestJumpToDateEndpoint(t *testing.T) { | ||
deployment := Deploy(t, b.BlueprintFederationTwoLocalOneRemote) | ||
defer deployment.Destroy(t) | ||
|
||
// Create the normal user which will send messages in the room | ||
userID := "@alice:hs1" | ||
alice := deployment.Client(t, "hs1", userID) | ||
|
||
roomID := alice.CreateRoom(t, map[string]interface{}{}) | ||
alice.JoinRoom(t, roomID, nil) | ||
|
||
timeBeforeEventA := time.Now() | ||
eventAID := alice.SendEventSynced(t, roomID, b.Event{ | ||
Type: "m.room.message", | ||
Content: map[string]interface{}{ | ||
"msgtype": "m.text", | ||
"body": "Message A", | ||
}, | ||
}) | ||
eventBID := alice.SendEventSynced(t, roomID, b.Event{ | ||
Type: "m.room.message", | ||
Content: map[string]interface{}{ | ||
"msgtype": "m.text", | ||
"body": "Message A", | ||
}, | ||
}) | ||
timeAfterEventB := time.Now() | ||
|
||
t.Run("parallel", func(t *testing.T) { | ||
t.Run("should find event after given timestmap", func(t *testing.T) { | ||
checkEventisReturnedForTime(t, alice, roomID, timeBeforeEventA, eventAID) | ||
}) | ||
|
||
t.Run("should find event before given timestmap", func(t *testing.T) { | ||
checkEventisReturnedForTime(t, alice, roomID, timeAfterEventB, eventBID) | ||
}) | ||
}) | ||
} | ||
|
||
func checkEventisReturnedForTime(t *testing.T, c *client.CSAPI, roomID string, givenTime time.Time, expectedEventId string) { | ||
t.Helper() | ||
|
||
givenTimestamp := makeTimestampFromTime(givenTime) | ||
timestampString := strconv.FormatInt(givenTimestamp, 10) | ||
timestampToEventRes := c.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "timestamp_to_event"}, client.WithContentType("application/json"), client.WithQueries(url.Values{ | ||
"ts": []string{timestampString}, | ||
})) | ||
timestampToEventResBody := client.ParseJSON(t, timestampToEventRes) | ||
|
||
actualEventIdRes := gjson.GetBytes(timestampToEventResBody, "event_id") | ||
actualEventId := actualEventIdRes.String() | ||
|
||
if actualEventId != expectedEventId { | ||
debugMessageList := getDebugMessageListFromMessagesResponse(t, c, roomID, expectedEventId, actualEventId, givenTimestamp) | ||
t.Fatalf( | ||
"Expected to see %s given %s but received %s\n%s", | ||
decorateStringWithAnsiColor(expectedEventId, AnsiColorGreen), | ||
decorateStringWithAnsiColor(timestampString, AnsiColorYellow), | ||
decorateStringWithAnsiColor(actualEventId, AnsiColorRed), | ||
debugMessageList, | ||
) | ||
kegsay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
func getDebugMessageListFromMessagesResponse(t *testing.T, c *client.CSAPI, roomID string, expectedEventId string, actualEventId string, givenTimestamp int64) string { | ||
t.Helper() | ||
|
||
messagesRes := c.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{ | ||
"dir": []string{"b"}, | ||
"limit": []string{"100"}, | ||
})) | ||
messsageResBody := client.ParseJSON(t, messagesRes) | ||
|
||
wantKey := "chunk" | ||
res := gjson.GetBytes(messsageResBody, wantKey) | ||
if !res.Exists() { | ||
t.Fatalf("missing key '%s'", wantKey) | ||
} | ||
if !res.IsArray() { | ||
t.Fatalf("key '%s' is not an array (was %s)", wantKey, res.Type) | ||
} | ||
|
||
givenTimestampMarker := decorateStringWithAnsiColor(fmt.Sprintf("-- givenTimestamp=%s --\n", strconv.FormatInt(givenTimestamp, 10)), AnsiColorYellow) | ||
|
||
resultantString := "" | ||
givenTimestampAlreadyInserted := false | ||
res.ForEach(func(key, r gjson.Result) bool { | ||
// The timestmap could be after-in-time of any of the events | ||
if r.Get("origin_server_ts").Int() < givenTimestamp && !givenTimestampAlreadyInserted { | ||
resultantString += givenTimestampMarker | ||
givenTimestampAlreadyInserted = true | ||
} | ||
|
||
event_id := r.Get("event_id").String() | ||
event_id_string := event_id | ||
if event_id == expectedEventId { | ||
event_id_string = decorateStringWithAnsiColor(event_id, AnsiColorGreen) | ||
} else if event_id == actualEventId { | ||
event_id_string = decorateStringWithAnsiColor(event_id, AnsiColorRed) | ||
} | ||
|
||
resultantString += fmt.Sprintf("%s (%s) - %s\n", event_id_string, strconv.FormatInt(r.Get("origin_server_ts").Int(), 10), r.Get("type").String()) | ||
|
||
// The timestmap could be before-in-time of any of the events | ||
if r.Get("origin_server_ts").Int() > givenTimestamp && !givenTimestampAlreadyInserted { | ||
resultantString += givenTimestampMarker | ||
givenTimestampAlreadyInserted = true | ||
} | ||
|
||
// keep iterating | ||
return true | ||
}) | ||
|
||
return resultantString | ||
} | ||
|
||
func makeTimestampFromTime(t time.Time) int64 { | ||
return t.UnixNano() / int64(time.Millisecond) | ||
} | ||
|
||
const AnsiColorRed string = "31" | ||
const AnsiColorGreen string = "32" | ||
const AnsiColorYellow string = "33" | ||
|
||
func decorateStringWithAnsiColor(inputString, decorationColor string) string { | ||
return fmt.Sprintf("\033[%sm%s\033[0m", decorationColor, inputString) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.