Skip to content

Commit

Permalink
Merge 1aa6dc9 into d06894f
Browse files Browse the repository at this point in the history
  • Loading branch information
Scar26 committed May 25, 2020
2 parents d06894f + 1aa6dc9 commit 4f2b455
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ node_modules/
package-lock.json
test.js
.idea/
.nyc_output
.nyc_output/
build/
25 changes: 23 additions & 2 deletions factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,32 @@
var trainingSet
var responseSet

function process (query) {
return global.responseSet[query]
var users = {
idmap: {},

addUser: function (token, name) {
this.idmap[token] = name
},

get: function (token) {
return this.idmap[token]
}
}

function train () {
global.responseSet = global.trainingSet
}

function process (query, token) {
if (users.get(token)) {
return { action: 'response', message: responseSet[query] }
} else {
return { action: 'unrecognized', message: 'user does not exist' }
}
}

function currentUser (token) {
return users.idmap[token]
}

/* eslint-enable no-unused-vars */
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ class Bot {
this.factory = new VM()
this.factory.run(ctx)
this.factory.run(`trainingSet=${trainingSet}`)
this.responses = null
}

greet () {
return this.render(this.greeting)
}

render (statement) {
return statement.replace(/<bot-name>/g, this.name)
addUser (token, name) {
this.factory.run(`users.addUser("${token}", "${name}")`)
}

respond (query) {
return this.render(this.factory.run(`process("${query}")`))
render (statement, token) {
return statement.replace(/<bot-name>/g, this.name).replace(/<customer-name>/g, this.factory.run(`currentUser(${token})`))
}

respond (query, token) {
const response = this.factory.run(`process("${query}", "${token}")`)
response.message = this.render(response.message, token)
return response
}

train () {
Expand Down
25 changes: 23 additions & 2 deletions test/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,32 @@
var trainingSet
var responseSet

function process (query) {
return global.responseSet[query]
var users = {
idmap: {},

addUser: function (token, name) {
this.idmap[token] = name
},

get: function (token) {
return this.idmap[token]
}
}

function train () {
global.responseSet = global.trainingSet
}

function process (query, token) {
if (users.get(token)) {
return { action: 'response', message: responseSet[query] }
} else {
return { action: 'unrecognized', message: 'user does not exist' }
}
}

function currentUser (token) {
return users.idmap[token]
}

/* eslint-enable no-unused-vars */
19 changes: 17 additions & 2 deletions test/initialize-spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const chai = require('chai')
const expect = chai.expect
const juice = require('../index')
const trainingSet = { 'hi bot': 'hello', "what's your name?": '<bot-name>' }
const trainingSet = { 'hi bot': 'hello <customer-name>', "what's your name?": '<bot-name>' }

describe('Initialize', () => {
let bot

beforeEach(() => {
bot = new juice.Bot('Jeff', 'Ma Nemma <bot-name>', JSON.stringify(trainingSet))
expect(() => bot.addUser('123', 'test-user')).to.not.throw()
})

it('should set up greeting for bot', () => {
Expand All @@ -23,8 +24,22 @@ describe('Initialize', () => {
expect(bot.factory.run('responseSet')).to.deep.equal(trainingSet)
})

it('should recognize registered user from token', () => {
expect(() => bot.train()).to.not.throw()
expect(bot.respond('hi bot', '123').action).to.equal('response')
expect(bot.respond('hi bot', '123').message).to.equal('hello test-user')
})

it('should register new user with corresponsing token', () => {
expect(() => bot.train()).to.not.throw()
expect(() => bot.addUser('1234', 'user2')).to.not.throw()
expect(bot.respond('hi bot', '1234').action).to.equal('response')
expect(bot.respond('hi bot', '1234').message).to.equal('hello user2')
})

it('should respond to registered query after training', () => {
expect(() => bot.train()).to.not.throw()
expect(bot.respond("what's your name?")).to.equal('Jeff')
expect(bot.respond("what's your name?", '123').action).to.equal('response')
expect(bot.respond("what's your name?", '123').message).to.equal('Jeff')
})
})

0 comments on commit 4f2b455

Please sign in to comment.