diff --git a/package.json b/package.json index d1c675a..59b1e08 100644 --- a/package.json +++ b/package.json @@ -18,10 +18,10 @@ "author": "H. Degroote", "license": "Apache-2.0", "devDependencies": { - "autobase-next": "~0.0.1", + "autobase-next": "~0.0.2", "brittle": "^3.1.1", "corestore": "^6.5.0", - "hyperbee": "^2.5.0", + "hyperbee": "github:holepunchto/hyperbee#watch-change-event-2", "random-access-memory": "^6.1.0", "standard": "^17.0.0" }, diff --git a/test/watcher-compat.js b/test/watcher-compat.js new file mode 100644 index 0000000..d1f5f30 --- /dev/null +++ b/test/watcher-compat.js @@ -0,0 +1,64 @@ +const test = require('brittle') +const { streamToArray, setup, confirm } = require('./helpers') + +test('compat - watch works with autobase bees', async function (t) { + // bee.watch() works very well with this module, so we add a test + // that ensures it remains compatible. + // The main possible compat issue is autobase-next + const bases = await setup(t) + const [base1, base2] = bases + + const bee = base1.view.bee // bee based on autobase linearised core + + const partialWatcher = bee.watch() + const fullWatcher = bee.watch() + const initBee = bee.snapshot() + + // Start consuming the watchers + const consumePartialWatcherProm = consumeWatcher(partialWatcher) + const consumeFullWatcherProm = consumeWatcher(fullWatcher) + + // Add shared entry + await base1.append({ entry: ['1-1', '1-entry1'] }) + await confirm(base1, base2) + + await partialWatcher.destroy() + const partialDiffs = await consumePartialWatcherProm + + // Init state + t.alike(initBee.version, partialDiffs[0].previous.version) + // Final state + const partialFinal = await streamToArray(partialDiffs[partialDiffs.length - 1].current.createReadStream()) + t.alike(partialFinal.length, 1) // Sanity check + t.alike(partialFinal, await streamToArray(bee.createReadStream())) + + await Promise.all([ + base1.append({ entry: ['1-2', '1-entry2'] }), + base2.append({ entry: ['2-1', '2-entry1'] }), + base2.append({ entry: ['2-2', '2-entry2'] }) + ]) + await confirm(base1, base2) + + await fullWatcher.destroy() + const fullDiffs = await consumeFullWatcherProm + + // sanity check. Even though the exact amount is non-deterministic + // it should have been triggered at least a few times. + t.is(fullDiffs.length > 1, true) + t.alike( + await streamToArray(fullDiffs[0].previous.createReadStream()), + await streamToArray(initBee.createReadStream()) + ) + // Final state + const finalEntries = await streamToArray(fullDiffs[fullDiffs.length - 1].current.createReadStream()) + t.is(finalEntries.length, 4) // Sanity check + t.alike(finalEntries, await streamToArray(bee.createReadStream())) +}) + +async function consumeWatcher (watcher) { + const entries = [] + for await (const { current, previous } of watcher) { + entries.push({ previous, current }) + } + return entries +}