1 | const debug = require('debug')('ssb-blobs')
|
2 | var tape = require('tape')
|
3 | var pull = require('pull-stream')
|
4 | var assert = require('assert')
|
5 |
|
6 | var u = require('./util')
|
7 | var Fake = u.fake
|
8 | var hash = u.hash
|
9 |
|
10 | module.exports = function (createBlobs, createAsync) {
|
11 |
|
12 |
|
13 |
|
14 | tape('simple', function (t) {
|
15 | createAsync(function (async) {
|
16 | var blobs = createBlobs('simple', async)
|
17 | debug(blobs)
|
18 | var b = Fake('hello', 256), h = hash(b)
|
19 | pull(pull.once(b), async.through(), blobs.add(h, function (err, _h) {
|
20 | if(err) throw err
|
21 | debug('added', _h)
|
22 | t.equal(_h, h)
|
23 |
|
24 | var req = {}
|
25 | req[h] = -1
|
26 | var res = {}
|
27 | res[h] = 256
|
28 |
|
29 | pull(
|
30 | pull.once(req),
|
31 | async.through(),
|
32 | blobs._wantSink({id: 'test'})
|
33 | )
|
34 |
|
35 | pull(
|
36 | blobs.createWants.call({id: 'test'}),
|
37 | async.through(),
|
38 | pull.take(2),
|
39 | pull.collect(function (err, _res) {
|
40 | if(err) throw err
|
41 | debug("_RES", _res)
|
42 | assert.deepEqual(_res, [{}, res])
|
43 | async.done()
|
44 | })
|
45 | )
|
46 | }))
|
47 | }, function (err, results) {
|
48 | if(err) throw err
|
49 | t.end()
|
50 | })
|
51 | })
|
52 |
|
53 |
|
54 |
|
55 | tape('simple wants', function (t) {
|
56 | createAsync(function (async) {
|
57 | var blobs = createBlobs('simple', async)
|
58 |
|
59 | var b = Fake('hello', 256), h = hash(b)
|
60 |
|
61 | var req = {}
|
62 | req[h] = -1
|
63 | var res = {}
|
64 | res[h] = -2
|
65 |
|
66 | pull(
|
67 |
|
68 | pull.values([req, res]),
|
69 | async.through(),
|
70 | blobs._wantSink({id: 'test'})
|
71 | )
|
72 |
|
73 | var c = 0
|
74 |
|
75 | pull(
|
76 | blobs.createWants.call({id: 'test'}),
|
77 | async.through(),
|
78 | pull.take(2),
|
79 | pull.collect(function (err, _res) {
|
80 |
|
81 | debug('END', c, _res)
|
82 | assert.deepEqual(_res, [{}, res])
|
83 | async.done()
|
84 |
|
85 | })
|
86 | )
|
87 |
|
88 | }, function (err, results) {
|
89 | debug(err)
|
90 | if(err) throw err
|
91 | t.end()
|
92 | })
|
93 | })
|
94 |
|
95 |
|
96 | tape('want', function (t) {
|
97 | createAsync(function (async) {
|
98 | var blobs = createBlobs('want', async)
|
99 | var h = hash(Fake('foobar', 64))
|
100 | var res = {}
|
101 | res[h] = -1
|
102 |
|
103 | pull(
|
104 | blobs.createWants.call({id: 'test'}),
|
105 | async.through(),
|
106 | pull.take(2),
|
107 | pull.collect(function (err, _res) {
|
108 | if(err) throw err
|
109 |
|
110 | assert.deepEqual(_res, [{}, res])
|
111 | async.done()
|
112 | })
|
113 | )
|
114 |
|
115 | blobs.want(h)
|
116 |
|
117 | }, function (err, results) {
|
118 | if(err) throw err
|
119 |
|
120 | t.end()
|
121 | })
|
122 | })
|
123 |
|
124 |
|
125 | tape('already wanted, before connection', function (t) {
|
126 | createAsync(function (async) {
|
127 | var blobs = createBlobs('want', async)
|
128 | var h = hash(Fake('foobar', 64))
|
129 | var res = {}
|
130 | res[h] = -1
|
131 |
|
132 | blobs.want(h)
|
133 |
|
134 | pull(
|
135 | blobs.createWants.call({id: 'test'}),
|
136 | async.through(),
|
137 | pull.find(null, function (err, _res) {
|
138 | if(err) throw err
|
139 |
|
140 | t.deepEqual(_res, res)
|
141 | async.done()
|
142 | })
|
143 | )
|
144 |
|
145 | }, function (err, results) {
|
146 | if(err) throw err
|
147 |
|
148 | t.end()
|
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 | if(!module.parent) u.tests(module.exports)
|
184 |
|
185 |
|