From 10ce6f3a123dde76428a31b2e545d776d5fd214b Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Thu, 8 Nov 2012 18:09:18 -0800 Subject: [PATCH] UT: Add support for verifying offers & fix amount checking. --- src/js/remote.js | 28 +++++++++++++++++-- test/testutils.js | 70 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 82 insertions(+), 16 deletions(-) diff --git a/src/js/remote.js b/src/js/remote.js index 488df0b4..bdca2f3f 100644 --- a/src/js/remote.js +++ b/src/js/remote.js @@ -97,6 +97,25 @@ Request.prototype.index = function (hash) { return this; }; +// Provide the information id an offer. +// --> account +// --> seq : sequence number of transaction creating offer (integer) +Request.prototype.offer_id = function (account, seq) { + this.message.offer = { + 'account' : UInt160.json_rewrite(account), + 'seq' : seq + }; + + return this; +}; + +// --> index : ledger entry index. +Request.prototype.offer_index = function (index) { + this.message.offer = index; + + return this; +}; + Request.prototype.secret = function (s) { if (s) this.message.secret = s; @@ -403,6 +422,7 @@ Remote.prototype._connect_message = function (ws, json) { else { switch (message.type) { case 'response': + // A response to a request. { request = ws.response[message.id]; @@ -510,8 +530,10 @@ Remote.prototype.request_ledger_current = function () { return request; }; -// --> ledger : optional -// --> ledger_index : optional +// --> type : the type of ledger entry. +// .ledger() +// .ledger_index() +// .offer_id() Remote.prototype.request_ledger_entry = function (type) { assert(this.trusted); // If not trusted, need to check proof, maybe talk packet protocol. @@ -684,7 +706,7 @@ Remote.prototype._server_subscribe = function () { return this; }; -// Ask the remote to accept the current ledger. +// For unit testing: ask the remote to accept the current ledger. // - To be notified when the ledger is accepted, server_subscribe() then listen to 'ledger_hash' events. // A good way to be notified of the result of this is: // remote.once('ledger_closed', function (ledger_closed, ledger_index) { ... } ); diff --git a/test/testutils.js b/test/testutils.js index a82cc1a4..b59d46ae 100644 --- a/test/testutils.js +++ b/test/testutils.js @@ -226,7 +226,7 @@ var verify_balance = function (remote, src, amount_json, callback) { if (amount.is_native()) { // XXX Not implemented. - callback(false); + callback(); } else { remote.request_ripple_balance(src, amount.issuer.to_json(), amount.currency.to_json(), 'CURRENT') @@ -237,11 +237,13 @@ var verify_balance = function (remote, src, amount_json, callback) { // console.log("issuer_balance: %s", m.issuer_balance.to_text_full()); // console.log("issuer_limit: %s", m.issuer_limit.to_text_full()); - if (!m.account_balance.equals(amount)) { + var account_balance = Amount.from_json(m.account_balance); + + if (!account_balance.equals(amount)) { console.log("verify_balance: failed: %s vs %s is %s", src, amount_json, amount.to_text_full()); } - callback(!m.account_balance.equals(amount)); + callback(!account_balance.equals(amount)); }) .request(); } @@ -269,15 +271,57 @@ var verify_balances = function (remote, balances, callback) { }); }; -exports.build_setup = build_setup; -exports.create_accounts = create_accounts; -exports.credit_limit = credit_limit; -exports.credit_limits = credit_limits; -exports.payment = payment; -exports.payments = payments; -exports.build_teardown = build_teardown; -exports.transfer_rate = transfer_rate; -exports.verify_balance = verify_balance; -exports.verify_balances = verify_balances; +// --> owner: account +// --> seq: sequence number of creating transaction. +// --> taker_gets: json amount +// --> taker_pays: json amount +var verify_offer = function (remote, owner, seq, taker_gets, taker_pays, callback) { + assert(6 === arguments.length); + + remote.request_ledger_entry('offer') + .offer_id(owner, seq) + .on('success', function (m) { + var wrong = (!Amount.from_json(m.node.TakerGets).equals(Amount.from_json(taker_gets)) + || !Amount.from_json(m.node.TakerPays).equals(Amount.from_json(taker_pays))); + + if (wrong) + console.log("verify_offer: failed: %s", JSON.stringify(m)); + + callback(wrong); + }) + .request(); +}; + +var verify_offer_not_found = function (remote, owner, seq, callback) { + assert(4 === arguments.length); + + remote.request_ledger_entry('offer') + .offer_id(owner, seq) + .on('success', function (m) { + console.log("verify_no_offer: found offer: %s", JSON.stringify(m)); + + callback('entryFound'); + }) + .on('error', function (m) { + console.log("verify_no_offer: success: %s", JSON.stringify(m)); + + callback('remoteError' !== m.error + || 'entryNotFound' !== m.remote.error); + }) + .request(); +}; + +exports.build_setup = build_setup; +exports.create_accounts = create_accounts; +exports.credit_limit = credit_limit; +exports.credit_limits = credit_limits; +exports.payment = payment; +exports.payments = payments; +exports.build_teardown = build_teardown; +exports.transfer_rate = transfer_rate; +exports.verify_balance = verify_balance; +exports.verify_balances = verify_balances; +exports.verify_offer = verify_offer; +exports.verify_offer_not_found = verify_offer_not_found; // vim:sw=2:sts=2:ts=8