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"
\ No newline at end of file - "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) { })
return context.end(); .then(function(result) {
}) 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);
\ No newline at end of file
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",
command: "RECEIVE CHAR",
params: 1
},
{
name: "receiveText",
command: "RECEIVE TEXT",
params: 1
},
{
name: "setAutoHangup",
command: "SET AUTOHANGUP",
params: 1
},
{
name: "setCallerID",
command: "SET CALLERID",
params: 1
},
{
name: "setContext",
command: "SET CONTEXT",
params: 1
},
{
name: "setExtension",
command: "SET EXTENSION",
params: 1
},
{
name: "setPriority",
command: "SET PRIORITY",
params: 1
},
{
name: "setMusic",
command: "SET MUSIC",
params: 1
},
{
name: "setVariable",
command: "SET VARIABLE",
params: 2,
paramRules: [
null,
{
prepare: function (value) {
return '"' + value + '"';
}
}
]
},
{
name: "sendImage",
command: "SEND IMAGE",
params: 1
},
{
name: "sendText",
command: "SEND TEXT",
params: 1,
paramRules: [
{
prepare: function (value) {
return '"' + value + '"';
}
}
]
},
{
name: "channelStatus",
command: "CHANNEL STATUS",
params: 1
},
{
name: "answer",
command: "ANSWER",
params: 0
},
{
name: "verbose",
command: "VERBOSE",
params: 2,
paramRules: [
{
prepare: function (value) {
return '"' + value + '"';
}
}
]
},
{
name: "tddMode",
command: "TDD MODE",
params: 1
},
{
name: "noop",
command: "NOOP",
params: 0
},
{
name: "gosub",
command: "GOSUB",
params: 4
},
{
name: "recordFile",
command: "RECORD FILE",
params: 7,
paramRules: [
{
default: "#",
prepare: function (value) {
return '"' + value + '"';
}
}, },
null, },
null, ],
{ },
prepare: function (value) { {
return value * 1000; name: 'receiveChar',
} command: 'RECEIVE CHAR',
} params: 1,
] },
}, {
{ name: 'receiveText',
name: "sayNumber", command: 'RECEIVE TEXT',
command: "SAY NUMBER", params: 1,
params: 2, },
paramRules: [ {
null, name: 'setAutoHangup',
{ command: 'SET AUTOHANGUP',
default: "#", params: 1,
prepare: function (value) { },
return '"' + value + '"'; {
} name: 'setCallerID',
} command: 'SET CALLERID',
] params: 1,
}, },
{ {
name: "sayAlpha", name: 'setContext',
command: "SAY ALPHA", command: 'SET CONTEXT',
params: 2, params: 1,
paramRules: [ },
null, {
{ name: 'setExtension',
default: "#", command: 'SET EXTENSION',
prepare: function (value) { params: 1,
return '"' + value + '"'; },
} {
} name: 'setPriority',
] command: 'SET PRIORITY',
}, params: 1,
{ },
name: "sayDate", {
command: "SAY DATE", name: 'setMusic',
params: 2, command: 'SET MUSIC',
paramRules: [ params: 1,
null, },
{ {
default: "#", name: 'setVariable',
prepare: function (value) { command: 'SET VARIABLE',
return '"' + value + '"'; params: 2,
} paramRules: [
} null,
] {
}, prepare: function(value) {
{ return '"' + value + '"';
name: "sayTime",
command: "SAY TIME",
params: 2,
paramRules: [
null,
{
default: "#",
prepare: function (value) {
return '"' + value + '"';
}
}
]
},
{
name: "sayDateTime",
command: "SAY DATETIME",
params: 4,
paramRules: [
null,
{
prepare: function (value) {
return '"' + value + '"';
}
}
]
},
{
name: "sayDigits",
command: "SAY DIGITS",
params: 2,
paramRules: [
null,
{
prepare: function (value) {
return '"' + value + '"'
}
}
]
},
{
name: "sayPhonetic",
command: "SAY PHONETIC",
params: 2,
paramRules: [
null,
{
prepare: function (value) {
return '"' + value + '"';
}
}
]
},
{
name: "controlStreamFile",
command: "CONTROL STREAM FILE",
params: 7
},
{
name: "streamFile",
command: "STREAM FILE",
params: 2,
paramRules: [
{
prepare: function (value) {
return '"' + value + '"';
}
}, },
{ },
default: '#', ],
prepare: function (value) { },
return '"' + value + '"'; {
} name: 'sendImage',
} command: 'SEND IMAGE',
] params: 1,
}, },
{ {
name: "waitForDigit", name: 'sendText',
command: "WAIT FOR DIGIT", command: 'SEND TEXT',
params: 1 params: 1,
}, paramRules: [
{ {
name: "hangup", prepare: function(value) {
command: "HANGUP", return '"' + value + '"';
params: 0 },
}, },
{ ],
name: "asyncAGIBreak", },
command: "ASYNCAGI BREAK", {
params: 0 name: 'channelStatus',
} command: 'CHANNEL STATUS',
]; params: 1,
\ No newline at end of file },
{
name: 'answer',
command: 'ANSWER',
params: 0,
},
{
name: 'verbose',
command: 'VERBOSE',
params: 2,
paramRules: [
{
prepare: function(value) {
return '"' + value + '"';
},
},
],
},
{
name: 'tddMode',
command: 'TDD MODE',
params: 1,
},
{
name: 'noop',
command: 'NOOP',
params: 0,
},
{
name: 'gosub',
command: 'GOSUB',
params: 4,
},
{
name: 'recordFile',
command: 'RECORD FILE',
params: 7,
paramRules: [
{
default: '#',
prepare: function(value) {
return '"' + value + '"';
},
},
null,
null,
{
prepare: function(value) {
return value * 1000;
},
},
],
},
{
name: 'sayNumber',
command: 'SAY NUMBER',
params: 2,
paramRules: [
null,
{
default: '#',
prepare: function(value) {
return '"' + value + '"';
},
},
],
},
{
name: 'sayAlpha',
command: 'SAY ALPHA',
params: 2,
paramRules: [
null,
{
default: '#',
prepare: function(value) {
return '"' + value + '"';
},
},
],
},
{
name: 'sayDate',
command: 'SAY DATE',
params: 2,
paramRules: [
null,
{
default: '#',
prepare: function(value) {
return '"' + value + '"';
},
},
],
},
{
name: 'sayTime',
command: 'SAY TIME',
params: 2,
paramRules: [
null,
{
default: '#',
prepare: function(value) {
return '"' + value + '"';
},
},
],
},
{
name: 'sayDateTime',
command: 'SAY DATETIME',
params: 4,
paramRules: [
null,
{
prepare: function(value) {
return '"' + value + '"';
},
},
],
},
{
name: 'sayDigits',
command: 'SAY DIGITS',
params: 2,
paramRules: [
null,
{
prepare: function(value) {
return '"' + value + '"';
},
},
],
},
{
name: 'sayPhonetic',
command: 'SAY PHONETIC',
params: 2,
paramRules: [
null,
{
prepare: function(value) {
return '"' + value + '"';
},
},
],
},
{
name: 'controlStreamFile',
command: 'CONTROL STREAM FILE',
params: 7,
},
{
name: 'streamFile',
command: 'STREAM FILE',
params: 2,
paramRules: [
{
prepare: function(value) {
return '"' + value + '"';
},
},
{
default: '#',
prepare: function(value) {
return '"' + value + '"';
},
},
],
},
{
name: 'waitForDigit',
command: 'WAIT FOR DIGIT',
params: 1,
},
{
name: 'hangup',
command: 'HANGUP',
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'); 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();
}); });
this.stream.on('error', this.emit.bind(this, 'error')); this.stream.on('error', this.emit.bind(this, 'error'));
this.stream.on('close', this.emit.bind(this, 'close')); this.stream.on('close', this.emit.bind(this, 'close'));
}; };
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] :
return prepare(String(arg)); 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); 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, 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 || [];
...@@ -25,14 +25,14 @@ var context = function(cb) { ...@@ -25,14 +25,14 @@ var context = function(cb) {
ctx.once('variables', function(vars) { ctx.once('variables', function(vars) {
cb(ctx); cb(ctx);
}); });
writeVars(stream); writeVars(stream);
}; };
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,8 +71,8 @@ describe('Context', function() { ...@@ -72,8 +71,8 @@ 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');
context.stream.write('200'); context.stream.write('200');
...@@ -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,31 +116,29 @@ describe('Context', function() { ...@@ -119,31 +116,29 @@ 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');
})
.then(function(res) {
expect(res.result).to.eql('1');
done();
}); });
return context.exec('test 2');
})
.then(function (res) {
expect(res.result).to.eql('1');
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');
}); });
}); });
...@@ -332,7 +337,7 @@ describe('Context', function() { ...@@ -332,7 +337,7 @@ describe('Context', function() {
it('sends correct command', function() { it('sends correct command', function() {
this.context.getVariable('test'); this.context.getVariable('test');
expect(this.context.sent.join('')).to.eql('GET VARIABLE test\n'); expect(this.context.sent.join('')).to.eql('GET VARIABLE test\n');
}); });
}); });
describe('receiveChar', function() { describe('receiveChar', function() {
...@@ -350,97 +355,100 @@ describe('Context', function() { ...@@ -350,97 +355,100 @@ 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');
}); });
}); });
describe('context dial', function() { describe('context dial', function() {
it('context dial', function() { it('context dial', function() {
this.context.dial('123', 10, 'A', function() {}); this.context.dial('123', 10, 'A', function() {});
expect(this.context.sent.join('')).to.eql('EXEC Dial 123,10,A\n'); expect(this.context.sent.join('')).to.eql('EXEC Dial 123,10,A\n');
}); });
}); });
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,18 +528,17 @@ describe('Context', function() { ...@@ -520,18 +528,17 @@ 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);
server.emit('connection', new MemoryStream()); server.emit('connection', new MemoryStream());
}); });
}); });
\ No newline at end of file
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