Thread test utilities (#7881)

* move js utils into directory

Signed-off-by: Kerry Archibald <kerrya@element.io>

* typescripterize js test-utils

Signed-off-by: Kerry Archibald <kerrya@element.io>

* move test utils to directory

Signed-off-by: Kerry Archibald <kerrya@element.io>

* move remaining mock functions to directory

Signed-off-by: Kerry Archibald <kerrya@element.io>

* update imports

Signed-off-by: Kerry Archibald <kerrya@element.io>

* missed copyright

Signed-off-by: Kerry Archibald <kerrya@element.io>

* threads test helpers

Signed-off-by: Kerry Archibald <kerrya@element.io>

* forgotten copyright

Signed-off-by: Kerry Archibald <kerrya@element.io>

* comments

Signed-off-by: Kerry Archibald <kerrya@element.io>

* add unsigned to root event

Signed-off-by: Kerry Archibald <kerrya@element.io>
pull/21833/head
Kerry 2022-02-23 17:12:48 +01:00 committed by GitHub
parent 49bf0abeb5
commit 1830de2733
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 105 additions and 5 deletions

View File

@ -12,6 +12,7 @@ import {
EventTimeline, EventTimeline,
RoomState, RoomState,
EventType, EventType,
IEventRelation,
} from 'matrix-js-sdk'; } from 'matrix-js-sdk';
import { MatrixClientPeg as peg } from '../../src/MatrixClientPeg'; import { MatrixClientPeg as peg } from '../../src/MatrixClientPeg';
@ -259,6 +260,12 @@ export function mkMembership(opts: MakeEventPassThruProps & {
return e; return e;
} }
export type MessageEventProps = MakeEventPassThruProps & {
room: Room["roomId"];
relatesTo?: IEventRelation;
msg?: string;
};
/** /**
* Create an m.room.message event. * Create an m.room.message event.
* @param {Object} opts Values for the message * @param {Object} opts Values for the message
@ -269,20 +276,20 @@ export function mkMembership(opts: MakeEventPassThruProps & {
* @param {string=} opts.msg Optional. The content.body for the event. * @param {string=} opts.msg Optional. The content.body for the event.
* @return {Object|MatrixEvent} The event * @return {Object|MatrixEvent} The event
*/ */
export function mkMessage(opts: MakeEventPassThruProps & { export function mkMessage({
room: Room["roomId"]; msg, relatesTo, ...opts
msg?: string; }: MessageEventProps): MatrixEvent {
}): MatrixEvent {
if (!opts.room || !opts.user) { if (!opts.room || !opts.user) {
throw new Error("Missing .room or .user from options"); throw new Error("Missing .room or .user from options");
} }
const message = opts.msg ?? "Random->" + Math.random(); const message = msg ?? "Random->" + Math.random();
const event: MakeEventProps = { const event: MakeEventProps = {
...opts, ...opts,
type: "m.room.message", type: "m.room.message",
content: { content: {
msgtype: "m.text", msgtype: "m.text",
body: message, body: message,
['m.relates_to']: relatesTo,
}, },
}; };

View File

@ -0,0 +1,93 @@
/*
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.
*/
import { IUnsigned, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk";
import { Thread } from "matrix-js-sdk/src/models/thread";
import { mkMessage, MessageEventProps } from "./test-utils";
export const makeThreadEvent = ({ rootEventId, replyToEventId, ...props }: MessageEventProps & {
rootEventId: string; replyToEventId: string;
}): MatrixEvent => mkMessage({
...props,
relatesTo: {
event_id: rootEventId,
rel_type: "io.element.thread",
['m.in_reply_to']: {
event_id: replyToEventId,
},
},
});
type MakeThreadEventsProps = {
roomId: Room["roomId"];
// root message user id
authorId: string;
// user ids of thread replies
// cycled through until thread length is fulfilled
participantUserIds: string[];
// number of messages in the thread, root message included
// optional, default 2
length?: number;
ts?: number;
// provide to set current_user_participated accurately
currentUserId?: string;
};
export const makeThreadEvents = ({
roomId, authorId, participantUserIds, length = 2, ts = 1, currentUserId,
}): { rootEvent: MatrixEvent, events: MatrixEvent[] } => {
const rootEvent = mkMessage({
user: authorId,
event: true,
room: roomId,
msg: 'root event message',
ts,
});
const rootEventId = rootEvent.getId();
const events = [rootEvent];
for (let i = 1; i < length; i++) {
const prevEvent = events[i - 1];
const replyToEventId = prevEvent.getId();
const user = participantUserIds[i % participantUserIds.length];
events.push(makeThreadEvent({
user,
room: roomId,
event: true,
msg: `reply ${i} by ${user}`,
rootEventId,
replyToEventId,
// replies are 1ms after each other
ts: ts + i,
}));
}
rootEvent.setUnsigned({
latest_event: events[events.length - 1],
count: length,
current_user_participated: [...participantUserIds, authorId].includes(currentUserId),
} as IUnsigned);
return { rootEvent, events };
};
export const makeThread = (client: MatrixClient, room: Room, props: MakeThreadEventsProps): Thread => {
const { rootEvent, events } = makeThreadEvents(props);
return new Thread(rootEvent, { initialEvents: events, room, client });
};