<commit_before>const CommandManager = require('./../core/command_manager.js');
const MockLogger = require('./mock_objects/mock_logger.js');
const MockMessage = require('./mock_objects/mock_message.js');
const assert = require('assert');
const MockConfig = require('./mock_objects/mock_config.js');
const strings = require('./../core/string_factory.js').commandManager;
const SettingsManager = require('./../core/settings_manager.js');

const config = new MockConfig('Server Admin', ['bot-admin-id']);
const MsgAboutCommand = new MockMessage('channel1', 'user1', 'Username', ['Server Admin'], [], 'bot!about suffix');
const MsgAboutCommandExtension = new MockMessage('channel1', 'user1', 'Username', ['Server Admin'], [], 'bot!aboutextension suffix');
const MsgHelpCommand = new MockMessage('channel1', 'user1', 'Username', ['Server Admin'], [], 'bot!help');

function createSettingsGetter(commandEnabled, otherSettings) {
  return {
    getSettings: (bot, msg, fullyQualifiedUserFacingSettingNames) => {
      let settings = {};
      for (let fullyQualifiedUserFacingSettingName of fullyQualifiedUserFacingSettingNames) {
        settings[fullyQualifiedUserFacingSettingName] = commandEnabled;
      }
      otherSettings = otherSettings || {};
      for (let otherSetting of Object.keys(otherSettings)) {
        settings[otherSetting] = otherSettings[otherSetting];
      }
      return Promise.resolve(settings);
    }
  };
}

let enabledSettingsGetter = createSettingsGetter(true);

describe('CommandManager', function() {
  describe('Load', function() {
    it('Refuses to load the command and complains in the logger if there is a bad command', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/mock_commands/invalid_and_valid', []).then(() => {
        assert(logger.failed === true);
        let invokeResult = commandManager.processInput(null, MsgHelpCommand, config);
        assert(invokeResult === false);
      });
    });
    it('Loads good commands even if it encounters a bad one', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/mock_commands/invalid_and_valid', []).then(() => {
        let invokeResult = commandManager.processInput(null, MsgAboutCommand, config);
        assert(invokeResult === true);
      });
    });
    it('Refuses to load command and complains if two commands have save uniqueId', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/mock_commands/duplicate_unique_ids', []).then(() => {
        assert(logger.failureMessage === strings.validation.createNonUniqueUniqueIdMessage('not unique'));
        let invokeResult1 = commandManager.processInput(null, MsgAboutCommand, config);
        let invokeResult2 = commandManager.processInput(null, MsgHelpCommand, config);
        assert(logger.failed === true);
        assert((invokeResult1 === true && invokeResult2 === false) || (invokeResult1 === false && invokeResult2 === true));
      });
    });
    it('Refuses to load command and complains if two commands have the same alias', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/mock_commands/duplicate_aliases', []).then(() => {
        assert(logger.failureMessage === strings.validation.createNonUniqueAliasMessage(undefined, 'duplicate'));
        let invokeResult1 = commandManager.processInput(null, MsgAboutCommand, config);
        let invokeResult2 = commandManager.processInput(null, MsgHelpCommand, config);
        assert(logger.failed === true);
        assert((invokeResult1 === true && invokeResult2 === false) || (invokeResult1 === false && invokeResult2 === true));
      });
    });
    it('Errors trying to load commands from nonexistent directory', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/nonexistent_directory', []).then(() => {
        assert(logger.failureMessage === strings.validation.genericError);
        assert(logger.failed === true);
      });
    });
    it('Gracefully handles command that throws', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/mock_commands/valid_throws', []).then(() => {
        commandManager.processInput(null, MsgAboutCommand, config);
        setTimeout(
          () => {
            assert(logger.failureMessage === strings.commandExecutionFailure.createErrorDescription(MsgAboutCommand.content));
            assert(logger.failed === true);
          }, 100);
      });
    });
    it('Converts string return values into failures and logs them', function(done) {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      commandManager.load(__dirname + '/mock_commands/valid_returns_string', []).then(() => {
        commandManager.processInput(null, MsgAboutCommand, config);
        setTimeout(
          () => {
            if (logger.failureMessage === 'Fail' && logger.failed) {
              done();
            } else {
              done('fail');
            }
          }, 100);
      });
    });
    it('Invokes command with extension', function(done) {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      commandManager.load(__dirname + '/mock_commands/valid_has_extension', []).then(() => {
        commandManager.processInput(null, MsgAboutCommandExtension, config);
        setTimeout(
          () => {
            let command = require('./mock_commands/valid_has_extension/has_extension.js');
            if (command.validateInvoked()) {
              done();
            } else {
              done('Validate invoked returned false');
            }
          }, 100);
      });
    });
    it('Creates the correct number of settings categories and the settings manager is able to load them', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/mock_commands/settings_category_test_commands_standard', []).then(() => {
        let categories = commandManager.collectSettingsCategories();
        assert(categories.length === 1);
        assert(categories[0].children.length === 6);
        let settingsManager = new SettingsManager(logger, config);
        settingsManager.load(categories, [], config);
      });
    });
    it('Returns empty array if no commands are allowed to be restricted', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/mock_commands/settings_category_test_commands_no_settings', []).then(() => {
        let categories = commandManager.collectSettingsCategories();
        assert(categories.length === 0);
        let settingsManager = new SettingsManager(logger, config);
        settingsManager.load(categories, [], config);
      });
    });
  });
});
<commit_msg>add tests to test reload command<commit_after>const CommandManager = require('./../core/command_manager.js');
const MockLogger = require('./mock_objects/mock_logger.js');
const MockMessage = require('./mock_objects/mock_message.js');
const assert = require('assert');
const MockConfig = require('./mock_objects/mock_config.js');
const strings = require('./../core/string_factory.js').commandManager;
const SettingsManager = require('./../core/settings_manager.js');

const config = new MockConfig('Server Admin', ['bot-admin-id']);
const MsgAboutCommand = new MockMessage('channel1', 'user1', 'Username', ['Server Admin'], [], 'bot!about suffix');
const MsgAboutCommandExtension = new MockMessage('channel1', 'user1', 'Username', ['Server Admin'], [], 'bot!aboutextension suffix');
const MsgHelpCommand = new MockMessage('channel1', 'user1', 'Username', ['Server Admin'], [], 'bot!help');
const MsgIsBotAdminReload = new MockMessage('channel1', 'bot-admin-id', 'Username', ['Server Admin'], [], '}reload');
const MsgIsServerAdminReload = new MockMessage('channel1', 'user1', 'Username', [], [], '}reload', ['manageGuild']);
const MsgNoPermsReload = new MockMessage('channel1', 'user1', 'Username', ['Server Admin'], [], '}reload');

function createSettingsGetter(commandEnabled, otherSettings) {
  return {
    getSettings: (bot, msg, fullyQualifiedUserFacingSettingNames) => {
      let settings = {};
      for (let fullyQualifiedUserFacingSettingName of fullyQualifiedUserFacingSettingNames) {
        settings[fullyQualifiedUserFacingSettingName] = commandEnabled;
      }
      otherSettings = otherSettings || {};
      for (let otherSetting of Object.keys(otherSettings)) {
        settings[otherSetting] = otherSettings[otherSetting];
      }
      return Promise.resolve(settings);
    }
  };
}

let enabledSettingsGetter = createSettingsGetter(true);

function testReloadCommand(msg, callback) {
  let logger = new MockLogger();
  let reloaded = false;
  let reloadLamba = () => {
    reloaded = true;
  };
  let commandManager = new CommandManager(reloadLamba, logger, config, enabledSettingsGetter);
  commandManager.load(__dirname + '/mock_commands/settings_category_test_commands_no_settings', []).then(() => {
    commandManager.processInput(null, msg, config);
    setTimeout(
      () => {
        debugger;
        callback(reloaded);
      }, 100);
  });
}

describe('CommandManager', function() {
  describe('Load', function() {
    it('Refuses to load the command and complains in the logger if there is a bad command', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/mock_commands/invalid_and_valid', []).then(() => {
        assert(logger.failed === true);
        let invokeResult = commandManager.processInput(null, MsgHelpCommand, config);
        assert(invokeResult === false);
      });
    });
    it('Loads good commands even if it encounters a bad one', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/mock_commands/invalid_and_valid', []).then(() => {
        let invokeResult = commandManager.processInput(null, MsgAboutCommand, config);
        assert(invokeResult === true);
      });
    });
    it('Refuses to load command and complains if two commands have save uniqueId', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/mock_commands/duplicate_unique_ids', []).then(() => {
        assert(logger.failureMessage === strings.validation.createNonUniqueUniqueIdMessage('not unique'));
        let invokeResult1 = commandManager.processInput(null, MsgAboutCommand, config);
        let invokeResult2 = commandManager.processInput(null, MsgHelpCommand, config);
        assert(logger.failed === true);
        assert((invokeResult1 === true && invokeResult2 === false) || (invokeResult1 === false && invokeResult2 === true));
      });
    });
    it('Refuses to load command and complains if two commands have the same alias', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/mock_commands/duplicate_aliases', []).then(() => {
        assert(logger.failureMessage === strings.validation.createNonUniqueAliasMessage(undefined, 'duplicate'));
        let invokeResult1 = commandManager.processInput(null, MsgAboutCommand, config);
        let invokeResult2 = commandManager.processInput(null, MsgHelpCommand, config);
        assert(logger.failed === true);
        assert((invokeResult1 === true && invokeResult2 === false) || (invokeResult1 === false && invokeResult2 === true));
      });
    });
    it('Errors trying to load commands from nonexistent directory', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/nonexistent_directory', []).then(() => {
        assert(logger.failureMessage === strings.validation.genericError);
        assert(logger.failed === true);
      });
    });
    it('Gracefully handles command that throws', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/mock_commands/valid_throws', []).then(() => {
        commandManager.processInput(null, MsgAboutCommand, config);
        setTimeout(
          () => {
            assert(logger.failureMessage === strings.commandExecutionFailure.createErrorDescription(MsgAboutCommand.content));
            assert(logger.failed === true);
          }, 100);
      });
    });
    it('Converts string return values into failures and logs them', function(done) {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      commandManager.load(__dirname + '/mock_commands/valid_returns_string', []).then(() => {
        commandManager.processInput(null, MsgAboutCommand, config);
        setTimeout(
          () => {
            if (logger.failureMessage === 'Fail' && logger.failed) {
              done();
            } else {
              done('fail');
            }
          }, 100);
      });
    });
    it('Invokes command with extension', function(done) {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      commandManager.load(__dirname + '/mock_commands/valid_has_extension', []).then(() => {
        commandManager.processInput(null, MsgAboutCommandExtension, config);
        setTimeout(
          () => {
            let command = require('./mock_commands/valid_has_extension/has_extension.js');
            if (command.validateInvoked()) {
              done();
            } else {
              done('Validate invoked returned false');
            }
          }, 100);
      });
    });
    it('Creates the correct number of settings categories and the settings manager is able to load them', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/mock_commands/settings_category_test_commands_standard', []).then(() => {
        let categories = commandManager.collectSettingsCategories();
        assert(categories.length === 1);
        assert(categories[0].children.length === 6);
        let settingsManager = new SettingsManager(logger, config);
        settingsManager.load(categories, [], config);
      });
    });
    it('Returns empty array if no commands are allowed to be restricted', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(null, logger, config, enabledSettingsGetter);
      return commandManager.load(__dirname + '/mock_commands/settings_category_test_commands_no_settings', []).then(() => {
        let categories = commandManager.collectSettingsCategories();
        assert(categories.length === 0);
        let settingsManager = new SettingsManager(logger, config);
        settingsManager.load(categories, [], config);
      });
    });
    it('Reload command works for bot admin', function(done) {
      testReloadCommand(MsgIsBotAdminReload, reloaded => {
        if (reloaded) {
          return done();
        }
        done('Didn\'t reload');
      });
    });
    it('Reload command doesnt work for server admin', function(done) {
      testReloadCommand(MsgIsServerAdminReload, reloaded => {
        if (!reloaded) {
          return done();
        }
        done('Reloaded, but should not have');
      });
    });
    it('Reload command doesnt work for regular user', function(done) {
      testReloadCommand(MsgNoPermsReload, reloaded => {
        if (!reloaded) {
          return done();
        }
        done('Reloaded, but should not have');
      });
    });
  });
});

{'fraction_non_alphanumeric': 0.13229160547494567, 'fraction_numerical': 0.004112083651530283, 'mean_word_length': 3.5911542610571736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6693507', 'n_tokens_mistral': 4281, 'n_tokens_neox': 4270, 'n_words': 1079}