Skip to content

Commit

Permalink
Adds babel for supporting older node versions (#95)
Browse files Browse the repository at this point in the history
* build for lambda

* adds legacy object to index

* removes legacy object, adds dev for running examples using lib

* removes dev index

* adds npmignore file

* adds publish.sh and tests

* adds missing line breaks

* adds missing line break

* removes unnecessary dev dependencies
  • Loading branch information
laardee authored and stopachka committed Nov 18, 2016
1 parent 84bc334 commit f20d6ee
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": [
"es2015",
"stage-0"
],
"comments": true,
"sourceMaps": false
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
node_modules
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- '6.9.0'
script:
- npm test
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module.exports = {
log: require('./lib/log'),
Wit: require('./lib/wit'),
interactive: require('./lib/interactive')
}
};
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>",
Expand All @@ -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"
}
}
14 changes: 14 additions & 0 deletions publish.sh
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions tests/dist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

describe('dist', () => {
require('./shared').runTests(require('../dist/index'));
});
5 changes: 5 additions & 0 deletions tests/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

describe('lib', () => {
require('./shared').runTests(require('../index'));
});
106 changes: 106 additions & 0 deletions tests/shared.js
Original file line number Diff line number Diff line change
@@ -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;
});
});
};
Binary file added tests/wit-ai-app-for-tests.zip
Binary file not shown.

0 comments on commit f20d6ee

Please sign in to comment.