Skip to content

Commit f20d6ee

Browse files
laardeestopachka
authored andcommitted
Adds babel for supporting older node versions (wit-ai#95)
* 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
1 parent 84bc334 commit f20d6ee

11 files changed

+160
-2
lines changed

.babelrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": [
3+
"es2015",
4+
"stage-0"
5+
],
6+
"comments": true,
7+
"sourceMaps": false
8+
}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
dist
12
node_modules

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- '6.9.0'
4+
script:
5+
- npm test

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,9 @@ Version prior to 20160511 will return the old format:
209209
"WARNING" : "DEPRECATED"
210210
}
211211
```
212+
213+
## Running tests
214+
215+
1. Create a new app in wit.ai web console using tests/wit-ai-app-for-tests.zip
216+
2. Copy the Server Access Token from app settings
217+
3. Run `WIT_TOKEN=XXX npm test`, where XXX is the Server Access Token

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module.exports = {
22
log: require('./lib/log'),
33
Wit: require('./lib/wit'),
44
interactive: require('./lib/interactive')
5-
}
5+
};

package.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"main": "index.js",
1515
"scripts": {
16-
"test": "echo \"Error: no test specified\" && exit 1"
16+
"test": "mocha ./tests/lib.js"
1717
},
1818
"repository": "https://github.com/wit-ai/node-wit",
1919
"author": "The Wit Team <[email protected]>",
@@ -23,5 +23,13 @@
2323
},
2424
"engines": {
2525
"node": ">=4.0.0"
26+
},
27+
"devDependencies": {
28+
"babel-cli": "^6.16.0",
29+
"babel-preset-es2015": "^6.16.0",
30+
"babel-preset-stage-0": "^6.16.0",
31+
"chai": "^3.5.0",
32+
"mocha": "^3.1.2",
33+
"sinon": "^1.17.6"
2634
}
2735
}

publish.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
set -ex
4+
5+
mkdir -p dist
6+
cp package.json dist
7+
babel lib --out-dir dist/lib
8+
babel index.js --out-file dist/index.js
9+
mocha ./tests/dist.js
10+
(
11+
cd dist
12+
npm publish
13+
)
14+
rm -rf dist

tests/dist.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
describe('dist', () => {
4+
require('./shared').runTests(require('../dist/index'));
5+
});

tests/lib.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
describe('lib', () => {
4+
require('./shared').runTests(require('../index'));
5+
});

tests/shared.js

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
};

tests/wit-ai-app-for-tests.zip

1.41 KB
Binary file not shown.

0 commit comments

Comments
 (0)