1 | const ssbKeys = require('ssb-keys')
|
2 | const cont = require('cont')
|
3 | const tape = require('tape')
|
4 | const u = require('./util')
|
5 | const pull = require('pull-stream')
|
6 |
|
7 | function sort (ary) {
|
8 | return ary.sort(function (a, b) {
|
9 | return a.id < b.id ? -1 : a.id === b.id ? 1 : 0
|
10 | })
|
11 | }
|
12 |
|
13 | function liveFriends (ssbServer) {
|
14 | const live = {}
|
15 | pull(
|
16 | ssbServer.friends.createFriendStream({ live: true, meta: true }),
|
17 | pull.drain(function (friend) {
|
18 | if (friend.sync) return
|
19 | live[friend.id] = friend.hops
|
20 | })
|
21 | )
|
22 | return live
|
23 | }
|
24 |
|
25 | const aliceKeys = ssbKeys.generate()
|
26 |
|
27 | const ssbServer = u.Server({
|
28 | keys: aliceKeys
|
29 | })
|
30 |
|
31 | const alice = ssbServer.createFeed(aliceKeys)
|
32 | const bob = ssbServer.createFeed()
|
33 | const carol = ssbServer.createFeed()
|
34 |
|
35 | tape('add friends, and retrive all friends for a peer', function (t) {
|
36 | const live = liveFriends(ssbServer)
|
37 |
|
38 | cont.para([
|
39 | alice.add({
|
40 | type: 'contact',
|
41 | contact: bob.id,
|
42 | following: true
|
43 |
|
44 | }),
|
45 | alice.add(u.follow(carol.id)),
|
46 | bob.add(u.follow(alice.id)),
|
47 | bob.add({
|
48 | type: 'contact',
|
49 | contact: carol.id,
|
50 | following: false,
|
51 | flagged: true
|
52 | }),
|
53 | carol.add(u.follow(alice.id))
|
54 | ])(function (err, results) {
|
55 | if (err) throw err
|
56 | ssbServer.friends.hops(function (err, hops) {
|
57 | if (err) throw err
|
58 | t.deepEqual(live, hops)
|
59 | t.end()
|
60 | })
|
61 | })
|
62 | })
|
63 |
|
64 | tape('createFriendStream', function (t) {
|
65 | pull(
|
66 | ssbServer.friends.createFriendStream(),
|
67 | pull.collect(function (err, ary) {
|
68 | t.notOk(err)
|
69 | t.equal(ary.length, 3)
|
70 | t.deepEqual(ary.sort(), [alice.id, bob.id, carol.id].sort())
|
71 | t.end()
|
72 | })
|
73 | )
|
74 | })
|
75 |
|
76 | tape('createFriendStream - meta', function (t) {
|
77 | pull(
|
78 | ssbServer.friends.createFriendStream({ meta: true }),
|
79 | pull.collect(function (err, ary) {
|
80 | t.notOk(err)
|
81 | t.equal(ary.length, 3)
|
82 | t.deepEqual(sort(ary), sort([
|
83 | { id: alice.id, hops: 0 },
|
84 | { id: bob.id, hops: 1 },
|
85 | { id: carol.id, hops: 1 }
|
86 | ]))
|
87 |
|
88 | t.end()
|
89 | })
|
90 | )
|
91 | })
|
92 |
|
93 | tape('cleanup', function (t) {
|
94 | ssbServer.close()
|
95 | t.end()
|
96 | })
|