From 81ba65eafc28626b4ecbef094856083acaff88ee Mon Sep 17 00:00:00 2001 From: HDegroote <75906619+HDegroote@users.noreply.github.com> Date: Thu, 23 Mar 2023 20:41:53 +0100 Subject: [PATCH 1/2] Add compat test with hyperbee watch and autobase --- package.json | 4 +-- test/watcher-compat.js | 68 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 test/watcher-compat.js 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..6a06b89 --- /dev/null +++ b/test/watcher-compat.js @@ -0,0 +1,68 @@ +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 + + // Make base2 writer too + await base1.append({ add: base2.local.key.toString('hex') }) + await confirm(base1, base2) + + 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 +} From 2c24af01e417a433368114529f9bee8ccfb65547 Mon Sep 17 00:00:00 2001 From: HDegroote <75906619+HDegroote@users.noreply.github.com> Date: Thu, 23 Mar 2023 20:45:20 +0100 Subject: [PATCH 2/2] Simplify --- test/watcher-compat.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/watcher-compat.js b/test/watcher-compat.js index 6a06b89..d1f5f30 100644 --- a/test/watcher-compat.js +++ b/test/watcher-compat.js @@ -8,10 +8,6 @@ test('compat - watch works with autobase bees', async function (t) { const bases = await setup(t) const [base1, base2] = bases - // Make base2 writer too - await base1.append({ add: base2.local.key.toString('hex') }) - await confirm(base1, base2) - const bee = base1.view.bee // bee based on autobase linearised core const partialWatcher = bee.watch()