2020-03-16 03:30:00 +00:00
require ( 'dotenv' ) . config ( )
import * as path from "path" ;
import * as os from "os" ;
2020-03-17 06:09:52 +00:00
import * as fs from "fs" ;
2020-03-16 03:30:00 +00:00
import waitForExpect from 'wait-for-expect'
import {
createClient ,
WebStorageSessionStore ,
mockClient ,
mockRegisterRequest ,
mockInitCrypto ,
mockStartClient ,
mockSetPowerLevel ,
mockCreateRoom ,
mockLeave ,
mockDeactivateAccount ,
mockStopClient ,
mockClearStores ,
mockOn ,
mockOnce ,
2020-03-27 04:48:03 +00:00
mockSendMessage ,
2020-03-16 03:30:00 +00:00
mockSendTextMessage ,
mockLogin ,
mockGetDevices ,
mockGetDeviceId ,
mockDeleteMultipleDevices ,
mockGetJoinedRooms ,
mockSetDeviceVerified ,
mockInvite ,
mockKick ,
mockGetJoinedRoomMembers ,
2020-03-27 04:48:03 +00:00
mockGetUser ,
mockGetGroupUsers
2020-03-16 03:30:00 +00:00
} from "matrix-js-sdk" ;
import OcrccBot from './bot'
2020-04-29 13:35:57 +00:00
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
}
2020-03-16 03:30:00 +00:00
2020-03-18 07:00:43 +00:00
const mockAppendFileSync = jest . fn ( )
fs . appendFileSync = mockAppendFileSync
describe ( 'OcrccBot' , ( ) => {
2020-03-16 03:30:00 +00:00
beforeEach ( ( ) => {
createClient . mockClear ( )
mockInitCrypto . mockClear ( )
mockStartClient . mockClear ( )
mockRegisterRequest . mockClear ( )
mockSetPowerLevel . mockClear ( )
mockCreateRoom . mockClear ( )
mockLeave . mockClear ( )
mockDeactivateAccount . mockClear ( )
mockStopClient . mockClear ( )
mockClearStores . mockClear ( )
mockOnce . mockClear ( )
mockOn . mockClear ( )
mockLogin . mockClear ( )
mockGetDevices . mockClear ( )
mockGetDeviceId . mockClear ( )
mockDeleteMultipleDevices . mockClear ( )
mockGetJoinedRooms . mockClear ( )
mockSetDeviceVerified . mockClear ( )
mockInvite . mockClear ( )
mockKick . mockClear ( )
mockGetJoinedRoomMembers . mockClear ( )
mockGetUser . mockClear ( )
2020-03-27 04:48:03 +00:00
mockSendMessage . mockClear ( )
2020-03-17 06:09:52 +00:00
mockSendTextMessage . mockClear ( )
2020-03-18 07:00:43 +00:00
mockAppendFileSync . mockClear ( )
2020-03-27 04:48:03 +00:00
mockGetGroupUsers . mockClear ( )
2020-03-16 03:30:00 +00:00
} )
2020-04-29 13:35:57 +00:00
2020-03-27 04:48:03 +00:00
test ( 'constructor should inititialize class variables' , ( ) => {
2020-04-29 13:35:57 +00:00
const bot = new OcrccBot ( botConfig )
2020-03-16 03:30:00 +00:00
expect ( bot . joinedRooms ) . toEqual ( [ ] )
} )
test ( '#createLocalStorage should have correct storage location' , ( ) => {
2020-04-29 13:35:57 +00:00
const bot = new OcrccBot ( botConfig )
2020-03-16 03:30:00 +00:00
const localStorage = bot . createLocalStorage ( )
const localStoragePath = path . resolve ( path . join ( os . homedir ( ) , ".local-storage" , ` matrix-chatbot- ${ process . env . BOT _USERNAME } ` ) ) ;
expect ( localStorage . _location ) . toBe ( localStoragePath )
} )
test ( '#sendMessage should send a text message' , ( ) => {
2020-04-29 13:35:57 +00:00
const bot = new OcrccBot ( botConfig )
2020-03-16 03:30:00 +00:00
bot . start ( )
waitForExpect ( ( ) => {
expect ( mockStartClient ) . toHaveBeenCalled ( )
} )
const testRoom = 'room_id_1234'
const testMsg = 'test message'
bot . sendMessage ( testRoom , testMsg )
waitForExpect ( ( ) => {
expect ( mockSetDeviceVerified ) . toHaveBeenCalledTimes ( 2 )
} )
waitForExpect ( ( ) => {
2020-03-27 04:48:03 +00:00
expect ( mockSendMessage ) . toHaveBeenCalledWith ( testRoom , testMsg )
2020-03-16 03:30:00 +00:00
} )
} )
test ( '#inviteUserToRoom should add member to room and retry on rate limit error' , ( ) => {
2020-04-29 13:35:57 +00:00
const bot = new OcrccBot ( botConfig )
2020-03-16 03:30:00 +00:00
bot . start ( )
waitForExpect ( ( ) => {
expect ( mockStartClient ) . toHaveBeenCalled ( )
} )
bot . inviteUserToRoom ( bot . client , 'room_id_1234' , process . env . BOT _USERNAME )
waitForExpect ( ( ) => {
expect ( mockInvite ) . toHaveBeenCalledTimes ( 2 )
} )
} )
test ( '#kickUserFromRoom should remove member from room and retry on rate limit error' , ( ) => {
2020-04-29 13:35:57 +00:00
const bot = new OcrccBot ( botConfig )
2020-03-16 03:30:00 +00:00
bot . start ( )
waitForExpect ( ( ) => {
expect ( mockStartClient ) . toHaveBeenCalled ( )
} )
bot . kickUserFromRoom ( bot . client , 'room_id_1234' , process . env . BOT _USERNAME )
waitForExpect ( ( ) => {
expect ( mockKick ) . toHaveBeenCalledTimes ( 2 )
} )
} )
test ( '#inviteFacilitators should invite all members from Facilitator room' , ( ) => {
2020-04-29 13:35:57 +00:00
const bot = new OcrccBot ( botConfig )
2020-03-16 03:30:00 +00:00
bot . start ( )
waitForExpect ( ( ) => {
expect ( mockStartClient ) . toHaveBeenCalled ( )
} )
bot . inviteFacilitators ( )
waitForExpect ( ( ) => {
expect ( mockGetJoinedRoomMembers ) . toHaveBeenCalledWith ( process . env . FACILITATOR _ROOM _ID )
} )
waitForExpect ( ( ) => {
expect ( mockGetUser ) . toHaveBeenCalledTimes ( 2 )
} )
waitForExpect ( ( ) => {
expect ( mockInvite ) . toHaveBeenCalledTimes ( 2 )
} )
} )
test ( '#uninviteFacilitators should remove all members that have not accepted the invite' , ( ) => {
2020-04-29 13:35:57 +00:00
const bot = new OcrccBot ( botConfig )
2020-03-16 03:30:00 +00:00
bot . start ( )
waitForExpect ( ( ) => {
expect ( mockStartClient ) . toHaveBeenCalled ( )
} )
bot . uninviteFacilitators ( )
waitForExpect ( ( ) => {
expect ( mockGetJoinedRoomMembers ) . toHaveBeenCalledWith ( process . env . FACILITATOR _ROOM _ID )
} )
waitForExpect ( ( ) => {
expect ( mockGetJoinedRoomMembers ) . toHaveBeenCalledWith ( 'room_id_1234' )
} )
waitForExpect ( ( ) => {
expect ( mockKick ) . toHaveBeenCalled ( )
} )
} )
test ( '#handleBotCrash should notify rooms' , ( ) => {
2020-04-29 13:35:57 +00:00
const bot = new OcrccBot ( botConfig )
2020-03-17 06:09:52 +00:00
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." )
} )
2020-03-16 03:30:00 +00:00
2020-03-17 06:09:52 +00:00
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. ` )
} )
2020-03-16 03:30:00 +00:00
} )
test ( '#writeToTranscript should parse event and write to transcript file' , ( ) => {
2020-04-29 13:35:57 +00:00
const bot = new OcrccBot ( botConfig )
2020-03-17 06:09:52 +00:00
bot . start ( )
2020-04-29 13:35:57 +00:00
bot . localStorage . setItem ( ` test_room_id-transcript ` , '__mocks__/test_transcript.txt' )
2020-03-17 06:09:52 +00:00
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 ) }
}
2020-03-16 03:30:00 +00:00
2020-03-17 06:09:52 +00:00
bot . writeToTranscript ( mockEvent )
waitForExpect ( ( ) => {
2020-03-18 07:00:43 +00:00
expect ( mockAppendFileSync ) . toHaveBeenCalledWith ( '__mocks__/test_transcript.txt' , 'test_sender [00:00:00]: test content' , 'utf8' )
2020-03-17 06:09:52 +00:00
} )
2020-03-16 03:30:00 +00:00
} )
test ( '#deleteOldDevices should delete old sessions' , ( ) => {
2020-04-29 13:35:57 +00:00
const bot = new OcrccBot ( botConfig )
2020-03-17 06:09:52 +00:00
bot . start ( )
waitForExpect ( ( ) => {
expect ( mockStartClient ) . toHaveBeenCalled ( )
} )
bot . deleteOldDevices ( )
2020-03-16 03:30:00 +00:00
2020-03-17 06:09:52 +00:00
waitForExpect ( ( ) => {
expect ( mockGetDevices ) . toHaveBeenCalled ( )
} )
waitForExpect ( ( ) => {
expect ( mockGetDevicdId ) . toHaveBeenCalled ( )
} )
waitForExpect ( ( ) => {
expect ( deleteMultipleDevices ) . toHaveBeenCalled ( )
} )
2020-03-16 03:30:00 +00:00
} )
// TODO test listeners for membership events and message events
test ( '#start should start bot and set up listeners' , ( ) => {
2020-04-29 13:35:57 +00:00
const bot = new OcrccBot ( botConfig )
2020-03-16 03:30:00 +00:00
bot . start ( )
waitForExpect ( ( ) => {
expect ( mockLogin ) . toHaveBeenCalled ( )
} )
waitForExpect ( ( ) => {
expect ( WebStorageSessionStore ) . toHaveBeenCalled ( )
} )
waitForExpect ( ( ) => {
expect ( createClient ) . toHaveBeenCalled ( )
} )
waitForExpect ( ( ) => {
expect ( mockGetDevices ) . toHaveBeenCalled ( )
} )
waitForExpect ( ( ) => {
expect ( mockGetDeviceId ) . toHaveBeenCalled ( )
} )
waitForExpect ( ( ) => {
expect ( mockDeleteMultipleDevices ) . toHaveBeenCalled ( )
} )
waitForExpect ( ( ) => {
expect ( mockInitCrypto ) . toHaveBeenCalled ( )
} )
waitForExpect ( ( ) => {
expect ( mockGetJoinedRooms ) . toHaveBeenCalled ( )
} )
waitForExpect ( ( ) => {
expect ( mockOn ) . toHaveBeenCalled ( )
} )
waitForExpect ( ( ) => {
expect ( mockStartClient ) . toHaveBeenCalled ( )
} )
} )
} )