Skip to content

Commit 5f72724

Browse files
authored
Throw if snapshots not opened (#30)
1 parent 49c434b commit 5f72724

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ class BeeDiffStream extends Union {
8989
throw new Error('Incompatible Hypercore version--must have signedLength property')
9090
}
9191

92+
if (!leftSnapshot.opened || !rightSnapshot.opened) {
93+
throw new Error('Snapshot must be opened')
94+
}
95+
9296
// We know that everything indexed in both snapshots is shared
9397
const sharedIndexedL = Math.min(
9498
leftSnapshot.core.signedLength, rightSnapshot.core.signedLength

test/basic.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const test = require('brittle')
22
const Hyperbee = require('hyperbee')
33
const b4a = require('b4a')
44
const Hypercore = require('hypercore')
5+
const tmpDir = require('test-tmp')
6+
const Corestore = require('corestore')
57

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

857+
test('throws if a snapshot is not opened', async t => {
858+
const store = new Corestore(await tmpDir())
859+
const core = store.get({ name: 'bee' })
860+
const bee = new Hyperbee(core)
861+
t.exception(
862+
() => new BeeDiffStream(bee, bee),
863+
/Snapshot must be opened/,
864+
'none open'
865+
)
866+
867+
await bee.ready()
868+
const snap = bee.snapshot()
869+
870+
t.exception(
871+
() => new BeeDiffStream(bee, snap),
872+
/Snapshot must be opened/,
873+
'right not open'
874+
)
875+
t.exception(
876+
() => new BeeDiffStream(snap, bee),
877+
/Snapshot must be opened/,
878+
'left not open'
879+
)
880+
881+
await snap.ready()
882+
t.execution(() => new BeeDiffStream(snap, bee), 'sanity check that happy path does not throw')
883+
})
884+
855885
function sameKeysAndValues (t, actual, expected) {
856886
const extractKeysAndValues = ({ left, right }) => {
857887
const res = {}

test/complex.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ test('complex scenario with many diff cases', async t => {
2323

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

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

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

57+
const refBeeSnap = refBee.snapshot()
58+
await refBeeSnap.ready()
5659
const [refNewState, refOldState] = diffsToValues(
57-
await streamToArray(new BeeDiffStream(baselineBee, refBee.snapshot()))
60+
await streamToArray(new BeeDiffStream(baselineBee, refBeeSnap))
5861
)
5962
t.alike(refNewState, [
6063
'2-1 added by 2',
@@ -106,6 +109,7 @@ test('complex scenario with many diff cases', async t => {
106109
// The peers sync and the autobases are linearised
107110
await confirm([base1, base2])
108111
const newBee = base2.view.bee.snapshot()
112+
await newBee.ready()
109113
const [newState, refState] = diffsToValues(
110114
await streamToArray(new BeeDiffStream(refBee, newBee))
111115
)

0 commit comments

Comments
 (0)