|
| 1 | +package csapi_tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/tidwall/gjson" |
| 9 | + |
| 10 | + "github.com/matrix-org/complement" |
| 11 | + "github.com/matrix-org/complement/client" |
| 12 | + "github.com/matrix-org/complement/helpers" |
| 13 | + "github.com/matrix-org/complement/match" |
| 14 | + "github.com/matrix-org/complement/must" |
| 15 | +) |
| 16 | + |
| 17 | +func TestPublicRooms(t *testing.T) { |
| 18 | + deployment := complement.Deploy(t, 1) |
| 19 | + defer deployment.Destroy(t) |
| 20 | + |
| 21 | + t.Run("parallel", func(t *testing.T) { |
| 22 | + // sytest: Can search public room list |
| 23 | + t.Run("Can search public room list", func(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + authedClient := deployment.Register(t, "hs1", helpers.RegistrationOpts{}) |
| 26 | + |
| 27 | + roomID := authedClient.MustCreateRoom(t, map[string]any{ |
| 28 | + "visibility": "public", |
| 29 | + "name": "Test Name", |
| 30 | + "topic": "Test Topic Wombles", |
| 31 | + }) |
| 32 | + |
| 33 | + authedClient.MustDo( |
| 34 | + t, |
| 35 | + "POST", |
| 36 | + []string{"_matrix", "client", "v3", "publicRooms"}, |
| 37 | + client.WithJSONBody(t, map[string]any{ |
| 38 | + "filter": map[string]any{ |
| 39 | + "generic_search_term": "wombles", |
| 40 | + }, |
| 41 | + }), |
| 42 | + client.WithRetryUntil(15*time.Second, func(res *http.Response) bool { |
| 43 | + body := must.ParseJSON(t, res.Body) |
| 44 | + |
| 45 | + must.MatchGJSON( |
| 46 | + t, |
| 47 | + body, |
| 48 | + match.JSONKeyPresent("chunk"), |
| 49 | + match.JSONKeyTypeEqual("chunk", gjson.JSON), |
| 50 | + ) |
| 51 | + |
| 52 | + chunk := body.Get("chunk") |
| 53 | + if !chunk.IsArray() { |
| 54 | + t.Logf("chunk is not an array") |
| 55 | + return false |
| 56 | + } |
| 57 | + |
| 58 | + results := chunk.Array() |
| 59 | + if len(results) != 1 { |
| 60 | + t.Logf("expected a single search result, got %d", len(results)) |
| 61 | + return false |
| 62 | + } |
| 63 | + |
| 64 | + foundRoomID := results[0].Get("room_id").Str |
| 65 | + if foundRoomID != roomID { |
| 66 | + t.Logf("expected room_id %s in search results, got %s", roomID, foundRoomID) |
| 67 | + return false |
| 68 | + } |
| 69 | + |
| 70 | + return true |
| 71 | + }), |
| 72 | + ) |
| 73 | + }) |
| 74 | + }) |
| 75 | +} |
0 commit comments