UNPKG

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