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