This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.js
161 lines (144 loc) · 4.62 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
var toilet = require('toiletdb/inmemory')
var rimraf = require('rimraf')
var mkdirp = require('mkdirp')
var Dat = require('dat-node')
var path = require('path')
var tape = require('tape')
var Multidat = require('./')
var noop = function () {}
tape('multidat = Multidat()', function (t) {
t.test('should assert input types', function (t) {
t.plan(2)
t.throws(Multidat.bind(null), /object/)
t.throws(Multidat.bind(null, {}), /function/)
})
})
tape('multidat.create()', function (t) {
t.test('should assert input types', function (t) {
t.plan(4)
var db = toilet({})
Multidat(db, function (err, multidat) {
t.ifError(err, 'no error')
t.throws(multidat.create.bind(null), 'string')
t.throws(multidat.create.bind(null, ''), 'function')
t.throws(multidat.create.bind(null, 123, noop), 'object')
})
})
t.test('should create a dat', function (t) {
t.plan(4)
var db = toilet({})
Multidat(db, function (err, multidat) {
t.ifError(err, 'no error')
var location = path.join('/tmp', String(Date.now()))
mkdirp.sync(location)
multidat.create(location, function (err, dat) {
t.ifError(err, 'no error')
t.equal(typeof dat, 'object', 'dat exists')
dat.close(function (err) {
t.ifError(err, 'no error')
rimraf.sync(location)
})
})
})
})
t.test('created dat should not be exposed to the network', function (t) {
t.plan(3)
var db = toilet({})
Multidat(db, function (err, multidat) {
t.ifError(err, 'no error')
var location = path.join('/tmp', String(Date.now()))
mkdirp.sync(location)
multidat.create(location, function (err, dat) {
t.ifError(err, 'no error')
dat.close(function (err) {
t.ifError(err, 'no error')
rimraf.sync(location)
})
})
})
})
})
tape('multidat.list()', function (t) {
t.test('should list all dats', function (t) {
t.plan(4)
var db = toilet({})
Multidat(db, function (err, multidat) {
t.ifError(err, 'no error')
var location = path.join('/tmp', String(Date.now()))
mkdirp.sync(location)
multidat.create(location, function (err, dat) {
t.ifError(err, 'no error')
var dats = multidat.list()
t.equal(dats.length, 1, 'one dat')
dat.close(function (err) {
t.ifError(err, 'no error')
rimraf.sync(location)
})
})
})
/* t.test('creation error', function (t) {
t.plan(3)
var db = toilet({})
Multidat(db, {}, function (err, multidat) {
t.ifError(err, 'no error')
multidat.create('/non/existing/path', function (err, dat) {
t.ok(err, 'error')
t.notOk(dat, 'no dat')
})
})
}) */
})
})
tape('multidat.close()', function (t) {
t.test('should be able to close a dat by its key', function (t) {
t.plan(4)
var db = toilet({})
Multidat(db, function (err, multidat) {
t.ifError(err, 'no error')
var location = path.join('/tmp', String(Date.now()))
mkdirp.sync(location)
multidat.create(location, function (err, dat) {
t.ifError(err, 'no error')
multidat.close(dat.key, function (err) {
t.ifError(err, 'no error')
var dats = multidat.list()
t.equal(dats.length, 0, 'no dats')
rimraf.sync(location)
})
})
})
})
})
tape('multidat.readManifest', function (t) {
t.test('should read a manifest if there is one', function (t) {
var sourceLocation = path.join('/tmp', String(Date.now()))
Dat(sourceLocation, { indexing: false }, function (err, sourceDat) {
t.ifError(err, 'no error')
sourceDat.joinNetwork()
var ws = sourceDat.archive.createWriteStream('dat.json')
ws.end(JSON.stringify({ name: 'hello-planet' }))
var db = toilet({})
Multidat(db, function (err, multidat) {
t.ifError(err, 'no error')
var location = path.join('/tmp', String(Date.now()))
mkdirp.sync(location)
multidat.create(location, { key: sourceDat.key }, function (err, dat) {
t.ifError(err, 'no error')
dat.joinNetwork()
multidat.readManifest(dat, function (err, manifest) {
t.ifError(err, 'no err')
t.equal(typeof manifest, 'object', 'right type')
t.equal(manifest.name, 'hello-planet', 'right value')
dat.close(function () {
sourceDat.close(function () {
rimraf.sync(location)
rimraf.sync(sourceLocation)
t.end()
})
})
})
})
})
})
})
})