2022-05-06 12:09:28 +03: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.
|
|
|
|
*/
|
|
|
|
|
2021-12-17 12:26:32 +03:00
|
|
|
import EventEmitter from "events";
|
2022-03-11 19:03:33 +03:00
|
|
|
import { mocked, MockedObject } from 'jest-mock';
|
2021-10-23 01:23:32 +03:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2022-01-06 13:09:26 +03:00
|
|
|
import { JoinRule } from 'matrix-js-sdk/src/@types/partials';
|
2022-02-23 14:21:11 +03:00
|
|
|
import {
|
|
|
|
Room,
|
|
|
|
User,
|
|
|
|
IContent,
|
|
|
|
IEvent,
|
|
|
|
RoomMember,
|
|
|
|
MatrixClient,
|
|
|
|
EventTimeline,
|
|
|
|
RoomState,
|
|
|
|
EventType,
|
2022-02-23 19:12:48 +03:00
|
|
|
IEventRelation,
|
2022-03-04 18:53:22 +03:00
|
|
|
IUnsigned,
|
2022-09-27 15:35:54 +03:00
|
|
|
IPusher,
|
2022-10-24 11:58:36 +03:00
|
|
|
RoomType,
|
2022-11-10 11:38:48 +03:00
|
|
|
KNOWN_SAFE_ROOM_VERSION,
|
2022-03-01 23:42:05 +03:00
|
|
|
} from 'matrix-js-sdk/src/matrix';
|
2022-07-20 10:26:25 +03:00
|
|
|
import { normalize } from "matrix-js-sdk/src/utils";
|
2022-08-30 22:13:39 +03:00
|
|
|
import { ReEmitter } from "matrix-js-sdk/src/ReEmitter";
|
2022-09-30 19:28:53 +03:00
|
|
|
import { MediaHandler } from "matrix-js-sdk/src/webrtc/mediaHandler";
|
2022-10-13 20:22:25 +03:00
|
|
|
import { Feature, ServerSupport } from "matrix-js-sdk/src/feature";
|
2021-10-23 01:23:32 +03:00
|
|
|
|
2022-11-29 00:37:32 +03:00
|
|
|
import type { GroupCall } from "matrix-js-sdk/src/webrtc/groupCall";
|
2022-02-23 14:21:11 +03:00
|
|
|
import { MatrixClientPeg as peg } from '../../src/MatrixClientPeg';
|
|
|
|
import { makeType } from "../../src/utils/TypeUtils";
|
2022-07-14 16:03:34 +03:00
|
|
|
import { ValidatedServerConfig } from "../../src/utils/ValidatedServerConfig";
|
2022-02-23 14:21:11 +03:00
|
|
|
import { EnhancedMap } from "../../src/utils/maps";
|
2022-03-09 16:23:58 +03:00
|
|
|
import { AsyncStoreWithClient } from "../../src/stores/AsyncStoreWithClient";
|
2022-03-04 01:09:06 +03:00
|
|
|
import MatrixClientBackedSettingsHandler from "../../src/settings/handlers/MatrixClientBackedSettingsHandler";
|
2017-03-16 20:26:42 +03:00
|
|
|
|
2016-03-29 00:59:34 +03:00
|
|
|
/**
|
|
|
|
* Stub out the MatrixClient, and configure the MatrixClientPeg object to
|
|
|
|
* return it when get() is called.
|
2016-04-07 18:47:17 +03:00
|
|
|
*
|
2016-11-14 21:20:15 +03:00
|
|
|
* TODO: once the components are updated to get their MatrixClients from
|
|
|
|
* the react context, we can get rid of this and just inject a test client
|
|
|
|
* via the context instead.
|
2016-03-29 00:59:34 +03:00
|
|
|
*/
|
2022-09-21 19:46:28 +03:00
|
|
|
export function stubClient(): MatrixClient {
|
2017-10-11 19:56:17 +03:00
|
|
|
const client = createTestClient();
|
2016-11-14 21:20:15 +03:00
|
|
|
|
|
|
|
// stub out the methods in MatrixClientPeg
|
|
|
|
//
|
|
|
|
// 'sandbox.restore()' doesn't work correctly on inherited methods,
|
|
|
|
// so we do this for each method
|
2022-03-09 16:23:58 +03:00
|
|
|
jest.spyOn(peg, 'get');
|
|
|
|
jest.spyOn(peg, 'unset');
|
|
|
|
jest.spyOn(peg, 'replaceUsingCreds');
|
2016-11-14 21:20:15 +03:00
|
|
|
// MatrixClientPeg.get() is called a /lot/, so implement it with our own
|
|
|
|
// fast stub function rather than a sinon stub
|
|
|
|
peg.get = function() { return client; };
|
2022-03-04 01:09:06 +03:00
|
|
|
MatrixClientBackedSettingsHandler.matrixClient = client;
|
2022-09-21 19:46:28 +03:00
|
|
|
return client;
|
2016-11-14 21:20:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a stubbed-out MatrixClient
|
|
|
|
*
|
|
|
|
* @returns {object} MatrixClient stub
|
|
|
|
*/
|
2022-03-09 16:23:58 +03:00
|
|
|
export function createTestClient(): MatrixClient {
|
2021-12-17 12:26:32 +03:00
|
|
|
const eventEmitter = new EventEmitter();
|
2022-07-13 08:56:36 +03:00
|
|
|
let txnId = 1;
|
2021-12-17 12:26:32 +03:00
|
|
|
|
2022-08-10 16:26:42 +03:00
|
|
|
const client = {
|
2019-12-16 14:12:48 +03:00
|
|
|
getHomeserverUrl: jest.fn(),
|
|
|
|
getIdentityServerUrl: jest.fn(),
|
2022-07-13 08:56:36 +03:00
|
|
|
getDomain: jest.fn().mockReturnValue("matrix.org"),
|
|
|
|
getUserId: jest.fn().mockReturnValue("@userId:matrix.org"),
|
2022-11-22 09:58:37 +03:00
|
|
|
getUserIdLocalpart: jest.fn().mockResolvedValue("userId"),
|
2021-10-01 16:54:26 +03:00
|
|
|
getUser: jest.fn().mockReturnValue({ on: jest.fn() }),
|
2022-03-28 16:12:09 +03:00
|
|
|
getDeviceId: jest.fn().mockReturnValue("ABCDEFGHI"),
|
2022-11-08 12:02:07 +03:00
|
|
|
deviceId: "ABCDEFGHI",
|
2022-05-16 23:54:08 +03:00
|
|
|
getDevices: jest.fn().mockResolvedValue({ devices: [{ device_id: "ABCDEFGHI" }] }),
|
2022-07-13 08:56:36 +03:00
|
|
|
credentials: { userId: "@userId:matrix.org" },
|
2016-04-08 16:50:04 +03:00
|
|
|
|
2022-06-14 23:29:24 +03:00
|
|
|
store: {
|
|
|
|
getPendingEvents: jest.fn().mockResolvedValue([]),
|
|
|
|
setPendingEvents: jest.fn().mockResolvedValue(undefined),
|
2022-07-13 08:56:36 +03:00
|
|
|
storeRoom: jest.fn(),
|
|
|
|
removeRoom: jest.fn(),
|
2022-06-14 23:29:24 +03:00
|
|
|
},
|
|
|
|
|
2022-08-10 15:57:56 +03:00
|
|
|
crypto: {
|
|
|
|
deviceList: {
|
|
|
|
downloadKeys: jest.fn(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-12-16 14:12:48 +03:00
|
|
|
getPushActionsForEvent: jest.fn(),
|
2022-09-16 18:12:27 +03:00
|
|
|
getRoom: jest.fn().mockImplementation(roomId => mkStubRoom(roomId, "My room", client)),
|
2019-12-16 14:12:48 +03:00
|
|
|
getRooms: jest.fn().mockReturnValue([]),
|
|
|
|
getVisibleRooms: jest.fn().mockReturnValue([]),
|
|
|
|
loginFlows: jest.fn(),
|
2021-12-17 12:26:32 +03:00
|
|
|
on: eventEmitter.on.bind(eventEmitter),
|
2022-02-17 19:30:36 +03:00
|
|
|
off: eventEmitter.off.bind(eventEmitter),
|
2021-12-17 12:26:32 +03:00
|
|
|
removeListener: eventEmitter.removeListener.bind(eventEmitter),
|
2022-02-17 19:30:36 +03:00
|
|
|
emit: eventEmitter.emit.bind(eventEmitter),
|
2019-12-16 14:12:48 +03:00
|
|
|
isRoomEncrypted: jest.fn().mockReturnValue(false),
|
2022-03-09 16:23:58 +03:00
|
|
|
peekInRoom: jest.fn().mockResolvedValue(mkStubRoom(undefined, undefined, undefined)),
|
2022-06-10 15:59:07 +03:00
|
|
|
stopPeeking: jest.fn(),
|
2016-04-08 16:50:04 +03:00
|
|
|
|
2019-12-16 14:12:48 +03:00
|
|
|
paginateEventTimeline: jest.fn().mockResolvedValue(undefined),
|
|
|
|
sendReadReceipt: jest.fn().mockResolvedValue(undefined),
|
|
|
|
getRoomIdForAlias: jest.fn().mockResolvedValue(undefined),
|
|
|
|
getRoomDirectoryVisibility: jest.fn().mockResolvedValue(undefined),
|
|
|
|
getProfileInfo: jest.fn().mockResolvedValue({}),
|
2021-04-23 16:39:39 +03:00
|
|
|
getThirdpartyProtocols: jest.fn().mockResolvedValue({}),
|
|
|
|
getClientWellKnown: jest.fn().mockReturnValue(null),
|
|
|
|
supportsVoip: jest.fn().mockReturnValue(true),
|
2022-08-10 16:26:42 +03:00
|
|
|
getTurnServers: jest.fn().mockReturnValue([]),
|
2022-03-09 16:23:58 +03:00
|
|
|
getTurnServersExpiry: jest.fn().mockReturnValue(2 ^ 32),
|
2021-04-23 16:39:39 +03:00
|
|
|
getThirdpartyUser: jest.fn().mockResolvedValue([]),
|
2016-09-09 15:37:42 +03:00
|
|
|
getAccountData: (type) => {
|
|
|
|
return mkEvent({
|
2022-03-09 16:23:58 +03:00
|
|
|
user: undefined,
|
|
|
|
room: undefined,
|
2016-09-09 15:37:42 +03:00
|
|
|
type,
|
|
|
|
event: true,
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
},
|
2021-10-01 16:54:26 +03:00
|
|
|
mxcUrlToHttp: (mxc) => `http://this.is.a.url/${mxc.substring(6)}`,
|
2019-12-16 14:12:48 +03:00
|
|
|
setAccountData: jest.fn(),
|
2021-08-04 11:46:39 +03:00
|
|
|
setRoomAccountData: jest.fn(),
|
2022-06-07 23:20:32 +03:00
|
|
|
setRoomTopic: jest.fn(),
|
2022-08-05 18:33:57 +03:00
|
|
|
setRoomReadMarkers: jest.fn().mockResolvedValue({}),
|
2019-12-16 14:12:48 +03:00
|
|
|
sendTyping: jest.fn().mockResolvedValue({}),
|
2022-07-13 08:56:36 +03:00
|
|
|
sendMessage: jest.fn().mockResolvedValue({}),
|
2022-03-09 16:23:58 +03:00
|
|
|
sendStateEvent: jest.fn().mockResolvedValue(undefined),
|
2022-10-26 19:59:47 +03:00
|
|
|
getSyncState: jest.fn().mockReturnValue("SYNCING"),
|
2017-02-24 14:41:23 +03:00
|
|
|
generateClientSecret: () => "t35tcl1Ent5ECr3T",
|
2022-03-04 12:39:16 +03:00
|
|
|
isGuest: jest.fn().mockReturnValue(false),
|
2021-07-29 19:35:15 +03:00
|
|
|
getRoomHierarchy: jest.fn().mockReturnValue({
|
2021-04-23 16:45:22 +03:00
|
|
|
rooms: [],
|
|
|
|
}),
|
2022-04-07 14:34:07 +03:00
|
|
|
createRoom: jest.fn().mockResolvedValue({ room_id: "!1:example.org" }),
|
|
|
|
setPowerLevel: jest.fn().mockResolvedValue(undefined),
|
2021-08-03 16:43:56 +03:00
|
|
|
pushRules: {},
|
2021-05-18 15:46:47 +03:00
|
|
|
decryptEventIfNeeded: () => Promise.resolve(),
|
2021-07-15 20:17:07 +03:00
|
|
|
isUserIgnored: jest.fn().mockReturnValue(false),
|
2021-07-06 12:34:50 +03:00
|
|
|
getCapabilities: jest.fn().mockResolvedValue({}),
|
2021-10-01 16:54:26 +03:00
|
|
|
supportsExperimentalThreads: () => false,
|
2021-10-08 12:30:46 +03:00
|
|
|
getRoomUpgradeHistory: jest.fn().mockReturnValue([]),
|
2022-03-09 16:23:58 +03:00
|
|
|
getOpenIdToken: jest.fn().mockResolvedValue(undefined),
|
2021-12-09 13:48:58 +03:00
|
|
|
registerWithIdentityServer: jest.fn().mockResolvedValue({}),
|
|
|
|
getIdentityAccount: jest.fn().mockResolvedValue({}),
|
2022-10-12 20:59:07 +03:00
|
|
|
getTerms: jest.fn().mockResolvedValue({ policies: [] }),
|
2022-03-09 16:23:58 +03:00
|
|
|
doesServerSupportUnstableFeature: jest.fn().mockResolvedValue(undefined),
|
2022-10-07 16:10:18 +03:00
|
|
|
isVersionSupported: jest.fn().mockResolvedValue(undefined),
|
2022-03-09 16:23:58 +03:00
|
|
|
getPushRules: jest.fn().mockResolvedValue(undefined),
|
2022-01-06 12:47:03 +03:00
|
|
|
getPushers: jest.fn().mockResolvedValue({ pushers: [] }),
|
|
|
|
getThreePids: jest.fn().mockResolvedValue({ threepids: [] }),
|
2022-03-09 16:23:58 +03:00
|
|
|
setPusher: jest.fn().mockResolvedValue(undefined),
|
|
|
|
setPushRuleEnabled: jest.fn().mockResolvedValue(undefined),
|
|
|
|
setPushRuleActions: jest.fn().mockResolvedValue(undefined),
|
2022-11-10 13:53:49 +03:00
|
|
|
relations: jest.fn().mockResolvedValue({
|
|
|
|
events: [],
|
|
|
|
}),
|
2022-03-04 18:53:22 +03:00
|
|
|
isCryptoEnabled: jest.fn().mockReturnValue(false),
|
2022-06-10 15:59:07 +03:00
|
|
|
hasLazyLoadMembersEnabled: jest.fn().mockReturnValue(false),
|
|
|
|
isInitialSyncComplete: jest.fn().mockReturnValue(true),
|
2022-04-07 14:34:07 +03:00
|
|
|
downloadKeys: jest.fn(),
|
2022-03-21 12:03:03 +03:00
|
|
|
fetchRoomEvent: jest.fn(),
|
2022-07-13 08:56:36 +03:00
|
|
|
makeTxnId: jest.fn().mockImplementation(() => `t${txnId++}`),
|
2022-08-10 15:57:56 +03:00
|
|
|
sendToDevice: jest.fn().mockResolvedValue(undefined),
|
|
|
|
queueToDevice: jest.fn().mockResolvedValue(undefined),
|
|
|
|
encryptAndSendToDevices: jest.fn().mockResolvedValue(undefined),
|
2022-10-21 20:26:33 +03:00
|
|
|
cancelPendingEvent: jest.fn(),
|
2022-09-30 19:28:53 +03:00
|
|
|
|
|
|
|
getMediaHandler: jest.fn().mockReturnValue({
|
|
|
|
setVideoInput: jest.fn(),
|
|
|
|
setAudioInput: jest.fn(),
|
2022-11-09 23:14:55 +03:00
|
|
|
setAudioSettings: jest.fn(),
|
2022-11-29 00:37:32 +03:00
|
|
|
stopAllStreams: jest.fn(),
|
2022-09-30 19:28:53 +03:00
|
|
|
} as unknown as MediaHandler),
|
2022-10-12 20:59:07 +03:00
|
|
|
uploadContent: jest.fn(),
|
2022-10-14 17:57:12 +03:00
|
|
|
getEventMapper: () => (opts) => new MatrixEvent(opts),
|
2022-11-07 16:45:34 +03:00
|
|
|
leaveRoomChain: jest.fn(roomId => ({ [roomId]: null })),
|
2022-11-22 09:58:37 +03:00
|
|
|
doesServerSupportLogoutDevices: jest.fn().mockReturnValue(true),
|
|
|
|
requestPasswordEmailToken: jest.fn().mockRejectedValue({}),
|
|
|
|
setPassword: jest.fn().mockRejectedValue({}),
|
2022-11-29 00:37:32 +03:00
|
|
|
groupCallEventHandler: { groupCalls: new Map<string, GroupCall>() },
|
2022-03-09 16:23:58 +03:00
|
|
|
} as unknown as MatrixClient;
|
2022-08-10 16:26:42 +03:00
|
|
|
|
2022-08-30 22:13:39 +03:00
|
|
|
client.reEmitter = new ReEmitter(client);
|
|
|
|
|
2022-10-13 20:22:25 +03:00
|
|
|
client.canSupport = new Map();
|
|
|
|
Object.keys(Feature).forEach(feature => {
|
|
|
|
client.canSupport.set(feature as Feature, ServerSupport.Stable);
|
|
|
|
});
|
|
|
|
|
2022-08-10 16:26:42 +03:00
|
|
|
Object.defineProperty(client, "pollingTurnServers", {
|
|
|
|
configurable: true,
|
|
|
|
get: () => true,
|
|
|
|
});
|
|
|
|
return client;
|
2016-03-29 00:59:34 +03:00
|
|
|
}
|
|
|
|
|
2022-02-23 14:21:11 +03:00
|
|
|
type MakeEventPassThruProps = {
|
|
|
|
user: User["userId"];
|
2022-03-09 16:23:58 +03:00
|
|
|
relatesTo?: IEventRelation;
|
2022-02-23 14:21:11 +03:00
|
|
|
event?: boolean;
|
|
|
|
ts?: number;
|
|
|
|
skey?: string;
|
|
|
|
};
|
|
|
|
type MakeEventProps = MakeEventPassThruProps & {
|
|
|
|
type: string;
|
2022-10-28 14:48:15 +03:00
|
|
|
redacts?: string;
|
2022-02-23 14:21:11 +03:00
|
|
|
content: IContent;
|
2022-08-10 15:57:56 +03:00
|
|
|
room?: Room["roomId"]; // to-device messages are roomless
|
2022-02-23 14:21:11 +03:00
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
prev_content?: IContent;
|
2022-03-04 18:53:22 +03:00
|
|
|
unsigned?: IUnsigned;
|
2022-02-23 14:21:11 +03:00
|
|
|
};
|
|
|
|
|
2022-11-10 11:38:48 +03:00
|
|
|
export const mkRoomCreateEvent = (userId: string, roomId: string): MatrixEvent => {
|
|
|
|
return mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomCreate,
|
|
|
|
content: {
|
|
|
|
creator: userId,
|
|
|
|
room_version: KNOWN_SAFE_ROOM_VERSION,
|
|
|
|
},
|
|
|
|
skey: "",
|
|
|
|
user: userId,
|
|
|
|
room: roomId,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-03-31 02:48:46 +03:00
|
|
|
/**
|
|
|
|
* Create an Event.
|
|
|
|
* @param {Object} opts Values for the event.
|
|
|
|
* @param {string} opts.type The event.type
|
|
|
|
* @param {string} opts.room The event.room_id
|
|
|
|
* @param {string} opts.user The event.user_id
|
2021-04-22 16:45:13 +03:00
|
|
|
* @param {string=} opts.skey Optional. The state key (auto inserts empty string)
|
|
|
|
* @param {number=} opts.ts Optional. Timestamp for the event
|
2016-03-31 02:48:46 +03:00
|
|
|
* @param {Object} opts.content The event.content
|
|
|
|
* @param {boolean} opts.event True to make a MatrixEvent.
|
2021-08-10 09:55:11 +03:00
|
|
|
* @param {unsigned=} opts.unsigned
|
2016-03-31 02:48:46 +03:00
|
|
|
* @return {Object} a JSON object representing this event.
|
|
|
|
*/
|
2022-02-23 14:21:11 +03:00
|
|
|
export function mkEvent(opts: MakeEventProps): MatrixEvent {
|
2016-03-31 02:48:46 +03:00
|
|
|
if (!opts.type || !opts.content) {
|
|
|
|
throw new Error("Missing .type or .content =>" + JSON.stringify(opts));
|
|
|
|
}
|
2022-02-23 14:21:11 +03:00
|
|
|
const event: Partial<IEvent> = {
|
2016-03-31 02:48:46 +03:00
|
|
|
type: opts.type,
|
|
|
|
room_id: opts.room,
|
|
|
|
sender: opts.user,
|
|
|
|
content: opts.content,
|
2017-01-18 13:53:17 +03:00
|
|
|
prev_content: opts.prev_content,
|
2016-03-31 02:48:46 +03:00
|
|
|
event_id: "$" + Math.random() + "-" + Math.random(),
|
2022-03-03 20:20:13 +03:00
|
|
|
origin_server_ts: opts.ts ?? 0,
|
2022-03-03 15:54:44 +03:00
|
|
|
unsigned: opts.unsigned,
|
2022-10-28 14:48:15 +03:00
|
|
|
redacts: opts.redacts,
|
2016-03-31 02:48:46 +03:00
|
|
|
};
|
2022-03-04 18:53:22 +03:00
|
|
|
if (opts.skey !== undefined) {
|
2016-03-31 02:48:46 +03:00
|
|
|
event.state_key = opts.skey;
|
2021-10-01 16:54:26 +03:00
|
|
|
} else if ([
|
|
|
|
"m.room.name", "m.room.topic", "m.room.create", "m.room.join_rules",
|
|
|
|
"m.room.power_levels", "m.room.topic", "m.room.history_visibility",
|
|
|
|
"m.room.encryption", "m.room.member", "com.example.state",
|
2022-03-04 18:53:22 +03:00
|
|
|
"m.room.guest_access", "m.room.tombstone",
|
2021-10-01 16:54:26 +03:00
|
|
|
].indexOf(opts.type) !== -1) {
|
2016-03-31 02:48:46 +03:00
|
|
|
event.state_key = "";
|
|
|
|
}
|
2022-03-27 01:06:25 +03:00
|
|
|
|
|
|
|
const mxEvent = opts.event ? new MatrixEvent(event) : event as unknown as MatrixEvent;
|
|
|
|
if (!mxEvent.sender && opts.user && opts.room) {
|
|
|
|
mxEvent.sender = {
|
|
|
|
userId: opts.user,
|
|
|
|
membership: "join",
|
|
|
|
name: opts.user,
|
|
|
|
rawDisplayName: opts.user,
|
|
|
|
roomId: opts.room,
|
|
|
|
getAvatarUrl: () => {},
|
|
|
|
getMxcAvatarUrl: () => {},
|
|
|
|
} as unknown as RoomMember;
|
|
|
|
}
|
|
|
|
return mxEvent;
|
2017-10-11 19:56:17 +03:00
|
|
|
}
|
2016-03-31 02:48:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an m.presence event.
|
|
|
|
* @param {Object} opts Values for the presence.
|
|
|
|
* @return {Object|MatrixEvent} The event
|
|
|
|
*/
|
2016-09-09 15:37:42 +03:00
|
|
|
export function mkPresence(opts) {
|
2016-03-31 02:48:46 +03:00
|
|
|
if (!opts.user) {
|
|
|
|
throw new Error("Missing user");
|
|
|
|
}
|
2017-10-11 19:56:17 +03:00
|
|
|
const event = {
|
2016-03-31 02:48:46 +03:00
|
|
|
event_id: "$" + Math.random() + "-" + Math.random(),
|
|
|
|
type: "m.presence",
|
|
|
|
sender: opts.user,
|
|
|
|
content: {
|
|
|
|
avatar_url: opts.url,
|
|
|
|
displayname: opts.name,
|
|
|
|
last_active_ago: opts.ago,
|
2017-10-11 19:56:17 +03:00
|
|
|
presence: opts.presence || "offline",
|
|
|
|
},
|
2016-03-31 02:48:46 +03:00
|
|
|
};
|
|
|
|
return opts.event ? new MatrixEvent(event) : event;
|
2017-10-11 19:56:17 +03:00
|
|
|
}
|
2016-03-31 02:48:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an m.room.member event.
|
|
|
|
* @param {Object} opts Values for the membership.
|
|
|
|
* @param {string} opts.room The room ID for the event.
|
|
|
|
* @param {string} opts.mship The content.membership for the event.
|
2017-01-18 13:53:17 +03:00
|
|
|
* @param {string} opts.prevMship The prev_content.membership for the event.
|
2021-08-10 09:55:11 +03:00
|
|
|
* @param {number=} opts.ts Optional. Timestamp for the event
|
2016-03-31 02:48:46 +03:00
|
|
|
* @param {string} opts.user The user ID for the event.
|
2017-01-18 13:53:17 +03:00
|
|
|
* @param {RoomMember} opts.target The target of the event.
|
2021-08-10 09:55:11 +03:00
|
|
|
* @param {string=} opts.skey The other user ID for the event if applicable
|
2016-03-31 02:48:46 +03:00
|
|
|
* e.g. for invites/bans.
|
|
|
|
* @param {string} opts.name The content.displayname for the event.
|
2021-08-10 09:55:11 +03:00
|
|
|
* @param {string=} opts.url The content.avatar_url for the event.
|
2016-03-31 02:48:46 +03:00
|
|
|
* @param {boolean} opts.event True to make a MatrixEvent.
|
|
|
|
* @return {Object|MatrixEvent} The event
|
|
|
|
*/
|
2022-02-23 14:21:11 +03:00
|
|
|
export function mkMembership(opts: MakeEventPassThruProps & {
|
|
|
|
room: Room["roomId"];
|
|
|
|
mship: string;
|
|
|
|
prevMship?: string;
|
|
|
|
name?: string;
|
|
|
|
url?: string;
|
|
|
|
skey?: string;
|
|
|
|
target?: RoomMember;
|
|
|
|
}): MatrixEvent {
|
|
|
|
const event: MakeEventProps = {
|
|
|
|
...opts,
|
|
|
|
type: "m.room.member",
|
|
|
|
content: {
|
|
|
|
membership: opts.mship,
|
|
|
|
},
|
|
|
|
};
|
2016-03-31 02:48:46 +03:00
|
|
|
if (!opts.skey) {
|
2022-02-23 14:21:11 +03:00
|
|
|
event.skey = opts.user;
|
2016-03-31 02:48:46 +03:00
|
|
|
}
|
|
|
|
if (!opts.mship) {
|
|
|
|
throw new Error("Missing .mship => " + JSON.stringify(opts));
|
|
|
|
}
|
2022-02-23 14:21:11 +03:00
|
|
|
|
2017-01-18 13:53:17 +03:00
|
|
|
if (opts.prevMship) {
|
2022-02-23 14:21:11 +03:00
|
|
|
event.prev_content = { membership: opts.prevMship };
|
2017-01-18 13:53:17 +03:00
|
|
|
}
|
2022-02-23 14:21:11 +03:00
|
|
|
if (opts.name) { event.content.displayname = opts.name; }
|
|
|
|
if (opts.url) { event.content.avatar_url = opts.url; }
|
|
|
|
const e = mkEvent(event);
|
2017-01-18 13:53:17 +03:00
|
|
|
if (opts.target) {
|
|
|
|
e.target = opts.target;
|
|
|
|
}
|
|
|
|
return e;
|
2017-10-11 19:56:17 +03:00
|
|
|
}
|
2016-03-31 02:48:46 +03:00
|
|
|
|
2022-08-30 22:13:39 +03:00
|
|
|
export function mkRoomMember(roomId: string, userId: string, membership = "join"): RoomMember {
|
|
|
|
return {
|
|
|
|
userId,
|
|
|
|
membership,
|
|
|
|
name: userId,
|
|
|
|
rawDisplayName: userId,
|
|
|
|
roomId,
|
2022-09-16 18:12:27 +03:00
|
|
|
events: {},
|
2022-08-30 22:13:39 +03:00
|
|
|
getAvatarUrl: () => {},
|
|
|
|
getMxcAvatarUrl: () => {},
|
2022-09-16 18:12:27 +03:00
|
|
|
getDMInviter: () => {},
|
2022-08-30 22:13:39 +03:00
|
|
|
} as unknown as RoomMember;
|
|
|
|
}
|
|
|
|
|
2022-02-23 19:12:48 +03:00
|
|
|
export type MessageEventProps = MakeEventPassThruProps & {
|
|
|
|
room: Room["roomId"];
|
|
|
|
relatesTo?: IEventRelation;
|
|
|
|
msg?: string;
|
|
|
|
};
|
|
|
|
|
2016-03-31 02:48:46 +03:00
|
|
|
/**
|
|
|
|
* Create an m.room.message event.
|
|
|
|
* @param {Object} opts Values for the message
|
|
|
|
* @param {string} opts.room The room ID for the event.
|
|
|
|
* @param {string} opts.user The user ID for the event.
|
2021-08-03 12:06:21 +03:00
|
|
|
* @param {number} opts.ts The timestamp for the event.
|
2016-03-31 02:48:46 +03:00
|
|
|
* @param {boolean} opts.event True to make a MatrixEvent.
|
2021-08-03 12:06:21 +03:00
|
|
|
* @param {string=} opts.msg Optional. The content.body for the event.
|
2016-03-31 02:48:46 +03:00
|
|
|
* @return {Object|MatrixEvent} The event
|
|
|
|
*/
|
2022-03-09 16:23:58 +03:00
|
|
|
export function mkMessage({ msg, relatesTo, ...opts }: MakeEventPassThruProps & {
|
|
|
|
room: Room["roomId"];
|
|
|
|
msg?: string;
|
|
|
|
}): MatrixEvent {
|
2016-03-31 02:48:46 +03:00
|
|
|
if (!opts.room || !opts.user) {
|
2022-02-23 14:21:11 +03:00
|
|
|
throw new Error("Missing .room or .user from options");
|
2016-03-31 02:48:46 +03:00
|
|
|
}
|
2022-02-23 19:12:48 +03:00
|
|
|
const message = msg ?? "Random->" + Math.random();
|
2022-02-23 14:21:11 +03:00
|
|
|
const event: MakeEventProps = {
|
|
|
|
...opts,
|
|
|
|
type: "m.room.message",
|
|
|
|
content: {
|
|
|
|
msgtype: "m.text",
|
|
|
|
body: message,
|
2022-02-23 19:12:48 +03:00
|
|
|
['m.relates_to']: relatesTo,
|
2022-02-23 14:21:11 +03:00
|
|
|
},
|
2016-03-31 02:48:46 +03:00
|
|
|
};
|
2022-02-23 14:21:11 +03:00
|
|
|
|
|
|
|
return mkEvent(event);
|
2016-09-09 15:37:42 +03:00
|
|
|
}
|
2016-06-17 14:20:26 +03:00
|
|
|
|
2022-03-09 16:23:58 +03:00
|
|
|
export function mkStubRoom(roomId: string = null, name: string, client: MatrixClient): Room {
|
2022-02-23 14:21:11 +03:00
|
|
|
const stubTimeline = { getEvents: () => [] } as unknown as EventTimeline;
|
2016-06-17 14:20:26 +03:00
|
|
|
return {
|
2016-09-09 15:37:42 +03:00
|
|
|
roomId,
|
2019-12-16 14:12:48 +03:00
|
|
|
getReceiptsForEvent: jest.fn().mockReturnValue([]),
|
|
|
|
getMember: jest.fn().mockReturnValue({
|
2017-07-24 16:42:20 +03:00
|
|
|
userId: '@member:domain.bla',
|
|
|
|
name: 'Member',
|
2020-01-06 16:28:29 +03:00
|
|
|
rawDisplayName: 'Member',
|
2017-07-24 16:42:20 +03:00
|
|
|
roomId: roomId,
|
|
|
|
getAvatarUrl: () => 'mxc://avatar.url/image.png',
|
2021-03-11 19:42:55 +03:00
|
|
|
getMxcAvatarUrl: () => 'mxc://avatar.url/image.png',
|
2017-07-24 16:42:20 +03:00
|
|
|
}),
|
2019-12-16 14:12:48 +03:00
|
|
|
getMembersWithMembership: jest.fn().mockReturnValue([]),
|
|
|
|
getJoinedMembers: jest.fn().mockReturnValue([]),
|
2021-07-06 13:35:56 +03:00
|
|
|
getJoinedMemberCount: jest.fn().mockReturnValue(1),
|
2022-08-05 18:33:57 +03:00
|
|
|
getInvitedAndJoinedMemberCount: jest.fn().mockReturnValue(1),
|
|
|
|
setUnreadNotificationCount: jest.fn(),
|
2021-05-19 14:34:27 +03:00
|
|
|
getMembers: jest.fn().mockReturnValue([]),
|
2016-10-10 19:51:26 +03:00
|
|
|
getPendingEvents: () => [],
|
2022-06-10 15:59:07 +03:00
|
|
|
getLiveTimeline: jest.fn().mockReturnValue(stubTimeline),
|
2022-09-28 11:22:50 +03:00
|
|
|
getUnfilteredTimelineSet: jest.fn(),
|
2021-05-10 07:54:00 +03:00
|
|
|
findEventById: () => null,
|
2016-10-10 19:51:26 +03:00
|
|
|
getAccountData: () => null,
|
|
|
|
hasMembershipState: () => null,
|
2018-08-15 14:22:26 +03:00
|
|
|
getVersion: () => '1',
|
2018-08-17 17:15:53 +03:00
|
|
|
shouldUpgradeToVersion: () => null,
|
2021-04-23 14:19:08 +03:00
|
|
|
getMyMembership: jest.fn().mockReturnValue("join"),
|
2019-12-16 14:12:48 +03:00
|
|
|
maySendMessage: jest.fn().mockReturnValue(true),
|
2016-06-17 14:20:26 +03:00
|
|
|
currentState: {
|
2022-11-29 00:37:32 +03:00
|
|
|
getStateEvents: jest.fn((_type, key) => key === undefined ? [] : null),
|
2021-05-19 12:31:05 +03:00
|
|
|
getMember: jest.fn(),
|
2019-12-16 14:12:48 +03:00
|
|
|
mayClientSendStateEvent: jest.fn().mockReturnValue(true),
|
|
|
|
maySendStateEvent: jest.fn().mockReturnValue(true),
|
2022-03-05 02:15:03 +03:00
|
|
|
maySendRedactionForEvent: jest.fn().mockReturnValue(true),
|
2019-12-16 14:12:48 +03:00
|
|
|
maySendEvent: jest.fn().mockReturnValue(true),
|
2022-02-23 14:21:11 +03:00
|
|
|
members: {},
|
2022-01-06 13:09:26 +03:00
|
|
|
getJoinRule: jest.fn().mockReturnValue(JoinRule.Invite),
|
|
|
|
on: jest.fn(),
|
2022-07-29 12:32:03 +03:00
|
|
|
off: jest.fn(),
|
2022-02-23 14:21:11 +03:00
|
|
|
} as unknown as RoomState,
|
2021-04-23 14:19:08 +03:00
|
|
|
tags: {},
|
2019-12-16 14:12:48 +03:00
|
|
|
setBlacklistUnverifiedDevices: jest.fn(),
|
|
|
|
on: jest.fn(),
|
2021-05-16 15:39:22 +03:00
|
|
|
off: jest.fn(),
|
2019-12-16 14:12:48 +03:00
|
|
|
removeListener: jest.fn(),
|
2020-11-05 19:27:41 +03:00
|
|
|
getDMInviter: jest.fn(),
|
2021-05-09 02:51:51 +03:00
|
|
|
name,
|
2022-07-20 10:26:25 +03:00
|
|
|
normalizedName: normalize(name || ""),
|
2021-02-03 18:18:19 +03:00
|
|
|
getAvatarUrl: () => 'mxc://avatar.url/room.png',
|
2021-03-11 19:42:55 +03:00
|
|
|
getMxcAvatarUrl: () => 'mxc://avatar.url/room.png',
|
2022-03-23 01:14:11 +03:00
|
|
|
isSpaceRoom: jest.fn().mockReturnValue(false),
|
2022-10-24 11:58:36 +03:00
|
|
|
getType: jest.fn().mockReturnValue(undefined),
|
2022-04-04 17:29:40 +03:00
|
|
|
isElementVideoRoom: jest.fn().mockReturnValue(false),
|
2021-04-23 14:19:08 +03:00
|
|
|
getUnreadNotificationCount: jest.fn(() => 0),
|
|
|
|
getEventReadUpTo: jest.fn(() => null),
|
2021-06-02 03:36:28 +03:00
|
|
|
getCanonicalAlias: jest.fn(),
|
|
|
|
getAltAliases: jest.fn().mockReturnValue([]),
|
2021-04-23 14:19:08 +03:00
|
|
|
timeline: [],
|
2021-07-06 12:44:09 +03:00
|
|
|
getJoinRule: jest.fn().mockReturnValue("invite"),
|
2022-03-01 11:33:29 +03:00
|
|
|
loadMembersIfNeeded: jest.fn(),
|
2021-07-06 13:35:56 +03:00
|
|
|
client,
|
2022-04-21 14:41:38 +03:00
|
|
|
myUserId: client?.getUserId(),
|
2022-04-12 17:23:04 +03:00
|
|
|
canInvite: jest.fn(),
|
2022-05-03 16:25:08 +03:00
|
|
|
getThreads: jest.fn().mockReturnValue([]),
|
2022-08-05 18:33:57 +03:00
|
|
|
eventShouldLiveIn: jest.fn().mockReturnValue({}),
|
2022-09-22 17:08:14 +03:00
|
|
|
createThreadsTimelineSets: jest.fn().mockReturnValue(new Promise(() => {})),
|
|
|
|
fetchRoomThreads: jest.fn().mockReturnValue(new Promise(() => {})),
|
2022-02-23 14:21:11 +03:00
|
|
|
} as unknown as Room;
|
2016-09-09 15:37:42 +03:00
|
|
|
}
|
2017-05-24 18:56:13 +03:00
|
|
|
|
2019-05-03 08:46:43 +03:00
|
|
|
export function mkServerConfig(hsUrl, isUrl) {
|
|
|
|
return makeType(ValidatedServerConfig, {
|
|
|
|
hsUrl,
|
|
|
|
hsName: "TEST_ENVIRONMENT",
|
|
|
|
hsNameIsDifferent: false, // yes, we lie
|
|
|
|
isUrl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-23 14:21:11 +03:00
|
|
|
// These methods make some use of some private methods on the AsyncStoreWithClient to simplify getting into a consistent
|
|
|
|
// ready state without needing to wire up a dispatcher and pretend to be a js-sdk client.
|
2018-02-06 20:50:53 +03:00
|
|
|
|
2022-03-09 16:23:58 +03:00
|
|
|
export const setupAsyncStoreWithClient = async <T = unknown>(store: AsyncStoreWithClient<T>, client: MatrixClient) => {
|
2022-02-23 14:21:11 +03:00
|
|
|
// @ts-ignore
|
|
|
|
store.readyStore.useUnitTestClient(client);
|
|
|
|
// @ts-ignore
|
|
|
|
await store.onReady();
|
|
|
|
};
|
|
|
|
|
2022-03-09 16:23:58 +03:00
|
|
|
export const resetAsyncStoreWithClient = async <T = unknown>(store: AsyncStoreWithClient<T>) => {
|
2022-02-23 14:21:11 +03:00
|
|
|
// @ts-ignore
|
|
|
|
await store.onNotReady();
|
|
|
|
};
|
|
|
|
|
2022-03-09 16:23:58 +03:00
|
|
|
export const mockStateEventImplementation = (events: MatrixEvent[]) => {
|
2022-02-23 14:21:11 +03:00
|
|
|
const stateMap = new EnhancedMap<string, Map<string, MatrixEvent>>();
|
|
|
|
events.forEach(event => {
|
|
|
|
stateMap.getOrCreate(event.getType(), new Map()).set(event.getStateKey(), event);
|
|
|
|
});
|
2018-02-06 20:50:53 +03:00
|
|
|
|
2022-03-09 16:23:58 +03:00
|
|
|
// recreate the overloading in RoomState
|
|
|
|
function getStateEvents(eventType: EventType | string): MatrixEvent[];
|
|
|
|
function getStateEvents(eventType: EventType | string, stateKey: string): MatrixEvent;
|
|
|
|
function getStateEvents(eventType: EventType | string, stateKey?: string) {
|
2022-02-23 14:21:11 +03:00
|
|
|
if (stateKey || stateKey === "") {
|
|
|
|
return stateMap.get(eventType)?.get(stateKey) || null;
|
2018-02-06 20:50:53 +03:00
|
|
|
}
|
2022-02-23 14:21:11 +03:00
|
|
|
return Array.from(stateMap.get(eventType)?.values() || []);
|
2022-03-09 16:23:58 +03:00
|
|
|
}
|
|
|
|
return getStateEvents;
|
2022-02-23 14:21:11 +03:00
|
|
|
};
|
|
|
|
|
2022-03-11 19:03:33 +03:00
|
|
|
export const mkRoom = (
|
|
|
|
client: MatrixClient,
|
|
|
|
roomId: string,
|
|
|
|
rooms?: ReturnType<typeof mkStubRoom>[],
|
|
|
|
): MockedObject<Room> => {
|
|
|
|
const room = mocked(mkStubRoom(roomId, roomId, client));
|
2022-02-23 14:21:11 +03:00
|
|
|
mocked(room.currentState).getStateEvents.mockImplementation(mockStateEventImplementation([]));
|
|
|
|
rooms?.push(room);
|
|
|
|
return room;
|
|
|
|
};
|
2018-05-02 13:19:01 +03:00
|
|
|
|
|
|
|
/**
|
2022-02-23 14:21:11 +03:00
|
|
|
* Upserts given events into room.currentState
|
|
|
|
* @param room
|
|
|
|
* @param events
|
2018-05-02 13:19:01 +03:00
|
|
|
*/
|
2022-02-23 14:21:11 +03:00
|
|
|
export const upsertRoomStateEvents = (room: Room, events: MatrixEvent[]): void => {
|
|
|
|
const eventsMap = events.reduce((acc, event) => {
|
|
|
|
const eventType = event.getType();
|
|
|
|
if (!acc.has(eventType)) {
|
|
|
|
acc.set(eventType, new Map());
|
|
|
|
}
|
|
|
|
acc.get(eventType).set(event.getStateKey(), event);
|
|
|
|
return acc;
|
|
|
|
}, room.currentState.events || new Map<string, Map<string, MatrixEvent>>());
|
2018-05-02 13:19:01 +03:00
|
|
|
|
2022-02-23 14:21:11 +03:00
|
|
|
room.currentState.events = eventsMap;
|
|
|
|
};
|
2018-05-02 13:19:01 +03:00
|
|
|
|
2022-02-23 14:21:11 +03:00
|
|
|
export const mkSpace = (
|
|
|
|
client: MatrixClient,
|
|
|
|
spaceId: string,
|
|
|
|
rooms?: ReturnType<typeof mkStubRoom>[],
|
|
|
|
children: string[] = [],
|
2022-03-11 19:03:33 +03:00
|
|
|
): MockedObject<Room> => {
|
|
|
|
const space = mocked(mkRoom(client, spaceId, rooms));
|
|
|
|
space.isSpaceRoom.mockReturnValue(true);
|
2022-10-24 11:58:36 +03:00
|
|
|
space.getType.mockReturnValue(RoomType.Space);
|
2022-02-23 14:21:11 +03:00
|
|
|
mocked(space.currentState).getStateEvents.mockImplementation(mockStateEventImplementation(children.map(roomId =>
|
|
|
|
mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.SpaceChild,
|
|
|
|
room: spaceId,
|
|
|
|
user: "@user:server",
|
|
|
|
skey: roomId,
|
|
|
|
content: { via: [] },
|
|
|
|
ts: Date.now(),
|
|
|
|
}),
|
|
|
|
)));
|
|
|
|
return space;
|
|
|
|
};
|
2022-09-27 15:35:54 +03:00
|
|
|
|
2022-11-10 11:38:48 +03:00
|
|
|
export const mkRoomMemberJoinEvent = (user: string, room: string): MatrixEvent => {
|
|
|
|
return mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMember,
|
|
|
|
content: {
|
|
|
|
membership: "join",
|
|
|
|
},
|
|
|
|
skey: user,
|
|
|
|
user,
|
|
|
|
room,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-09-27 15:35:54 +03:00
|
|
|
export const mkPusher = (extra: Partial<IPusher> = {}): IPusher => ({
|
|
|
|
app_display_name: "app",
|
|
|
|
app_id: "123",
|
|
|
|
data: {},
|
|
|
|
device_display_name: "name",
|
|
|
|
kind: "http",
|
|
|
|
lang: "en",
|
|
|
|
pushkey: "pushpush",
|
|
|
|
...extra,
|
|
|
|
});
|