-
Notifications
You must be signed in to change notification settings - Fork 46
/
test.js
274 lines (211 loc) · 6.93 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import test from 'tape'
import { once } from 'events'
import { create } from './index.js'
import b4a from 'b4a'
import { withDir } from 'tmp-promise'
const NULL_KEY = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
const NULL_BUFFER = b4a.alloc(32, 0)
const NULL_HEX_KEY = NULL_BUFFER.toString('hex')
const NULL_URL = `hyper://${NULL_KEY}/`
test('Specify storage for sdk', async (t) => {
await withDir(async (dir) => {
const storage = dir.path
const name = 'example'
const data = 'Hello World!'
let sdk = await create({ storage })
let sdk2 = null
try {
try {
sdk2 = await create({ storage })
t.fail(new Error('Should not be able to load SDK over existing dir'))
} catch {
t.pass('Threw error when opening same storage path twice')
} finally {
if (sdk2) await sdk2.close()
}
const core1 = await sdk.get(name)
const url1 = core1.url
await core1.append(data)
await sdk.close()
sdk = await create({ storage })
const core2 = await sdk.get(name)
const url2 = core2.url
t.equal(url1, url2, 'Loaded core has same key')
const contents = await core2.get(0)
t.deepEqual(contents.toString('utf8'), data, 'Got data back from disk')
} finally {
await sdk.close()
}
}, { unsafeCleanup: true })
})
test('Support storage reuse by default', async (t) => {
const sdk = await create({ storage: false })
const core = await sdk.get('persist in memory')
const key = core.key
const data = b4a.from('beep')
await core.append(data)
await core.close()
t.ok(core.closed, 'initial core was closed')
const coreAgain = await sdk.get(key)
t.deepEqual(await coreAgain.get(0, { wait: false }), data, 'found persisted data')
await sdk.close()
})
test('Load hypercores by names and urls', async (t) => {
const sdk = await create({ storage: false })
const name = 'example'
try {
const core = await sdk.get(name)
t.ok(core, 'Got core for name')
const toTry = [
NULL_KEY,
NULL_BUFFER,
NULL_HEX_KEY,
`hyper://${NULL_KEY}`,
`hyper://${NULL_HEX_KEY}`
]
for (const key of toTry) {
const core = await sdk.get(key)
t.ok(core, `Got core for ${key}`)
t.equal(core.url, NULL_URL, 'Correct URL got loaded')
}
} finally {
await sdk.close()
}
})
test('Loading same key twice results in same core', async (t) => {
const sdk = await create({ storage: false })
const name = 'example'
try {
const core1 = await sdk.get(name)
const core2 = await sdk.get(core1.key)
const core3 = await sdk.get(core1.url)
t.equal(core1, core2, 'Key loaded same core from memory')
t.equal(core1, core3, 'URL loaded same core from memory')
const drive1 = await sdk.getDrive(name)
const drive2 = await sdk.getDrive(drive1.key)
const drive3 = await sdk.getDrive(drive1.url)
t.equal(drive1, drive2, 'Key loaded same drive from memory')
t.equal(drive1, drive3, 'URL loaded same drive from memory')
const bee1 = await sdk.getBee(name)
const bee2 = await sdk.getBee(bee1.key)
const bee3 = await sdk.getBee(bee1.url)
t.equal(bee1, bee2, 'Key loaded same bee from memory')
t.equal(bee1, bee3, 'URL loaded same bee from memory')
await core1.close()
await drive1.close()
const core4 = await sdk.get(name)
t.notEquals(core1, core4, 'New core after close')
const drive4 = await sdk.getDrive(name)
t.notEquals(drive1, drive4, 'New drive after close')
const bee4 = await sdk.getBee(name)
t.notEquals(bee1, bee4, 'New bee after close')
} finally {
await sdk.close()
}
})
test('Resolve DNS entries to keys', async (t) => {
const expected = NULL_KEY
const sdk = await create({ storage: false })
try {
const resolved = await sdk.resolveDNSToKey('example.mauve.moe')
t.equal(resolved, expected, 'Resolved to correct key')
} finally {
await sdk.close()
}
})
test('Resolve DNS in hyper URLs', async (t) => {
const expected = NULL_KEY
const sdk = await create({ storage: false })
try {
const core = await sdk.get('hyper://example.mauve.moe')
t.equal(core.id, expected, 'Loaded correct core from DNSLink')
} finally {
await sdk.close()
}
})
test('Load a core between two peers', async (t) => {
t.timeoutAfter(30000)
const sdk1 = await create({ storage: false })
const sdk2 = await create({ storage: false })
try {
t.comment('Initializing core on first peer')
const core1 = await sdk1.get('example')
await core1.append('Hello World!')
t.comment('Loading core on second peer')
const core2 = await sdk2.get(core1.url)
t.ok(core2.peers?.length, 'Found peer')
t.equal(core2.url, core1.url, 'Got expected URL')
t.equal(core2.length, 1, 'Not empty')
const data = await core2.get(0)
t.deepEqual(data, Buffer.from('Hello World!'), 'Got block back out')
} finally {
await Promise.all([
sdk1.close(),
sdk2.close()
])
}
})
test('Connect directly between two peers', async (t) => {
t.timeoutAfter(30000)
const sdk1 = await create({ storage: false })
const sdk2 = await create({ storage: false })
const onPeer = once(sdk2, 'peer-add')
const onPeernt = once(sdk2, 'peer-remove')
try {
await sdk1.joinPeer(sdk2.publicKey)
const [peerInfo] = await onPeer
t.deepEquals(peerInfo.publicKey, sdk1.publicKey, 'Connected to peer')
} finally {
await Promise.all([
sdk1.close(),
sdk2.close()
])
}
await onPeernt
t.pass('Peer remove event detected')
})
test('Get a hyperdrive and share a file', async (t) => {
const sdk1 = await create({ storage: false })
const sdk2 = await create({ storage: false })
try {
const drive1 = await sdk1.getDrive('example')
const ws = drive1.createWriteStream('/blob.txt')
const onWrote = once(ws, 'close')
ws.write('Hello, ')
ws.write('world!')
ws.end()
await onWrote
const drive2 = await sdk2.getDrive(drive1.url)
t.equal(drive2.url, drive1.url, 'Loaded drive has same URL')
const rs = drive2.createReadStream('/blob.txt')
let data = ''
for await (const chunk of rs) {
data += chunk.toString('utf8')
}
t.equal(data, 'Hello, world!', 'Loaded expected data')
} finally {
await Promise.all([
sdk1.close(),
sdk2.close()
])
}
})
test('Get a hyperbee and share a key value pair', async (t) => {
const sdk1 = await create({ storage: false })
const sdk2 = await create({ storage: false })
try {
const encodingOpts = { keyEncoding: 'utf8', valueEncoding: 'utf8' }
const db1 = await sdk1.getBee('example', encodingOpts)
await db1.put('hello', 'world')
const db2 = await sdk2.getBee(db1.url, encodingOpts)
t.equal(db2.url, db1.url, 'Loaded bee has same URL')
const { value } = await db2.get('hello')
t.equal(value, 'world', 'Got value for key')
} finally {
await Promise.all([
sdk1.close(),
sdk2.close()
])
}
})
// test('', async (t) => {})