-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
62 lines (53 loc) · 1.86 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
const { generate, resolve } = require('./')
const hyperdrive = require('hyperdrive')
const test = require('tape')
const ram = require('random-access-memory')
const url = require('url')
test('generate(drive, filename, cb)', (t) => {
const drive = hyperdrive(ram)
const buffer = Buffer.from('hello world')
const filename = '/hello.txt'
drive.writeFile(filename, buffer, onwrite)
function onwrite(err) {
t.error(err, 'onwrite(err) is ok')
generate(drive, filename, ongenerate)
drive.writeFile(filename, 'new world 1')
drive.writeFile(filename, 'new world 2')
}
function ongenerate(err, uri) {
t.error(err, 'ongenerate(err, uri) is ok')
t.ok('string' === typeof uri)
const { protocol, auth, hostname, port } = url.parse(uri)
t.equal('dat:', protocol, 'dat:// protocol present in uri')
t.equal(64, auth.length, 'drive key is present in uri')
t.ok(hostname === drive.key.toString('hex'), 'drive key matches uri key')
t.equal(1, parseInt(port), 'correct version given')
drive.close()
t.end()
}
})
test('resolve(drive, uri, cb)', (t) => {
const drive = hyperdrive(ram)
const buffer = Buffer.from('hello world')
const filename = '/hello.txt'
drive.writeFile(filename, buffer, onwrite)
function onwrite(err) {
t.error(err, 'onwrite(err) is ok')
generate(drive, filename, ongenerate)
drive.writeFile(filename, 'new world 1')
drive.writeFile(filename, 'new world 2')
}
function ongenerate(err, uri) {
t.error(err, 'ongenerate(err, uri) is ok')
resolve(drive, uri, onresolve)
}
function onresolve(err, checkout, file) {
t.error(err, 'onresolve(err, checkout, filename) is ok')
t.equal(file, filename, 'parsed file matches filename')
checkout.readFile(file, (err, buf) => {
t.error(err)
t.ok(0 === Buffer.compare(buf, buffer), 'reads correct version')
t.end()
})
}
})