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