2022-05-10 19:09:31 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// <reference types="cypress" />
|
|
|
|
|
2023-08-03 14:56:30 +02:00
|
|
|
import type {
|
|
|
|
MatrixClient,
|
|
|
|
Room,
|
|
|
|
MatrixEvent,
|
|
|
|
IContent,
|
|
|
|
FileType,
|
|
|
|
Upload,
|
|
|
|
UploadOpts,
|
|
|
|
ICreateRoomOpts,
|
|
|
|
ISendEventResponse,
|
2023-08-23 11:04:25 +02:00
|
|
|
ReceiptType,
|
2023-08-03 14:56:30 +02:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2022-05-10 19:09:31 +02:00
|
|
|
import Chainable = Cypress.Chainable;
|
2023-01-19 16:19:07 +01:00
|
|
|
import { UserCredentials } from "./login";
|
2022-05-10 19:09:31 +02:00
|
|
|
|
|
|
|
declare global {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
|
|
namespace Cypress {
|
|
|
|
interface Chainable {
|
|
|
|
/**
|
|
|
|
* Returns the MatrixClient from the MatrixClientPeg
|
|
|
|
*/
|
|
|
|
getClient(): Chainable<MatrixClient | undefined>;
|
|
|
|
/**
|
|
|
|
* Create a room with given options.
|
|
|
|
* @param options the options to apply when creating the room
|
|
|
|
* @return the ID of the newly created room
|
|
|
|
*/
|
|
|
|
createRoom(options: ICreateRoomOpts): Chainable<string>;
|
2022-05-26 11:19:00 +02:00
|
|
|
/**
|
|
|
|
* Create a space with given options.
|
|
|
|
* @param options the options to apply when creating the space
|
|
|
|
* @return the ID of the newly created space (room)
|
|
|
|
*/
|
|
|
|
createSpace(options: ICreateRoomOpts): Chainable<string>;
|
2022-05-10 19:09:31 +02:00
|
|
|
/**
|
|
|
|
* Invites the given user to the given room.
|
|
|
|
* @param roomId the id of the room to invite to
|
|
|
|
* @param userId the id of the user to invite
|
|
|
|
*/
|
|
|
|
inviteUser(roomId: string, userId: string): Chainable<{}>;
|
2022-06-10 17:24:15 +02:00
|
|
|
/**
|
|
|
|
* Sets account data for the user.
|
|
|
|
* @param type The type of account data.
|
|
|
|
* @param data The data to store.
|
|
|
|
*/
|
|
|
|
setAccountData(type: string, data: object): Chainable<{}>;
|
2022-07-04 21:37:48 +02:00
|
|
|
/**
|
|
|
|
* @param {string} roomId
|
|
|
|
* @param {string} threadId
|
|
|
|
* @param {string} eventType
|
|
|
|
* @param {Object} content
|
|
|
|
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
|
|
|
*/
|
|
|
|
sendEvent(
|
|
|
|
roomId: string,
|
|
|
|
threadId: string | null,
|
|
|
|
eventType: string,
|
2022-12-12 12:24:14 +01:00
|
|
|
content: IContent,
|
2022-07-04 21:37:48 +02:00
|
|
|
): Chainable<ISendEventResponse>;
|
2023-05-03 13:13:46 +02:00
|
|
|
/**
|
|
|
|
* @param {MatrixEvent} event
|
|
|
|
* @param {ReceiptType} receiptType
|
|
|
|
* @param {boolean} unthreaded
|
|
|
|
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
|
|
|
*/
|
|
|
|
sendReadReceipt(event: MatrixEvent, receiptType?: ReceiptType, unthreaded?: boolean): Chainable<{}>;
|
2022-07-04 21:37:48 +02:00
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @param {module:client.callback} callback Optional.
|
|
|
|
* @return {Promise} Resolves: {} an empty object.
|
|
|
|
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
|
|
|
*/
|
|
|
|
setDisplayName(name: string): Chainable<{}>;
|
|
|
|
/**
|
|
|
|
* @param {string} url
|
|
|
|
* @param {module:client.callback} callback Optional.
|
|
|
|
* @return {Promise} Resolves: {} an empty object.
|
|
|
|
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
|
|
|
*/
|
|
|
|
setAvatarUrl(url: string): Chainable<{}>;
|
|
|
|
/**
|
|
|
|
* Upload a file to the media repository on the homeserver.
|
|
|
|
*
|
|
|
|
* @param {object} file The object to upload. On a browser, something that
|
|
|
|
* can be sent to XMLHttpRequest.send (typically a File). Under node.js,
|
|
|
|
* a a Buffer, String or ReadStream.
|
|
|
|
*/
|
2022-12-12 12:24:14 +01:00
|
|
|
uploadContent(file: FileType, opts?: UploadOpts): Chainable<Awaited<Upload["promise"]>>;
|
2022-07-04 21:37:48 +02:00
|
|
|
/**
|
|
|
|
* Turn an MXC URL into an HTTP one. <strong>This method is experimental and
|
|
|
|
* may change.</strong>
|
|
|
|
* @param {string} mxcUrl The MXC URL
|
|
|
|
* @param {Number} width The desired width of the thumbnail.
|
|
|
|
* @param {Number} height The desired height of the thumbnail.
|
|
|
|
* @param {string} resizeMethod The thumbnail resize method to use, either
|
|
|
|
* "crop" or "scale".
|
|
|
|
* @param {Boolean} allowDirectLinks If true, return any non-mxc URLs
|
|
|
|
* directly. Fetching such URLs will leak information about the user to
|
|
|
|
* anyone they share a room with. If false, will return null for such URLs.
|
|
|
|
* @return {?string} the avatar URL or null.
|
|
|
|
*/
|
|
|
|
mxcUrlToHttp(
|
|
|
|
mxcUrl: string,
|
|
|
|
width?: number,
|
|
|
|
height?: number,
|
|
|
|
resizeMethod?: string,
|
|
|
|
allowDirectLinks?: boolean,
|
|
|
|
): string | null;
|
2022-06-30 11:44:56 +02:00
|
|
|
/**
|
|
|
|
* Gets the list of DMs with a given user
|
|
|
|
* @param userId The ID of the user
|
|
|
|
* @return the list of DMs with that user
|
|
|
|
*/
|
|
|
|
getDmRooms(userId: string): Chainable<string[]>;
|
2022-06-30 10:59:25 +02:00
|
|
|
/**
|
|
|
|
* Boostraps cross-signing.
|
|
|
|
*/
|
2023-01-19 16:19:07 +01:00
|
|
|
bootstrapCrossSigning(credendtials: UserCredentials): Chainable<void>;
|
2022-07-18 14:16:44 +02:00
|
|
|
/**
|
|
|
|
* Joins the given room by alias or ID
|
|
|
|
* @param roomIdOrAlias the id or alias of the room to join
|
|
|
|
*/
|
|
|
|
joinRoom(roomIdOrAlias: string): Chainable<Room>;
|
2022-05-10 19:09:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Cypress.Commands.add("getClient", (): Chainable<MatrixClient | undefined> => {
|
2022-12-12 12:24:14 +01:00
|
|
|
return cy.window({ log: false }).then((win) => win.mxMatrixClientPeg.matrixClient);
|
2022-05-10 19:09:31 +02:00
|
|
|
});
|
|
|
|
|
2022-06-30 11:44:56 +02:00
|
|
|
Cypress.Commands.add("getDmRooms", (userId: string): Chainable<string[]> => {
|
2022-12-12 12:24:14 +01:00
|
|
|
return cy
|
|
|
|
.getClient()
|
|
|
|
.then((cli) => cli.getAccountData("m.direct")?.getContent<Record<string, string[]>>())
|
|
|
|
.then((dmRoomMap) => dmRoomMap[userId] ?? []);
|
2022-06-30 11:44:56 +02:00
|
|
|
});
|
|
|
|
|
2022-05-10 19:09:31 +02:00
|
|
|
Cypress.Commands.add("createRoom", (options: ICreateRoomOpts): Chainable<string> => {
|
2022-12-12 12:24:14 +01:00
|
|
|
return cy.window({ log: false }).then(async (win) => {
|
2022-05-10 19:09:31 +02:00
|
|
|
const cli = win.mxMatrixClientPeg.matrixClient;
|
|
|
|
const resp = await cli.createRoom(options);
|
|
|
|
const roomId = resp.room_id;
|
|
|
|
|
|
|
|
if (!cli.getRoom(roomId)) {
|
2022-12-12 12:24:14 +01:00
|
|
|
await new Promise<void>((resolve) => {
|
2022-05-10 19:09:31 +02:00
|
|
|
const onRoom = (room: Room) => {
|
|
|
|
if (room.roomId === roomId) {
|
|
|
|
cli.off(win.matrixcs.ClientEvent.Room, onRoom);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
cli.on(win.matrixcs.ClientEvent.Room, onRoom);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return roomId;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-05-26 11:19:00 +02:00
|
|
|
Cypress.Commands.add("createSpace", (options: ICreateRoomOpts): Chainable<string> => {
|
|
|
|
return cy.createRoom({
|
|
|
|
...options,
|
|
|
|
creation_content: {
|
2022-12-12 12:24:14 +01:00
|
|
|
type: "m.space",
|
2022-05-26 11:19:00 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-05-10 19:09:31 +02:00
|
|
|
Cypress.Commands.add("inviteUser", (roomId: string, userId: string): Chainable<{}> => {
|
|
|
|
return cy.getClient().then(async (cli: MatrixClient) => {
|
2023-04-18 12:17:46 +02:00
|
|
|
const res = await cli.invite(roomId, userId);
|
|
|
|
Cypress.log({ name: "inviteUser", message: `sent invite in ${roomId} for ${userId}` });
|
|
|
|
return res;
|
2022-05-10 19:09:31 +02:00
|
|
|
});
|
|
|
|
});
|
2022-06-10 17:24:15 +02:00
|
|
|
|
|
|
|
Cypress.Commands.add("setAccountData", (type: string, data: object): Chainable<{}> => {
|
|
|
|
return cy.getClient().then(async (cli: MatrixClient) => {
|
|
|
|
return cli.setAccountData(type, data);
|
|
|
|
});
|
|
|
|
});
|
2022-06-30 10:59:25 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
Cypress.Commands.add(
|
|
|
|
"sendEvent",
|
|
|
|
(roomId: string, threadId: string | null, eventType: string, content: IContent): Chainable<ISendEventResponse> => {
|
|
|
|
return cy.getClient().then(async (cli: MatrixClient) => {
|
|
|
|
return cli.sendEvent(roomId, threadId, eventType, content);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2022-07-04 21:37:48 +02:00
|
|
|
|
2023-05-03 13:13:46 +02:00
|
|
|
Cypress.Commands.add(
|
|
|
|
"sendReadReceipt",
|
|
|
|
(event: MatrixEvent, receiptType?: ReceiptType, unthreaded?: boolean): Chainable<{}> => {
|
|
|
|
return cy.getClient().then(async (cli: MatrixClient) => {
|
|
|
|
return cli.sendReadReceipt(event, receiptType, unthreaded);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-07-04 21:37:48 +02:00
|
|
|
Cypress.Commands.add("setDisplayName", (name: string): Chainable<{}> => {
|
|
|
|
return cy.getClient().then(async (cli: MatrixClient) => {
|
|
|
|
return cli.setDisplayName(name);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-12 19:59:07 +02:00
|
|
|
Cypress.Commands.add("uploadContent", (file: FileType, opts?: UploadOpts): Chainable<Awaited<Upload["promise"]>> => {
|
2022-07-04 21:37:48 +02:00
|
|
|
return cy.getClient().then(async (cli: MatrixClient) => {
|
2022-10-12 19:59:07 +02:00
|
|
|
return cli.uploadContent(file, opts);
|
2022-07-04 21:37:48 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Cypress.Commands.add("setAvatarUrl", (url: string): Chainable<{}> => {
|
|
|
|
return cy.getClient().then(async (cli: MatrixClient) => {
|
|
|
|
return cli.setAvatarUrl(url);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-01-19 16:19:07 +01:00
|
|
|
Cypress.Commands.add("bootstrapCrossSigning", (credentials: UserCredentials) => {
|
2022-12-12 12:24:14 +01:00
|
|
|
cy.window({ log: false }).then((win) => {
|
2022-06-30 10:59:25 +02:00
|
|
|
win.mxMatrixClientPeg.matrixClient.bootstrapCrossSigning({
|
2022-12-12 12:24:14 +01:00
|
|
|
authUploadDeviceSigningKeys: async (func) => {
|
2023-01-19 16:19:07 +01:00
|
|
|
await func({
|
|
|
|
type: "m.login.password",
|
|
|
|
identifier: {
|
|
|
|
type: "m.id.user",
|
|
|
|
user: credentials.userId,
|
|
|
|
},
|
|
|
|
password: credentials.password,
|
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
},
|
2022-06-30 10:59:25 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-07-18 14:16:44 +02:00
|
|
|
|
|
|
|
Cypress.Commands.add("joinRoom", (roomIdOrAlias: string): Chainable<Room> => {
|
2022-12-12 12:24:14 +01:00
|
|
|
return cy.getClient().then((cli) => cli.joinRoom(roomIdOrAlias));
|
2022-07-18 14:16:44 +02:00
|
|
|
});
|