Skip to content

Commit

Permalink
Merge pull request #274 from SockDrawer/accalia-dev
Browse files Browse the repository at this point in the history
Refactor `help` command
  • Loading branch information
RaceProUK committed Jan 26, 2016
2 parents 629255f + d9671bd commit be78c45
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/api/lib/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Shut the bot up until manually restarted

<a name="module_commands..cmdHelp"></a>
### commands~cmdHelp(command)
Reply with help test top the command !help
Reply with help to the command !help

**Kind**: inner method of <code>[commands](#module_commands)</code>

Expand Down
41 changes: 34 additions & 7 deletions lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,36 @@ exports.parseCommands = function parseCommands(post, topic, callback) {
* @returns {string} command list for posting
*/
function getCommandHelps() {
const cmds = Object.keys(internals.commands),
const cmds = {},
topics = {},
result = ['Registered commands:'];
cmds.sort();
cmds.forEach((cmd) => result.push(cmd + ': ' + internals.commands[cmd].help));
let keys = {};
Object.keys(internals.commands).map((key) => {
keys[key] = 1;
});
Object.keys(internals.helpMessages).map((key) => {
keys[key] = 1;
});
Object.keys(keys).map((key) => {
if (internals.commands[key]) {
cmds[key] = internals.commands[key].help;
if (internals.helpMessages[key]) {
cmds[key] += ' *';
}
} else {
topics[key] = 'Extended help topic';
}
});
keys = Object.keys(cmds);
keys.sort();
keys.forEach((cmd) => result.push(cmd + ': ' + cmds[cmd]));
keys = Object.keys(topics);
if (keys.length) {
result.push('');
result.push('Help Topics:');
keys.sort();
keys.forEach((topic) => result.push(topic + ': ' + topics[topic]));
}
return result.join('\n');
}

Expand Down Expand Up @@ -197,7 +223,7 @@ function cmdShutUp(command) {
}

/**
* Reply with help test top the command !help
* Reply with help to the command !help
*
* @param {command} command help command
*/
Expand All @@ -210,13 +236,13 @@ function cmdHelp(command) {
ext = command.args.join(' ');
}
if (ext && internals.helpMessages[ext]) {
const txt = 'Extended help for `' + ext + '`\n\n' + internals.helpMessages[ext] +
const txt = 'Help topic for `' + ext + '`\n\n' + internals.helpMessages[ext] +
'\n\nIssue the `help` command without any parameters to see all available commands';
browser.createPost(command.post.topic_id, command.post.post_number, txt, () => 0);
return;
}
const help = internals.getCommandHelps() + '\n\nMore details may be available by passing a command name as ' +
'the first parameter to `help`';
const help = internals.getCommandHelps() + '\n\n\* Help topic available.\n\nIssue the `help` command with an ' +
'available help topic as a parameter to read additonal help';
browser.createPost(command.post.topic_id, command.post.post_number, help, () => 0);
}

Expand Down Expand Up @@ -277,6 +303,7 @@ function registerHelp(command, helptext, callback) {
if (!helptext || typeof helptext !== 'string') {
return callback(new Error('helptext must be provided'));
}
command = command.toLowerCase();
if (internals.helpMessages[command]) {
internals.events.emit('logWarning', 'Overwriting existing extended help for: `' + command + '`!');
}
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/statusTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('status', () => {
});
describe('memoryUsage', () => {
it('should return the correct values', () => {
const expected = 'Memory usage: 1 kB free out of 2 kB';
const expected = 'Memory usage: 1 KB free out of 2 KB';
sandbox.stub(os, 'freemem', () => 1024);
sandbox.stub(os, 'totalmem', () => 2048);
expect(status.internals.memoryUsage()).to.be.equal(expected);
Expand Down
67 changes: 60 additions & 7 deletions test/lib/commandsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ describe('commands', () => {
let sandbox, events;
beforeEach(() => {
commands.internals.commands = {};
commands.internals.helpMessages = {};
sandbox = sinon.sandbox.create();
clock = sandbox.useFakeTimers();
events = {
Expand All @@ -183,8 +184,8 @@ describe('commands', () => {
});
it('should post expected text', () => {
const expected = 'Registered commands:\nhelp: print command help listing\n' +
'shutup: tell me to shutup\n\nMore details may be available by passing ' +
'a command name as the first parameter to `help`';
'shutup: tell me to shutup\n\n* Help topic available.\n\nIssue the `help`' +
' command with an available help topic as a parameter to read additonal help';
commands.internals.commands.help = {
help: 'print command help listing'
};
Expand All @@ -202,6 +203,57 @@ describe('commands', () => {
browser.createPost.calledWith(15, 75).should.be.true;
browser.createPost.lastCall.args[2].should.equal(expected);
});
it('should post expected text with help topics', () => {
const expected = 'Registered commands:\nhelp: print command help listing\n\n' +
'Help Topics:\nshutup: Extended help topic\n\n* Help topic available.\n\n' +
'Issue the `help` command with an available help topic as a parameter to ' +
'read additonal help';
commands.internals.commands.help = {
help: 'print command help listing'
};
commands.internals.helpMessages.shutup = 'tell me to shutup';
cmdHelp({
command: 'foobar',
post: {
'topic_id': 15,
'post_number': 75
}
});
browser.createPost.callCount.should.equal(1);
browser.createPost.calledWith(15, 75).should.be.true;
browser.createPost.lastCall.args[2].should.equal(expected);
});
it('should pass callback to createPost', () => {
cmdHelp({
command: 'foobar',
post: {
'topic_id': 1,
'post_number': 5
}
});
browser.createPost.lastCall.args[3].should.be.a('function');
browser.createPost.lastCall.args[3]().should.equal(0);
});

it('should indicate presence of help topic on command', () => {
const expected = 'Registered commands:\nhelp: print command help listing *\n\n' +
'* Help topic available.\n\nIssue the `help` command with an available help ' +
'topic as a parameter to read additonal help';
commands.internals.commands.help = {
help: 'print command help listing'
};
commands.internals.helpMessages.help = 'foobar';
cmdHelp({
command: 'foobar',
post: {
'topic_id': 15,
'post_number': 75
}
});
browser.createPost.callCount.should.equal(1);
browser.createPost.calledWith(15, 75).should.be.true;
browser.createPost.lastCall.args[2].should.equal(expected);
});
it('should pass callback to createPost', () => {
cmdHelp({
command: 'foobar',
Expand Down Expand Up @@ -256,7 +308,7 @@ describe('commands', () => {
browser.createPost.lastCall.args[2].should.startWith(expected);
});
it('should post extended help message one word command', () => {
const expected = 'Extended help for `whosit`\n\nwhosit extended help' +
const expected = 'Help topic for `whosit`\n\nwhosit extended help' +
'\n\nIssue the `help` command without any parameters to see all available commands';
commands.internals.helpMessages.whosit = 'whosit extended help';
cmdHelp({
Expand All @@ -272,7 +324,7 @@ describe('commands', () => {
browser.createPost.lastCall.args[2].should.equal(expected);
});
it('should post extended help message multi-word command', () => {
const expected = 'Extended help for `who am i`';
const expected = 'Help topic for `who am i`';
commands.internals.helpMessages['who am i'] = 'whosit extended help';
cmdHelp({
command: 'foobar',
Expand Down Expand Up @@ -764,13 +816,14 @@ describe('commands', () => {
it('should log registration', () => {
const cmd = 'CMD' + Math.random();
registerHelp(cmd, 'help', spy);
events.emit.calledWith('logMessage', 'Extended help registered for: ' + cmd).should.be.true;
events.emit.calledWith('logMessage', 'Extended help registered for: ' +
cmd.toLowerCase()).should.be.true;
});
it('should warn on help overwrite', () => {
const cmd = 'CMD' + Math.random();
commands.internals.helpMessages[cmd] = 'foo';
commands.internals.helpMessages[cmd.toLowerCase()] = 'foo';
registerHelp(cmd, 'help', spy);
events.emit.calledWith('logWarning', 'Overwriting existing extended help for: `' + cmd +
events.emit.calledWith('logWarning', 'Overwriting existing extended help for: `' + cmd.toLowerCase() +
'`!').should.be.true;
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/plugins/anonymizeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ describe('anonymize', () => {
const spy = sandbox.stub(browser, 'createPost'),
rawContent = '[quote="SockBot, post:4, topic:3"]Anonymized quote![/quote]Anonymized reply!';
spy.yields(null, {
topic_id: 5,
post_number: 6
'topic_id': 5,
'post_number': 6
}); //eslint-disable-line camelcase
anonymize.prepare(undefined, dummyCfg, {
onNotification: () => 0,
Expand Down

0 comments on commit be78c45

Please sign in to comment.