-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtest-promise.js
101 lines (73 loc) · 2.19 KB
/
test-promise.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
const SDK = require('./promise')
const test = require('tape')
const isBrowser = process.title === 'browser'
const storageLocation = isBrowser ? '/' : require('tmp').dirSync({
prefix: 'universal-dat-storage-'
}).name
const { DatArchive, destroy } = SDK({
storageOpts: {
storageLocation
}
})
const DATPROJECT_KEY = 'dat://60c525b5589a5099aa3610a8ee550dcd454c3e118f7ac93b7d41b6b850272330'
const DATPROJECT_URL = 'dat://dat.foundation'
const TEST_TIMEOUT = 10 * 1000
test.onFinish(destroy)
test('DatArchive - load drive', async (t) => {
t.timeoutAfter(TEST_TIMEOUT)
try {
const drive = await DatArchive.load(DATPROJECT_KEY)
t.pass('loaded archive')
const data = await drive.readFile('/dat.json', 'utf8')
t.ok(data, 'loaded data from archive')
t.end()
} catch (e) {
t.error(e)
}
})
test('DatArchive - create drive', async (t) => {
t.timeoutAfter(TEST_TIMEOUT)
try {
const drive = new DatArchive()
await drive.writeFile('/example.txt', 'Hello World!')
t.ok(drive.url, 'got url in new drive')
t.end()
} catch (e) {
t.error(e)
}
})
test('DatArchive - get existing drive', async (t) => {
try {
const drive = await DatArchive.create()
const existing = await DatArchive.load(drive.url)
t.equal(existing._archive, drive._archive, 'Got existing drive by reference')
t.equal(existing.url, drive.url, 'got same URL')
t.end()
} catch (e) { t.error(e) }
})
test('DatArchive - new drive created after close', async (t) => {
try {
const drive = await DatArchive.create()
drive.addEventListener('close', async () => {
const existing = await DatArchive.load(drive.url)
t.notEqual(existing._archive, drive._archive, 'Got new drive by reference')
t.equal(existing.url, drive.url, 'got same URL')
t.end()
})
await drive.close()
} catch (e) {
t.error(e)
}
})
test('DatArchive - resolve and load archive', async (t) => {
t.timeoutAfter(TEST_TIMEOUT)
try {
const drive = await DatArchive.load(DATPROJECT_URL)
t.pass('resolved archive')
const data = await drive.readFile('/dat.json', 'utf8')
t.ok(data, 'loaded data from archive')
t.end()
} catch (e) {
t.error(e)
}
})