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 config = new MockConfig('Server Admin', ['bot-admin-id']);
const MsgAboutCommand = new MockMessage('channel1', 'user1', 'Username', ['Server Admin'], [], 'bot!about suffix');
const MsgHelpCommand = new MockMessage('channel1', 'user1', 'Username', ['Server Admin'], [], 'bot!help');

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(__dirname + '/mock_commands/invalid_and_valid', null, logger, MockConfig);
      return commandManager.load(undefined, MockConfig).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(__dirname + '/mock_commands/invalid_and_valid', null, logger, MockConfig);
      return commandManager.load(undefined, MockConfig).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(__dirname + '/mock_commands/duplicate_unique_ids', null, logger, MockConfig);
      return commandManager.load(undefined, MockConfig).then(() => {
        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(__dirname + '/mock_commands/duplicate_aliases', null, logger, MockConfig);
      return commandManager.load(undefined, MockConfig).then(() => {
        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(__dirname + '/nonexistent_directory', null, logger, MockConfig);
      return commandManager.load(undefined, MockConfig).then(() => {
        assert(logger.failed === true);
      });
    });
    it('Gracefully handles command that throws', function() {
      let logger = new MockLogger();
      let commandManager = new CommandManager(__dirname + '/mock_commands/valid_throws', null, logger, MockConfig);
      return commandManager.load(undefined, MockConfig).then(() => {
        commandManager.processInput(null, MsgAboutCommand, config);
        assert(logger.failed === true);
      });
    });
  });
});


@@ -566,16 +566,362 @@
 elp');%0A%0A
+let enabledSettingsGetter = %7B%0A  getSettings: (bot, msg, fullyQualifiedUserFacingSettingNames) =%3E %7B%0A    let settings = %7B%0A      serverSettings: %7B%7D,%0A    %7D;%0A    for (let fullyQualifiedUserFacingSettingName of fullyQualifiedUserFacingSettingNames) %7B%0A      serverSettings%5BfullyQualifiedUserFacingSettingName%5D = true;%0A    %7D%0A    return settings;%0A  %7D%0A%7D;%0A%0A
 describe
@@ -1176,56 +1176,8 @@
 ger(
-__dirname + '/mock_commands/invalid_and_valid', 
 null
@@ -1188,32 +1188,55 @@
 gger, MockConfig
+, enabledSettingsGetter
 );%0A      return 
@@ -1255,37 +1255,66 @@
 er.load(
-undefined, MockConfig
+__dirname + '/mock_commands/invalid_and_valid', %5B%5D
 ).then((
@@ -1666,56 +1666,8 @@
 ger(
-__dirname + '/mock_commands/invalid_and_valid', 
 null
@@ -1678,32 +1678,55 @@
 gger, MockConfig
+, enabledSettingsGetter
 );%0A      return 
@@ -1745,37 +1745,66 @@
 er.load(
-undefined, MockConfig
+__dirname + '/mock_commands/invalid_and_valid', %5B%5D
 ).then((
@@ -2137,59 +2137,8 @@
 ger(
-__dirname + '/mock_commands/duplicate_unique_ids', 
 null
@@ -2149,32 +2149,55 @@
 gger, MockConfig
+, enabledSettingsGetter
 );%0A      return 
@@ -2216,37 +2216,69 @@
 er.load(
-undefined, MockConfig
+__dirname + '/mock_commands/duplicate_unique_ids', %5B%5D
 ).then((
@@ -2825,56 +2825,8 @@
 ger(
-__dirname + '/mock_commands/duplicate_aliases', 
 null
@@ -2837,32 +2837,55 @@
 gger, MockConfig
+, enabledSettingsGetter
 );%0A      return 
@@ -2904,37 +2904,66 @@
 er.load(
-undefined, MockConfig
+__dirname + '/mock_commands/duplicate_aliases', %5B%5D
 ).then((
@@ -3494,46 +3494,8 @@
 ger(
-__dirname + '/nonexistent_directory', 
 null
@@ -3506,32 +3506,55 @@
 gger, MockConfig
+, enabledSettingsGetter
 );%0A      return 
@@ -3573,37 +3573,56 @@
 er.load(
-undefined, MockConfig
+__dirname + '/nonexistent_directory', %5B%5D
 ).then((
@@ -3835,51 +3835,8 @@
 ger(
-__dirname + '/mock_commands/valid_throws', 
 null
@@ -3851,24 +3851,47 @@
 , MockConfig
+, enabledSettingsGetter
 );%0A      ret
@@ -3918,29 +3918,53 @@
 oad(
-undefined, MockConfig
+__dirname + '/mock_commands/valid_throws', %5B%5D
 ).th

fix command manager tests