Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class BeeDiffStream extends Union {
throw new Error('Incompatible Hypercore version--must have signedLength property')
}

if (!leftSnapshot.opened || !rightSnapshot.opened) {
throw new Error('Snapshot must be opened')
}

// We know that everything indexed in both snapshots is shared
const sharedIndexedL = Math.min(
leftSnapshot.core.signedLength, rightSnapshot.core.signedLength
Expand Down
30 changes: 30 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const test = require('brittle')
const Hyperbee = require('hyperbee')
const b4a = require('b4a')
const Hypercore = require('hypercore')
const tmpDir = require('test-tmp')
const Corestore = require('corestore')

const BeeDiffStream = require('../index')
const { streamToArray, setup, encodedOpen, jsonKeyedOpen, confirm, replicateAndSync } = require('./helpers')
Expand Down Expand Up @@ -852,6 +854,34 @@ test('supports diffing values skipped by hyperbee encoding', async t => {
t.alike(diffs.map(({ right }) => right?.key), [undefined, undefined]) // deletions
})

test('throws if a snapshot is not opened', async t => {
const store = new Corestore(await tmpDir())
const core = store.get({ name: 'bee' })
const bee = new Hyperbee(core)
t.exception(
() => new BeeDiffStream(bee, bee),
/Snapshot must be opened/,
'none open'
)

await bee.ready()
const snap = bee.snapshot()

t.exception(
() => new BeeDiffStream(bee, snap),
/Snapshot must be opened/,
'right not open'
)
t.exception(
() => new BeeDiffStream(snap, bee),
/Snapshot must be opened/,
'left not open'
)

await snap.ready()
t.execution(() => new BeeDiffStream(snap, bee), 'sanity check that happy path does not throw')
})

function sameKeysAndValues (t, actual, expected) {
const extractKeysAndValues = ({ left, right }) => {
const res = {}
Expand Down
6 changes: 5 additions & 1 deletion test/complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ test('complex scenario with many diff cases', async t => {

// Corresponds to the state just before going offline
const baselineBee = base2.view.bee.snapshot()
await baselineBee.ready()

t.is(base2.view.bee.core.signedLength, 13) // Sanity check

Expand Down Expand Up @@ -53,8 +54,10 @@ test('complex scenario with many diff cases', async t => {
// State just before reconnecting with base1
const refBee = base2.view.bee.snapshot()

const refBeeSnap = refBee.snapshot()
await refBeeSnap.ready()
const [refNewState, refOldState] = diffsToValues(
await streamToArray(new BeeDiffStream(baselineBee, refBee.snapshot()))
await streamToArray(new BeeDiffStream(baselineBee, refBeeSnap))
)
t.alike(refNewState, [
'2-1 added by 2',
Expand Down Expand Up @@ -106,6 +109,7 @@ test('complex scenario with many diff cases', async t => {
// The peers sync and the autobases are linearised
await confirm([base1, base2])
const newBee = base2.view.bee.snapshot()
await newBee.ready()
const [newState, refState] = diffsToValues(
await streamToArray(new BeeDiffStream(refBee, newBee))
)
Expand Down