diff --git a/__mocks__/matrix-js-sdk.js b/__mocks__/matrix-js-sdk.js index 3c429f2..bc356ca 100644 --- a/__mocks__/matrix-js-sdk.js +++ b/__mocks__/matrix-js-sdk.js @@ -75,7 +75,7 @@ export const mockSendTextMessage = jest.fn() devices: ['device1', 'device2'] }) }) - .mockImplementationOnce(() => { + .mockImplementation(() => { return Promise.resolve() }) @@ -87,7 +87,7 @@ export const mockInvite = jest.fn() name: "M_LIMIT_EXCEEDED", }) }) - .mockImplementationOnce(() => { + .mockImplementation(() => { return Promise.resolve() }) @@ -97,7 +97,7 @@ export const mockKick = jest.fn() name: "M_LIMIT_EXCEEDED", }) }) - .mockImplementationOnce(() => { + .mockImplementation(() => { return Promise.resolve() }) @@ -134,9 +134,7 @@ export const mockLogin = jest.fn(() => { export const mockGetJoinedRoomMembers = jest.fn(() => { return Promise.resolve({ - data: { - joined: ['user_id_1', 'user_id_2'] - } + joined: { 'user_id_1': {}, 'user_id_2': {} } }) }); @@ -168,6 +166,7 @@ export const mockClient = { invite: mockInvite, kick: mockKick, getJoinedRoomMembers: mockGetJoinedRoomMembers, + getUser: mockGetUser, } export const WebStorageSessionStore = jest.fn() diff --git a/src/bot.js b/src/bot.js index 7a04bd8..a92322e 100644 --- a/src/bot.js +++ b/src/bot.js @@ -217,6 +217,10 @@ class OcrccBot { .deleteMultipleDevices(oldDevices, auth) .then(() => logger.log("info", "DELETED OLD DEVICES")) .catch(err => + // TODO: catch rate limiting error and retry + // if (err.errcode === "M_LIMIT_EXCEEDED") { + // setTimeout(err.retry_after_ms) + // } logger.log( "error", `ERROR DELETING OLD DEVICES: ${JSON.stringify(err.data)}` diff --git a/src/bot.test.js b/src/bot.test.js index f281c44..088acc3 100644 --- a/src/bot.test.js +++ b/src/bot.test.js @@ -2,6 +2,7 @@ require('dotenv').config() import * as path from "path"; import * as os from "os"; +import * as fs from "fs"; import waitForExpect from 'wait-for-expect' import { @@ -59,6 +60,7 @@ describe('OcrccBot', () => { mockKick.mockClear() mockGetJoinedRoomMembers.mockClear() mockGetUser.mockClear() + mockSendTextMessage.mockClear() }) test('constructor should inititialize matrix client', () => { @@ -175,15 +177,70 @@ describe('OcrccBot', () => { }) test('#handleBotCrash should notify rooms', () => { + const bot = new OcrccBot() + bot.start() + waitForExpect(() => { + expect(mockStartClient).toHaveBeenCalled() + }) + + bot.handleBotCrash('test_room_id', 'test error message') + + waitForExpect(() => { + expect(mockSendTextMessage).toHaveBeenCalledWith('test_room_id', "Something went wrong on our end, please restart the chat and try again.") + }) + + waitForExpect(() => { + expect(mockSendTextMessage).toHaveBeenCalledWith(process.env.FACILITATOR_ROOM_ID, `The Help Bot ran into an error: test error message. Please verify that the chat service is working.`) + }) }) test('#writeToTranscript should parse event and write to transcript file', () => { + const appendFileSyncSpy = jest.spyOn(fs, 'appendFileSync') + const bot = new OcrccBot() + bot.start() + bot.activeChatrooms['test_room_id'] = { transcriptFile: 'test filepath' } + + waitForExpect(() => { + expect(mockStartClient).toHaveBeenCalled() + }) + + const mockEvent = { + getSender: () => 'test_sender', + getRoomId: () => 'test_room_id', + getContent: () => { return { body: 'test content' }}, + getDate: () => { return new Date(2020,2,17,0,0,0,0) } + } + + bot.writeToTranscript(mockEvent) + + waitForExpect(() => { + expect(appendFileSyncSpy).toHaveBeenCalledWith('test filepath', 'test_sender [00:00:00]: test content', 'utf8') + }) }) test('#deleteOldDevices should delete old sessions', () => { + const bot = new OcrccBot() + bot.start() + waitForExpect(() => { + expect(mockStartClient).toHaveBeenCalled() + }) + + bot.deleteOldDevices() + + waitForExpect(() => { + expect(mockGetDevices).toHaveBeenCalled() + }) + + waitForExpect(() => { + expect(mockGetDevicdId).toHaveBeenCalled() + }) + + waitForExpect(() => { + expect(deleteMultipleDevices).toHaveBeenCalled() + }) }) // TODO test listeners for membership events and message events