test-pack-ops.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var run = require('./run.js');
  2. var repo = {};
  3. require('../mixins/mem-db.js')(repo);
  4. var pack = require('./sample-pack.js');
  5. var hashes;
  6. run([
  7. function setup() {
  8. require('../mixins/pack-ops.js')(repo);
  9. },
  10. function testUnpack(end) {
  11. repo.unpack(singleStream(pack), {
  12. onProgress: onProgress
  13. }, function (err, result) {
  14. if (err) return end(err);
  15. hashes = result;
  16. if (hashes.length !== 16) {
  17. return end(new Error("Wrong number of objects unpacked"));
  18. }
  19. end();
  20. });
  21. function onProgress(progress) {
  22. // console.log(progress);
  23. }
  24. },
  25. function testPack(end) {
  26. var stream;
  27. var parts = [];
  28. repo.pack(hashes, {}, function (err, result) {
  29. if (err) return end(err);
  30. stream = result;
  31. stream.take(onRead);
  32. });
  33. function onRead(err, chunk) {
  34. if (err) return end(err);
  35. // console.log(chunk);
  36. if (chunk) {
  37. parts.push(chunk);
  38. return stream.take(onRead);
  39. }
  40. end();
  41. }
  42. }
  43. ]);
  44. function singleStream(item) {
  45. var done = false;
  46. return { take: function (callback) {
  47. if (done) return callback();
  48. done = true;
  49. callback(null, item);
  50. }};
  51. }