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