mirror of
https://github.com/matrix-org/matrix-hookshot.git
synced 2025-03-10 13:17:08 +00:00

* Update dependencies * Node 22 is now the new minimum version. * changelog. * Begin porting eslint to new config format. * Make linter happy. * Update reqwest to fix SSL issue? * Fix test types * quick check on ubuntu LTS 24.04 * Change cache key * update rust action * revert mocha due to esminess * Remove the only usage of pqueue * Use babel for TS transformations to get around ESM import bug. * Dependency bundle upgrade * Drop babel, not actually used. * lint * lint * update default config (mostly sections moving around)
58 lines
2.5 KiB
TypeScript
58 lines
2.5 KiB
TypeScript
import { IntentMock, MatrixClientMock } from "./utils/IntentMock";
|
|
import { ensureUserIsInRoom } from "../src/IntentUtils";
|
|
import { expect } from "chai";
|
|
import { MatrixError } from "matrix-bot-sdk";
|
|
|
|
const ROOM_ID = "!foo:bar";
|
|
const SENDER_USER_ID = "@my_target:foo";
|
|
|
|
describe("IntentUtils", () => {
|
|
describe("ensureUserIsInRoom", () => {
|
|
it("no-ops if the user is already joined to the room", () => {
|
|
const targetIntent = IntentMock.create(SENDER_USER_ID);
|
|
targetIntent.ensureJoined = () => { /* No-op */ };
|
|
const matrixClient = MatrixClientMock.create();
|
|
ensureUserIsInRoom(targetIntent, matrixClient, ROOM_ID);
|
|
});
|
|
|
|
it("invites the user to the room and joins", () => {
|
|
const targetIntent = IntentMock.create(SENDER_USER_ID);
|
|
const matrixClient = MatrixClientMock.create();
|
|
let hasInvited = false;
|
|
// This should fail the first time, then pass once we've tried to invite the user
|
|
targetIntent.ensureJoined = (roomId: string) => {
|
|
if (hasInvited) {
|
|
return;
|
|
}
|
|
expect(roomId).to.equal(ROOM_ID);
|
|
throw new MatrixError({ errcode: "M_FORBIDDEN", error: "Test forced error"}, 401, {})
|
|
};
|
|
|
|
// This should invite the puppet user.
|
|
matrixClient.inviteUser = (userId: string, roomId: string) => {
|
|
expect(userId).to.equal(SENDER_USER_ID);
|
|
expect(roomId).to.equal(ROOM_ID);
|
|
hasInvited = true;
|
|
}
|
|
|
|
ensureUserIsInRoom(targetIntent, matrixClient, ROOM_ID);
|
|
// Only pass if we've actually bothered to invite the bot.
|
|
expect(hasInvited).to.be.true;
|
|
});
|
|
|
|
it("invites the user to the room and handles the failure", () => {
|
|
const targetIntent = IntentMock.create(SENDER_USER_ID);
|
|
const matrixClient = MatrixClientMock.create();
|
|
|
|
// This should fail the first time, then pass once we've tried to invite the user
|
|
targetIntent.ensureJoined = () => {
|
|
throw new MatrixError({ errcode: "FORCED_FAILURE", error: "Test forced error"}, 500, { })
|
|
};
|
|
try {
|
|
ensureUserIsInRoom(targetIntent, matrixClient, ROOM_ID);
|
|
} catch (ex) {
|
|
expect(ex.message).to.contain(`Could not ensure that ${SENDER_USER_ID} is in ${ROOM_ID}`)
|
|
}
|
|
});
|
|
})
|
|
}); |