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 | var crypto = require('crypto')
|
7 |
|
8 | var createSsbServer = require('secret-stack')({
|
9 | caps: {shs: crypto.randomBytes(32).toString('base64')}
|
10 | })
|
11 | .use(require('ssb-db'))
|
12 | .use(require('ssb-replicate'))
|
13 | .use(require('ssb-friends'))
|
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 | function liveFriends(ssbServer) {
|
22 | var live = {}
|
23 | pull(
|
24 | ssbServer.friends.createFriendStream({live: true, meta: true}),
|
25 | pull.drain(function (friend) {
|
26 | if(friend.sync) return
|
27 | live[friend.id] = friend.hops
|
28 | })
|
29 | )
|
30 | return live
|
31 | }
|
32 |
|
33 | var aliceKeys = ssbKeys.generate()
|
34 |
|
35 | var ssbServer = createSsbServer({
|
36 | temp:'test-friends1',
|
37 | port: 45451, host: 'localhost', timeout: 1000,
|
38 | keys: aliceKeys
|
39 | })
|
40 |
|
41 | var alice = ssbServer.createFeed(aliceKeys)
|
42 | var bob = ssbServer.createFeed()
|
43 | var carol = ssbServer.createFeed()
|
44 |
|
45 | tape('add friends, and retrive all friends for a peer', function (t) {
|
46 | var live = liveFriends(ssbServer)
|
47 |
|
48 | cont.para([
|
49 | alice.add({
|
50 | type: 'contact', contact: bob.id,
|
51 | following: true,
|
52 |
|
53 | }),
|
54 | alice.add(u.follow(carol.id)),
|
55 | bob.add(u.follow(alice.id)),
|
56 | bob.add({
|
57 | type: 'contact', contact: carol.id,
|
58 | following: false, flagged: true
|
59 | }),
|
60 | carol.add(u.follow(alice.id))
|
61 | ]) (function (err, results) {
|
62 | if(err) throw err
|
63 | ssbServer.friends.hops(function (err, hops) {
|
64 | if(err) throw err
|
65 | t.deepEqual(live, hops)
|
66 | t.end()
|
67 | })
|
68 | })
|
69 | })
|
70 |
|
71 | tape('createFriendStream', function (t) {
|
72 | pull(
|
73 | ssbServer.friends.createFriendStream(),
|
74 | pull.collect(function (err, ary) {
|
75 | t.notOk(err)
|
76 | t.equal(ary.length, 3)
|
77 | t.deepEqual(ary.sort(), [alice.id, bob.id, carol.id].sort())
|
78 | t.end()
|
79 | })
|
80 | )
|
81 | })
|
82 |
|
83 | tape('createFriendStream - meta', function (t) {
|
84 | pull(
|
85 | ssbServer.friends.createFriendStream({meta: true}),
|
86 | pull.collect(function (err, ary) {
|
87 | t.notOk(err)
|
88 | t.equal(ary.length, 3)
|
89 | t.deepEqual(sort(ary), sort([
|
90 | {id: alice.id, hops: 0},
|
91 | {id: bob.id, hops: 1},
|
92 | {id: carol.id, hops: 1}
|
93 | ]))
|
94 |
|
95 | t.end()
|
96 | })
|
97 | )
|
98 | })
|
99 |
|
100 | tape('cleanup', function (t) {
|
101 | ssbServer.close()
|
102 | t.end()
|
103 | })
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|