forked from RangerMauve/hyper-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
111 lines (77 loc) · 2.27 KB
/
test.js
File metadata and controls
111 lines (77 loc) · 2.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
const SDK = require('./')
const test = require('tape')
const isBrowser = process.title === 'browser'
const storageLocation = isBrowser ? '/' : require('tmp').dirSync({
prefix: 'universal-dat-storage-'
}).name
const { Hyperdrive, Hypercore, resolveName, destroy } = SDK({
storageOpts: {
storageLocation
}
})
const DATPROJECT_KEY = 'dat://60c525b5589a5099aa3610a8ee550dcd454c3e118f7ac93b7d41b6b850272330'
const TEST_TIMEOUT = 10 * 1000
test.onFinish(destroy)
test('Hyperdrive - load drive', (t) => {
t.timeoutAfter(TEST_TIMEOUT)
const drive = Hyperdrive(DATPROJECT_KEY)
drive.readFile('/dat.json', 'utf8', (err, data) => {
t.notOk(err, 'loaded file without error')
t.end()
})
})
test('Hyperdrive - create drive', (t) => {
t.timeoutAfter(TEST_TIMEOUT)
const drive = Hyperdrive()
drive.writeFile('/example.txt', 'Hello World!', (err) => {
t.notOk(err, 'Able to write to hyperdrive')
t.end()
})
})
test('Hyperdrive - get existing drive', (t) => {
const drive = Hyperdrive()
drive.ready(() => {
const existing = Hyperdrive(drive.key)
t.equal(existing, drive, 'Got existing drive by reference')
t.end()
})
})
test('Hyperdrive - new drive created after close', (t) => {
const drive = Hyperdrive()
drive.ready(() => {
drive.once('close', () => {
const existing = Hyperdrive(drive.key)
t.notEqual(existing, drive, 'Got new drive by reference')
t.end()
})
drive.close()
})
})
test('resolveName - resolve and load archive', (t) => {
t.timeoutAfter(TEST_TIMEOUT)
resolveName('dat://dat.foundation', (err, resolved) => {
t.notOk(err, 'Resolved successfully')
const drive = Hyperdrive(resolved)
drive.readFile('/dat.json', 'utf8', (err2) => {
t.notOk(err2, 'loaded file without error')
t.end()
})
})
})
test('Hypercore - create', (t) => {
t.timeoutAfter(TEST_TIMEOUT)
const core = Hypercore()
core.append('Hello World', (err) => {
t.notOk(err, 'able to write to hypercore')
t.end()
})
})
test('Hypercore - load', (t) => {
t.timeoutAfter(TEST_TIMEOUT)
const key = '60c525b5589a5099aa3610a8ee550dcd454c3e118f7ac93b7d41b6b850272330'
const core = Hypercore(key)
core.ready(() => {
t.equal(core.key.toString('hex'), key, 'loaded key')
t.end()
})
})