2022-05-11 01:09:31 +08: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" />
|
|
|
|
|
|
|
|
import type { ICreateRoomOpts } from "matrix-js-sdk/src/@types/requests";
|
|
|
|
import type { MatrixClient } from "matrix-js-sdk/src/client";
|
|
|
|
import type { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import Chainable = Cypress.Chainable;
|
|
|
|
|
|
|
|
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 17:19:00 +08: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-11 01:09:31 +08: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<{}>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Cypress.Commands.add("getClient", (): Chainable<MatrixClient | undefined> => {
|
2022-05-17 22:38:45 +08:00
|
|
|
return cy.window({ log: false }).then(win => win.mxMatrixClientPeg.matrixClient);
|
2022-05-11 01:09:31 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
Cypress.Commands.add("createRoom", (options: ICreateRoomOpts): Chainable<string> => {
|
2022-05-17 22:38:45 +08:00
|
|
|
return cy.window({ log: false }).then(async win => {
|
2022-05-11 01:09:31 +08:00
|
|
|
const cli = win.mxMatrixClientPeg.matrixClient;
|
|
|
|
const resp = await cli.createRoom(options);
|
|
|
|
const roomId = resp.room_id;
|
|
|
|
|
|
|
|
if (!cli.getRoom(roomId)) {
|
|
|
|
await new Promise<void>(resolve => {
|
|
|
|
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 17:19:00 +08:00
|
|
|
Cypress.Commands.add("createSpace", (options: ICreateRoomOpts): Chainable<string> => {
|
|
|
|
return cy.createRoom({
|
|
|
|
...options,
|
|
|
|
creation_content: {
|
|
|
|
"type": "m.space",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-05-11 01:09:31 +08:00
|
|
|
Cypress.Commands.add("inviteUser", (roomId: string, userId: string): Chainable<{}> => {
|
|
|
|
return cy.getClient().then(async (cli: MatrixClient) => {
|
|
|
|
return cli.invite(roomId, userId);
|
|
|
|
});
|
|
|
|
});
|