diff --git a/.eslintrc b/.eslintrc index 549455b..3a08d0f 100644 --- a/.eslintrc +++ b/.eslintrc @@ -2,7 +2,8 @@ "extends": "eslint:recommended", "env": { "node": true, - "es6": true + "es6": true, + "jest": true }, "rules": { "no-console": 0 diff --git a/.travis.yml b/.travis.yml index 95ba788..d1f8267 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,12 @@ +dist: trusty language: node_js node_js: - "--lts" +before_install: + - sudo apt-get -qq update + - sudo apt-get install -y sox libatlas-base-dev + cache: directories: - - node_modules + - node_modules \ No newline at end of file diff --git a/package.json b/package.json index f138a00..ec6d4ff 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "Open source cross platform decentralized always-on speech recognition framework", "main": "index.js", "scripts": { - "test": "eslint ." + "test": "jest", + "lint": "eslint ." }, "repository": { "type": "git", @@ -31,6 +32,7 @@ "snowboy": "^1.1.0" }, "devDependencies": { - "eslint": "^3.7.0" + "eslint": "^3.7.0", + "jest": "^20.0.4" } } diff --git a/test/Sonus.test.js b/test/Sonus.test.js new file mode 100644 index 0000000..9362da4 --- /dev/null +++ b/test/Sonus.test.js @@ -0,0 +1,35 @@ +const Sonus = require('../index') +const mockData = "mydata" +const mockStream = require('./mockStream')(mockData) +const speech = {} +speech.createRecognizeStream = mockStream +const hotwords = [{ + file: 'resources/snowboy.umdl', + hotword: 'snowboy' +}] +const sonus = Sonus.init({ + hotwords +}, speech) + +describe("Sonus", () => { + beforeEach(() => { + Sonus.start(sonus) + }) + + afterEach(() => { + Sonus.stop(sonus) + }) + + it("listen event", () => { + sonus.on('hotword', () => console.log("!")) + sonus.on('final-result', console.log) + }) + + it("trigger hotword", () => { + sonus.on('hotword', () => console.log("!")) + sonus.on('final-result', (data) => { + expect(data).toBe(mockData) + }) + Sonus.trigger(sonus, 1) + }) +}) \ No newline at end of file diff --git a/test/mockStream.js b/test/mockStream.js new file mode 100644 index 0000000..e813c86 --- /dev/null +++ b/test/mockStream.js @@ -0,0 +1,25 @@ +var Readable = require("stream").Readable +var util = require("util") +var _transcript +module.exports = transcript => { + _transcript = transcript + return mockStream +} + +function mockStream(options) { + if (!(this instanceof mockStream)) return new mockStream(options) + if (!options) options = {} + options.objectMode = true + Readable.call(this, options) +} + +util.inherits(mockStream, Readable) + +mockStream.prototype._read = function read() { + this.emit("data", { + results: [{ + isFinal: true, + transcript: _transcript + }] + }) +} \ No newline at end of file