-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmount.spec.ts
More file actions
140 lines (124 loc) · 4.27 KB
/
mount.spec.ts
File metadata and controls
140 lines (124 loc) · 4.27 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */
import { expect, assert } from 'aegir/chai'
import { Key } from 'interface-datastore/key'
import { interfaceDatastoreTests } from 'interface-datastore-tests'
import all from 'it-all'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { KeyTransformDatastore } from '../src/keytransform.js'
import { MemoryDatastore } from '../src/memory.js'
import { MountDatastore } from '../src/mount.js'
import type { Datastore } from 'interface-datastore'
const stripPrefixDatastore = (datastore: Datastore, prefix: Key): Datastore => {
return new KeyTransformDatastore(
datastore, {
convert: (key) => {
if (!prefix.isAncestorOf(key)) {
throw new Error(`Expected prefix: (${prefix.toString()}) in key: ${key.toString()}`)
}
return Key.withNamespaces(key.namespaces().slice(prefix.namespaces().length))
},
invert: (key) => Key.withNamespaces([...prefix.namespaces(), ...key.namespaces()])
})
}
describe('MountDatastore', () => {
it('put - no mount', async () => {
const m = new MountDatastore([])
try {
await m.put(new Key('hello'), uint8ArrayFromString('foo'))
assert(false, 'Failed to throw error on no mount')
} catch (err) {
expect(err).to.be.an('Error')
}
})
it('put - wrong mount', async () => {
const m = new MountDatastore([{
datastore: stripPrefixDatastore(new MemoryDatastore(), new Key('cool')),
prefix: new Key('cool')
}])
try {
await m.put(new Key('/fail/hello'), uint8ArrayFromString('foo'))
assert(false, 'Failed to throw error on wrong mount')
} catch (err) {
expect(err).to.be.an('Error')
}
})
it('put', async () => {
const mds = new MemoryDatastore()
const m = new MountDatastore([{
datastore: stripPrefixDatastore(mds, new Key('cool')),
prefix: new Key('cool')
}])
const val = uint8ArrayFromString('hello')
await m.put(new Key('/cool/hello'), val)
const res = await mds.get(new Key('/hello'))
expect(res).to.eql(val)
})
it('get', async () => {
const mds = new MemoryDatastore()
const m = new MountDatastore([{
datastore: stripPrefixDatastore(mds, new Key('cool')),
prefix: new Key('cool')
}])
const val = uint8ArrayFromString('hello')
await mds.put(new Key('/hello'), val)
const res = await m.get(new Key('/cool/hello'))
expect(res).to.eql(val)
})
it('has', async () => {
const mds = new MemoryDatastore()
const m = new MountDatastore([{
datastore: stripPrefixDatastore(mds, new Key('cool')),
prefix: new Key('cool')
}])
const val = uint8ArrayFromString('hello')
await mds.put(new Key('/hello'), val)
const exists = await m.has(new Key('/cool/hello'))
expect(exists).to.eql(true)
})
it('delete', async () => {
const mds = new MemoryDatastore()
const m = new MountDatastore([{
datastore: stripPrefixDatastore(mds, new Key('cool')),
prefix: new Key('cool')
}])
const val = uint8ArrayFromString('hello')
await m.put(new Key('/cool/hello'), val)
await m.delete(new Key('/cool/hello'))
let exists = await m.has(new Key('/cool/hello'))
expect(exists).to.eql(false)
exists = await mds.has(new Key('/hello'))
expect(exists).to.eql(false)
})
it('query simple', async () => {
const mds = new MemoryDatastore()
const m = new MountDatastore([{
datastore: stripPrefixDatastore(mds, new Key('cool')),
prefix: new Key('cool')
}])
const val = uint8ArrayFromString('hello')
await m.put(new Key('/cool/hello'), val)
const res = await all(m.query({ prefix: '/cool' }))
expect(res).to.eql([{ key: new Key('/cool/hello'), value: val }])
})
describe('interface-datastore', () => {
interfaceDatastoreTests({
setup () {
return new MountDatastore([{
prefix: new Key('/a'),
datastore: new MemoryDatastore()
}, {
prefix: new Key('/z'),
datastore: new MemoryDatastore()
}, {
prefix: new Key('/q'),
datastore: new MemoryDatastore()
}, {
prefix: new Key('/dht/provider/third'),
datastore: new MemoryDatastore()
}])
},
teardown () { }
})
})
})