test(plugins): plugin load + configure tests added

This commit is contained in:
Damian Mee 2014-08-21 04:10:13 +02:00
parent f97fda585f
commit edffd2f595
11 changed files with 509 additions and 267 deletions

View file

@ -1,54 +1,54 @@
'use strict';
// 'use strict';
var assert = require('chai').assert;
var Trader = require('../../lib/trader.js');
var PostgresqlInterface = require('../../lib/postgresql_interface.js');
// var assert = require('chai').assert;
// var Trader = require('../../lib/trader.js');
// var PostgresqlInterface = require('../../lib/postgresql_interface.js');
var db = 'psql://lamassu:lamassu@localhost/lamassu-test';
var psqlInterface = new PostgresqlInterface(db);
// var db = 'psql://lamassu:lamassu@localhost/lamassu-test';
// var psqlInterface = new PostgresqlInterface(db);
describe('trader/api', function () {
it('should throw when trying to create a trader with no DB', function () {
assert.throws(function () {
new Trader();
});
});
// describe('trader/api', function () {
// it('should throw when trying to create a trader with no DB', function () {
// assert.throws(function () {
// new Trader();
// });
// });
it('should throw when trying to configure a trader with `lowBalanceMargin` < 1', function () {
var trader = new Trader(psqlInterface);
assert.throws(function () {
trader.configure({
exchanges: {
settings: {
lowBalanceMargin: 0.8
}
}
});
});
});
// it('should throw when trying to configure a trader with `lowBalanceMargin` < 1', function () {
// var trader = new Trader(psqlInterface);
// assert.throws(function () {
// trader.configure({
// exchanges: {
// settings: {
// lowBalanceMargin: 0.8
// }
// }
// });
// });
// });
it('should find and instantiate ticker and trade exchanges', function () {
var trader = new Trader(psqlInterface);
trader.configure({
exchanges: {
plugins: {
current: {
ticker: 'bitpay',
transfer: 'blockchain'
},
settings: {
bitpay: {},
blockchain: {}
}
},
settings: {
currency: 'USD',
lowBalanceMargin: 2
}
}
});
// it('should find and instantiate ticker and trade exchanges', function () {
// var trader = new Trader(psqlInterface);
// trader.configure({
// exchanges: {
// plugins: {
// current: {
// ticker: 'bitpay',
// transfer: 'blockchain'
// },
// settings: {
// bitpay: {},
// blockchain: {}
// }
// },
// settings: {
// currency: 'USD',
// lowBalanceMargin: 2
// }
// }
// });
assert.ok(trader.tickerExchange);
assert.ok(trader.transferExchange);
});
});
// assert.ok(trader.tickerExchange);
// assert.ok(trader.transferExchange);
// });
// });

View file

@ -1,107 +1,107 @@
/*global describe, it */
'use strict';
// /*global describe, it */
// 'use strict';
var assert = require('chai').assert;
var Trader = require('../../lib/trader.js');
// var assert = require('chai').assert;
// var Trader = require('../../lib/trader.js');
var db = 'psql://lamassu:lamassu@localhost/lamassu-test';
// var db = 'psql://lamassu:lamassu@localhost/lamassu-test';
var RATE = 101;
var CURRENCY = 'USD';
var SATOSHI_FACTOR = 1e8;
var LOW_BALANCE_MARGIN = 1.2;
var COMMISSION = 1.1;
var FINGERPRINT = '00:7A:5A:B3:02:F1:44:46:E2:EA:24:D3:A8:29:DE:22:BA:1B:F9:50';
// var RATE = 101;
// var CURRENCY = 'USD';
// var SATOSHI_FACTOR = 1e8;
// var LOW_BALANCE_MARGIN = 1.2;
// var COMMISSION = 1.1;
// var FINGERPRINT = '00:7A:5A:B3:02:F1:44:46:E2:EA:24:D3:A8:29:DE:22:BA:1B:F9:50';
var settings = {
currency: CURRENCY,
lowBalanceMargin: LOW_BALANCE_MARGIN,
commission: COMMISSION
};
// var settings = {
// currency: CURRENCY,
// lowBalanceMargin: LOW_BALANCE_MARGIN,
// commission: COMMISSION
// };
describe('trader/fiatBalance', function() {
it('should calculate balance correctly with transfer exchange only', function() {
var trader = new Trader(db);
trader.configure({
exchanges: {
plugins: {
current: {
transfer: 'blockchain',
ticker: 'bitpay'
},
settings: { blockchain: {}, bitpay: {} }
},
settings: settings
}
});
// describe('trader/fiatBalance', function() {
// it('should calculate balance correctly with transfer exchange only', function() {
// var trader = new Trader(db);
// trader.configure({
// exchanges: {
// plugins: {
// current: {
// transfer: 'blockchain',
// ticker: 'bitpay'
// },
// settings: { blockchain: {}, bitpay: {} }
// },
// settings: settings
// }
// });
// We have 3 bitcoins, want to trade 1 bitcoin for 100 fiat
trader.balance = {
transferBalance: 3 * SATOSHI_FACTOR,
tradeBalance: null
};
trader.rates[CURRENCY] = { rate: RATE };
trader.rateInfo = {rates: {USD: {rate: RATE}}};
var fiatBalance = trader.fiatBalance(FINGERPRINT);
assert.equal(fiatBalance, (3 * RATE * COMMISSION / LOW_BALANCE_MARGIN));
});
// // We have 3 bitcoins, want to trade 1 bitcoin for 100 fiat
// trader.balance = {
// transferBalance: 3 * SATOSHI_FACTOR,
// tradeBalance: null
// };
// trader.rates[CURRENCY] = { rate: RATE };
// trader.rateInfo = {rates: {USD: {rate: RATE}}};
// var fiatBalance = trader.fiatBalance(FINGERPRINT);
// assert.equal(fiatBalance, (3 * RATE * COMMISSION / LOW_BALANCE_MARGIN));
// });
it('should calculate balance correctly with transfer and trade exchange', function() {
var trader = new Trader(db);
trader.configure({
exchanges: {
plugins: {
current: {
transfer: 'blockchain',
ticker: 'bitpay',
trade: 'bitstamp'
},
settings: { blockchain: {}, bitpay: {}, bitstamp: {} }
},
settings: settings
}
});
// it('should calculate balance correctly with transfer and trade exchange', function() {
// var trader = new Trader(db);
// trader.configure({
// exchanges: {
// plugins: {
// current: {
// transfer: 'blockchain',
// ticker: 'bitpay',
// trade: 'bitstamp'
// },
// settings: { blockchain: {}, bitpay: {}, bitstamp: {} }
// },
// settings: settings
// }
// });
// We have 3 bitcoins in transfer, worth 3 * RATE * COMMISSION = 333.3
// We have 150 USD in trade
trader.balance = {
transferBalance: 3 * SATOSHI_FACTOR,
tradeBalance: 150
};
trader.rates[CURRENCY] = { rate: RATE };
trader.rateInfo = {rates: {USD: {rate: RATE}}};
var fiatBalance = trader.fiatBalance(FINGERPRINT);
assert.equal(fiatBalance, 150 / LOW_BALANCE_MARGIN);
});
// // We have 3 bitcoins in transfer, worth 3 * RATE * COMMISSION = 333.3
// // We have 150 USD in trade
// trader.balance = {
// transferBalance: 3 * SATOSHI_FACTOR,
// tradeBalance: 150
// };
// trader.rates[CURRENCY] = { rate: RATE };
// trader.rateInfo = {rates: {USD: {rate: RATE}}};
// var fiatBalance = trader.fiatBalance(FINGERPRINT);
// assert.equal(fiatBalance, 150 / LOW_BALANCE_MARGIN);
// });
it('should calculate balance correctly with transfer and ' +
'trade exchange with different currencies', function() {
var trader = new Trader(db);
trader.configure({
exchanges: {
plugins: {
current: {
transfer: 'blockchain',
ticker: 'bitpay',
trade: 'bitstamp'
},
settings: { blockchain: {}, bitpay: {}, bitstamp: {} }
},
settings: settings
}
});
// it('should calculate balance correctly with transfer and ' +
// 'trade exchange with different currencies', function() {
// var trader = new Trader(db);
// trader.configure({
// exchanges: {
// plugins: {
// current: {
// transfer: 'blockchain',
// ticker: 'bitpay',
// trade: 'bitstamp'
// },
// settings: { blockchain: {}, bitpay: {}, bitstamp: {} }
// },
// settings: settings
// }
// });
// We have 6 bitcoins in transfer, worth 6 * RATE * COMMISSION = 666.6
// We have 150 USD in trade, 1 USD = 4 ILS => 600 ILS in trade
trader.balance = {
transferBalance: 6 * SATOSHI_FACTOR,
tradeBalance: 600
};
trader.rates = {USD: {rate: RATE}, ILS: {rate: RATE * 4} };
trader.rateInfo = {rates: {USD: {rate: RATE}}};
var fiatBalance = trader.fiatBalance(FINGERPRINT);
assert.equal(fiatBalance, 600 / LOW_BALANCE_MARGIN);
});
// // We have 6 bitcoins in transfer, worth 6 * RATE * COMMISSION = 666.6
// // We have 150 USD in trade, 1 USD = 4 ILS => 600 ILS in trade
// trader.balance = {
// transferBalance: 6 * SATOSHI_FACTOR,
// tradeBalance: 600
// };
// trader.rates = {USD: {rate: RATE}, ILS: {rate: RATE * 4} };
// trader.rateInfo = {rates: {USD: {rate: RATE}}};
// var fiatBalance = trader.fiatBalance(FINGERPRINT);
// assert.equal(fiatBalance, 600 / LOW_BALANCE_MARGIN);
// });
});
// });

View file

@ -1,77 +1,77 @@
'use strict';
// 'use strict';
var assert = require('chai').assert;
var hock = require('hock');
var uuid = require('node-uuid').v4;
var Trader = require('../../lib/trader.js');
var PostgresqlInterface = require('../../lib/postgresql_interface.js');
// var assert = require('chai').assert;
// var hock = require('hock');
// var uuid = require('node-uuid').v4;
// var Trader = require('../../lib/trader.js');
// var PostgresqlInterface = require('../../lib/postgresql_interface.js');
var db = 'psql://lamassu:lamassu@localhost/lamassu-test';
var psqlInterface = new PostgresqlInterface(db);
// var db = 'psql://lamassu:lamassu@localhost/lamassu-test';
// var psqlInterface = new PostgresqlInterface(db);
var TRANSACTION_FEE = 1;
var FINGERPRINT = 'CB:3D:78:49:03:39:BA:47:0A:33:29:3E:31:25:F7:C6:4F:74:71:D7';
var TXID = '216dabdb692670bae940deb71e59486038a575f637903d3c9af601ddd48057fc';
var ADDRESS = '1LhkU2R8nJaU8Zj6jB8VjWrMpvVKGqCZ64';
var SATOSHIS = 1337;
var CURRENCY = 'USD';
// var TRANSACTION_FEE = 1;
// var FINGERPRINT = 'CB:3D:78:49:03:39:BA:47:0A:33:29:3E:31:25:F7:C6:4F:74:71:D7';
// var TXID = '216dabdb692670bae940deb71e59486038a575f637903d3c9af601ddd48057fc';
// var ADDRESS = '1LhkU2R8nJaU8Zj6jB8VjWrMpvVKGqCZ64';
// var SATOSHIS = 1337;
// var CURRENCY = 'USD';
var OUR_TXID = uuid();
// var OUR_TXID = uuid();
describe('trader/send', function () {
var trader = new Trader(psqlInterface);
trader.config = {
exchanges: {
settings: {
transactionFee: TRANSACTION_FEE
}
}
};
// describe('trader/send', function () {
// var trader = new Trader(psqlInterface);
// trader.config = {
// exchanges: {
// settings: {
// transactionFee: TRANSACTION_FEE
// }
// }
// };
trader.pollRate = function () {};
// trader.pollRate = function () {};
it('should call `sendBitcoins` on the transfer exchange', function (done) {
trader.transferExchange = {
sendBitcoins: function (address, satoshis, transactionFee, callback) {
assert.equal(ADDRESS, address);
assert.equal(SATOSHIS, satoshis);
assert.equal(transactionFee, TRANSACTION_FEE);
callback(null, TXID);
},
balance: function () {}
};
// it('should call `sendBitcoins` on the transfer exchange', function (done) {
// trader.transferExchange = {
// sendBitcoins: function (address, satoshis, transactionFee, callback) {
// assert.equal(ADDRESS, address);
// assert.equal(SATOSHIS, satoshis);
// assert.equal(transactionFee, TRANSACTION_FEE);
// callback(null, TXID);
// },
// balance: function () {}
// };
trader.sendBitcoins(FINGERPRINT, {
fiat: 100,
txId: OUR_TXID,
currencyCode: CURRENCY,
toAddress: ADDRESS,
satoshis: SATOSHIS
}, function (err, txId) {
assert.notOk(err);
assert.equal(txId, TXID);
done();
});
});
// trader.sendBitcoins(FINGERPRINT, {
// fiat: 100,
// txId: OUR_TXID,
// currencyCode: CURRENCY,
// toAddress: ADDRESS,
// satoshis: SATOSHIS
// }, function (err, txId) {
// assert.notOk(err);
// assert.equal(txId, TXID);
// done();
// });
// });
it('should not call `sendBitcoins` on the transfer exchange with same send', function (done) {
trader.transferExchange = {
sendBitcoins: function () {
throw new Error('This should not have been called');
},
balance: function () {}
};
// it('should not call `sendBitcoins` on the transfer exchange with same send', function (done) {
// trader.transferExchange = {
// sendBitcoins: function () {
// throw new Error('This should not have been called');
// },
// balance: function () {}
// };
trader.sendBitcoins(FINGERPRINT, {
fiat: 100,
txId: OUR_TXID,
currencyCode: CURRENCY,
toAddress: ADDRESS,
satoshis: SATOSHIS
}, function (err, txId) {
assert.notOk(err);
assert.equal(txId, TXID);
done();
});
});
});
// trader.sendBitcoins(FINGERPRINT, {
// fiat: 100,
// txId: OUR_TXID,
// currencyCode: CURRENCY,
// toAddress: ADDRESS,
// satoshis: SATOSHIS
// }, function (err, txId) {
// assert.notOk(err);
// assert.equal(txId, TXID);
// done();
// });
// });
// });

View file

@ -1,52 +1,52 @@
/*global describe, it */
'use strict';
// /*global describe, it */
// 'use strict';
var assert = require('chai').assert;
var Trader = require('../../lib/trader.js');
var PostgresqlInterface = require('../../lib/postgresql_interface.js');
// var assert = require('chai').assert;
// var Trader = require('../../lib/trader.js');
// var PostgresqlInterface = require('../../lib/postgresql_interface.js');
var db = 'psql://lamassu:lamassu@localhost/lamassu-test';
var psqlInterface = new PostgresqlInterface(db);
// var db = 'psql://lamassu:lamassu@localhost/lamassu-test';
// var psqlInterface = new PostgresqlInterface(db);
var CURRENCY = 'USD';
// var CURRENCY = 'USD';
describe('trader/send', function () {
var trader = new Trader(psqlInterface);
trader.config = {
exchanges: {
settings: { currency: CURRENCY }
}
};
// describe('trader/send', function () {
// var trader = new Trader(psqlInterface);
// trader.config = {
// exchanges: {
// settings: { currency: CURRENCY }
// }
// };
it('should call `balance` on the transfer exchange', function (done) {
trader.transferExchange = {
balance: function (callback) {
callback(null, 100);
}
};
// it('should call `balance` on the transfer exchange', function (done) {
// trader.transferExchange = {
// balance: function (callback) {
// callback(null, 100);
// }
// };
trader.pollBalance(function (err) {
assert.notOk(err);
assert.equal(trader.balance.transferBalance, 100);
assert.ok(trader.balance.timestamp);
done();
});
});
// trader.pollBalance(function (err) {
// assert.notOk(err);
// assert.equal(trader.balance.transferBalance, 100);
// assert.ok(trader.balance.timestamp);
// done();
// });
// });
it('should call `ticker` on the ticker exchange', function (done) {
trader.tickerExchange = {
ticker: function (currencies, callback) {
assert.equal(currencies[0], CURRENCY);
callback(null, {USD: {rate: 100}});
}
};
// it('should call `ticker` on the ticker exchange', function (done) {
// trader.tickerExchange = {
// ticker: function (currencies, callback) {
// assert.equal(currencies[0], CURRENCY);
// callback(null, {USD: {rate: 100}});
// }
// };
trader.pollRate(function (err) {
assert.notOk(err);
var rate = trader.rate(CURRENCY);
assert.equal(rate.rate, 100);
assert.ok(rate.timestamp);
done();
});
});
});
// trader.pollRate(function (err) {
// assert.notOk(err);
// var rate = trader.rate(CURRENCY);
// assert.equal(rate.rate, 100);
// assert.ok(rate.timestamp);
// done();
// });
// });
// });