diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..94d651d --- /dev/null +++ b/.babelrc @@ -0,0 +1,8 @@ +{ + "presets": [ + "es2015", + "stage-0" + ], + "comments": true, + "sourceMaps": false +} diff --git a/.gitignore b/.gitignore index 3c3629e..de4d1f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +dist node_modules diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..86d3bc9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: +- '6.9.0' +script: +- npm test diff --git a/README.md b/README.md index 795c9d9..56c6f84 100644 --- a/README.md +++ b/README.md @@ -209,3 +209,9 @@ Version prior to 20160511 will return the old format: "WARNING" : "DEPRECATED" } ``` + +## Running tests + +1. Create a new app in wit.ai web console using tests/wit-ai-app-for-tests.zip +2. Copy the Server Access Token from app settings +3. Run `WIT_TOKEN=XXX npm test`, where XXX is the Server Access Token diff --git a/index.js b/index.js index 2fcc76e..214be7a 100644 --- a/index.js +++ b/index.js @@ -2,4 +2,4 @@ module.exports = { log: require('./lib/log'), Wit: require('./lib/wit'), interactive: require('./lib/interactive') -} +}; diff --git a/package.json b/package.json index c66973a..ed45d6f 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ ], "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "mocha ./tests/lib.js" }, "repository": "https://github.com/wit-ai/node-wit", "author": "The Wit Team ", @@ -23,5 +23,13 @@ }, "engines": { "node": ">=4.0.0" + }, + "devDependencies": { + "babel-cli": "^6.16.0", + "babel-preset-es2015": "^6.16.0", + "babel-preset-stage-0": "^6.16.0", + "chai": "^3.5.0", + "mocha": "^3.1.2", + "sinon": "^1.17.6" } } diff --git a/publish.sh b/publish.sh new file mode 100755 index 0000000..6528d82 --- /dev/null +++ b/publish.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +set -ex + +mkdir -p dist +cp package.json dist +babel lib --out-dir dist/lib +babel index.js --out-file dist/index.js +mocha ./tests/dist.js +( + cd dist + npm publish +) +rm -rf dist diff --git a/tests/dist.js b/tests/dist.js new file mode 100644 index 0000000..0973a8f --- /dev/null +++ b/tests/dist.js @@ -0,0 +1,5 @@ +'use strict'; + +describe('dist', () => { + require('./shared').runTests(require('../dist/index')); +}); diff --git a/tests/lib.js b/tests/lib.js new file mode 100644 index 0000000..3b1a1d4 --- /dev/null +++ b/tests/lib.js @@ -0,0 +1,5 @@ +'use strict'; + +describe('lib', () => { + require('./shared').runTests(require('../index')); +}); diff --git a/tests/shared.js b/tests/shared.js new file mode 100644 index 0000000..fbebed2 --- /dev/null +++ b/tests/shared.js @@ -0,0 +1,106 @@ +'use strict'; + +const expect = require('chai').expect; +const sinon = require('sinon'); + +module.exports.runTests = (wit) => { + const log = wit.log; + const Wit = wit.Wit; + const interactive = wit.interactive; + + describe('logger', () => { + let loggerStub; + + it('tests log flags', () => { + expect(log.DEBUG).to.be.equal('debug'); + expect(log.INFO).to.be.equal('info'); + expect(log.WARN).to.be.equal('warn'); + expect(log.ERROR).to.be.equal('error'); + }); + + it('tests logger (DEBUG)', () => { + const logger = new log.Logger(log.DEBUG); + loggerStub = sinon.stub(logger, 'info').returns(Promise.resolve()); + logger.info('one', 'two', 'three'); + expect(loggerStub.calledOnce).to.be.true; + expect(loggerStub.thisValues[0].level).to.be.equal('debug'); + expect(loggerStub.calledWith('one', 'two', 'three')).to.be.true; + }); + + it('tests logger (INFO)', () => { + const logger = new log.Logger(log.INFO); + loggerStub = sinon.stub(logger, 'info').returns(Promise.resolve()); + logger.info('one', 'two', 'three'); + expect(loggerStub.calledOnce).to.be.true; + expect(loggerStub.thisValues[0].level).to.be.equal('info'); + expect(loggerStub.calledWith('one', 'two', 'three')).to.be.true; + }); + + it('tests logger (WARN)', () => { + const logger = new log.Logger(log.WARN); + loggerStub = sinon.stub(logger, 'info').returns(Promise.resolve()); + logger.info('one', 'two', 'three'); + expect(loggerStub.calledOnce).to.be.true; + expect(loggerStub.thisValues[0].level).to.be.equal('warn'); + expect(loggerStub.calledWith('one', 'two', 'three')).to.be.true; + }); + + it('tests logger (ERROR)', () => { + const logger = new log.Logger(log.ERROR); + loggerStub = sinon.stub(logger, 'info').returns(Promise.resolve()); + logger.info('one', 'two', 'three'); + expect(loggerStub.calledOnce).to.be.true; + expect(loggerStub.thisValues[0].level).to.be.equal('error'); + expect(loggerStub.calledWith('one', 'two', 'three')).to.be.true; + }); + }); + + describe('Wit', () => { + let client = new Wit({ + accessToken: process.env.WIT_TOKEN + }); + + it('tests that Wit has correct functions', () => { + const witFunctions = Object.keys(client); + expect(witFunctions).to.eql(['config', '_sessions', 'message', 'converse', 'runActions']); + }); + + it('tests message', () => { + return client.message('Hello', {}) + .then((data) => { + expect(data.entities.intent[0].value).to.be.equal('greet'); + expect(data._text).to.be.equal('Hello'); + }); + }); + + it('tests converse', () => { + return client.converse(`session-${Date.now()}`, 'Hello', {}) + .then((data) => { + expect(data.entities.intent[0].value).to.be.equal('greet'); + expect(data.msg).to.be.equal('Hello to you too!'); + }); + }); + + it('tests runActions', () => { + const actions = { + send: (request, response) => new Promise((resolve) => { + expect(request.entities.intent[0].value).to.be.equal('greet'); + expect(request.text).to.be.equal('Hello'); + expect(response.text).to.be.equal('Hello to you too!'); + resolve(); + }) + }; + client = new Wit({ + accessToken: process.env.WIT_TOKEN, + actions + }); + return client.runActions(`session-${Date.now()}`, 'Hello', {}, 2); + }); + }); + + describe('interactive', () => { + it('checks that interactive exists', () => { + expect(interactive).to.exists; + }); + }); +}; diff --git a/tests/wit-ai-app-for-tests.zip b/tests/wit-ai-app-for-tests.zip new file mode 100644 index 0000000..797d6d7 Binary files /dev/null and b/tests/wit-ai-app-for-tests.zip differ