|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const expect = require('chai').expect; |
| 4 | +const sinon = require('sinon'); |
| 5 | + |
| 6 | +module.exports.runTests = (wit) => { |
| 7 | + const log = wit.log; |
| 8 | + const Wit = wit.Wit; |
| 9 | + const interactive = wit.interactive; |
| 10 | + |
| 11 | + describe('logger', () => { |
| 12 | + let loggerStub; |
| 13 | + |
| 14 | + it('tests log flags', () => { |
| 15 | + expect(log.DEBUG).to.be.equal('debug'); |
| 16 | + expect(log.INFO).to.be.equal('info'); |
| 17 | + expect(log.WARN).to.be.equal('warn'); |
| 18 | + expect(log.ERROR).to.be.equal('error'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('tests logger (DEBUG)', () => { |
| 22 | + const logger = new log.Logger(log.DEBUG); |
| 23 | + loggerStub = sinon.stub(logger, 'info').returns(Promise.resolve()); |
| 24 | + logger.info('one', 'two', 'three'); |
| 25 | + expect(loggerStub.calledOnce).to.be.true; |
| 26 | + expect(loggerStub.thisValues[0].level).to.be.equal('debug'); |
| 27 | + expect(loggerStub.calledWith('one', 'two', 'three')).to.be.true; |
| 28 | + }); |
| 29 | + |
| 30 | + it('tests logger (INFO)', () => { |
| 31 | + const logger = new log.Logger(log.INFO); |
| 32 | + loggerStub = sinon.stub(logger, 'info').returns(Promise.resolve()); |
| 33 | + logger.info('one', 'two', 'three'); |
| 34 | + expect(loggerStub.calledOnce).to.be.true; |
| 35 | + expect(loggerStub.thisValues[0].level).to.be.equal('info'); |
| 36 | + expect(loggerStub.calledWith('one', 'two', 'three')).to.be.true; |
| 37 | + }); |
| 38 | + |
| 39 | + it('tests logger (WARN)', () => { |
| 40 | + const logger = new log.Logger(log.WARN); |
| 41 | + loggerStub = sinon.stub(logger, 'info').returns(Promise.resolve()); |
| 42 | + logger.info('one', 'two', 'three'); |
| 43 | + expect(loggerStub.calledOnce).to.be.true; |
| 44 | + expect(loggerStub.thisValues[0].level).to.be.equal('warn'); |
| 45 | + expect(loggerStub.calledWith('one', 'two', 'three')).to.be.true; |
| 46 | + }); |
| 47 | + |
| 48 | + it('tests logger (ERROR)', () => { |
| 49 | + const logger = new log.Logger(log.ERROR); |
| 50 | + loggerStub = sinon.stub(logger, 'info').returns(Promise.resolve()); |
| 51 | + logger.info('one', 'two', 'three'); |
| 52 | + expect(loggerStub.calledOnce).to.be.true; |
| 53 | + expect(loggerStub.thisValues[0].level).to.be.equal('error'); |
| 54 | + expect(loggerStub.calledWith('one', 'two', 'three')).to.be.true; |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('Wit', () => { |
| 59 | + let client = new Wit({ |
| 60 | + accessToken: process.env.WIT_TOKEN |
| 61 | + }); |
| 62 | + |
| 63 | + it('tests that Wit has correct functions', () => { |
| 64 | + const witFunctions = Object.keys(client); |
| 65 | + expect(witFunctions).to.eql(['config', '_sessions', 'message', 'converse', 'runActions']); |
| 66 | + }); |
| 67 | + |
| 68 | + it('tests message', () => { |
| 69 | + return client.message('Hello', {}) |
| 70 | + .then((data) => { |
| 71 | + expect(data.entities.intent[0].value).to.be.equal('greet'); |
| 72 | + expect(data._text).to.be.equal('Hello'); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('tests converse', () => { |
| 77 | + return client.converse(`session-${Date.now()}`, 'Hello', {}) |
| 78 | + .then((data) => { |
| 79 | + expect(data.entities.intent[0].value).to.be.equal('greet'); |
| 80 | + expect(data.msg).to.be.equal('Hello to you too!'); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('tests runActions', () => { |
| 85 | + const actions = { |
| 86 | + send: (request, response) => new Promise((resolve) => { |
| 87 | + expect(request.entities.intent[0].value).to.be.equal('greet'); |
| 88 | + expect(request.text).to.be.equal('Hello'); |
| 89 | + expect(response.text).to.be.equal('Hello to you too!'); |
| 90 | + resolve(); |
| 91 | + }) |
| 92 | + }; |
| 93 | + client = new Wit({ |
| 94 | + accessToken: process.env.WIT_TOKEN, |
| 95 | + actions |
| 96 | + }); |
| 97 | + return client.runActions(`session-${Date.now()}`, 'Hello', {}, 2); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('interactive', () => { |
| 102 | + it('checks that interactive exists', () => { |
| 103 | + expect(interactive).to.exists; |
| 104 | + }); |
| 105 | + }); |
| 106 | +}; |
0 commit comments