|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/// <reference types="cypress" /> |
| 18 | + |
| 19 | +import { MatrixClient } from "../../global"; |
| 20 | +import { SynapseInstance } from "../../plugins/synapsedocker"; |
| 21 | +import Chainable = Cypress.Chainable; |
| 22 | +import Loggable = Cypress.Loggable; |
| 23 | +import Timeoutable = Cypress.Timeoutable; |
| 24 | +import Withinable = Cypress.Withinable; |
| 25 | +import Shadow = Cypress.Shadow; |
| 26 | + |
| 27 | +export enum Filter { |
| 28 | + People = "people", |
| 29 | + PublicRooms = "public_rooms" |
| 30 | +} |
| 31 | + |
| 32 | +declare global { |
| 33 | + // eslint-disable-next-line @typescript-eslint/no-namespace |
| 34 | + namespace Cypress { |
| 35 | + interface Chainable { |
| 36 | + /** |
| 37 | + * Opens the spotlight dialog |
| 38 | + */ |
| 39 | + openSpotlightDialog( |
| 40 | + options?: Partial<Loggable & Timeoutable & Withinable & Shadow> |
| 41 | + ): Chainable<JQuery<HTMLElement>>; |
| 42 | + spotlightDialog( |
| 43 | + options?: Partial<Loggable & Timeoutable & Withinable & Shadow> |
| 44 | + ): Chainable<JQuery<HTMLElement>>; |
| 45 | + spotlightFilter( |
| 46 | + filter: Filter | null, |
| 47 | + options?: Partial<Loggable & Timeoutable & Withinable & Shadow> |
| 48 | + ): Chainable<JQuery<HTMLElement>>; |
| 49 | + spotlightSearch( |
| 50 | + options?: Partial<Loggable & Timeoutable & Withinable & Shadow> |
| 51 | + ): Chainable<JQuery<HTMLElement>>; |
| 52 | + spotlightResults( |
| 53 | + options?: Partial<Loggable & Timeoutable & Withinable & Shadow> |
| 54 | + ): Chainable<JQuery<HTMLElement>>; |
| 55 | + roomHeaderName( |
| 56 | + options?: Partial<Loggable & Timeoutable & Withinable & Shadow> |
| 57 | + ): Chainable<JQuery<HTMLElement>>; |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +Cypress.Commands.add("openSpotlightDialog", ( |
| 63 | + options?: Partial<Loggable & Timeoutable & Withinable & Shadow>, |
| 64 | +): Chainable<JQuery<HTMLElement>> => { |
| 65 | + cy.get('.mx_RoomSearch_spotlightTrigger', options).click({ force: true }); |
| 66 | + return cy.spotlightDialog(options); |
| 67 | +}); |
| 68 | + |
| 69 | +Cypress.Commands.add("spotlightDialog", ( |
| 70 | + options?: Partial<Loggable & Timeoutable & Withinable & Shadow>, |
| 71 | +): Chainable<JQuery<HTMLElement>> => { |
| 72 | + return cy.get('[role=dialog][aria-label="Search Dialog"]', options); |
| 73 | +}); |
| 74 | + |
| 75 | +Cypress.Commands.add("spotlightFilter", ( |
| 76 | + filter: Filter | null, |
| 77 | + options?: Partial<Loggable & Timeoutable & Withinable & Shadow>, |
| 78 | +): Chainable<JQuery<HTMLElement>> => { |
| 79 | + let selector: string; |
| 80 | + switch (filter) { |
| 81 | + case Filter.People: |
| 82 | + selector = "#mx_SpotlightDialog_button_startChat"; |
| 83 | + break; |
| 84 | + case Filter.PublicRooms: |
| 85 | + selector = "#mx_SpotlightDialog_button_explorePublicRooms"; |
| 86 | + break; |
| 87 | + default: |
| 88 | + selector = ".mx_SpotlightDialog_filter"; |
| 89 | + break; |
| 90 | + } |
| 91 | + return cy.get(selector, options).click(); |
| 92 | +}); |
| 93 | + |
| 94 | +Cypress.Commands.add("spotlightSearch", ( |
| 95 | + options?: Partial<Loggable & Timeoutable & Withinable & Shadow>, |
| 96 | +): Chainable<JQuery<HTMLElement>> => { |
| 97 | + return cy.get(".mx_SpotlightDialog_searchBox input", options); |
| 98 | +}); |
| 99 | + |
| 100 | +Cypress.Commands.add("spotlightResults", ( |
| 101 | + options?: Partial<Loggable & Timeoutable & Withinable & Shadow>, |
| 102 | +): Chainable<JQuery<HTMLElement>> => { |
| 103 | + return cy.get(".mx_SpotlightDialog_section.mx_SpotlightDialog_results .mx_SpotlightDialog_option", options); |
| 104 | +}); |
| 105 | + |
| 106 | +Cypress.Commands.add("roomHeaderName", ( |
| 107 | + options?: Partial<Loggable & Timeoutable & Withinable & Shadow>, |
| 108 | +): Chainable<JQuery<HTMLElement>> => { |
| 109 | + return cy.get(".mx_RoomHeader_nametext", options); |
| 110 | +}); |
| 111 | + |
| 112 | +describe("Spotlight", () => { |
| 113 | + let synapse: SynapseInstance; |
| 114 | + |
| 115 | + const bot1Name = "BotBob"; |
| 116 | + let bot1: MatrixClient; |
| 117 | + |
| 118 | + const bot2Name = "ByteBot"; |
| 119 | + let bot2: MatrixClient; |
| 120 | + |
| 121 | + const room1Name = "247"; |
| 122 | + let room1Id: string; |
| 123 | + |
| 124 | + const room2Name = "Lounge"; |
| 125 | + let room2Id: string; |
| 126 | + |
| 127 | + beforeEach(() => { |
| 128 | + cy.enableLabsFeature("feature_spotlight"); |
| 129 | + cy.startSynapse("default").then(data => { |
| 130 | + synapse = data; |
| 131 | + cy.initTestUser(synapse, "Jim").then(() => |
| 132 | + cy.getBot(synapse, bot1Name).then(_bot1 => { |
| 133 | + bot1 = _bot1; |
| 134 | + }), |
| 135 | + ).then(() => |
| 136 | + cy.getBot(synapse, bot2Name).then(_bot2 => { |
| 137 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 138 | + bot2 = _bot2; |
| 139 | + }), |
| 140 | + ).then(() => |
| 141 | + cy.window({ log: false }).then(({ matrixcs: { Visibility } }) => { |
| 142 | + cy.createRoom({ name: room1Name, visibility: Visibility.Public }).then(_room1Id => { |
| 143 | + room1Id = _room1Id; |
| 144 | + cy.inviteUser(room1Id, bot1.getUserId()); |
| 145 | + cy.visit("/#/room/" + room1Id); |
| 146 | + }); |
| 147 | + bot2.createRoom({ name: room2Name, visibility: Visibility.Public }) |
| 148 | + .then(({ room_id: _room2Id }) => { |
| 149 | + room2Id = _room2Id; |
| 150 | + bot2.invite(room2Id, bot1.getUserId()); |
| 151 | + }); |
| 152 | + }), |
| 153 | + ).then(() => |
| 154 | + cy.get('.mx_RoomSublist_skeletonUI').should('not.exist'), |
| 155 | + ); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + afterEach(() => { |
| 160 | + cy.stopSynapse(synapse); |
| 161 | + }); |
| 162 | + |
| 163 | + it("should be able to add and remove filters via keyboard", () => { |
| 164 | + cy.openSpotlightDialog().within(() => { |
| 165 | + cy.spotlightSearch().type("{downArrow}"); |
| 166 | + cy.get("#mx_SpotlightDialog_button_explorePublicRooms").should("have.attr", "aria-selected", "true"); |
| 167 | + cy.spotlightSearch().type("{enter}"); |
| 168 | + cy.get(".mx_SpotlightDialog_filter").should("contain", "Public rooms"); |
| 169 | + cy.spotlightSearch().type("{backspace}"); |
| 170 | + cy.get(".mx_SpotlightDialog_filter").should("not.exist"); |
| 171 | + |
| 172 | + cy.spotlightSearch().type("{downArrow}"); |
| 173 | + cy.spotlightSearch().type("{downArrow}"); |
| 174 | + cy.get("#mx_SpotlightDialog_button_startChat").should("have.attr", "aria-selected", "true"); |
| 175 | + cy.spotlightSearch().type("{enter}"); |
| 176 | + cy.get(".mx_SpotlightDialog_filter").should("contain", "People"); |
| 177 | + cy.spotlightSearch().type("{backspace}"); |
| 178 | + cy.get(".mx_SpotlightDialog_filter").should("not.exist"); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + it("should find joined rooms", () => { |
| 183 | + cy.openSpotlightDialog().within(() => { |
| 184 | + cy.spotlightSearch().clear().type(room1Name); |
| 185 | + cy.spotlightResults().should("have.length", 1); |
| 186 | + cy.spotlightResults().eq(0).should("contain", room1Name); |
| 187 | + cy.spotlightResults().eq(0).click(); |
| 188 | + cy.url().should("contain", room1Id); |
| 189 | + }).then(() => { |
| 190 | + cy.roomHeaderName().should("contain", room1Name); |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + it("should find known public rooms", () => { |
| 195 | + cy.openSpotlightDialog().within(() => { |
| 196 | + cy.spotlightFilter(Filter.PublicRooms); |
| 197 | + cy.spotlightSearch().clear().type(room1Name); |
| 198 | + cy.spotlightResults().should("have.length", 1); |
| 199 | + cy.spotlightResults().eq(0).should("contain", room1Name); |
| 200 | + cy.spotlightResults().eq(0).click(); |
| 201 | + cy.url().should("contain", room1Id); |
| 202 | + }).then(() => { |
| 203 | + cy.roomHeaderName().should("contain", room1Name); |
| 204 | + }); |
| 205 | + }); |
| 206 | + |
| 207 | + it("should find unknown public rooms", () => { |
| 208 | + cy.openSpotlightDialog().within(() => { |
| 209 | + cy.spotlightFilter(Filter.PublicRooms); |
| 210 | + cy.spotlightSearch().clear().type(room2Name); |
| 211 | + cy.spotlightResults().should("have.length", 1); |
| 212 | + cy.spotlightResults().eq(0).should("contain", room2Name); |
| 213 | + cy.spotlightResults().eq(0).click(); |
| 214 | + cy.url().should("contain", room2Id); |
| 215 | + }).then(() => { |
| 216 | + cy.get(".mx_RoomPreviewBar_actions .mx_AccessibleButton").click(); |
| 217 | + cy.roomHeaderName().should("contain", room2Name); |
| 218 | + }); |
| 219 | + }); |
| 220 | + |
| 221 | + // TODO: We currently can’t test finding rooms on other homeservers/other protocols |
| 222 | + // We obviously don’t have federation or bridges in cypress tests |
| 223 | + /* |
| 224 | + const room3Name = "Matrix HQ"; |
| 225 | + const room3Id = "#matrix:matrix.org"; |
| 226 | +
|
| 227 | + it("should find unknown public rooms on other homeservers", () => { |
| 228 | + cy.openSpotlightDialog().within(() => { |
| 229 | + cy.spotlightFilter(Filter.PublicRooms); |
| 230 | + cy.spotlightSearch().clear().type(room3Name); |
| 231 | + cy.get("[aria-haspopup=true][role=button]").click(); |
| 232 | + }).then(() => { |
| 233 | + cy.contains(".mx_GenericDropdownMenu_Option--header", "matrix.org") |
| 234 | + .next("[role=menuitemradio]") |
| 235 | + .click(); |
| 236 | + cy.wait(3_600_000); |
| 237 | + }).then(() => cy.spotlightDialog().within(() => { |
| 238 | + cy.spotlightResults().should("have.length", 1); |
| 239 | + cy.spotlightResults().eq(0).should("contain", room3Name); |
| 240 | + cy.spotlightResults().eq(0).should("contain", room3Id); |
| 241 | + })); |
| 242 | + }); |
| 243 | + */ |
| 244 | + it("should find known people", () => { |
| 245 | + cy.openSpotlightDialog().within(() => { |
| 246 | + cy.spotlightFilter(Filter.People); |
| 247 | + cy.spotlightSearch().clear().type(bot1Name); |
| 248 | + cy.spotlightResults().should("have.length", 1); |
| 249 | + cy.spotlightResults().eq(0).should("contain", bot1Name); |
| 250 | + cy.spotlightResults().eq(0).click(); |
| 251 | + }).then(() => { |
| 252 | + cy.roomHeaderName().should("contain", bot1Name); |
| 253 | + }); |
| 254 | + }); |
| 255 | + |
| 256 | + it("should find unknown people", () => { |
| 257 | + cy.openSpotlightDialog().within(() => { |
| 258 | + cy.spotlightFilter(Filter.People); |
| 259 | + cy.spotlightSearch().clear().type(bot2Name); |
| 260 | + cy.spotlightResults().should("have.length", 1); |
| 261 | + cy.spotlightResults().eq(0).should("contain", bot2Name); |
| 262 | + cy.spotlightResults().eq(0).click(); |
| 263 | + }).then(() => { |
| 264 | + cy.roomHeaderName().should("contain", bot2Name); |
| 265 | + }); |
| 266 | + }); |
| 267 | + |
| 268 | + it("should allow opening group chat dialog", () => { |
| 269 | + cy.openSpotlightDialog().within(() => { |
| 270 | + cy.spotlightFilter(Filter.People); |
| 271 | + cy.spotlightSearch().clear().type(bot2Name); |
| 272 | + cy.spotlightResults().should("have.length", 1); |
| 273 | + cy.spotlightResults().eq(0).should("contain", bot2Name); |
| 274 | + cy.get(".mx_SpotlightDialog_startGroupChat").should("contain", "Start a group chat"); |
| 275 | + cy.get(".mx_SpotlightDialog_startGroupChat").click(); |
| 276 | + }).then(() => { |
| 277 | + cy.get('[role=dialog]').should("contain", "Direct Messages"); |
| 278 | + }); |
| 279 | + }); |
| 280 | + |
| 281 | + it("should be able to navigate results via keyboard", () => { |
| 282 | + cy.openSpotlightDialog().within(() => { |
| 283 | + cy.spotlightFilter(Filter.People); |
| 284 | + cy.spotlightSearch().clear().type("b"); |
| 285 | + cy.spotlightResults().should("have.length", 2); |
| 286 | + cy.spotlightResults().eq(0).should("have.attr", "aria-selected", "true"); |
| 287 | + cy.spotlightResults().eq(1).should("have.attr", "aria-selected", "false"); |
| 288 | + cy.spotlightSearch().type("{downArrow}"); |
| 289 | + cy.spotlightResults().eq(0).should("have.attr", "aria-selected", "false"); |
| 290 | + cy.spotlightResults().eq(1).should("have.attr", "aria-selected", "true"); |
| 291 | + cy.spotlightSearch().type("{downArrow}"); |
| 292 | + cy.spotlightResults().eq(0).should("have.attr", "aria-selected", "false"); |
| 293 | + cy.spotlightResults().eq(1).should("have.attr", "aria-selected", "false"); |
| 294 | + cy.spotlightSearch().type("{upArrow}"); |
| 295 | + cy.spotlightResults().eq(0).should("have.attr", "aria-selected", "false"); |
| 296 | + cy.spotlightResults().eq(1).should("have.attr", "aria-selected", "true"); |
| 297 | + cy.spotlightSearch().type("{upArrow}"); |
| 298 | + cy.spotlightResults().eq(0).should("have.attr", "aria-selected", "true"); |
| 299 | + cy.spotlightResults().eq(1).should("have.attr", "aria-selected", "false"); |
| 300 | + }); |
| 301 | + }); |
| 302 | +}); |
0 commit comments