test suite for bot

This commit is contained in:
Sharon Kennedy 2020-03-17 02:09:52 -04:00
parent fcdd712351
commit e49e4dca76
3 changed files with 66 additions and 6 deletions

View File

@ -75,7 +75,7 @@ export const mockSendTextMessage = jest.fn()
devices: ['device1', 'device2'] devices: ['device1', 'device2']
}) })
}) })
.mockImplementationOnce(() => { .mockImplementation(() => {
return Promise.resolve() return Promise.resolve()
}) })
@ -87,7 +87,7 @@ export const mockInvite = jest.fn()
name: "M_LIMIT_EXCEEDED", name: "M_LIMIT_EXCEEDED",
}) })
}) })
.mockImplementationOnce(() => { .mockImplementation(() => {
return Promise.resolve() return Promise.resolve()
}) })
@ -97,7 +97,7 @@ export const mockKick = jest.fn()
name: "M_LIMIT_EXCEEDED", name: "M_LIMIT_EXCEEDED",
}) })
}) })
.mockImplementationOnce(() => { .mockImplementation(() => {
return Promise.resolve() return Promise.resolve()
}) })
@ -134,9 +134,7 @@ export const mockLogin = jest.fn(() => {
export const mockGetJoinedRoomMembers = jest.fn(() => { export const mockGetJoinedRoomMembers = jest.fn(() => {
return Promise.resolve({ 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, invite: mockInvite,
kick: mockKick, kick: mockKick,
getJoinedRoomMembers: mockGetJoinedRoomMembers, getJoinedRoomMembers: mockGetJoinedRoomMembers,
getUser: mockGetUser,
} }
export const WebStorageSessionStore = jest.fn() export const WebStorageSessionStore = jest.fn()

View File

@ -217,6 +217,10 @@ class OcrccBot {
.deleteMultipleDevices(oldDevices, auth) .deleteMultipleDevices(oldDevices, auth)
.then(() => logger.log("info", "DELETED OLD DEVICES")) .then(() => logger.log("info", "DELETED OLD DEVICES"))
.catch(err => .catch(err =>
// TODO: catch rate limiting error and retry
// if (err.errcode === "M_LIMIT_EXCEEDED") {
// setTimeout(err.retry_after_ms)
// }
logger.log( logger.log(
"error", "error",
`ERROR DELETING OLD DEVICES: ${JSON.stringify(err.data)}` `ERROR DELETING OLD DEVICES: ${JSON.stringify(err.data)}`

View File

@ -2,6 +2,7 @@ require('dotenv').config()
import * as path from "path"; import * as path from "path";
import * as os from "os"; import * as os from "os";
import * as fs from "fs";
import waitForExpect from 'wait-for-expect' import waitForExpect from 'wait-for-expect'
import { import {
@ -59,6 +60,7 @@ describe('OcrccBot', () => {
mockKick.mockClear() mockKick.mockClear()
mockGetJoinedRoomMembers.mockClear() mockGetJoinedRoomMembers.mockClear()
mockGetUser.mockClear() mockGetUser.mockClear()
mockSendTextMessage.mockClear()
}) })
test('constructor should inititialize matrix client', () => { test('constructor should inititialize matrix client', () => {
@ -175,15 +177,70 @@ describe('OcrccBot', () => {
}) })
test('#handleBotCrash should notify rooms', () => { 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', () => { 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', () => { 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 // TODO test listeners for membership events and message events