bitcore-node-zcash/test/node.unit.js

487 lines
14 KiB
JavaScript
Raw Normal View History

2015-07-16 12:53:44 -07:00
'use strict';
var should = require('chai').should();
var sinon = require('sinon');
2016-08-27 19:10:36 -07:00
var bitcore = require('bitcore-lib-zcash');
2015-07-16 12:53:44 -07:00
var Networks = bitcore.Networks;
var proxyquire = require('proxyquire');
2015-08-27 13:09:27 -07:00
var util = require('util');
2015-08-31 06:00:00 -07:00
var BaseService = require('../lib/service');
2016-05-17 21:33:57 -07:00
var index = require('../lib');
var log = index.log;
2015-07-16 12:53:44 -07:00
2015-08-27 13:09:27 -07:00
describe('Bitcore Node', function() {
var baseConfig = {};
2015-08-31 06:00:00 -07:00
var Node;
before(function() {
2015-08-31 06:00:00 -07:00
Node = proxyquire('../lib/node', {});
Node.prototype._loadConfiguration = sinon.spy();
Node.prototype._initialize = sinon.spy();
});
2015-08-31 06:00:00 -07:00
after(function() {
Networks.disableRegtest();
2015-08-31 06:00:00 -07:00
});
2015-08-27 13:09:27 -07:00
describe('@constructor', function() {
2015-08-31 06:00:00 -07:00
var TestService;
2015-08-31 06:00:00 -07:00
before(function() {
2015-08-31 06:00:00 -07:00
TestService = function TestService() {};
util.inherits(TestService, BaseService);
2015-08-31 06:00:00 -07:00
});
it('will set properties', function() {
2015-08-27 13:09:27 -07:00
var config = {
2015-08-31 06:00:00 -07:00
services: [
2015-08-27 13:09:27 -07:00
{
name: 'test1',
2015-08-31 06:00:00 -07:00
module: TestService
2015-08-27 13:09:27 -07:00
}
],
};
var TestNode = proxyquire('../lib/node', {});
2015-08-31 06:00:00 -07:00
TestNode.prototype.start = sinon.spy();
2015-08-27 13:09:27 -07:00
var node = new TestNode(config);
2015-08-31 06:00:00 -07:00
node._unloadedServices.length.should.equal(1);
node._unloadedServices[0].name.should.equal('test1');
node._unloadedServices[0].module.should.equal(TestService);
2015-08-31 06:00:00 -07:00
node.network.should.equal(Networks.defaultNetwork);
var node2 = TestNode(config);
node2._unloadedServices.length.should.equal(1);
node2._unloadedServices[0].name.should.equal('test1');
node2._unloadedServices[0].module.should.equal(TestService);
node2.network.should.equal(Networks.defaultNetwork);
2015-08-31 06:00:00 -07:00
});
it('will set network to testnet', function() {
var config = {
network: 'testnet',
2015-08-31 06:00:00 -07:00
services: [
2015-08-31 06:00:00 -07:00
{
name: 'test1',
2015-08-31 06:00:00 -07:00
module: TestService
2015-08-31 06:00:00 -07:00
}
],
};
var TestNode = proxyquire('../lib/node', {});
TestNode.prototype.start = sinon.spy();
var node = new TestNode(config);
node.network.should.equal(Networks.testnet);
});
it('will set network to regtest', function() {
var config = {
network: 'regtest',
2015-08-31 06:00:00 -07:00
services: [
2015-08-31 06:00:00 -07:00
{
name: 'test1',
2015-08-31 06:00:00 -07:00
module: TestService
2015-08-31 06:00:00 -07:00
}
],
};
var TestNode = proxyquire('../lib/node', {});
TestNode.prototype.start = sinon.spy();
var node = new TestNode(config);
var regtest = Networks.get('regtest');
should.exist(regtest);
node.network.should.equal(regtest);
});
it('will be able to disable log formatting', function() {
var config = {
network: 'regtest',
services: [
{
name: 'test1',
module: TestService
}
],
formatLogs: false
};
var TestNode = proxyquire('../lib/node', {});
var node = new TestNode(config);
node.log.formatting.should.equal(false);
var TestNode = proxyquire('../lib/node', {});
config.formatLogs = true;
var node2 = new TestNode(config);
node2.log.formatting.should.equal(true);
});
2015-08-27 13:09:27 -07:00
});
2015-07-29 14:22:34 -07:00
describe('#openBus', function() {
it('will create a new bus', function() {
2015-08-31 06:00:00 -07:00
var node = new Node(baseConfig);
2015-07-29 14:22:34 -07:00
var bus = node.openBus();
2015-08-28 13:16:51 -07:00
bus.node.should.equal(node);
2015-07-29 14:22:34 -07:00
});
it('will use remoteAddress config option', function() {
var node = new Node(baseConfig);
var bus = node.openBus({remoteAddress: '127.0.0.1'});
bus.remoteAddress.should.equal('127.0.0.1');
});
2015-07-29 14:22:34 -07:00
});
2015-08-27 13:09:27 -07:00
describe('#getAllAPIMethods', function() {
2015-08-31 06:00:00 -07:00
it('should return db methods and service methods', function() {
2015-08-31 06:00:00 -07:00
var node = new Node(baseConfig);
2015-08-31 06:00:00 -07:00
node.services = {
2015-08-31 06:00:00 -07:00
db: {
getAPIMethods: sinon.stub().returns(['db1', 'db2']),
},
2015-08-31 06:00:00 -07:00
service1: {
2015-08-27 13:09:27 -07:00
getAPIMethods: sinon.stub().returns(['mda1', 'mda2'])
},
2015-08-31 06:00:00 -07:00
service2: {
2015-08-27 13:09:27 -07:00
getAPIMethods: sinon.stub().returns(['mdb1', 'mdb2'])
}
};
var methods = node.getAllAPIMethods();
methods.should.deep.equal(['db1', 'db2', 'mda1', 'mda2', 'mdb1', 'mdb2']);
});
it('will handle service without getAPIMethods defined', function() {
var node = new Node(baseConfig);
node.services = {
db: {
getAPIMethods: sinon.stub().returns(['db1', 'db2']),
},
service1: {},
service2: {
getAPIMethods: sinon.stub().returns(['mdb1', 'mdb2'])
}
};
var methods = node.getAllAPIMethods();
methods.should.deep.equal(['db1', 'db2', 'mdb1', 'mdb2']);
});
});
2015-08-31 06:00:00 -07:00
describe('#getAllPublishEvents', function() {
2015-08-31 06:00:00 -07:00
it('should return services publish events', function() {
2015-08-31 06:00:00 -07:00
var node = new Node(baseConfig);
2015-08-31 06:00:00 -07:00
node.services = {
2015-08-31 06:00:00 -07:00
db: {
getPublishEvents: sinon.stub().returns(['db1', 'db2']),
},
2015-08-31 06:00:00 -07:00
service1: {
2015-08-27 13:09:27 -07:00
getPublishEvents: sinon.stub().returns(['mda1', 'mda2'])
},
2015-08-31 06:00:00 -07:00
service2: {
2015-08-27 13:09:27 -07:00
getPublishEvents: sinon.stub().returns(['mdb1', 'mdb2'])
}
};
var events = node.getAllPublishEvents();
2015-08-06 13:19:36 -07:00
events.should.deep.equal(['db1', 'db2', 'mda1', 'mda2', 'mdb1', 'mdb2']);
});
it('will handle service without getPublishEvents defined', function() {
var node = new Node(baseConfig);
node.services = {
db: {
getPublishEvents: sinon.stub().returns(['db1', 'db2']),
},
service1: {},
service2: {
getPublishEvents: sinon.stub().returns(['mdb1', 'mdb2'])
}
};
var events = node.getAllPublishEvents();
events.should.deep.equal(['db1', 'db2', 'mdb1', 'mdb2']);
});
});
2015-08-27 07:10:07 -07:00
2015-08-31 06:00:00 -07:00
describe('#getServiceOrder', function() {
it('should return the services in the correct order', function() {
2015-08-31 06:00:00 -07:00
var node = new Node(baseConfig);
2015-08-31 06:00:00 -07:00
node._unloadedServices = [
2015-08-31 06:00:00 -07:00
{
name: 'chain',
2015-08-31 10:38:21 -07:00
module: {
dependencies: ['db']
}
2015-07-24 10:45:31 -07:00
},
2015-08-31 06:00:00 -07:00
{
name: 'db',
2015-08-31 10:38:21 -07:00
module: {
2015-08-31 06:00:00 -07:00
dependencies: ['daemon', 'p2p']
2015-08-31 10:38:21 -07:00
}
2015-07-16 12:53:44 -07:00
},
2015-08-31 06:00:00 -07:00
{
name:'daemon',
2015-08-31 10:38:21 -07:00
module: {
dependencies: []
}
2015-08-21 13:13:11 -07:00
},
2015-08-31 06:00:00 -07:00
{
name: 'p2p',
2015-08-31 10:38:21 -07:00
module: {
dependencies: []
}
2015-08-21 13:13:11 -07:00
}
2015-08-31 06:00:00 -07:00
];
var order = node.getServiceOrder();
order[0].name.should.equal('daemon');
order[1].name.should.equal('p2p');
order[2].name.should.equal('db');
order[3].name.should.equal('chain');
2015-08-21 13:13:11 -07:00
});
2015-07-16 12:53:44 -07:00
});
2015-08-21 13:13:11 -07:00
describe('#_startService', function() {
2016-05-17 21:33:57 -07:00
var sandbox = sinon.sandbox.create();
beforeEach(function() {
sandbox.stub(log, 'info');
});
afterEach(function() {
sandbox.restore();
});
2015-08-31 06:00:00 -07:00
it('will instantiate an instance and load api methods', function() {
var node = new Node(baseConfig);
2015-08-31 06:00:00 -07:00
function TestService() {}
util.inherits(TestService, BaseService);
TestService.prototype.start = sinon.stub().callsArg(0);
var getData = sinon.stub();
TestService.prototype.getData = getData;
2015-08-31 06:00:00 -07:00
TestService.prototype.getAPIMethods = function() {
2015-08-31 06:00:00 -07:00
return [
['getData', this, this.getData, 1]
];
2015-07-16 12:53:44 -07:00
};
2015-08-31 06:00:00 -07:00
var service = {
2015-08-31 06:00:00 -07:00
name: 'testservice',
2015-08-31 10:38:21 -07:00
module: TestService,
config: {}
2015-08-31 06:00:00 -07:00
};
node._startService(service, function(err) {
if (err) {
throw err;
}
TestService.prototype.start.callCount.should.equal(1);
should.exist(node.services.testservice);
should.exist(node.getData);
2016-06-03 12:54:01 -07:00
node.getData();
getData.callCount.should.equal(1);
});
});
it('will handle config not being set', function() {
var node = new Node(baseConfig);
function TestService() {}
util.inherits(TestService, BaseService);
TestService.prototype.start = sinon.stub().callsArg(0);
var getData = sinon.stub();
TestService.prototype.getData = getData;
TestService.prototype.getAPIMethods = function() {
return [
['getData', this, this.getData, 1]
];
};
var service = {
name: 'testservice',
module: TestService,
};
node._startService(service, function(err) {
if (err) {
throw err;
}
TestService.prototype.start.callCount.should.equal(1);
should.exist(node.services.testservice);
should.exist(node.getData);
node.getData();
getData.callCount.should.equal(1);
});
});
it('will give an error from start', function() {
var node = new Node(baseConfig);
function TestService() {}
util.inherits(TestService, BaseService);
TestService.prototype.start = sinon.stub().callsArgWith(0, new Error('test'));
var service = {
name: 'testservice',
module: TestService,
config: {}
};
node._startService(service, function(err) {
err.message.should.equal('test');
});
2015-07-16 12:53:44 -07:00
});
});
2015-08-31 06:00:00 -07:00
describe('#start', function() {
2016-05-17 21:33:57 -07:00
var sandbox = sinon.sandbox.create();
beforeEach(function() {
sandbox.stub(log, 'info');
});
afterEach(function() {
sandbox.restore();
});
2015-08-31 06:00:00 -07:00
it('will call start for each service', function(done) {
2015-08-31 06:00:00 -07:00
var node = new Node(baseConfig);
2015-08-31 06:00:00 -07:00
function TestService() {}
util.inherits(TestService, BaseService);
TestService.prototype.start = sinon.stub().callsArg(0);
TestService.prototype.getData = function() {};
TestService.prototype.getAPIMethods = function() {
2015-08-31 06:00:00 -07:00
return [
['getData', this, this.getData, 1]
];
2015-08-24 11:46:48 -07:00
};
2015-08-31 06:00:00 -07:00
function TestService2() {}
util.inherits(TestService2, BaseService);
TestService2.prototype.start = sinon.stub().callsArg(0);
TestService2.prototype.getData2 = function() {};
TestService2.prototype.getAPIMethods = function() {
2015-08-31 06:00:00 -07:00
return [
['getData2', this, this.getData2, 1]
];
};
2015-08-31 06:00:00 -07:00
2015-08-31 06:00:00 -07:00
node.getServiceOrder = sinon.stub().returns([
{
name: 'test1',
2015-08-31 10:38:21 -07:00
module: TestService,
config: {}
2015-08-31 06:00:00 -07:00
},
{
name: 'test2',
2015-08-31 10:38:21 -07:00
module: TestService2,
config: {}
2015-08-31 06:00:00 -07:00
}
]);
node.start(function() {
2015-08-31 06:00:00 -07:00
TestService2.prototype.start.callCount.should.equal(1);
TestService.prototype.start.callCount.should.equal(1);
2015-08-31 06:00:00 -07:00
should.exist(node.getData2);
should.exist(node.getData);
2015-08-31 06:00:00 -07:00
done();
2015-07-16 12:53:44 -07:00
});
2015-08-21 13:13:11 -07:00
});
2015-08-31 06:00:00 -07:00
it('will error if there are conflicting API methods', function(done) {
2015-08-31 06:00:00 -07:00
var node = new Node(baseConfig);
2015-08-20 14:50:14 -07:00
2015-08-31 06:00:00 -07:00
function TestService() {}
util.inherits(TestService, BaseService);
TestService.prototype.start = sinon.stub().callsArg(0);
TestService.prototype.getData = function() {};
TestService.prototype.getAPIMethods = function() {
2015-08-27 13:09:27 -07:00
return [
['getData', this, this.getData, 1]
];
};
2015-08-31 06:00:00 -07:00
2015-08-31 06:00:00 -07:00
function ConflictService() {}
util.inherits(ConflictService, BaseService);
ConflictService.prototype.start = sinon.stub().callsArg(0);
ConflictService.prototype.getData = function() {};
ConflictService.prototype.getAPIMethods = function() {
2015-08-31 06:00:00 -07:00
return [
['getData', this, this.getData, 1]
];
};
2015-08-27 13:09:27 -07:00
node.getServiceOrder = sinon.stub().returns([
{
2015-08-31 06:00:00 -07:00
name: 'test',
2015-08-31 10:38:21 -07:00
module: TestService,
config: {}
2015-08-27 13:09:27 -07:00
},
{
2015-08-31 06:00:00 -07:00
name: 'conflict',
2015-08-31 10:38:21 -07:00
module: ConflictService,
config: {}
2015-08-27 13:09:27 -07:00
}
]);
2015-08-31 06:00:00 -07:00
node.start(function(err) {
should.exist(err);
err.message.should.match(/^Existing API method\(s\) exists\:/);
2015-08-27 13:09:27 -07:00
done();
});
2015-08-31 06:00:00 -07:00
2015-08-27 13:09:27 -07:00
});
it('will handle service with getAPIMethods undefined', function(done) {
var node = new Node(baseConfig);
function TestService() {}
util.inherits(TestService, BaseService);
TestService.prototype.start = sinon.stub().callsArg(0);
TestService.prototype.getData = function() {};
node.getServiceOrder = sinon.stub().returns([
{
name: 'test',
module: TestService,
config: {}
},
]);
node.start(function() {
TestService.prototype.start.callCount.should.equal(1);
done();
});
});
2015-08-27 13:09:27 -07:00
});
describe('#getNetworkName', function() {
afterEach(function() {
bitcore.Networks.disableRegtest();
});
it('it will return the network name for livenet', function() {
var node = new Node(baseConfig);
node.getNetworkName().should.equal('livenet');
});
it('it will return the network name for testnet', function() {
var baseConfig = {
network: 'testnet'
};
var node = new Node(baseConfig);
node.getNetworkName().should.equal('testnet');
});
it('it will return the network for regtest', function() {
var baseConfig = {
network: 'regtest'
};
var node = new Node(baseConfig);
node.getNetworkName().should.equal('regtest');
});
});
2015-08-27 13:09:27 -07:00
describe('#stop', function() {
2016-05-17 21:33:57 -07:00
var sandbox = sinon.sandbox.create();
beforeEach(function() {
sandbox.stub(log, 'info');
});
afterEach(function() {
sandbox.restore();
});
2015-08-31 06:00:00 -07:00
it('will call stop for each service', function(done) {
2015-08-31 06:00:00 -07:00
var node = new Node(baseConfig);
2015-08-31 06:00:00 -07:00
function TestService() {}
util.inherits(TestService, BaseService);
TestService.prototype.stop = sinon.stub().callsArg(0);
TestService.prototype.getData = function() {};
TestService.prototype.getAPIMethods = function() {
2015-08-27 13:09:27 -07:00
return [
['getData', this, this.getData, 1]
];
};
2015-08-31 06:00:00 -07:00
node.services = {
'test1': new TestService({node: node})
2015-08-27 13:09:27 -07:00
};
node.test2 = {};
node.test2.stop = sinon.stub().callsArg(0);
node.getServiceOrder = sinon.stub().returns([
{
name: 'test1',
2015-08-31 06:00:00 -07:00
module: TestService
2015-08-27 13:09:27 -07:00
}
]);
node.stop(function() {
2015-08-31 06:00:00 -07:00
TestService.prototype.stop.callCount.should.equal(1);
2015-08-27 13:09:27 -07:00
done();
});
});
});
2015-07-16 12:53:44 -07:00
});