Skip to content

Commit

Permalink
Shift behaviors to bot.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzokuken committed Jun 28, 2017
1 parent 12982d7 commit 7b1dccb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
15 changes: 10 additions & 5 deletions spec/help-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,37 @@ const helpMessage = require('../src/behaviors/help').helpMessage;

describe('Help Behavior', () => {
const botNick = 'testbot';
const behaviors = new Behaviors(botNick, undefined);
const helpBehavior = require('../src/behaviors/help').helpBehavior;
const behaviors = new Behaviors(botNick, undefined, [], [helpBehavior]);

const chatbotHelp = helpMessage(botNick, 'chatbot');
const invalidHelp = helpMessage(botNick, 'invalid');

it('should print general help', () => {
it('should print general help', (done) => {
behaviors.getResponse(botNick, 'help').then(response => {
expect(response).toBe(chatbotHelp);
done();
});
});

it('should print specific help for existing modules', () => {
it('should print specific help for existing modules', (done) => {
behaviors.getResponse(botNick, 'help chatbot').then(response => {
expect(response).toBe(chatbotHelp);
done();
});
});

it('should print specific help for nonexisting modules', () => {
it('should print specific help for nonexisting modules', (done) => {
behaviors.getResponse(botNick, 'help invalid').then(response => {
expect(response).toBe(invalidHelp);
done();
});
});

it('should print specific help for existing and nonexisting modules combined', () => {
it('should print specific help for existing and nonexisting modules combined', (done) => {
behaviors.getResponse(botNick, 'help chatbot invalid').then(response => {
expect(response).toBe(chatbotHelp + '\n\n' + invalidHelp);
done();
});
});
});
9 changes: 3 additions & 6 deletions src/behaviors/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const utils = require('../utils');

const greetBehavior = require('./greet');
const helpBehavior = require('./help').helpBehavior;

function parseMessage (message) {
return message.split(/[\s,.;:!?]/g).filter(String);
}
Expand Down Expand Up @@ -30,11 +27,11 @@ function messageResponse(botNick, parsed, behaviors) {
}

class Behaviors {
constructor(botNick, client) {
constructor(botNick, client, joinBehaviors, messageBehaviors) {
this.botNick = botNick;
this.client = client;
this.joinBehaviors = [ greetBehavior ];
this.messageBehaviors = [ helpBehavior ];
this.joinBehaviors = joinBehaviors;
this.messageBehaviors = messageBehaviors;
}

addJoinHandler() {
Expand Down
13 changes: 12 additions & 1 deletion src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const fs = require('fs');

const Behaviors = require('./behaviors');

const greetBehavior = require('./behaviors/greet');
const helpBehavior = require('./behaviors/help').helpBehavior;

// Read file synchronously because we'd need this object in later steps anyway.
const config = JSON.parse(fs.readFileSync('config.json', 'utf8'));

Expand All @@ -17,5 +20,13 @@ if (process.env.TEST) {
client = new IrcClient(config.server, config.name, config.channels);
}

const behaviors = new Behaviors(config.name, client);
const joinBehaviors = [
greetBehavior
];

const messageBehaviors = [
helpBehavior
];

const behaviors = new Behaviors(config.name, client, joinBehaviors, messageBehaviors);
behaviors.bootstrap();

0 comments on commit 7b1dccb

Please sign in to comment.