From ff1057fb0aeadcdc4642097ad49622e6d7b0bfbd Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Mon, 2 Oct 2023 16:14:56 +0100 Subject: [PATCH] Share code for room initialisation between read receipt tests --- .../read-receipts/editing-messages.spec.ts | 42 ++----------- cypress/e2e/read-receipts/high-level.spec.ts | 42 ++----------- .../read-receipts/missing-referents.spec.ts | 50 ++++------------ .../e2e/read-receipts/new-messages.spec.ts | 42 ++----------- cypress/e2e/read-receipts/reactions.spec.ts | 42 ++----------- .../e2e/read-receipts/read-receipts-utils.ts | 59 +++++++++++++++++++ cypress/e2e/read-receipts/redactions.spec.ts | 42 ++----------- 7 files changed, 90 insertions(+), 229 deletions(-) diff --git a/cypress/e2e/read-receipts/editing-messages.spec.ts b/cypress/e2e/read-receipts/editing-messages.spec.ts index bf0ab0a940..77f67476f1 100644 --- a/cypress/e2e/read-receipts/editing-messages.spec.ts +++ b/cypress/e2e/read-receipts/editing-messages.spec.ts @@ -18,7 +18,6 @@ limitations under the License. /// -import type { MatrixClient } from "matrix-js-sdk/src/matrix"; import { HomeserverInstance } from "../../plugins/utils/homeserver"; import { assertRead, @@ -32,22 +31,18 @@ import { MessageContentSpec, MessageFinder, openThread, + ReadReceiptSetup, saveAndReload, sendMessageAsClient, } from "./read-receipts-utils"; describe("Read receipts", () => { - const userName = "Mae"; - const botName = "Other User"; const roomAlpha = "Room Alpha"; const roomBeta = "Room Beta"; let homeserver: HomeserverInstance; - let betaRoomId: string; - let alphaRoomId: string; - let bot: MatrixClient | undefined; - let messageFinder: MessageFinder; + let testSetup: ReadReceiptSetup; function editOf(originalMessage: string, newMessage: string): MessageContentSpec { return messageFinder.editOf(originalMessage, newMessage); @@ -81,36 +76,7 @@ describe("Read receipts", () => { beforeEach(() => { messageFinder = new MessageFinder(); - - // Create 2 rooms: Alpha & Beta. We join the bot to both of them - cy.initTestUser(homeserver, userName) - .then(() => { - cy.createRoom({ name: roomAlpha }).then((createdRoomId) => { - alphaRoomId = createdRoomId; - }); - }) - .then(() => { - cy.createRoom({ name: roomBeta }).then((createdRoomId) => { - betaRoomId = createdRoomId; - }); - }) - .then(() => { - cy.getBot(homeserver, { displayName: botName }).then((botClient) => { - bot = botClient; - }); - }) - .then(() => { - // Invite the bot to both rooms - cy.inviteUser(alphaRoomId, bot.getUserId()); - cy.viewRoomById(alphaRoomId); - cy.get(".mx_LegacyRoomHeader").within(() => cy.findByTitle(roomAlpha).should("exist")); - cy.findByText(botName + " joined the room").should("exist"); - - cy.inviteUser(betaRoomId, bot.getUserId()); - cy.viewRoomById(betaRoomId); - cy.get(".mx_LegacyRoomHeader").within(() => cy.findByTitle(roomBeta).should("exist")); - cy.findByText(botName + " joined the room").should("exist"); - }); + testSetup = new ReadReceiptSetup(homeserver, "Mae", "Other User", roomAlpha, roomBeta); }); after(() => { @@ -123,7 +89,7 @@ describe("Read receipts", () => { * @param messages - the list of messages to send, these can be strings or implementations of MessageSpec like `editOf` */ function receiveMessages(room: string, messages: Message[]) { - sendMessageAsClient(bot, room, messages); + sendMessageAsClient(testSetup.bot, room, messages); } const room1 = roomAlpha; diff --git a/cypress/e2e/read-receipts/high-level.spec.ts b/cypress/e2e/read-receipts/high-level.spec.ts index 82683a1028..22eab60b61 100644 --- a/cypress/e2e/read-receipts/high-level.spec.ts +++ b/cypress/e2e/read-receipts/high-level.spec.ts @@ -18,7 +18,6 @@ limitations under the License. /// -import type { MatrixClient } from "matrix-js-sdk/src/matrix"; import { HomeserverInstance } from "../../plugins/utils/homeserver"; import { assertMessageLoaded, @@ -40,22 +39,18 @@ import { openThread, openThreadList, pageUp, + ReadReceiptSetup, saveAndReload, sendMessageAsClient, } from "./read-receipts-utils"; describe("Read receipts", () => { - const userName = "Mae"; - const botName = "Other User"; const roomAlpha = "Room Alpha"; const roomBeta = "Room Beta"; let homeserver: HomeserverInstance; - let betaRoomId: string; - let alphaRoomId: string; - let bot: MatrixClient | undefined; - let messageFinder: MessageFinder; + let testSetup: ReadReceiptSetup; function threadedOff(rootMessage: string, newMessage: string): MessageContentSpec { return messageFinder.threadedOff(rootMessage, newMessage); @@ -89,36 +84,7 @@ describe("Read receipts", () => { beforeEach(() => { messageFinder = new MessageFinder(); - - // Create 2 rooms: Alpha & Beta. We join the bot to both of them - cy.initTestUser(homeserver, userName) - .then(() => { - cy.createRoom({ name: roomAlpha }).then((createdRoomId) => { - alphaRoomId = createdRoomId; - }); - }) - .then(() => { - cy.createRoom({ name: roomBeta }).then((createdRoomId) => { - betaRoomId = createdRoomId; - }); - }) - .then(() => { - cy.getBot(homeserver, { displayName: botName }).then((botClient) => { - bot = botClient; - }); - }) - .then(() => { - // Invite the bot to both rooms - cy.inviteUser(alphaRoomId, bot.getUserId()); - cy.viewRoomById(alphaRoomId); - cy.get(".mx_LegacyRoomHeader").within(() => cy.findByTitle(roomAlpha).should("exist")); - cy.findByText(botName + " joined the room").should("exist"); - - cy.inviteUser(betaRoomId, bot.getUserId()); - cy.viewRoomById(betaRoomId); - cy.get(".mx_LegacyRoomHeader").within(() => cy.findByTitle(roomBeta).should("exist")); - cy.findByText(botName + " joined the room").should("exist"); - }); + testSetup = new ReadReceiptSetup(homeserver, "Mae", "Other User", roomAlpha, roomBeta); }); after(() => { @@ -131,7 +97,7 @@ describe("Read receipts", () => { * @param messages - the list of messages to send, these can be strings or implementations of MessageSpec like `editOf` */ function receiveMessages(room: string, messages: Message[]) { - sendMessageAsClient(bot, room, messages); + sendMessageAsClient(testSetup.bot, room, messages); } const room1 = roomAlpha; diff --git a/cypress/e2e/read-receipts/missing-referents.spec.ts b/cypress/e2e/read-receipts/missing-referents.spec.ts index 35929a72f4..da4b01b58b 100644 --- a/cypress/e2e/read-receipts/missing-referents.spec.ts +++ b/cypress/e2e/read-receipts/missing-referents.spec.ts @@ -18,22 +18,23 @@ limitations under the License. /// -import type { MatrixClient } from "matrix-js-sdk/src/matrix"; import { HomeserverInstance } from "../../plugins/utils/homeserver"; -import { goTo, Message, MessageContentSpec, MessageFinder, sendMessageAsClient } from "./read-receipts-utils"; +import { + goTo, + Message, + MessageContentSpec, + MessageFinder, + ReadReceiptSetup, + sendMessageAsClient, +} from "./read-receipts-utils"; describe("Read receipts", () => { - const userName = "Mae"; - const botName = "Other User"; const roomAlpha = "Room Alpha"; const roomBeta = "Room Beta"; let homeserver: HomeserverInstance; - let betaRoomId: string; - let alphaRoomId: string; - let bot: MatrixClient | undefined; - let messageFinder: MessageFinder; + let testSetup: ReadReceiptSetup; function threadedOff(rootMessage: string, newMessage: string): MessageContentSpec { return messageFinder.threadedOff(rootMessage, newMessage); @@ -59,36 +60,7 @@ describe("Read receipts", () => { beforeEach(() => { messageFinder = new MessageFinder(); - - // Create 2 rooms: Alpha & Beta. We join the bot to both of them - cy.initTestUser(homeserver, userName) - .then(() => { - cy.createRoom({ name: roomAlpha }).then((createdRoomId) => { - alphaRoomId = createdRoomId; - }); - }) - .then(() => { - cy.createRoom({ name: roomBeta }).then((createdRoomId) => { - betaRoomId = createdRoomId; - }); - }) - .then(() => { - cy.getBot(homeserver, { displayName: botName }).then((botClient) => { - bot = botClient; - }); - }) - .then(() => { - // Invite the bot to both rooms - cy.inviteUser(alphaRoomId, bot.getUserId()); - cy.viewRoomById(alphaRoomId); - cy.get(".mx_LegacyRoomHeader").within(() => cy.findByTitle(roomAlpha).should("exist")); - cy.findByText(botName + " joined the room").should("exist"); - - cy.inviteUser(betaRoomId, bot.getUserId()); - cy.viewRoomById(betaRoomId); - cy.get(".mx_LegacyRoomHeader").within(() => cy.findByTitle(roomBeta).should("exist")); - cy.findByText(botName + " joined the room").should("exist"); - }); + testSetup = new ReadReceiptSetup(homeserver, "Mae", "Other User", roomAlpha, roomBeta); }); /** @@ -97,7 +69,7 @@ describe("Read receipts", () => { * @param messages - the list of messages to send, these can be strings or implementations of MessageSpec like `editOf` */ function receiveMessages(room: string, messages: Message[]) { - sendMessageAsClient(bot, room, messages); + sendMessageAsClient(testSetup.bot, room, messages); } const room1 = roomAlpha; diff --git a/cypress/e2e/read-receipts/new-messages.spec.ts b/cypress/e2e/read-receipts/new-messages.spec.ts index eea82740d7..76ccdbfe54 100644 --- a/cypress/e2e/read-receipts/new-messages.spec.ts +++ b/cypress/e2e/read-receipts/new-messages.spec.ts @@ -18,7 +18,6 @@ limitations under the License. /// -import type { MatrixClient } from "matrix-js-sdk/src/matrix"; import { HomeserverInstance } from "../../plugins/utils/homeserver"; import { assertRead, @@ -27,6 +26,7 @@ import { assertUnreadLessThan, assertUnreadThread, backToThreadsList, + ReadReceiptSetup, goTo, many, markAsRead, @@ -39,17 +39,12 @@ import { } from "./read-receipts-utils"; describe("Read receipts", () => { - const userName = "Mae"; - const botName = "Other User"; const roomAlpha = "Room Alpha"; const roomBeta = "Room Beta"; let homeserver: HomeserverInstance; - let betaRoomId: string; - let alphaRoomId: string; - let bot: MatrixClient | undefined; - let messageFinder: MessageFinder; + let testSetup: ReadReceiptSetup; function replyTo(targetMessage: string, newMessage: string): MessageContentSpec { return messageFinder.replyTo(targetMessage, newMessage); @@ -87,36 +82,7 @@ describe("Read receipts", () => { beforeEach(() => { messageFinder = new MessageFinder(); - - // Create 2 rooms: Alpha & Beta. We join the bot to both of them - cy.initTestUser(homeserver, userName) - .then(() => { - cy.createRoom({ name: roomAlpha }).then((createdRoomId) => { - alphaRoomId = createdRoomId; - }); - }) - .then(() => { - cy.createRoom({ name: roomBeta }).then((createdRoomId) => { - betaRoomId = createdRoomId; - }); - }) - .then(() => { - cy.getBot(homeserver, { displayName: botName }).then((botClient) => { - bot = botClient; - }); - }) - .then(() => { - // Invite the bot to both rooms - cy.inviteUser(alphaRoomId, bot.getUserId()); - cy.viewRoomById(alphaRoomId); - cy.get(".mx_LegacyRoomHeader").within(() => cy.findByTitle(roomAlpha).should("exist")); - cy.findByText(botName + " joined the room").should("exist"); - - cy.inviteUser(betaRoomId, bot.getUserId()); - cy.viewRoomById(betaRoomId); - cy.get(".mx_LegacyRoomHeader").within(() => cy.findByTitle(roomBeta).should("exist")); - cy.findByText(botName + " joined the room").should("exist"); - }); + testSetup = new ReadReceiptSetup(homeserver, "Mae", "Other User", roomAlpha, roomBeta); }); after(() => { @@ -129,7 +95,7 @@ describe("Read receipts", () => { * @param messages - the list of messages to send, these can be strings or implementations of MessageSpec like `editOf` */ function receiveMessages(room: string, messages: Message[]) { - sendMessageAsClient(bot, room, messages); + sendMessageAsClient(testSetup.bot, room, messages); } /** diff --git a/cypress/e2e/read-receipts/reactions.spec.ts b/cypress/e2e/read-receipts/reactions.spec.ts index dca06f9d77..aa7a9ff5f6 100644 --- a/cypress/e2e/read-receipts/reactions.spec.ts +++ b/cypress/e2e/read-receipts/reactions.spec.ts @@ -18,7 +18,6 @@ limitations under the License. /// -import type { MatrixClient } from "matrix-js-sdk/src/matrix"; import { HomeserverInstance } from "../../plugins/utils/homeserver"; import { assertRead, @@ -32,22 +31,18 @@ import { MessageContentSpec, MessageFinder, openThread, + ReadReceiptSetup, saveAndReload, sendMessageAsClient, } from "./read-receipts-utils"; describe("Read receipts", () => { - const userName = "Mae"; - const botName = "Other User"; const roomAlpha = "Room Alpha"; const roomBeta = "Room Beta"; let homeserver: HomeserverInstance; - let betaRoomId: string; - let alphaRoomId: string; - let bot: MatrixClient | undefined; - let messageFinder: MessageFinder; + let testSetup: ReadReceiptSetup; function threadedOff(rootMessage: string, newMessage: string): MessageContentSpec { return messageFinder.threadedOff(rootMessage, newMessage); @@ -77,36 +72,7 @@ describe("Read receipts", () => { beforeEach(() => { messageFinder = new MessageFinder(); - - // Create 2 rooms: Alpha & Beta. We join the bot to both of them - cy.initTestUser(homeserver, userName) - .then(() => { - cy.createRoom({ name: roomAlpha }).then((createdRoomId) => { - alphaRoomId = createdRoomId; - }); - }) - .then(() => { - cy.createRoom({ name: roomBeta }).then((createdRoomId) => { - betaRoomId = createdRoomId; - }); - }) - .then(() => { - cy.getBot(homeserver, { displayName: botName }).then((botClient) => { - bot = botClient; - }); - }) - .then(() => { - // Invite the bot to both rooms - cy.inviteUser(alphaRoomId, bot.getUserId()); - cy.viewRoomById(alphaRoomId); - cy.get(".mx_LegacyRoomHeader").within(() => cy.findByTitle(roomAlpha).should("exist")); - cy.findByText(botName + " joined the room").should("exist"); - - cy.inviteUser(betaRoomId, bot.getUserId()); - cy.viewRoomById(betaRoomId); - cy.get(".mx_LegacyRoomHeader").within(() => cy.findByTitle(roomBeta).should("exist")); - cy.findByText(botName + " joined the room").should("exist"); - }); + testSetup = new ReadReceiptSetup(homeserver, "Mae", "Other User", roomAlpha, roomBeta); }); after(() => { @@ -119,7 +85,7 @@ describe("Read receipts", () => { * @param messages - the list of messages to send, these can be strings or implementations of MessageSpec like `editOf` */ function receiveMessages(room: string, messages: Message[]) { - sendMessageAsClient(bot, room, messages); + sendMessageAsClient(testSetup.bot, room, messages); } const room1 = roomAlpha; diff --git a/cypress/e2e/read-receipts/read-receipts-utils.ts b/cypress/e2e/read-receipts/read-receipts-utils.ts index db181cf171..e3dc01cbd9 100644 --- a/cypress/e2e/read-receipts/read-receipts-utils.ts +++ b/cypress/e2e/read-receipts/read-receipts-utils.ts @@ -15,8 +15,67 @@ limitations under the License. */ import type { MatrixClient, MatrixEvent, Room, IndexedDBStore } from "matrix-js-sdk/src/matrix"; +import { HomeserverInstance } from "../../plugins/utils/homeserver"; import Chainable = Cypress.Chainable; +/** + * Set up for a read receipt test: + * - Create a user with the supplied name + * - As that user, create two rooms with the supplied names + * - Create a bot with the supplied name + * - Invite the bot to both rooms and ensure that it has joined + */ +export class ReadReceiptSetup { + roomAlpha: string; + roomBeta: string; + alphaRoomId: string; + betaRoomId: string; + bot: MatrixClient; + + constructor( + homeserver: HomeserverInstance, + userName: string, + botName: string, + roomAlpha: string, + roomBeta: string, + ) { + this.roomAlpha = roomAlpha; + this.roomBeta = roomBeta; + + // Create a user + cy.initTestUser(homeserver, userName) + // Create 2 rooms + .then(() => { + cy.createRoom({ name: roomAlpha }).then((createdRoomId) => { + this.alphaRoomId = createdRoomId; + }); + }) + .then(() => { + cy.createRoom({ name: roomBeta }).then((createdRoomId) => { + this.betaRoomId = createdRoomId; + }); + }) + // Create a bot + .then(() => { + cy.getBot(homeserver, { displayName: botName }).then((botClient) => { + this.bot = botClient; + }); + }) + // Invite the bot to both rooms + .then(() => { + cy.inviteUser(this.alphaRoomId, this.bot.getUserId()); + cy.viewRoomById(this.alphaRoomId); + cy.get(".mx_LegacyRoomHeader").within(() => cy.findByTitle(roomAlpha).should("exist")); + cy.findByText(botName + " joined the room").should("exist"); + + cy.inviteUser(this.betaRoomId, this.bot.getUserId()); + cy.viewRoomById(this.betaRoomId); + cy.get(".mx_LegacyRoomHeader").within(() => cy.findByTitle(roomBeta).should("exist")); + cy.findByText(botName + " joined the room").should("exist"); + }); + } +} + /** * A utility that is able to find messages based on their content, by looking * inside the `timeline` objects in the object model. diff --git a/cypress/e2e/read-receipts/redactions.spec.ts b/cypress/e2e/read-receipts/redactions.spec.ts index b201453f10..7c0056307f 100644 --- a/cypress/e2e/read-receipts/redactions.spec.ts +++ b/cypress/e2e/read-receipts/redactions.spec.ts @@ -18,7 +18,6 @@ limitations under the License. /// -import type { MatrixClient } from "matrix-js-sdk/src/matrix"; import { HomeserverInstance } from "../../plugins/utils/homeserver"; import { assertRead, @@ -35,22 +34,18 @@ import { MessageContentSpec, MessageFinder, openThread, + ReadReceiptSetup, saveAndReload, sendMessageAsClient, } from "./read-receipts-utils"; describe("Read receipts", () => { - const userName = "Mae"; - const botName = "Other User"; const roomAlpha = "Room Alpha"; const roomBeta = "Room Beta"; let homeserver: HomeserverInstance; - let betaRoomId: string; - let alphaRoomId: string; - let bot: MatrixClient | undefined; - let messageFinder: MessageFinder; + let testSetup: ReadReceiptSetup; function editOf(originalMessage: string, newMessage: string): MessageContentSpec { return messageFinder.editOf(originalMessage, newMessage); @@ -92,36 +87,7 @@ describe("Read receipts", () => { beforeEach(() => { messageFinder = new MessageFinder(); - - // Create 2 rooms: Alpha & Beta. We join the bot to both of them - cy.initTestUser(homeserver, userName) - .then(() => { - cy.createRoom({ name: roomAlpha }).then((createdRoomId) => { - alphaRoomId = createdRoomId; - }); - }) - .then(() => { - cy.createRoom({ name: roomBeta }).then((createdRoomId) => { - betaRoomId = createdRoomId; - }); - }) - .then(() => { - cy.getBot(homeserver, { displayName: botName }).then((botClient) => { - bot = botClient; - }); - }) - .then(() => { - // Invite the bot to both rooms - cy.inviteUser(alphaRoomId, bot.getUserId()); - cy.viewRoomById(alphaRoomId); - cy.get(".mx_LegacyRoomHeader").within(() => cy.findByTitle(roomAlpha).should("exist")); - cy.findByText(botName + " joined the room").should("exist"); - - cy.inviteUser(betaRoomId, bot.getUserId()); - cy.viewRoomById(betaRoomId); - cy.get(".mx_LegacyRoomHeader").within(() => cy.findByTitle(roomBeta).should("exist")); - cy.findByText(botName + " joined the room").should("exist"); - }); + testSetup = new ReadReceiptSetup(homeserver, "Mae", "Other User", roomAlpha, roomBeta); }); after(() => { @@ -134,7 +100,7 @@ describe("Read receipts", () => { * @param messages - the list of messages to send, these can be strings or implementations of MessageSpec like `editOf` */ function receiveMessages(room: string, messages: Message[]) { - sendMessageAsClient(bot, room, messages); + sendMessageAsClient(testSetup.bot, room, messages); } const room1 = roomAlpha;