1 | var pull = require('pull-stream')
|
2 | var tape = require('tape')
|
3 | var series = require('run-series')
|
4 | var crypto = require('crypto')
|
5 |
|
6 | var createSbot = require('secret-stack')({
|
7 | caps: {shs: crypto.randomBytes(32).toString('base64')}
|
8 | })
|
9 | .use(require('ssb-db'))
|
10 | .use(require('ssb-replicate'))
|
11 | .use(require('..'))
|
12 |
|
13 | var botA = createSbot({
|
14 | temp: 'alice',
|
15 | port: 45451,
|
16 | host: 'localhost',
|
17 | timeout: 20001,
|
18 | replicate: {
|
19 | hops: 2,
|
20 | legacy: false
|
21 | }
|
22 | })
|
23 |
|
24 | tape('check that friends are re-emitted when distance changes when `hops: 2`', function (t) {
|
25 | var changes = []
|
26 | var hops = {}
|
27 |
|
28 |
|
29 |
|
30 | pull(
|
31 | botA.friends.createFriendStream({
|
32 | live: true,
|
33 | meta: true,
|
34 | hops: 2
|
35 | }),
|
36 | pull.drain(function (m) {
|
37 | if (hops[m.id] !== m.hops) {
|
38 | changes.push(m)
|
39 | }
|
40 | hops[m.id] = m.hops
|
41 | })
|
42 | )
|
43 |
|
44 | var feedA = botA.createFeed()
|
45 | var feedB = botA.createFeed()
|
46 | var feedC = botA.createFeed()
|
47 |
|
48 | series([
|
49 |
|
50 | cb => {
|
51 | feedA.publish({
|
52 | type: 'contact',
|
53 | contact: feedB.id,
|
54 | following: true
|
55 | }, cb)
|
56 | },
|
57 | cb => {
|
58 | t.deepEqual(changes, [
|
59 | { id: botA.id, hops: 0 }
|
60 | ])
|
61 |
|
62 | changes.length = 0
|
63 |
|
64 |
|
65 | feedB.publish({
|
66 | type: 'contact',
|
67 | contact: feedC.id,
|
68 | following: true
|
69 | }, cb)
|
70 | },
|
71 | cb => {
|
72 |
|
73 | botA.publish({
|
74 | type: 'contact',
|
75 | contact: feedA.id,
|
76 | following: true
|
77 | }, cb)
|
78 | },
|
79 | cb => {
|
80 | t.deepEqual(changes, [
|
81 | { id: feedA.id, hops: 1 },
|
82 | { id: feedB.id, hops: 2 }
|
83 | ])
|
84 |
|
85 | changes.length = 0
|
86 |
|
87 |
|
88 | botA.publish({
|
89 | type: 'contact',
|
90 | contact: feedB.id,
|
91 | following: true
|
92 | }, cb)
|
93 | },
|
94 | cb => {
|
95 | t.deepEqual(changes, [
|
96 | { id: feedB.id, hops: 1 },
|
97 | { id: feedC.id, hops: 2 }
|
98 | ])
|
99 |
|
100 | var G = {}
|
101 |
|
102 | series([
|
103 | cb => {
|
104 | botA.friends.get(function (err, g) {
|
105 | t.error(err)
|
106 |
|
107 | G[feedA.id] = {}
|
108 | G[feedA.id][feedB.id] = true
|
109 | G[feedB.id] = {}
|
110 | G[feedB.id][feedC.id] = true
|
111 | G[botA.id] = {}
|
112 | G[botA.id][feedA.id] = true
|
113 | G[botA.id][feedB.id] = true
|
114 | t.deepEqual(g, G)
|
115 |
|
116 | cb()
|
117 | })
|
118 | },
|
119 | cb => {
|
120 | botA.friends.get({
|
121 | source: botA.id
|
122 | }, function (err, g) {
|
123 | t.error(err)
|
124 | t.deepEqual(g, G[botA.id])
|
125 | cb()
|
126 | })
|
127 | }
|
128 | ], cb)
|
129 | },
|
130 | cb => {
|
131 | botA.friends.get({
|
132 | dest: feedB.id
|
133 | }, function (err, g) {
|
134 | t.error(err)
|
135 |
|
136 | var _c = {}
|
137 | _c[feedA.id] = true
|
138 | _c[botA.id] = true
|
139 |
|
140 | t.deepEqual(g, _c)
|
141 |
|
142 | cb()
|
143 | })
|
144 | },
|
145 | cb => {
|
146 | botA.friends.get({
|
147 | source: botA.id,
|
148 | dest: feedB.id
|
149 | }, function (err, follows) {
|
150 | t.error(err)
|
151 | t.equal(follows, true)
|
152 | cb()
|
153 | })
|
154 | },
|
155 | cb => {
|
156 | botA.friends.get({
|
157 | source: botA.id,
|
158 | dest: feedC.id
|
159 | }, function (err, follows) {
|
160 | t.error(err)
|
161 | t.notOk(follows)
|
162 | cb()
|
163 | })
|
164 | }
|
165 | ], t.end)
|
166 | })
|
167 |
|
168 | tape('legacy blocking / unblocking works', function (t) {
|
169 | var feedD = botA.createFeed()
|
170 | var feedE = botA.createFeed()
|
171 |
|
172 | series([
|
173 | cb => {
|
174 | feedD.publish({
|
175 | type: 'contact',
|
176 | contact: feedE.id,
|
177 | following: true
|
178 | }, cb)
|
179 | },
|
180 | cb => {
|
181 | botA.friends.get({
|
182 | source: feedD.id,
|
183 | dest: feedE.id
|
184 | }, function (err, follows) {
|
185 | t.error(err)
|
186 | t.equal(follows, true)
|
187 | cb()
|
188 | })
|
189 | },
|
190 | cb => {
|
191 | feedD.publish({
|
192 | type: 'contact',
|
193 | contact: feedE.id,
|
194 | blocking: true
|
195 | }, cb)
|
196 | },
|
197 | cb => {
|
198 | botA.friends.get({
|
199 | source: feedD.id,
|
200 | dest: feedE.id
|
201 | }, function (err, follows) {
|
202 | t.error(err)
|
203 | t.notOk(follows)
|
204 | cb()
|
205 | })
|
206 | },
|
207 | cb => {
|
208 | feedD.publish({
|
209 | type: 'contact',
|
210 | contact: feedE.id,
|
211 | blocking: false
|
212 | }, cb)
|
213 | },
|
214 | cb => {
|
215 | botA.friends.get({
|
216 | source: feedD.id,
|
217 | dest: feedE.id
|
218 | }, function (err, follows) {
|
219 | t.error(err)
|
220 |
|
221 | t.notOk(follows)
|
222 | cb()
|
223 | })
|
224 | }
|
225 | ], t.end)
|
226 | })
|
227 |
|
228 | tape('hops blocking / unblocking works', function (t) {
|
229 | var feedF = botA.createFeed()
|
230 | series([
|
231 | cb => {
|
232 | botA.publish({
|
233 | type: 'contact',
|
234 | contact: feedF.id,
|
235 | blocking: true
|
236 | }, cb)
|
237 | },
|
238 | cb => {
|
239 | botA.friends.hops(function (err, hops) {
|
240 | t.error(err)
|
241 | t.equal(hops[feedF.id], -1)
|
242 | cb()
|
243 | })
|
244 | },
|
245 | cb => {
|
246 | botA.publish({
|
247 | type: 'contact',
|
248 | contact: feedF.id,
|
249 | blocking: false
|
250 | }, cb)
|
251 | },
|
252 | cb => {
|
253 | botA.friends.hops(function (err, hops) {
|
254 | t.error(err)
|
255 | t.equal(hops[feedF.id], -2)
|
256 | cb()
|
257 | })
|
258 | }
|
259 | ], t.end)
|
260 | })
|
261 |
|
262 |
|
263 | tape('hops blocking / unblocking works', function (t) {
|
264 | var feedH = botA.createFeed()
|
265 | var feedI = botA.createFeed()
|
266 | series([
|
267 | cb => {
|
268 | botA.publish({
|
269 | type: 'contact',
|
270 | contact: feedH.id,
|
271 | following: true
|
272 | }, cb)
|
273 | },
|
274 | cb => {
|
275 | feedH.publish({
|
276 | type: 'contact',
|
277 | contact: feedI.id,
|
278 | following: true
|
279 | }, cb)
|
280 | },
|
281 | cb => {
|
282 | botA.friends.hops(function (err, hops) {
|
283 | t.error(err)
|
284 | t.equal(hops[feedH.id], 1)
|
285 | t.equal(hops[feedI.id], 2)
|
286 | cb()
|
287 | })
|
288 | },
|
289 | cb => {
|
290 | botA.publish({
|
291 | type: 'contact',
|
292 | contact: feedI.id,
|
293 | blocking: true
|
294 | }, cb)
|
295 | },
|
296 | cb => {
|
297 | botA.friends.hops(function (err, hops) {
|
298 | t.error(err)
|
299 | t.equal(hops[feedH.id], 1)
|
300 | t.equal(hops[feedI.id], -1)
|
301 | cb()
|
302 | })
|
303 | },
|
304 |
|
305 |
|
306 | cb => {
|
307 | botA.publish({
|
308 | type: 'contact',
|
309 | contact: feedI.id,
|
310 | blocking: false
|
311 | }, cb)
|
312 | },
|
313 | cb => {
|
314 | botA.friends.hops(function (err, hops) {
|
315 | t.error(err)
|
316 | t.equal(hops[feedH.id], 1)
|
317 | t.equal(hops[feedI.id], 2)
|
318 | cb()
|
319 | })
|
320 | }
|
321 | ], t.end)
|
322 | })
|
323 |
|
324 |
|
325 | tape('finish tests', function (t) {
|
326 | botA.close()
|
327 | t.end()
|
328 | })
|
329 |
|
330 |
|