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