-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
111 lines (103 loc) · 3.53 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* global describe, it, before, after */
const assert = require('assert').strict
const Hypercore = require('hypercore')
const multifeed = require('multifeed')
const PouchDB = require('pouchdb')
const PouchHypercore = require('.')
const rimraf = require('rimraf')
const TEST_POUCH = '.testpouch'
const TEST_HYPER = '.testhyper'
const TEST_MULTI = '.testmulti'
describe('pouchdb-hypercore', function () {
before(async function () {
// set up pouchdb
PouchDB.plugin(PouchHypercore)
this.pouch = new PouchDB(TEST_POUCH)
// set up hypercore
this.hyper = Hypercore(TEST_HYPER)
await new Promise((resolve) => { this.hyper.ready(resolve) })
// set up read-only hypercores
this.hypercores = []
for (let i = 0; i < 3; i++) {
const hypercore = Hypercore(`${TEST_HYPER}-${i}`)
await new Promise((resolve) => { hypercore.ready(resolve) })
this.hypercores.push(hypercore)
}
// set up multicore
this.multifeed = multifeed(TEST_MULTI)
// set up pouchdb-hypercore
for (const hypercore of [this.hyper, ...this.hypercores]) {
this.pouch.fromHypercore(hypercore)
}
this.pouch.fromMultifeed(this.multifeed)
})
after(async function () {
// destroy pouchdb, readable streams
await this.pouch.destroy()
// destroy hypercore
rimraf.sync(TEST_HYPER)
// destroy hypercores
for (let i = 0; i < 3; i++) {
rimraf.sync(`${TEST_HYPER}-${i}`)
}
// destroy multifeed
await new Promise((resolve) => {
this.multifeed.close(resolve)
})
rimraf.sync(TEST_MULTI)
})
it('should work', function () {
// assert that pouchdb-hypercore is set up
this.pouch._hypercores.forEach((feed) => {
assert(feed.readable)
})
})
it('should follow updates to the hypercore', async function () {
const seq = await new Promise((resolve, reject) => {
this.hyper.append(JSON.stringify({ hello: 'goodbye' }), function (err, seq) {
if (err) { reject(err) } else { resolve(seq) }
})
})
await new Promise((resolve) => { setTimeout(resolve, 100) })
const key = this.hyper.key.toString('hex')
const doc = await this.pouch.get(`${key}@${seq}`)
assert.equal(doc.hello, 'goodbye')
})
it('should follow updates to read-only cores', async function () {
for (let i = 0; i < 3; i++) {
const hypercore = this.hypercores[i]
// write
await new Promise((resolve, reject) => {
hypercore.append(JSON.stringify({ i: `${i}` }), (err) => {
if (err) { reject(err) } else { resolve() }
})
})
}
// wait
await new Promise((resolve) => { setTimeout(resolve, 100) })
// verify
const allDocs = await this.pouch.allDocs({ include_docs: true })
const docs = allDocs.rows.filter(({ doc }) => {
if (!doc.i) { return false }
const i = parseInt(doc.i)
return i < 3 && i >= 0
})
assert.equal(docs.length, 3)
})
it('should follow updates from multifeed feeds', async function () {
const local = await new Promise((resolve, reject) => {
this.multifeed.writer('local', (err, feed) => {
return err ? reject(err) : resolve(feed)
})
})
const key = local.key.toString('hex')
const seq = await new Promise((resolve, reject) => {
local.append(JSON.stringify({ hello: 'goodbye' }), function (err, seq) {
if (err) { reject(err) } else { resolve(seq) }
})
})
await new Promise((resolve) => { setTimeout(resolve, 100) })
const doc = await this.pouch.get(`${key}@${seq}`)
assert.equal(doc.hello, 'goodbye')
})
})