From a98d9e40704c28412005d30be242871772ba1d12 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 27 Sep 2022 12:09:03 +0100 Subject: [PATCH 1/2] Allow search results to appear in multiple orders Fixes https://github.com/matrix-org/complement/issues/478, I hope, though there may be other tests with the same problem. --- tests/csapi/apidoc_search_test.go | 32 +++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/tests/csapi/apidoc_search_test.go b/tests/csapi/apidoc_search_test.go index 7c7c6da8..968b4819 100644 --- a/tests/csapi/apidoc_search_test.go +++ b/tests/csapi/apidoc_search_test.go @@ -242,20 +242,32 @@ func TestSearch(t *testing.T) { resp := alice.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "search"}, searchRequest) sce := "search_categories.room_events" - result0 := sce + ".results.0.result" - result1 := sce + ".results.1.result" + + expectedEvents := map[string]string{ + eventBeforeUpgrade: "Message before upgrade", + eventAfterUpgrade: "Message after upgrade", + } must.MatchResponse(t, resp, match.HTTPResponse{ StatusCode: http.StatusOK, JSON: []match.JSON{ - match.JSONKeyPresent(sce + ".count"), - match.JSONKeyPresent(sce + ".results"), match.JSONKeyEqual(sce+".count", float64(2)), - match.JSONKeyPresent(result0 + ".content"), - match.JSONKeyPresent(result0 + ".type"), - match.JSONKeyEqual(result0+".event_id", eventBeforeUpgrade), - match.JSONKeyEqual(result1+".event_id", eventAfterUpgrade), - match.JSONKeyEqual(result0+".content.body", "Message before upgrade"), - match.JSONKeyEqual(result1+".content.body", "Message after upgrade"), + match.JSONKeyArrayOfSize(sce+".results", 2), + + // the results can be in either order: check that both are there and that the content is as expected + match.JSONCheckOff(sce+".results", []interface{} {eventBeforeUpgrade, eventAfterUpgrade}, func(res gjson.Result) interface{} { + return res.Get("event_id") + }, func(eventID interface{}, result gjson.Result) error { + matchers := []match.JSON{ + match.JSONKeyEqual("type", "m.room.message"), + match.JSONKeyEqual("content.body", expectedEvents[eventID.(string)]), + } + for _, jm := range matchers { + if err := jm(body); err != nil { + return fmt.Errorf("search result: %w", err) + } + } + return nil + }), }, }) }) From b0389ab47c724bc5803183d3e3634690092d5d36 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 27 Sep 2022 13:40:10 +0100 Subject: [PATCH 2/2] fix the test --- tests/csapi/apidoc_search_test.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/csapi/apidoc_search_test.go b/tests/csapi/apidoc_search_test.go index 968b4819..3c41415f 100644 --- a/tests/csapi/apidoc_search_test.go +++ b/tests/csapi/apidoc_search_test.go @@ -212,6 +212,7 @@ func TestSearch(t *testing.T) { "msgtype": "m.text", }, }) + t.Logf("Sent message before upgrade with event ID %s", eventBeforeUpgrade) upgradeBody := client.WithJSONBody(t, map[string]string{ "new_version": "9", @@ -219,6 +220,7 @@ func TestSearch(t *testing.T) { upgradeResp := alice.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "rooms", roomID, "upgrade"}, upgradeBody) body := must.ParseJSON(t, upgradeResp.Body) newRoomID := must.GetJSONFieldStr(t, body, "replacement_room") + t.Logf("Replaced room %s with %s", roomID, newRoomID) eventAfterUpgrade := alice.SendEventSynced(t, newRoomID, b.Event{ Type: "m.room.message", @@ -227,6 +229,7 @@ func TestSearch(t *testing.T) { "msgtype": "m.text", }, }) + t.Logf("Sent message after upgrade with event ID %s", eventAfterUpgrade) searchRequest := client.WithJSONBody(t, map[string]interface{}{ "search_categories": map[string]interface{}{ @@ -245,7 +248,7 @@ func TestSearch(t *testing.T) { expectedEvents := map[string]string{ eventBeforeUpgrade: "Message before upgrade", - eventAfterUpgrade: "Message after upgrade", + eventAfterUpgrade: "Message after upgrade", } must.MatchResponse(t, resp, match.HTTPResponse{ StatusCode: http.StatusOK, @@ -254,16 +257,16 @@ func TestSearch(t *testing.T) { match.JSONKeyArrayOfSize(sce+".results", 2), // the results can be in either order: check that both are there and that the content is as expected - match.JSONCheckOff(sce+".results", []interface{} {eventBeforeUpgrade, eventAfterUpgrade}, func(res gjson.Result) interface{} { - return res.Get("event_id") + match.JSONCheckOff(sce+".results", []interface{}{eventBeforeUpgrade, eventAfterUpgrade}, func(res gjson.Result) interface{} { + return res.Get("result.event_id").Str }, func(eventID interface{}, result gjson.Result) error { matchers := []match.JSON{ - match.JSONKeyEqual("type", "m.room.message"), - match.JSONKeyEqual("content.body", expectedEvents[eventID.(string)]), + match.JSONKeyEqual("result.type", "m.room.message"), + match.JSONKeyEqual("result.content.body", expectedEvents[eventID.(string)]), } for _, jm := range matchers { - if err := jm(body); err != nil { - return fmt.Errorf("search result: %w", err) + if err := jm([]byte(result.Raw)); err != nil { + return err } } return nil