Commit 6962e9c5 authored by sergey's avatar sergey

используем линтер для красоты кода ))

parent 508c005f
{
"extends": [
"google"
],
"env": {
"node": true
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"new-cap": ["error", { "properties": true }]
}
}
\ No newline at end of file
language: node_js
node_js:
- "7"
- "8"
- "10"
- "11"
- "12"
\ No newline at end of file
module.exports = function(grunt){
grunt.initConfig({
mocha_istanbul: {
coverage: {
src: 'test',
options: {
'ui': 'tdd',
'recursive': true,
'reporter': 'mocha-istanbul'
}
}
}
});
grunt.loadNpmTasks('grunt-mocha-istanbul');
grunt.registerTask('coverage', ['mocha_istanbul:coverage']);
};
\ No newline at end of file
var AGIServer = require('./../lib/index');
const AGIServer = require('./../lib/index');
var handler = function (context) {
const handler = function(context) {
context.onEvent('variables')
.then(function (vars) {
.then(function(vars) {
console.log('vars', vars);
return context.streamFile('beep');
})
.then(function (result) {
return context.setVariable('RECOGNITION_RESULT', 'I\'m your father, Luc');
.then(function(result) {
return context.setVariable(
'RECOGNITION_RESULT', 'I\'m your father, Luc');
})
.then(function (result) {
.then(function(result) {
return context.end();
})
.fail(console.log);
};
var agi = new AGIServer(handler, {debug: true});
const agi = new AGIServer(handler, {debug: true});
agi.start(3007);
module.exports = [
{
name: "exec",
command: "EXEC",
params: 10
name: 'exec',
command: 'EXEC',
params: 10,
},
{
name: "databaseDel",
command: "DATABASE DEL",
params: 2
name: 'databaseDel',
command: 'DATABASE DEL',
params: 2,
},
{
name: "databaseDelTree",
command: "DATABASE DELTREE",
params: 2
name: 'databaseDelTree',
command: 'DATABASE DELTREE',
params: 2,
},
{
name: "databaseGet",
command: "DATABASE GET",
params: 2
name: 'databaseGet',
command: 'DATABASE GET',
params: 2,
},
{
name: "databasePut",
command: "DATABASE PUT",
params: 3
name: 'databasePut',
command: 'DATABASE PUT',
params: 3,
},
{
name: "speechCreate",
command: "SPEECH CREATE",
params: 1
name: 'speechCreate',
command: 'SPEECH CREATE',
params: 1,
},
{
name: "speechDestroy",
command: "SPEECH DESTROY",
params: 0
name: 'speechDestroy',
command: 'SPEECH DESTROY',
params: 0,
},
{
name: "speechActivateGrammar",
command: "SPEECH ACTIVATE GRAMMAR",
params: 1
name: 'speechActivateGrammar',
command: 'SPEECH ACTIVATE GRAMMAR',
params: 1,
},
{
name: "speechDeactivateGrammar",
command: "SPEECH DEACTIVATE GRAMMAR",
params: 1
name: 'speechDeactivateGrammar',
command: 'SPEECH DEACTIVATE GRAMMAR',
params: 1,
},
{
name: "speechLoadGrammar",
command: "SPEECH LOAD GRAMMAR",
params: 2
name: 'speechLoadGrammar',
command: 'SPEECH LOAD GRAMMAR',
params: 2,
},
{
name: "speechUnloadGrammar",
command: "SPEECH UNLOAD GRAMMAR",
params: 1
name: 'speechUnloadGrammar',
command: 'SPEECH UNLOAD GRAMMAR',
params: 1,
},
{
name: "speechSet",
command: "SPEECH SET",
params: 2
name: 'speechSet',
command: 'SPEECH SET',
params: 2,
},
{
name: "speechRecognize",
command: "SPEECH RECOGNIZE",
params: 3
name: 'speechRecognize',
command: 'SPEECH RECOGNIZE',
params: 3,
},
{
name: "getVariable",
command: "GET VARIABLE",
params: 1
name: 'getVariable',
command: 'GET VARIABLE',
params: 1,
},
{
name: "getFullVariable",
command: "GET FULL VARIABLE",
params: 2
name: 'getFullVariable',
command: 'GET FULL VARIABLE',
params: 2,
},
{
name: "getData",
command: "GET DATA",
params: 3
name: 'getData',
command: 'GET DATA',
params: 3,
},
{
name: "getOption",
command: "GET OPTION",
name: 'getOption',
command: 'GET OPTION',
params: 3,
paramRules: [
null,
{
prepare: function (value) {
prepare: function(value) {
return '"' + value + '"';
}
}
]
},
},
],
},
{
name: "receiveChar",
command: "RECEIVE CHAR",
params: 1
name: 'receiveChar',
command: 'RECEIVE CHAR',
params: 1,
},
{
name: "receiveText",
command: "RECEIVE TEXT",
params: 1
name: 'receiveText',
command: 'RECEIVE TEXT',
params: 1,
},
{
name: "setAutoHangup",
command: "SET AUTOHANGUP",
params: 1
name: 'setAutoHangup',
command: 'SET AUTOHANGUP',
params: 1,
},
{
name: "setCallerID",
command: "SET CALLERID",
params: 1
name: 'setCallerID',
command: 'SET CALLERID',
params: 1,
},
{
name: "setContext",
command: "SET CONTEXT",
params: 1
name: 'setContext',
command: 'SET CONTEXT',
params: 1,
},
{
name: "setExtension",
command: "SET EXTENSION",
params: 1
name: 'setExtension',
command: 'SET EXTENSION',
params: 1,
},
{
name: "setPriority",
command: "SET PRIORITY",
params: 1
name: 'setPriority',
command: 'SET PRIORITY',
params: 1,
},
{
name: "setMusic",
command: "SET MUSIC",
params: 1
name: 'setMusic',
command: 'SET MUSIC',
params: 1,
},
{
name: "setVariable",
command: "SET VARIABLE",
name: 'setVariable',
command: 'SET VARIABLE',
params: 2,
paramRules: [
null,
{
prepare: function (value) {
prepare: function(value) {
return '"' + value + '"';
}
}
]
},
},
],
},
{
name: "sendImage",
command: "SEND IMAGE",
params: 1
name: 'sendImage',
command: 'SEND IMAGE',
params: 1,
},
{
name: "sendText",
command: "SEND TEXT",
name: 'sendText',
command: 'SEND TEXT',
params: 1,
paramRules: [
{
prepare: function (value) {
prepare: function(value) {
return '"' + value + '"';
}
}
]
},
},
],
},
{
name: "channelStatus",
command: "CHANNEL STATUS",
params: 1
name: 'channelStatus',
command: 'CHANNEL STATUS',
params: 1,
},
{
name: "answer",
command: "ANSWER",
params: 0
name: 'answer',
command: 'ANSWER',
params: 0,
},
{
name: "verbose",
command: "VERBOSE",
name: 'verbose',
command: 'VERBOSE',
params: 2,
paramRules: [
{
prepare: function (value) {
prepare: function(value) {
return '"' + value + '"';
}
}
]
},
},
],
},
{
name: "tddMode",
command: "TDD MODE",
params: 1
name: 'tddMode',
command: 'TDD MODE',
params: 1,
},
{
name: "noop",
command: "NOOP",
params: 0
name: 'noop',
command: 'NOOP',
params: 0,
},
{
name: "gosub",
command: "GOSUB",
params: 4
name: 'gosub',
command: 'GOSUB',
params: 4,
},
{
name: "recordFile",
command: "RECORD FILE",
name: 'recordFile',
command: 'RECORD FILE',
params: 7,
paramRules: [
{
default: "#",
prepare: function (value) {
default: '#',
prepare: function(value) {
return '"' + value + '"';
}
},
},
null,
null,
{
prepare: function (value) {
prepare: function(value) {
return value * 1000;
}
}
]
},
},
],
},
{
name: "sayNumber",
command: "SAY NUMBER",
name: 'sayNumber',
command: 'SAY NUMBER',
params: 2,
paramRules: [
null,
{
default: "#",
prepare: function (value) {
default: '#',
prepare: function(value) {
return '"' + value + '"';
}
}
]
},
},
],
},
{
name: "sayAlpha",
command: "SAY ALPHA",
name: 'sayAlpha',
command: 'SAY ALPHA',
params: 2,
paramRules: [
null,
{
default: "#",
prepare: function (value) {
default: '#',
prepare: function(value) {
return '"' + value + '"';
}
}
]
},
},
],
},
{
name: "sayDate",
command: "SAY DATE",
name: 'sayDate',
command: 'SAY DATE',
params: 2,
paramRules: [
null,
{
default: "#",
prepare: function (value) {
default: '#',
prepare: function(value) {
return '"' + value + '"';
}
}
]
},
},
],
},
{
name: "sayTime",
command: "SAY TIME",
name: 'sayTime',
command: 'SAY TIME',
params: 2,
paramRules: [
null,
{
default: "#",
prepare: function (value) {
default: '#',
prepare: function(value) {
return '"' + value + '"';
}
}
]
},
},
],
},
{
name: "sayDateTime",
command: "SAY DATETIME",
name: 'sayDateTime',
command: 'SAY DATETIME',
params: 4,
paramRules: [
null,
{
prepare: function (value) {
prepare: function(value) {
return '"' + value + '"';
}
}
]
},
},
],
},
{
name: "sayDigits",
command: "SAY DIGITS",
name: 'sayDigits',
command: 'SAY DIGITS',
params: 2,
paramRules: [
null,
{
prepare: function (value) {
return '"' + value + '"'
}
}
]
prepare: function(value) {
return '"' + value + '"';
},
},
],
},
{
name: "sayPhonetic",
command: "SAY PHONETIC",
name: 'sayPhonetic',
command: 'SAY PHONETIC',
params: 2,
paramRules: [
null,
{
prepare: function (value) {
prepare: function(value) {
return '"' + value + '"';
}
}
]
},
},
],
},
{
name: "controlStreamFile",
command: "CONTROL STREAM FILE",
params: 7
name: 'controlStreamFile',
command: 'CONTROL STREAM FILE',
params: 7,
},
{
name: "streamFile",
command: "STREAM FILE",
name: 'streamFile',
command: 'STREAM FILE',
params: 2,
paramRules: [
{
prepare: function (value) {
prepare: function(value) {
return '"' + value + '"';
}
},
},
{
default: '#',
prepare: function (value) {
prepare: function(value) {
return '"' + value + '"';
}
}
]
},
},
],
},
{
name: "waitForDigit",
command: "WAIT FOR DIGIT",
params: 1
name: 'waitForDigit',
command: 'WAIT FOR DIGIT',
params: 1,
},
{
name: "hangup",
command: "HANGUP",
params: 0
name: 'hangup',
command: 'HANGUP',
params: 0,
},
{
name: "asyncAGIBreak",
command: "ASYNCAGI BREAK",
params: 0
}
name: 'asyncAGIBreak',
command: 'ASYNCAGI BREAK',
params: 0,
},
];
var commands = require('./command');
console.log(commands);
var q = {};
commands.map(function (command) {
q[command.name] = function () {
var args = [].slice.call(arguments, 0, command.params);
return command.command + " " + args.join(" ");
};
});
console.log(q.databaseDel("1", "2", "3", "4"));
console.log(q.databaseDelTree("1", "2", "3", "4"));
console.log(commands.length)
\ No newline at end of file
var Readable = require('readable-stream');
var EventEmitter = require('events').EventEmitter;
var state = require('./state');
var commands = require('./command');
const Readable = require('readable-stream');
const EventEmitter = require('events').EventEmitter;
const state = require('./state');
const commands = require('./command');
//base context
// base context
var Context = function (stream, loggerOptions = {}) {
const Context = function(stream, loggerOptions = {}) {
EventEmitter.call(this);
function consoleDecorator(arrow, data){
return console.log(arrow, JSON.stringify (data));
}
const consoleDecorator = function(arrow, data) {
return console.log(arrow, JSON.stringify(data));
};
this.log = (loggerOptions.logger) ?
loggerOptions.logger :
consoleDecorator;
this.debug = loggerOptions.debug
this.debug = loggerOptions.debug;
this.stream = new Readable();
this.stream.setEncoding('utf8');
this.stream.wrap(stream);
this.state = state.init;
this.msg = "";
this.msg = '';
this.variables = {};
this.pending = null;
var self = this;
this.stream.on('readable', function () {
//always keep the 'leftover' part of the message
const self = this;
this.stream.on('readable', function() {
// always keep the 'leftover' part of the message
self.msg = self.read();
});
......@@ -38,30 +38,32 @@ var Context = function (stream, loggerOptions = {}) {
require('util').inherits(Context, EventEmitter);
Context.prototype.read = function () {
var buffer = this.stream.read();
Context.prototype.read = function() {
const buffer = this.stream.read();
if (!buffer) return this.msg;
this.msg += buffer;
if (this.state === state.init) {
if (this.msg.indexOf('\n\n') < 0) return this.msg; //we don't have whole message
// we don't have whole message
if (this.msg.indexOf('\n\n') < 0) return this.msg;
this.readVariables(this.msg);
} else if (this.state === state.waiting) {
if (this.msg.indexOf('\n') < 0) return this.msg; //we don't have whole message
// we don't have whole message
if (this.msg.indexOf('\n') < 0) return this.msg;
this.readResponse(this.msg);
}
return '';
};
Context.prototype.readVariables = function (msg) {
var lines = msg.split('\n');
Context.prototype.readVariables = function(msg) {
const lines = msg.split('\n');
lines.map(function (line) {
var split = line.split(':');
var name = split[0];
var value = split[1];
lines.map(function(line) {
const split = line.split(':');
const name = split[0];
const value = split[1];
this.variables[name] = (value || '').trim();
}, this);
......@@ -69,26 +71,26 @@ Context.prototype.readVariables = function (msg) {
this.setState(state.waiting);
};
Context.prototype.readResponse = function (msg) {
var lines = msg.split('\n');
Context.prototype.readResponse = function(msg) {
const lines = msg.split('\n');
lines.map(function (line) {
lines.map(function(line) {
this.readResponseLine(line);
}, this);
};
Context.prototype.readResponseLine = function (line) {
Context.prototype.readResponseLine = function(line) {
if (!line) return;
//var parsed = /^(\d{3})(?: result=)(.*)/.exec(line);
var parsed = /^(\d{3})(?: result=)([^(]*)(?:\((.*)\))?/.exec(line);
// var parsed = /^(\d{3})(?: result=)(.*)/.exec(line);
const parsed = /^(\d{3})(?: result=)([^(]*)(?:\((.*)\))?/.exec(line);
if (!parsed) {
return this.emit('hangup');
}
var response = {
const response = {
code: parseInt(parsed[1]),
result: parsed[2].trim(),
};
......@@ -96,63 +98,62 @@ Context.prototype.readResponseLine = function (line) {
response.value = parsed[3];
}
//our last command had a pending callback
// our last command had a pending callback
if (this.pending) {
var pending = this.pending;
const pending = this.pending;
this.pending = null;
pending(null, response);
}
this.emit('response', response);
}
};
Context.prototype.setState = function (state) {
Context.prototype.setState = function(state) {
this.state = state;
};
Context.prototype.send = function (msg, cb) {
Context.prototype.send = function(msg, cb) {
this.pending = cb;
this.stream.write(msg);
};
Context.prototype.end = function () {
Context.prototype.end = function() {
this.stream.end();
return Promise.resolve();
};
Context.prototype.sendCommand = function (command) {
if (this.debug) this.log('------->', { command: command });
var self = this;
return new Promise(function (resolve, reject) {
self.send(command + '\n', function (err, result) {
if (self.debug) self.log('<-------', { err: err, result: result });
Context.prototype.sendCommand = function(command) {
if (this.debug) this.log('------->', {command: command});
const self = this;
return new Promise(function(resolve, reject) {
self.send(command + '\n', function(err, result) {
if (self.debug) self.log('<-------', {err: err, result: result});
if (err) {
reject(err);
} else {
resolve(result);
}
});
})
});
};
Context.prototype.onEvent = function (event) {
var self = this
return new Promise(function (resolve) {
self.on(event, function (data) {
Context.prototype.onEvent = function(event) {
const self = this;
return new Promise(function(resolve) {
self.on(event, function(data) {
resolve(data);
})
})
});
});
};
//additional agi commands
// additional agi commands
commands.forEach(function (command) {
var str = '';
Context.prototype[command.name] = function () {
commands.forEach(function(command) {
let str = '';
Context.prototype[command.name] = function(...args) {
if (command.params > 0) {
var args = [].slice.call(arguments, 0, command.params);
str = command.command + " " + prepareArgs(args, command.paramRules, command.params).join(" ");
// const args = [].slice.call(arguments, 0, command.params);
str = command.command + ' ' +
prepareArgs(args, command.paramRules, command.params).join(' ');
} else {
str = command.command;
}
......@@ -160,22 +161,28 @@ commands.forEach(function (command) {
};
});
var prepareArgs = function (args, argsRules, count) {
const prepareArgs = function(args, argsRules, count) {
if (!argsRules || !count) {
return args;
}
return Array.apply(null, new Array(count)) // old node.js versions don't support Array.fill()
.map(function (arg, i) {
arg = args[i] !== undefined && args[i] !== null ? args[i] : argsRules[i] && argsRules[i].default || '';
var prepare = argsRules[i] && argsRules[i].prepare || function (x) { return x; };
return (new Array(count)).fill(null)
.map(function(arg, i) {
arg = args[i] !== undefined && args[i] !== null ?
args[i] :
argsRules[i] && argsRules[i].default || '';
const prepare = argsRules[i] && argsRules[i].prepare ||
function(x) {
return x;
};
return prepare(String(arg));
});
};
//sugar commands
// sugar commands
Context.prototype.dial = function (target, timeout, params) {
Context.prototype.dial = function(target, timeout, params) {
return this.exec('Dial', target + ',' + timeout + ',' + params);
};
......
var Context = require('./context');
const Context = require('./context');
var agi = function (handler, optionsIn) {
const agi = function(handler, optionsIn) {
const options = optionsIn || {};
var server;
var options = optionsIn || {};
var settings = {
const settings = {
port: options.port || 3000,
debug: options.debug || false,
logger: options.logger || false,
host: options.host,
};
var handle = function (stream) {
var context = new Context(stream, { debug: settings.debug, logger: options.logger});
const handle = function(stream) {
const context = new Context(stream, {
debug: settings.debug,
logger: options.logger,
});
handler(context);
};
var start = function (portIn, hostIn) {
var port = portIn || settings.port;
var host = hostIn || settings.host;
const start = function(portIn, hostIn) {
const port = portIn || settings.port;
const host = hostIn || settings.host;
return require('net').createServer(handle).listen(port, host);
};
return {
start: start
start: start,
};
};
......
//states for context
var state = {
// states for context
const state = {
init: 0,
waiting: 2
waiting: 2,
};
module.exports = state;
......@@ -9,21 +9,17 @@
},
"main": "lib/",
"scripts": {
"test": "mocha -R tap --exit"
},
"engines": {
"node": ">=6"
"test": "mocha -R tap --exit",
"lint-fix": "eslint --fix ."
},
"dependencies": {
"expect.js": "^0.3.1",
"readable-stream": "^2.3.0"
},
"devDependencies": {
"expect.js": "*",
"grunt": "^0.4.5",
"grunt-mocha-istanbul": "^2.2.0",
"istanbul": "^0.3.5",
"eslint": "^5.16.0",
"eslint-config-google": "^0.12.0",
"memorystream": "^0.3.0",
"mocha": "*",
"mocha-istanbul": "^0.2.0"
"mocha": "6.1.4"
}
}
var MemoryStream = require('memorystream');
var agi = require('./../lib')
var expect = require('expect.js');
var Context = require('./../lib/context');
var state = require('./../lib/state');
//helpers
var writeVars = function(stream) {
const MemoryStream = require('memorystream');
const Agi = require('./../lib');
const expect = require('expect.js');
const Context = require('./../lib/context');
const state = require('./../lib/state');
// helpers
const writeVars = function(stream) {
stream.write('agi_network: yes\n');
stream.write('agi_uniqueid: 13507138.14\n');
stream.write('agi_arg_1: test\n');
stream.write('\n\n');
};
var context = function(cb) {
var stream = new MemoryStream();
var ctx = new Context(stream);
//TODO nasty
const context = function(cb) {
const stream = new MemoryStream();
const ctx = new Context(stream);
// TODO nasty
ctx.send = function(msg, cb) {
ctx.pending = cb;
ctx.sent = ctx.sent || [];
......@@ -30,9 +30,9 @@ var context = function(cb) {
};
describe('Context', function() {
beforeEach(function (done) {
var self = this;
context(function (context) {
beforeEach(function(done) {
const self = this;
context(function(context) {
self.context = context;
done();
});
......@@ -40,13 +40,12 @@ describe('Context', function() {
describe('parsing variables', function() {
it('works', function(done) {
var vars = this.context.variables;
const vars = this.context.variables;
expect(vars['agi_network']).ok();
expect(vars['agi_network']).to.eql('yes');
expect(vars['agi_uniqueid']).to.eql('13507138.14');
expect(vars['agi_arg_1']).to.eql('test');
done();
});
it('puts context into waiting state', function() {
......@@ -72,7 +71,7 @@ describe('Context', function() {
describe('command flow', function() {
describe('success', function() {
it('emits proper repsonse', function(done) {
var context = this.context;
const context = this.context;
process.nextTick(function() {
context.exec('test', 'bang', 'another');
......@@ -85,27 +84,25 @@ describe('Context', function() {
expect(msg.result).to.eql('0');
done();
});
});
it('invokes callback with response', function(done) {
var context = this.context;
const context = this.context;
process.nextTick(function(){
process.nextTick(function() {
context.stream.write('200 result=0');
context.stream.write('\n');
context.stream.write('200 result=0');
context.stream.write('\n');
});
context.exec('test', 'boom').then(function(){
context.exec('test', 'boom').then(function() {
done();
});
});
it('includes the response value', function(done) {
var context = this.context;
const context = this.context;
process.nextTick(function() {
context.exec('test', 'bang', 'another');
......@@ -119,28 +116,26 @@ describe('Context', function() {
expect(msg.value).to.eql('a value');
done();
});
});
});
describe('two commands', function(done) {
it('invokes two callbacks', function(done) {
var context = this.context;
const context = this.context;
process.nextTick(function() {
context.stream.write('200 result=0\n');
});
context.exec('test')
.then(function (res) {
.then(function(res) {
expect(res.result).to.eql('0');
process.nextTick(function() {
context.stream.write('200 result=1\n');
});
return context.exec('test 2');
})
.then(function (res) {
.then(function(res) {
expect(res.result).to.eql('1');
done();
});
......@@ -156,13 +151,13 @@ describe('Context', function() {
describe('in command response', function() {
it('is passed to callback', function(done) {
var context = this.context;
const context = this.context;
this.context.exec('whatever', function(err, res) {
});
this.context.on('hangup', done);
process.nextTick(function() {
context.stream.write('200 result=-1\nHANGUP\n');
})
});
});
});
});
......@@ -170,28 +165,32 @@ describe('Context', function() {
describe('databaseDel', function() {
it('sends correct command', function() {
this.context.databaseDel('family', 'test');
expect(this.context.sent.join('')).to.eql('DATABASE DEL family test\n');
expect(this.context.sent.join(''))
.to.eql('DATABASE DEL family test\n');
});
});
describe('databaseDelTree', function() {
it('sends correct command', function() {
this.context.databaseDelTree('family', 'test');
expect(this.context.sent.join('')).to.eql('DATABASE DELTREE family test\n');
expect(this.context.sent.join(''))
.to.eql('DATABASE DELTREE family test\n');
});
});
describe('databaseGet', function() {
it('sends correct command', function() {
this.context.databaseGet('family', 'test');
expect(this.context.sent.join('')).to.eql('DATABASE GET family test\n');
expect(this.context.sent.join(''))
.to.eql('DATABASE GET family test\n');
});
});
describe('databasePut', function() {
it('sends correct command', function() {
this.context.databasePut('family', 'test', 'value');
expect(this.context.sent.join('')).to.eql('DATABASE PUT family test value\n');
expect(this.context.sent.join('')
).to.eql('DATABASE PUT family test value\n');
});
});
......@@ -212,21 +211,24 @@ describe('Context', function() {
describe('speechActivateGrammar', function() {
it('sends correct command', function() {
this.context.speechActivateGrammar('name');
expect(this.context.sent.join('')).to.eql('SPEECH ACTIVATE GRAMMAR name\n');
expect(this.context.sent.join(''))
.to.eql('SPEECH ACTIVATE GRAMMAR name\n');
});
});
describe('speechDeactivateGrammar', function() {
it('sends correct command', function() {
this.context.speechDeactivateGrammar('name');
expect(this.context.sent.join('')).to.eql('SPEECH DEACTIVATE GRAMMAR name\n');
expect(this.context.sent.join(''))
.to.eql('SPEECH DEACTIVATE GRAMMAR name\n');
});
});
describe('speechLoadGrammar', function() {
it('sends correct command', function() {
this.context.speechLoadGrammar('name', 'path');
expect(this.context.sent.join('')).to.eql('SPEECH LOAD GRAMMAR name path\n');
expect(this.context.sent.join(''))
.to.eql('SPEECH LOAD GRAMMAR name path\n');
});
});
......@@ -247,14 +249,16 @@ describe('Context', function() {
describe('speechRecognize', function() {
it('sends correct command', function() {
this.context.speechRecognize('prompt', 'timeout', 'offset');
expect(this.context.sent.join('')).to.eql('SPEECH RECOGNIZE prompt timeout offset\n');
expect(this.context.sent.join(''))
.to.eql('SPEECH RECOGNIZE prompt timeout offset\n');
});
});
describe('setVariable', function() {
it('sends correct command', function() {
this.context.setVariable('test', 'test test test');
expect(this.context.sent.join('')).to.eql('SET VARIABLE test "test test test"\n');
expect(this.context.sent.join(''))
.to.eql('SET VARIABLE test "test test test"\n');
});
});
......@@ -310,7 +314,8 @@ describe('Context', function() {
describe('getFullVariable', function() {
it('sends correct command', function() {
this.context.getFullVariable('test', 'test');
expect(this.context.sent.join('')).to.eql('GET FULL VARIABLE test test\n');
expect(this.context.sent.join(''))
.to.eql('GET FULL VARIABLE test test\n');
});
});
......@@ -350,56 +355,59 @@ describe('Context', function() {
});
describe('stream file', function() {
it('sends', function () {
it('sends', function() {
this.context.streamFile('test', '1234567890#*', function() {});
expect(this.context.sent.join('')).to.eql('STREAM FILE "test" "1234567890#*"\n');
expect(this.context.sent.join(''))
.to.eql('STREAM FILE "test" "1234567890#*"\n');
});
});
describe('record file', function() {
it('record', function () {
it('record', function() {
this.context.recordFile('test', 'wav', '#', 10, 0, 1, 2, function() {});
expect(this.context.sent.join('')).to.eql('RECORD FILE "test" wav # 10000 0 1 2\n');
expect(this.context.sent.join(''))
.to.eql('RECORD FILE "test" wav # 10000 0 1 2\n');
});
});
describe('say number', function() {
it('say number', function () {
it('say number', function() {
this.context.sayNumber('1234', '#', function() {});
expect(this.context.sent.join('')).to.eql('SAY NUMBER 1234 "#"\n');
});
});
describe('say alpha', function() {
it('say alpha', function () {
it('say alpha', function() {
this.context.sayAlpha('1234', '#', function() {});
expect(this.context.sent.join('')).to.eql('SAY ALPHA 1234 "#"\n');
});
});
describe('say date', function() {
it('say date', function () {
it('say date', function() {
this.context.sayDate('1234', '#', function() {});
expect(this.context.sent.join('')).to.eql('SAY DATE 1234 "#"\n');
});
});
describe('say time', function() {
it('say time', function () {
it('say time', function() {
this.context.sayTime('1234', '#', function() {});
expect(this.context.sent.join('')).to.eql('SAY TIME 1234 "#"\n');
});
});
describe('say datetime', function() {
it('say datetime', function () {
this.context.sayDateTime('1234', '#', 'Y', 'DST',function() {});
expect(this.context.sent.join('')).to.eql('SAY DATETIME 1234 "#" Y DST\n');
it('say datetime', function() {
this.context.sayDateTime('1234', '#', 'Y', 'DST', function() {});
expect(this.context.sent.join(''))
.to.eql('SAY DATETIME 1234 "#" Y DST\n');
});
});
describe('say phonetic', function() {
it('say phonetic', function () {
it('say phonetic', function() {
this.context.sayPhonetic('1234ABCD', '#', function() {});
expect(this.context.sent.join('')).to.eql('SAY PHONETIC 1234ABCD "#"\n');
});
......@@ -413,34 +421,34 @@ describe('Context', function() {
});
describe('say digits', function() {
it('say digits', function () {
this.context.sayDigits('1234', '#', function() {});
it('say digits', function() {
this.context.sayDigits('1234', '#');
expect(this.context.sent.join('')).to.eql('SAY DIGITS 1234 "#"\n');
});
});
describe('send image', function() {
it('send image', function () {
this.context.sendImage('1234', function() {});
it('send image', function() {
this.context.sendImage('1234');
expect(this.context.sent.join('')).to.eql('SEND IMAGE 1234\n');
});
});
describe('send text', function() {
it('send text', function () {
it('send text', function() {
this.context.sendText('1234');
expect(this.context.sent.join('')).to.eql('SEND TEXT "1234"\n');
});
});
describe('waitForDigit', function () {
describe('waitForDigit', function() {
it('sends with default timeout', function() {
this.context.waitForDigit(5000);
expect(this.context.sent.join('')).to.eql('WAIT FOR DIGIT 5000\n');
});
it('sends with specified timeout', function() {
this.context.waitForDigit(-1, function() {});
this.context.waitForDigit(-1);
expect(this.context.sent.join('')).to.eql('WAIT FOR DIGIT -1\n');
});
});
......@@ -475,7 +483,7 @@ describe('Context', function() {
describe('tddMode', function() {
it('sends correct command', function() {
this.context.tddMode("on");
this.context.tddMode('on');
expect(this.context.sent.join('')).to.eql('TDD MODE on\n');
});
});
......@@ -489,19 +497,19 @@ describe('Context', function() {
describe('gosub', function() {
it('sends correct command', function() {
this.context.gosub('out','241','6','do');
this.context.gosub('out', '241', '6', 'do');
expect(this.context.sent.join('')).to.eql('GOSUB out 241 6 do\n');
});
});
describe('events', function() {
describe('error', function () {
describe('error', function() {
it('is emitted when socket emits error', function(done) {
this.context.on('error', function(err) {
expect(err).to.eql('test');
done();
});
this.context.stream.emit('error', "test");
this.context.stream.emit('error', 'test');
});
});
......@@ -520,14 +528,13 @@ describe('Context', function() {
describe('agi#createServer', function() {
it('returns instance of net.Server', function() {
var net = require('net');
var server = (new agi()).start(3000);
const net = require('net');
const server = (new Agi()).start(3000);
expect(server instanceof net.Server).ok();
});
it('invokes callback when a new connection is established', function(done) {
var server = new agi(function(context) {
const server = new Agi(function(context) {
expect(context instanceof Context);
done();
}).start(3001);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment