<commit_before>var
  async = require('async'),
  traverse = require('traverse');

var pockets = module.exports;

pockets.ROOT = null;

pockets.list = function (options, callback) {
  callback = callback || function () {
  };

  var calls = [];
  traverse.forEach(pockets.ROOT, function (x) {
    var point = this;
    if (x && typeof x === 'object' && x.name) {
      var fn = function (cb) {
        pockets.get(x, function (err, pocket_with_balance) {
          if (err)
            return cb(err);
          point.update(pocket_with_balance);
          return cb(null);
        });
      };
      if (options.mock)
        fn = function (cb) {
          return cb(null);
        };
      calls.push(fn);
    }
  });
  async.series(calls, function (err) {
    if (err)
      return callback(err);
    engine.bitcoin.balance(pockets.ROOT, function (err, balance) {
      if (err)
        return callback(err);
      pockets.ROOT.wallet.balance = balance;
      return callback(null, pockets.ROOT);
    });

  });
};

pockets.create = function (options, callback) {
  callback = callback || function () {
  };

  if (options.level_ratio)
    options.hard_ratio = options.level_ratio;

  if (!options.parent) {
    var calls = [];
    traverse.forEach(options, function (x) {
      var point = this;
      if (x && typeof x === 'object' && x.name) {
        var fn = function (cb) {
          engine.bitcoin.createWallet({}, function (err, result) {
            if (err)
              return callback(err);
            x.wallet = x.wallet || result;
            point.update(x);
            engine.listener.add(x.wallet.address);
            return cb(null, result);
          });
        };
        calls.push(fn);
      }
    });
    async.series(calls, function (err, results) {
      pockets.ROOT = options;
      return pockets.save({}, callback);
    });
  }
  else {
    pockets.get({name: options.parent}, function (err, parent) {
      if (err)
        return callback(err);
      if (!parent.pockets)
        parent.pockets = {};
      engine.bitcoin.createWallet({}, function (err, result) {
        if (err)
          return callback(err);
        options.wallet = result;
        parent.pockets[options.name] = options;
        return pockets.save({}, callback);
      });
    });
  }
};

pockets.get = function (options, callback) {
  callback = callback || function () {
  };
  var recurse = function (parent, lookup) {
    if (!parent.pockets)
      return;
    var found = null;
    Object.keys(parent.pockets).forEach(function (key) {
      var pocket = parent.pockets[key];
      if (pocket.name === lookup) {
        found = pocket;
      }
      if (!found && pocket.pockets)
        found = recurse(pocket, lookup);
    });
    if (found)
      return found;
  };

  if (pockets.ROOT.name === options.name)
    return callback(null, pockets.ROOT);
  var found = recurse(pockets.ROOT, options.name);
  if (found) {
    if (options.mock)
      return callback(null, found);
    engine.bitcoin.balance(found, function (err, balance) {
      if (err)
        return callback(err);
      found.wallet.balance = balance;
      return callback(null, found);
    });
  }
  else
    return callback(new Error('Failed to find pocket with name [' + options.name + '].'));
};

pockets.update = function (options, callback) {
  callback = callback || function () {
  };

  pockets.get(options, function (err, pocket) {
    if (err)
      return callback(err);

    engine.common.extend(pocket, options);
    return pockets.save({}, callback);
  });
};

pockets.delete = function (options, callback) {
  callback = callback || function () {
  };
  var recurse = function (parent, lookup) {
    var found = null;
    Object.keys(parent.pockets).forEach(function (key, i) {
      var pocket = parent.pockets[key];
      if (pocket.name === lookup) {
        found = traverse.clone(parent.pockets[pocket.name]);
        delete parent.pockets[pocket.name];
      }
      if (!found && pocket.pockets)
        found = recurse(pocket, lookup);
    });
    if (found)
      return found;
  };

  if (pockets.ROOT[options.name] === options.name)
    return callback(null, pockets.ROOT = {});

  var found = recurse(pockets.ROOT, options.name);
  if (found) {
    pockets.get({name: 'root'}, function (err, rootPocket) {
      engine.bitcoin.handleTransaction({
        transactions: [
          {
            from: {
              name: found.name,
              wallet: found.wallet
            },
            to: {
              name: 'root',
              wallet: rootPocket.wallet
            },
            amount: found.wallet.balance
          }
        ]
      }, function (err) {
        if (err)
          return callback(err);
        engine.listener.remove(found.wallet.address);
        pockets.save({}, callback);
      });
    });
  }
  else
    return callback(new Error('Failed to find pocket with name [' + options.name + '].'));
};


pockets.save = function (options, callback) {
  engine.db.set('pockets.json', pockets.ROOT);
  return callback(null);
};

pockets.load = function (options, callback) {
  pockets.ROOT = engine.db.get('pockets.json');
  return callback(null);
};

pockets.snapshot = function (options, callback) {
  var result = [];
  traverse.map(pockets.ROOT, function (x) {
    if (x && typeof x === 'object' && x.wallet && x.wallet.balance) {
      result.push({name: x.name, balance: x.wallet.balance});
    }
  });
  return callback(null, result);
};

pockets.totalBalance = function (options, callback) {
  var balance = 0;
  return engine.pockets.get({name: 'root'}, function (err, rootPocket) {
    traverse.forEach(rootPocket, function (x) {
      if (x && typeof x === 'object' && x.wallet && x.wallet.balance)<commit_msg>#16 tests failing.<commit_after>var
  async = require('async'),
  traverse = require('traverse');

var pockets = module.exports;

pockets.ROOT = null;

pockets.list = function (options, callback) {
  callback = callback || function () {
  };

  var calls = [];
  traverse.forEach(pockets.ROOT, function (x) {
    var point = this;
    if (x && typeof x === 'object' && x.name) {
      var fn = function (cb) {
        pockets.get(x, function (err, pocket_with_balance) {
          if (err)
            return cb(err);
          point.update(pocket_with_balance);
          return cb(null);
        });
      };
      if (options.mock)
        fn = function (cb) {
          return cb(null);
        };
      calls.push(fn);
    }
  });
  async.series(calls, function (err) {
    if (err)
      return callback(err);
    engine.bitcoin.balance(pockets.ROOT, function (err, balance) {
      if (err)
        return callback(err);
      pockets.ROOT.wallet.balance = balance;
      return callback(null, pockets.ROOT);
    });

  });
};

pockets.create = function (options, callback) {
  callback = callback || function () {
  };

  if (options.level_ratio)
    options.hard_ratio = options.level_ratio;

  if (!options.parent) {
    var calls = [];
    traverse.forEach(options, function (x) {
      var point = this;
      if (x && typeof x === 'object' && x.name) {
        var fn = function (cb) {
          engine.bitcoin.createWallet({}, function (err, result) {
            if (err)
              return callback(err);
            x.wallet = x.wallet || result;
            point.update(x);
            engine.listener.add(x.wallet.address);
            return cb(null, result);
          });
        };
        calls.push(fn);
      }
    });
    async.series(calls, function (err, results) {
      pockets.ROOT = options;
      return pockets.save({}, callback);
    });
  }
  else {
    pockets.get({name: options.parent}, function (err, parent) {
      if (err)
        return callback(err);
      if (!parent.pockets)
        parent.pockets = {};
      engine.bitcoin.createWallet({}, function (err, result) {
        if (err)
          return callback(err);
        options.wallet = result;
        parent.pockets[options.name] = options;
        return pockets.save({}, callback);
      });
    });
  }
};

pockets.get = function (options, callback) {
  callback = callback || function () {
  };
  var recurse = function (parent, lookup) {
    if (!parent.pockets)
      return;
    var found = null;
    Object.keys(parent.pockets).forEach(function (key) {
      var pocket = parent.pockets[key];
      if (pocket.name === lookup) {
        found = pocket;
      }
      if (!found && pocket.pockets)
        found = recurse(pocket, lookup);
    });
    if (found)
      return found;
  };

  if (pockets.ROOT.name === options.name)
    return callback(null, pockets.ROOT);
  var found = recurse(pockets.ROOT, options.name);
  if (found) {
    if (options.mock)
      return callback(null, found);
    engine.bitcoin.balance(found, function (err, balance) {
      if (err)
        return callback(err);
      found.wallet.balance = balance;
      return callback(null, found);
    });
  }
  else
    return callback(new Error('Failed to find pocket with name [' + options.name + '].'));
};

pockets.update = function (options, callback) {
  callback = callback || function () {
  };

  pockets.get(options, function (err, pocket) {
    if (err)
      return callback(err);

    engine.common.extend(pocket, options);
    return pockets.save({}, callback);
  });
};

pockets.delete = function (options, callback) {
  callback = callback || function () {
  };
  var recurse = function (parent, lookup) {
    var found = null;
    Object.keys(parent.pockets).forEach(function (key, i) {
      var pocket = parent.pockets[key];
      if (pocket.name === lookup) {
        found = traverse.clone(parent.pockets[pocket.name]);
        delete parent.pockets[pocket.name];
      }
      if (!found && pocket.pockets)
        found = recurse(pocket, lookup);
    });
    if (found)
      return found;
  };

  if (pockets.ROOT[options.name] === options.name)
    return callback(null, pockets.ROOT = {});

  var found = recurse(pockets.ROOT, options.name);
  if (found) {
    pockets.get({name: 'root'}, function (err, rootPocket) {
      engine.bitcoin.handleTransaction({
        transactions: [
          {
            from: {
              name: found.name,
              wallet: found.wallet
            },
            to: {
              name: 'root',
              wallet: rootPocket.wallet
            },
            amount: found.wallet.balance
          }
        ]
      }, function (err) {
        if (err)
          return callback(err);
        engine.listener.remove(found.wallet.address);
        pockets.save({}, callback);
      });
    });
  }
  else
    return callback(new Error('Failed to find pocket with name [' + options.name + '].'));
};


pockets.validate = function (options, callback) {
  callback = callback || function () {
  };

  return engine.bitcoin.validate({address: options.wallet.address}, callback);
};

pockets.save = function (options, callback) {
  callback = callback || function () {
  };

  return pockets.realign({mock: true}, function (err) {
    return pockets.ensureLevelPercentages({}, function (err) {
      return pockets.realign({}, function (err) {
        if (err)
          return callback(err);
        engine.db.set('pockets.json', JSON.stringify(pockets.ROOT, null, '  '));
        return callback(null);
      });
    });
  });
};

pockets.load = function (options, callback) {
  pockets.ROOT = engine.db.get('pockets.json');
  return callback(null);
};

pockets.snapshot = function (options, callback) {
  var result = [];
  traverse.map(pockets.ROOT, function (x) {
    if (x && typeof x === 'object' && x.wallet && x.wallet.balance) {
      result.push({name: x.name, balance: x.wallet.balance});
    }
  });
  return callback(null, result);
};

pockets.totalBalance = function (options, callback) {
  var balance = 0;
  return engine.pockets.get({name: 'root'}, function (err, rootPocket) {
    traverse.forEach(rootPocket, function (x) {
      if (x && typeof x === 'object' && x.wallet && x.wallet.balance)
{'fraction_non_alphanumeric': 0.15055638598239496, 'fraction_numerical': 0.0003321707357581797, 'mean_word_length': 2.3508625486922647, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3205995', 'n_tokens_mistral': 3649, 'n_tokens_neox': 3672, 'n_words': 847}