2020-04-22 22:04:48 +00:00
"use strict" ;
var _interopRequireDefault = require ( "@babel/runtime/helpers/interopRequireDefault" ) ;
var _interopRequireWildcard = require ( "@babel/runtime/helpers/interopRequireWildcard" ) ;
var path = _interopRequireWildcard ( require ( "path" ) ) ;
var os = _interopRequireWildcard ( require ( "os" ) ) ;
var fs = _interopRequireWildcard ( require ( "fs" ) ) ;
var _waitForExpect = _interopRequireDefault ( require ( "wait-for-expect" ) ) ;
var _matrixJsSdk = require ( "matrix-js-sdk" ) ;
var _bot = _interopRequireDefault ( require ( "./bot" ) ) ;
require ( 'dotenv' ) . config ( ) ;
2020-06-19 16:57:23 +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 _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 _ROOM _ID ,
CHAT _OFFLINE _MESSAGE ,
CAPTURE _TRANSCRIPTS
} ;
2020-04-22 22:04:48 +00:00
const mockAppendFileSync = jest . fn ( ) ;
fs . appendFileSync = mockAppendFileSync ;
describe ( 'OcrccBot' , ( ) => {
beforeEach ( ( ) => {
_matrixJsSdk . createClient . mockClear ( ) ;
_matrixJsSdk . mockInitCrypto . mockClear ( ) ;
_matrixJsSdk . mockStartClient . mockClear ( ) ;
_matrixJsSdk . mockRegisterRequest . mockClear ( ) ;
_matrixJsSdk . mockSetPowerLevel . mockClear ( ) ;
_matrixJsSdk . mockCreateRoom . mockClear ( ) ;
_matrixJsSdk . mockLeave . mockClear ( ) ;
_matrixJsSdk . mockDeactivateAccount . mockClear ( ) ;
_matrixJsSdk . mockStopClient . mockClear ( ) ;
_matrixJsSdk . mockClearStores . mockClear ( ) ;
_matrixJsSdk . mockOnce . mockClear ( ) ;
_matrixJsSdk . mockOn . mockClear ( ) ;
_matrixJsSdk . mockLogin . mockClear ( ) ;
_matrixJsSdk . mockGetDevices . mockClear ( ) ;
_matrixJsSdk . mockGetDeviceId . mockClear ( ) ;
_matrixJsSdk . mockDeleteMultipleDevices . mockClear ( ) ;
_matrixJsSdk . mockGetJoinedRooms . mockClear ( ) ;
_matrixJsSdk . mockSetDeviceVerified . mockClear ( ) ;
_matrixJsSdk . mockInvite . mockClear ( ) ;
_matrixJsSdk . mockKick . mockClear ( ) ;
_matrixJsSdk . mockGetJoinedRoomMembers . mockClear ( ) ;
_matrixJsSdk . mockGetUser . mockClear ( ) ;
_matrixJsSdk . mockSendMessage . mockClear ( ) ;
_matrixJsSdk . mockSendTextMessage . mockClear ( ) ;
mockAppendFileSync . mockClear ( ) ;
_matrixJsSdk . mockGetGroupUsers . mockClear ( ) ;
2020-09-06 18:07:44 +00:00
_matrixJsSdk . mockSendStateEvent . mockClear ( ) ;
2020-04-22 22:04:48 +00:00
} ) ;
test ( 'constructor should inititialize class variables' , ( ) => {
2020-06-19 16:57:23 +00:00
const bot = new _bot . default ( botConfig ) ;
2020-04-22 22:04:48 +00:00
expect ( bot . joinedRooms ) . toEqual ( [ ] ) ;
} ) ;
test ( '#createLocalStorage should have correct storage location' , ( ) => {
2020-06-19 16:57:23 +00:00
const bot = new _bot . default ( botConfig ) ;
2020-04-22 22:04:48 +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-06-19 16:57:23 +00:00
const bot = new _bot . default ( botConfig ) ;
2020-04-22 22:04:48 +00:00
bot . start ( ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockStartClient ) . toHaveBeenCalled ( ) ;
} ) ;
const testRoom = 'room_id_1234' ;
const testMsg = 'test message' ;
bot . sendMessage ( testRoom , testMsg ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockSetDeviceVerified ) . toHaveBeenCalledTimes ( 2 ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockSendMessage ) . toHaveBeenCalledWith ( testRoom , testMsg ) ;
} ) ;
} ) ;
test ( '#inviteUserToRoom should add member to room and retry on rate limit error' , ( ) => {
2020-06-19 16:57:23 +00:00
const bot = new _bot . default ( botConfig ) ;
2020-04-22 22:04:48 +00:00
bot . start ( ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockStartClient ) . toHaveBeenCalled ( ) ;
} ) ;
bot . inviteUserToRoom ( bot . client , 'room_id_1234' , process . env . BOT _USERNAME ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockInvite ) . toHaveBeenCalledTimes ( 2 ) ;
} ) ;
} ) ;
test ( '#kickUserFromRoom should remove member from room and retry on rate limit error' , ( ) => {
2020-06-19 16:57:23 +00:00
const bot = new _bot . default ( botConfig ) ;
2020-04-22 22:04:48 +00:00
bot . start ( ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockStartClient ) . toHaveBeenCalled ( ) ;
} ) ;
bot . kickUserFromRoom ( bot . client , 'room_id_1234' , process . env . BOT _USERNAME ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockKick ) . toHaveBeenCalledTimes ( 2 ) ;
} ) ;
} ) ;
test ( '#inviteFacilitators should invite all members from Facilitator room' , ( ) => {
2020-06-19 16:57:23 +00:00
const bot = new _bot . default ( botConfig ) ;
2020-04-22 22:04:48 +00:00
bot . start ( ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockStartClient ) . toHaveBeenCalled ( ) ;
} ) ;
bot . inviteFacilitators ( ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockGetJoinedRoomMembers ) . toHaveBeenCalledWith ( process . env . FACILITATOR _ROOM _ID ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockGetUser ) . toHaveBeenCalledTimes ( 2 ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockInvite ) . toHaveBeenCalledTimes ( 2 ) ;
} ) ;
} ) ;
test ( '#uninviteFacilitators should remove all members that have not accepted the invite' , ( ) => {
2020-06-19 16:57:23 +00:00
const bot = new _bot . default ( botConfig ) ;
2020-04-22 22:04:48 +00:00
bot . start ( ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockStartClient ) . toHaveBeenCalled ( ) ;
} ) ;
bot . uninviteFacilitators ( ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockGetJoinedRoomMembers ) . toHaveBeenCalledWith ( process . env . FACILITATOR _ROOM _ID ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockGetJoinedRoomMembers ) . toHaveBeenCalledWith ( 'room_id_1234' ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockKick ) . toHaveBeenCalled ( ) ;
} ) ;
} ) ;
test ( '#handleBotCrash should notify rooms' , ( ) => {
2020-06-19 16:57:23 +00:00
const bot = new _bot . default ( botConfig ) ;
2020-04-22 22:04:48 +00:00
bot . start ( ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockStartClient ) . toHaveBeenCalled ( ) ;
} ) ;
bot . handleBotCrash ( 'test_room_id' , 'test error message' ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockSendTextMessage ) . toHaveBeenCalledWith ( 'test_room_id' , "Something went wrong on our end, please restart the chat and try again." ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . 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' , ( ) => {
2020-06-19 16:57:23 +00:00
const bot = new _bot . default ( botConfig ) ;
2020-04-22 22:04:48 +00:00
bot . start ( ) ;
2020-06-19 16:57:23 +00:00
bot . localStorage . setItem ( ` test_room_id-transcript ` , '__mocks__/test_transcript.txt' ) ;
2020-04-22 22:04:48 +00:00
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . 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 ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( mockAppendFileSync ) . toHaveBeenCalledWith ( '__mocks__/test_transcript.txt' , 'test_sender [00:00:00]: test content' , 'utf8' ) ;
} ) ;
} ) ;
test ( '#deleteOldDevices should delete old sessions' , ( ) => {
2020-06-19 16:57:23 +00:00
const bot = new _bot . default ( botConfig ) ;
2020-04-22 22:04:48 +00:00
bot . start ( ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockStartClient ) . toHaveBeenCalled ( ) ;
} ) ;
bot . deleteOldDevices ( ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockGetDevices ) . toHaveBeenCalled ( ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( mockGetDevicdId ) . toHaveBeenCalled ( ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( deleteMultipleDevices ) . toHaveBeenCalled ( ) ;
} ) ;
} ) ; // TODO test listeners for membership events and message events
test ( '#start should start bot and set up listeners' , ( ) => {
2020-06-19 16:57:23 +00:00
const bot = new _bot . default ( botConfig ) ;
2020-04-22 22:04:48 +00:00
bot . start ( ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockLogin ) . toHaveBeenCalled ( ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . WebStorageSessionStore ) . toHaveBeenCalled ( ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . createClient ) . toHaveBeenCalled ( ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockGetDevices ) . toHaveBeenCalled ( ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockGetDeviceId ) . toHaveBeenCalled ( ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockDeleteMultipleDevices ) . toHaveBeenCalled ( ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockInitCrypto ) . toHaveBeenCalled ( ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockGetJoinedRooms ) . toHaveBeenCalled ( ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockOn ) . toHaveBeenCalled ( ) ;
} ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockStartClient ) . toHaveBeenCalled ( ) ;
} ) ;
} ) ;
2020-09-06 18:07:44 +00:00
test ( '#sendBotSignal should send custom state event' , ( ) => {
const bot = new _bot . default ( botConfig ) ;
bot . start ( ) ;
const test _room _id = 'test_room_id' ;
const signal = 'END_CHAT' ;
bot . sendBotSignal ( test _room _id , signal ) ;
( 0 , _waitForExpect . default ) ( ( ) => {
expect ( _matrixJsSdk . mockSendStateEvent ) . toHaveBeenCalledWith ( test _room _id , 'm.bot.signal' , {
signal
} ) ;
} ) ;
} ) ;
2020-04-22 22:04:48 +00:00
} ) ;