|
| 1 | +var test = require('tape') |
| 2 | +var Jawn = require('../') |
| 3 | +var memdb = require('memdb') |
| 4 | +var feedOps = require('../lib/feedOperations.js') |
| 5 | + |
| 6 | +test('appendable feed', function (t) { |
| 7 | + var jawn = freshJawn() |
| 8 | + var feed = jawn.core.add() |
| 9 | + |
| 10 | + feed.pappend = function (data) { |
| 11 | + return new Promise(function (resolve, reject) { |
| 12 | + feed.append(data, resolve) |
| 13 | + }) |
| 14 | + } |
| 15 | + |
| 16 | + feed.pfinalize = function () { |
| 17 | + return new Promise(function (resolve, reject) { |
| 18 | + feed.finalize(resolve) |
| 19 | + }) |
| 20 | + } |
| 21 | + |
| 22 | + feed.pappend('hello').then(function () { |
| 23 | + console.log('Appended') |
| 24 | + return feed |
| 25 | + }) |
| 26 | + .then(function (feed) { |
| 27 | + return feed.pfinalize() |
| 28 | + }) |
| 29 | + .then(function () { |
| 30 | + console.log('Finalized with id ' + feed.id.toString('hex')) |
| 31 | + var appfeed = feedOps.appendTo(jawn.core, feed.id) |
| 32 | + appfeed.initialize().then(function () { |
| 33 | + return appfeed.finalize_p() |
| 34 | + }) |
| 35 | + .then(function () { |
| 36 | + t.same(appfeed.blocks, feed.blocks, 'testing') |
| 37 | + t.end() |
| 38 | + }) |
| 39 | + }) |
| 40 | +}) |
| 41 | + |
| 42 | +test('append to feed', function (t) { |
| 43 | + var jawn = freshJawn() |
| 44 | + var feed = jawn.core.add() |
| 45 | + var expected = ['hello', 'there', 'goodbye'] |
| 46 | + |
| 47 | + storeDataInFeed(feed, ['hello', 'there']) |
| 48 | + .then(function () { |
| 49 | + console.log('Finalized with id ' + feed.id.toString('hex')) |
| 50 | + |
| 51 | + var appfeed = feedOps.appendTo(jawn.core, feed.id) |
| 52 | + |
| 53 | + appfeed.initialize().then(function () { |
| 54 | + return appfeed.append_p('goodbye') |
| 55 | + }) |
| 56 | + .then(function () { |
| 57 | + return appfeed.finalize_p() |
| 58 | + }) |
| 59 | + .then(function () { |
| 60 | + t.same(appfeed.blocks, expected.length, 'Correct number of blocks') |
| 61 | + for (var i = 0; i < appfeed.blocks; i++) { |
| 62 | + appfeed.get(i, function (err, block) { |
| 63 | + if (err) { |
| 64 | + console.log(err) |
| 65 | + } |
| 66 | + t.same(block.toString(), expected.shift(), 'Feed block match') |
| 67 | + if (expected.length === 0) { |
| 68 | + t.end() |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | + }) |
| 73 | + }) |
| 74 | +}) |
| 75 | + |
| 76 | +function storeDataInFeed (feed, data) { |
| 77 | + feed.pappend = function (data) { |
| 78 | + return new Promise(function (resolve, reject) { |
| 79 | + feed.append(data, resolve) |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + feed.pfinalize = function () { |
| 84 | + return new Promise(function (resolve, reject) { |
| 85 | + feed.finalize(resolve) |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + return new Promise(function (resolve, reject) { |
| 90 | + feed.pappend(data).then(function () { |
| 91 | + console.log('Appended') |
| 92 | + return feed |
| 93 | + }) |
| 94 | + .then(function (feed) { |
| 95 | + return feed.pfinalize() |
| 96 | + }) |
| 97 | + .then(resolve) |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +function freshJawn () { |
| 102 | + return new Jawn({db: memdb()}) |
| 103 | +} |
0 commit comments