"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } var path = _interopRequireWildcard(require("path")); var os = _interopRequireWildcard(require("os")); var _waitForExpect = _interopRequireDefault(require("wait-for-expect")); var _matrixJsSdk = require("matrix-js-sdk"); var _bot = _interopRequireDefault(require("./bot")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } require('dotenv').config(); describe('OcrccBot', function () { beforeEach(function () { _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(); }); test('constructor should inititialize matrix client', function () { var bot = new _bot["default"](); expect(_matrixJsSdk.createClient).toHaveBeenCalledWith(process.env.MATRIX_SERVER_URL); expect(bot.joinedRooms).toEqual([]); expect(bot.awaitingFacilitator).toEqual({}); expect(bot.activeChatrooms).toEqual({}); }); test('#createLocalStorage should have correct storage location', function () { var bot = new _bot["default"](); var localStorage = bot.createLocalStorage(); var localStoragePath = path.resolve(path.join(os.homedir(), ".local-storage", "matrix-chatbot-".concat(process.env.BOT_USERNAME))); expect(localStorage._location).toBe(localStoragePath); }); test('#sendMessage should send a text message', function () { var bot = new _bot["default"](); bot.start(); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockStartClient).toHaveBeenCalled(); }); var testRoom = 'room_id_1234'; var testMsg = 'test message'; bot.sendMessage(testRoom, testMsg); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockSetDeviceVerified).toHaveBeenCalledTimes(2); }); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockSendTextMessage).toHaveBeenCalledWith(testRoom, testMsg); }); }); test('#inviteUserToRoom should add member to room and retry on rate limit error', function () { var bot = new _bot["default"](); bot.start(); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockStartClient).toHaveBeenCalled(); }); bot.inviteUserToRoom(bot.client, 'room_id_1234', process.env.BOT_USERNAME); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockInvite).toHaveBeenCalledTimes(2); }); }); test('#kickUserFromRoom should remove member from room and retry on rate limit error', function () { var bot = new _bot["default"](); bot.start(); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockStartClient).toHaveBeenCalled(); }); bot.kickUserFromRoom(bot.client, 'room_id_1234', process.env.BOT_USERNAME); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockKick).toHaveBeenCalledTimes(2); }); }); test('#inviteFacilitators should invite all members from Facilitator room', function () { var bot = new _bot["default"](); bot.start(); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockStartClient).toHaveBeenCalled(); }); bot.inviteFacilitators(); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockGetJoinedRoomMembers).toHaveBeenCalledWith(process.env.FACILITATOR_ROOM_ID); }); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockGetUser).toHaveBeenCalledTimes(2); }); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockInvite).toHaveBeenCalledTimes(2); }); }); test('#uninviteFacilitators should remove all members that have not accepted the invite', function () { var bot = new _bot["default"](); bot.start(); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockStartClient).toHaveBeenCalled(); }); bot.uninviteFacilitators(); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockGetJoinedRoomMembers).toHaveBeenCalledWith(process.env.FACILITATOR_ROOM_ID); }); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockGetJoinedRoomMembers).toHaveBeenCalledWith('room_id_1234'); }); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockKick).toHaveBeenCalled(); }); }); test('#handleBotCrash should notify rooms', function () {}); test('#writeToTranscript should parse event and write to transcript file', function () {}); test('#deleteOldDevices should delete old sessions', function () {}); // TODO test listeners for membership events and message events test('#start should start bot and set up listeners', function () { var bot = new _bot["default"](); bot.start(); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockLogin).toHaveBeenCalled(); }); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.WebStorageSessionStore).toHaveBeenCalled(); }); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.createClient).toHaveBeenCalled(); }); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockGetDevices).toHaveBeenCalled(); }); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockGetDeviceId).toHaveBeenCalled(); }); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockDeleteMultipleDevices).toHaveBeenCalled(); }); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockInitCrypto).toHaveBeenCalled(); }); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockGetJoinedRooms).toHaveBeenCalled(); }); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockOn).toHaveBeenCalled(); }); (0, _waitForExpect["default"])(function () { expect(_matrixJsSdk.mockStartClient).toHaveBeenCalled(); }); }); });