git ssb

2+

ev / minsbot



Tree: 3d269c98d409d84e87895f410008a8df9e94f15d

Files: 3d269c98d409d84e87895f410008a8df9e94f15d / test / friends.js

9074 bytesRaw
1var ssbKeys = require('ssb-keys')
2var cont = require('cont')
3var tape = require('tape')
4var u = require('./util')
5var pull = require('pull-stream')
6
7//THERE IS NO TEST COVERAGE FOR LIVE STREAMS!
8
9// create 3 feeds
10// add some of friend edges (follow, flag)
11// make sure the friends plugin analyzes correctly
12
13var createSbot = require('../')
14 .use(require('../plugins/replicate'))
15 .use(require('ssb-friends'))
16
17
18function sort (ary) {
19 return ary.sort(function (a, b) {
20 return a.id < b.id ? -1 : a.id === b.id ? 1 : 0
21 })
22}
23
24function toAliases(aliasMap) {
25 return function (g) {
26 var g_ = {}
27 for (var k in g) {
28 var k_ = aliasMap[k]
29 if (typeof g[k] == 'object') {
30 g_[k_] = {}
31 for (var l in g[k]) {
32 var l_ = aliasMap[l]
33 g_[k_][l_] = g[k][l]
34 }
35 } else {
36 g_[k_] = g[k]
37 }
38 }
39 return g_
40 }
41}
42
43function liveFriends(sbot) {
44 var live = {}
45 pull(
46 sbot.friends.createFriendStream({live: true, meta: true}),
47 pull.drain(function (friend) {
48 if(friend.sync) return
49 live[friend.id] = friend.hops
50 })
51 )
52 return live
53}
54
55tape('construct and analyze graph', function (t) {
56
57 var aliceKeys = ssbKeys.generate()
58
59 var sbot = createSbot({
60 temp:'test-friends1',
61 port: 45451, host: 'localhost', timeout: 1000,
62 keys: aliceKeys
63 })
64
65 var alice = sbot.createFeed(aliceKeys)
66 var bob = sbot.createFeed()
67 var carol = sbot.createFeed()
68
69 t.test('add friends, and retrive all friends for a peer', function (t) {
70 var live = liveFriends(sbot)
71
72 cont.para([
73 alice.add({
74 type: 'contact', contact: bob.id,
75 following: true,
76 flagged: { reason: 'foo' }
77 }),
78 alice.add(u.follow(carol.id)),
79 bob.add(u.follow(alice.id)),
80 bob.add({
81 type: 'contact', contact: carol.id,
82 following: false, flagged: true
83 }),
84 carol.add(u.follow(alice.id))
85 ]) (function (err, results) {
86 if(err) throw err
87
88 console.log(live)
89 sbot.friends.hops(function (err, hops) {
90 if(err) throw err
91 t.deepEqual(live, hops)
92 t.end()
93 })
94
95// cont.para([
96// cont(sbot.friends.all)(),
97// cont(sbot.friends.all)('follow'),
98// cont(sbot.friends.all)('flag'),
99//
100// cont(sbot.friends.hops)(alice.id),
101// cont(sbot.friends.hops)(alice.id, 'follow'),
102// cont(sbot.friends.hops)(alice.id, 'flag'),
103//
104// cont(sbot.friends.hops)(bob.id, 'follow'),
105// cont(sbot.friends.hops)(bob.id, 'flag'),
106//
107// cont(sbot.friends.hops)(carol.id, 'follow'),
108// cont(sbot.friends.hops)(carol.id, 'flag')
109// ], function (err, results) {
110// if(err) throw err
111//
112// var aliasMap = {}
113// aliasMap[alice.id] = 'alice'
114// aliasMap[bob.id] = 'bob'
115// aliasMap[carol.id] = 'carol'
116//
117// a = toAliases(aliasMap)
118//
119// results = results.map(a)
120// var i = 0
121//
122// t.deepEqual(results[i++], { alice: { bob: true, carol: true }, bob: { alice: true }, carol: { alice: true } })
123// t.deepEqual(results[i++], { alice: { bob: true, carol: true }, bob: { alice: true }, carol: { alice: true } })
124// t.deepEqual(results[i++], { alice: { bob: { reason: 'foo' } }, bob: { carol: true }, carol: {} })
125//
126// t.deepEqual(results[i++], { alice: 0, bob: 1, carol: 1 })
127// t.deepEqual(results[i++], { alice: 0, bob: 1, carol: 1 })
128// t.deepEqual(results[i++], { alice: 0, bob: 1, carol: 2 })
129//
130// t.deepEqual(results[i++], { bob: 0, alice: 1, carol: 2 })
131// t.deepEqual(results[i++], { bob: 0, carol: 1 })
132//
133// t.deepEqual(results[i++], { carol: 0, alice: 1, bob: 2 })
134// t.deepEqual(results[i++], { carol: 0 })
135//
136// t.end()
137// })
138 })
139 })
140
141 t.test('creatFriendStream', function () {
142 pull(
143 sbot.friends.createFriendStream(),
144 pull.collect(function (err, ary) {
145 t.notOk(err)
146 t.equal(ary.length, 3)
147 t.deepEqual(ary.sort(), [alice.id, bob.id, carol.id].sort())
148 t.end()
149 })
150 )
151 })
152
153 t.test('creatFriendStream - meta', function (t) {
154 pull(
155 sbot.friends.createFriendStream({meta: true}),
156 pull.collect(function (err, ary) {
157 t.notOk(err)
158 t.equal(ary.length, 3)
159 t.deepEqual(sort(ary), sort([
160 {id: alice.id, hops: 0},
161 {id: bob.id, hops: 1},
162 {id: carol.id, hops: 1}
163 ]))
164
165 t.end()
166 })
167 )
168 })
169
170 t.test('cleanup', function (t) {
171 sbot.close()
172 t.end()
173 })
174
175})
176
177tape('correctly delete edges', function (t) {
178 //XXX
179 return t.end()
180 var aliceKeys = ssbKeys.generate()
181
182 var sbot = createSbot({
183 temp:'test-friends2',
184 port: 45452, host: 'localhost', timeout: 1000,
185 keys: aliceKeys
186 })
187
188 var alice = sbot.createFeed(aliceKeys)
189 var bob = sbot.createFeed()
190 var carol = sbot.createFeed()
191
192 var live = liveFriends(sbot)
193
194 t.test('add and delete', function (t) {
195
196 cont.para([
197 alice.add({
198 type:'contact', contact:bob.id,
199 following: true, flagged: true
200 }),
201 alice.add(u.follow(carol.id)),
202 bob.add(u.follow(alice.id)),
203 bob.add({
204 type: 'contact', contact: carol.id,
205 following: false, flagged: { reason: 'foo' }
206 }),
207 carol.add(u.follow(alice.id)),
208 alice.add({
209 type:'contact', contact: carol.id,
210 following: false, flagged: true
211 }),
212 alice.add({
213 type:'contact', contact: bob.id,
214 following: true, flagged: false
215 }),
216 bob.add(u.unfollow(carol.id))
217 ]) (function () {
218
219// cont.para([
220// cont(sbot.friends.all)('follow'),
221// cont(sbot.friends.all)('flag'),
222//
223// cont(sbot.friends.hops)(alice.id, 'follow'),
224// cont(sbot.friends.hops)(alice.id, 'flag'),
225//
226// cont(sbot.friends.hops)(bob.id, 'follow'),
227// cont(sbot.friends.hops)(bob.id, 'flag'),
228//
229// cont(sbot.friends.hops)(carol.id, 'follow'),
230// cont(sbot.friends.hops)(carol.id, 'flag')
231// ], function (err, results) {
232//
233// var aliasMap = {}
234// aliasMap[alice.id] = 'alice'
235// aliasMap[bob.id] = 'bob'
236// aliasMap[carol.id] = 'carol'
237// a = toAliases(aliasMap)
238//
239// results = results.map(a)
240// var i = 0
241//
242// t.deepEqual(results[i++], { alice: { bob: true }, bob: { alice: true }, carol: { alice: true } })
243// t.deepEqual(results[i++], { alice: { carol: true }, bob: { carol: { reason: 'foo' }}, carol: {} })
244//
245// t.deepEqual(results[i++], { alice: 0, bob: 1 })
246// t.deepEqual(results[i++], { alice: 0, carol: 1 })
247//
248// t.deepEqual(results[i++], { bob: 0, alice: 1 })
249// t.deepEqual(results[i++], { bob: 0, carol: 1 })
250//
251// t.deepEqual(results[i++], { carol: 0, alice: 1, bob: 2 })
252// t.deepEqual(results[i++], { carol: 0 })
253//
254// t.end()
255// })
256 })
257 })
258
259 t.test('createFriendStream after delete', function (t) {
260 pull(
261 sbot.friends.createFriendStream(),
262 pull.collect(function (err, ary) {
263 t.notOk(err)
264 t.equal(ary.length, 2)
265 t.deepEqual(ary.sort(), [alice.id, bob.id].sort())
266 t.end()
267 })
268 )
269 })
270
271 t.test('cleanup', function (t) {
272 sbot.close()
273 t.end()
274 })
275
276})
277
278tape('indirect friends', function (t) {
279
280 var aliceKeys = ssbKeys.generate()
281
282 var sbot = createSbot({
283 temp:'test-friends3',
284 port: 45453, host: 'localhost', timeout: 1000,
285 keys: aliceKeys
286 })
287
288 var alice = sbot.createFeed(aliceKeys)
289 var bob = sbot.createFeed()
290 var carol = sbot.createFeed()
291 var dan = sbot.createFeed()
292
293 var live = liveFriends(sbot)
294
295 t.test('chain of friends', function (t) {
296 cont.para([
297 alice.add(u.follow(bob.id)),
298 bob.add(u.follow(carol.id)),
299 carol.add(u.follow(dan.id))
300 ]) (function (err, results) {
301 if(err) throw err
302
303 sbot.friends.hops({hops: 3}, function (err, all) {
304 if(err) throw err
305 var o = {}
306
307 o[alice.id] = 0
308 o[bob.id] = 1
309 o[carol.id] = 2
310 o[dan.id] = 3
311
312 t.deepEqual(all, o)
313
314 t.deepEqual(live, o)
315
316 t.end()
317 })
318 })
319 })
320
321 var expected = [
322 {id: alice.id, hops: 0},
323 {id: bob.id, hops: 1},
324 {id: carol.id, hops: 2},
325 {id: dan.id, hops: 3}
326 ]
327
328 t.test('createFriendStream on long chain', function (t) {
329
330 pull(
331 sbot.friends.createFriendStream(),
332 pull.collect(function (err, ary) {
333 if(err) throw err
334 t.deepEqual(ary, expected.map(function (e) { return e.id }))
335 t.end()
336 })
337 )
338
339 })
340
341 t.test('creatFriendStream - meta', function (t) {
342
343 pull(
344 sbot.friends.createFriendStream({meta: true}),
345 pull.collect(function (err, ary) {
346 t.notOk(err)
347
348 t.equal(ary.length, 4)
349 t.deepEqual(sort(ary), sort(expected))
350
351 t.end()
352 })
353 )
354
355 })
356
357
358 t.test('cleanup', function (t) {
359 sbot.close()
360 t.end()
361 })
362
363})
364
365
366

Built with git-ssb-web