update tests

This commit is contained in:
Sharon Kennedy 2020-04-29 09:35:57 -04:00
parent 38d22f8aed
commit 19217eb4d3
3 changed files with 54 additions and 19 deletions

View File

@ -151,7 +151,7 @@ export const mockGetJoinedRoomMembers = jest.fn(() => {
export const mockGetGroupUsers = jest.fn(() => { export const mockGetGroupUsers = jest.fn(() => {
return Promise.resolve({ return Promise.resolve({
chunk: { user_id: 'user_id_1', user_id: 'user_id_2' } chunk: [{ user_id: 'user_id_1'}, { user_id: 'user_id_2' }]
}) })
}); });

View File

@ -14,7 +14,6 @@ import logger from "./logger";
class OcrccBot { class OcrccBot {
constructor(botConfig) { constructor(botConfig) {
this.config = botConfig this.config = botConfig
this.awaitingFacilitator = {};
this.client = matrix.createClient(this.config.MATRIX_SERVER_URL); this.client = matrix.createClient(this.config.MATRIX_SERVER_URL);
this.joinedRooms = []; this.joinedRooms = [];
} }
@ -146,11 +145,15 @@ class OcrccBot {
const groupUsers = await this.client.getGroupUsers(this.config.FACILITATOR_GROUP_ID) const groupUsers = await this.client.getGroupUsers(this.config.FACILITATOR_GROUP_ID)
const roomMembers = await this.client.getJoinedRoomMembers(roomId) const roomMembers = await this.client.getJoinedRoomMembers(roomId)
const membersIds = Object.keys(roomMembers["joined"]); const roomMemberIds = Object.keys(roomMembers["joined"]);
const facilitatorsIds = groupUsers.chunk.map(f => f.user_id); const groupMemberIds = groupUsers["chunk"]
if (!roomMemberIds || !groupMemberIds) return;
const facilitatorsIds = groupMemberIds.map(f => f.user_id);
facilitatorsIds.forEach(f => { facilitatorsIds.forEach(f => {
if (!membersIds.includes(f)) { if (!roomMemberIds.includes(f)) {
this.kickUserFromRoom(roomId, f); this.kickUserFromRoom(roomId, f);
} }
}); });

View File

@ -37,6 +37,39 @@ import {
import OcrccBot from './bot' import OcrccBot from './bot'
const ENCRYPTION_CONFIG = { algorithm: "m.megolm.v1.aes-sha2" };
const KICK_REASON = "A facilitator has already joined this chat.";
const BOT_ERROR_MESSAGE =
"Something went wrong on our end, please restart the chat and try again.";
const MAX_RETRIES = 3;
const {
MATRIX_SERVER_URL,
BOT_USERNAME,
BOT_USERID,
BOT_PASSWORD,
BOT_DISPLAY_NAME,
FACILITATOR_GROUP_ID,
FACILITATOR_ROOM_ID,
CHAT_OFFLINE_MESSAGE,
CAPTURE_TRANSCRIPTS
} = process.env;
const botConfig = {
ENCRYPTION_CONFIG,
KICK_REASON,
BOT_ERROR_MESSAGE,
MAX_RETRIES,
MATRIX_SERVER_URL,
BOT_USERNAME,
BOT_USERID,
BOT_PASSWORD,
BOT_DISPLAY_NAME,
FACILITATOR_GROUP_ID,
FACILITATOR_ROOM_ID,
CHAT_OFFLINE_MESSAGE,
CAPTURE_TRANSCRIPTS
}
const mockAppendFileSync = jest.fn() const mockAppendFileSync = jest.fn()
fs.appendFileSync = mockAppendFileSync fs.appendFileSync = mockAppendFileSync
@ -71,22 +104,21 @@ describe('OcrccBot', () => {
mockGetGroupUsers.mockClear() mockGetGroupUsers.mockClear()
}) })
test('constructor should inititialize class variables', () => { test('constructor should inititialize class variables', () => {
const bot = new OcrccBot() const bot = new OcrccBot(botConfig)
expect(bot.joinedRooms).toEqual([]) expect(bot.joinedRooms).toEqual([])
expect(bot.awaitingFacilitator).toEqual({})
expect(bot.activeChatrooms).toEqual({})
}) })
test('#createLocalStorage should have correct storage location', () => { test('#createLocalStorage should have correct storage location', () => {
const bot = new OcrccBot() const bot = new OcrccBot(botConfig)
const localStorage = bot.createLocalStorage() const localStorage = bot.createLocalStorage()
const localStoragePath = path.resolve(path.join(os.homedir(), ".local-storage", `matrix-chatbot-${process.env.BOT_USERNAME}`)); const localStoragePath = path.resolve(path.join(os.homedir(), ".local-storage", `matrix-chatbot-${process.env.BOT_USERNAME}`));
expect(localStorage._location).toBe(localStoragePath) expect(localStorage._location).toBe(localStoragePath)
}) })
test('#sendMessage should send a text message', () => { test('#sendMessage should send a text message', () => {
const bot = new OcrccBot() const bot = new OcrccBot(botConfig)
bot.start() bot.start()
waitForExpect(() => { waitForExpect(() => {
@ -108,7 +140,7 @@ describe('OcrccBot', () => {
}) })
test('#inviteUserToRoom should add member to room and retry on rate limit error', () => { test('#inviteUserToRoom should add member to room and retry on rate limit error', () => {
const bot = new OcrccBot() const bot = new OcrccBot(botConfig)
bot.start() bot.start()
waitForExpect(() => { waitForExpect(() => {
@ -123,7 +155,7 @@ describe('OcrccBot', () => {
}) })
test('#kickUserFromRoom should remove member from room and retry on rate limit error', () => { test('#kickUserFromRoom should remove member from room and retry on rate limit error', () => {
const bot = new OcrccBot() const bot = new OcrccBot(botConfig)
bot.start() bot.start()
waitForExpect(() => { waitForExpect(() => {
@ -138,7 +170,7 @@ describe('OcrccBot', () => {
}) })
test('#inviteFacilitators should invite all members from Facilitator room', () => { test('#inviteFacilitators should invite all members from Facilitator room', () => {
const bot = new OcrccBot() const bot = new OcrccBot(botConfig)
bot.start() bot.start()
waitForExpect(() => { waitForExpect(() => {
@ -161,7 +193,7 @@ describe('OcrccBot', () => {
}) })
test('#uninviteFacilitators should remove all members that have not accepted the invite', () => { test('#uninviteFacilitators should remove all members that have not accepted the invite', () => {
const bot = new OcrccBot() const bot = new OcrccBot(botConfig)
bot.start() bot.start()
waitForExpect(() => { waitForExpect(() => {
@ -184,7 +216,7 @@ describe('OcrccBot', () => {
}) })
test('#handleBotCrash should notify rooms', () => { test('#handleBotCrash should notify rooms', () => {
const bot = new OcrccBot() const bot = new OcrccBot(botConfig)
bot.start() bot.start()
waitForExpect(() => { waitForExpect(() => {
@ -203,10 +235,10 @@ describe('OcrccBot', () => {
}) })
test('#writeToTranscript should parse event and write to transcript file', () => { test('#writeToTranscript should parse event and write to transcript file', () => {
const bot = new OcrccBot() const bot = new OcrccBot(botConfig)
bot.start() bot.start()
bot.activeChatrooms['test_room_id'] = { transcriptFile: '__mocks__/test_transcript.txt' } bot.localStorage.setItem(`test_room_id-transcript`, '__mocks__/test_transcript.txt')
waitForExpect(() => { waitForExpect(() => {
expect(mockStartClient).toHaveBeenCalled() expect(mockStartClient).toHaveBeenCalled()
@ -227,7 +259,7 @@ describe('OcrccBot', () => {
}) })
test('#deleteOldDevices should delete old sessions', () => { test('#deleteOldDevices should delete old sessions', () => {
const bot = new OcrccBot() const bot = new OcrccBot(botConfig)
bot.start() bot.start()
waitForExpect(() => { waitForExpect(() => {
@ -252,7 +284,7 @@ describe('OcrccBot', () => {
// TODO test listeners for membership events and message events // TODO test listeners for membership events and message events
test('#start should start bot and set up listeners', () => { test('#start should start bot and set up listeners', () => {
const bot = new OcrccBot() const bot = new OcrccBot(botConfig)
bot.start() bot.start()
waitForExpect(() => { waitForExpect(() => {