hookshot/tests/utils/IntentMock.ts
Michael Weimann 1b51189db1
Fix generic webhook 'user is already in the room error' (#627)
* Fix generic webhook 'user is already in the room error'

* Add changelog

* Cleanup to support usecases without slamming the homeserver.

* Fix fallthrough

* Add some tests

* Add tests for intentutils

* Refactor the rest of the app

Co-authored-by: Half-Shot <will@half-shot.uk>
2023-01-16 14:58:23 +00:00

85 lines
2.3 KiB
TypeScript

import { expect } from "chai";
export class MatrixClientMock {
static create(){
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new this() as any;
}
// map room Id → user Ids
private joinedMembers: Map<string, string[]> = new Map();
async setDisplayName() {
return;
}
async getJoinedRoomMembers(roomId: string): Promise<string[]> {
return this.joinedMembers.get(roomId) || [];
}
async inviteUser(userId: string, roomId: string): Promise<void> {
const roomMembers = this.joinedMembers.get(roomId) || [];
if (roomMembers.includes(userId)) {
throw new Error("User already in room");
}
roomMembers.push(userId);
this.joinedMembers.set(roomId, roomMembers);
}
}
export class IntentMock {
public readonly underlyingClient = new MatrixClientMock();
public sentEvents: {roomId: string, content: any}[] = [];
constructor(readonly userId: string) {}
static create(userId: string){
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new this(userId) as any;
}
sendText(roomId: string, noticeText: string, msgtype: string) {
this.sentEvents.push({
roomId,
content: {
msgtype,
body: noticeText,
}
});
}
sendEvent(roomId: string, content: any) {
this.sentEvents.push({
roomId,
content,
});
}
expectNoEvent() {
expect(this.sentEvents, 'Expected no events to be sent.').to.be.empty;
}
expectEventBodyContains(matcher: string|RegExp, eventIndex?: number) {
if (eventIndex !== undefined) {
expect(this.sentEvents[eventIndex], `Expected event ${eventIndex} to exist`).to.not.be.undefined;
const body = this.sentEvents[eventIndex].content.body;
expect(
body.includes(matcher),
`Expected event body ${eventIndex} to match '${matcher}'.\nMessage was: '${body}'`
).to.be.true;
}
expect(!!this.sentEvents.find(ev => ev.content.body.includes(matcher)), `Expected any event body to match '${matcher}'`).to.be.true;
}
async ensureJoined() {
return true;
}
async ensureRegistered() {
return true;
}
}