UNPKG

2.15 kBJavaScriptView Raw
1const ssbKeys = require('ssb-keys')
2const cont = require('cont')
3const tape = require('tape')
4const u = require('./util')
5const 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
11function 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
17function 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
29const aliceKeys = ssbKeys.generate()
30const ssbServer = u.Server({
31 keys: aliceKeys
32})
33
34const alice = ssbServer.createFeed(aliceKeys)
35const bob = ssbServer.createFeed()
36const carol = ssbServer.createFeed()
37const dan = ssbServer.createFeed()
38
39const live = liveFriends(ssbServer)
40
41tape('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
67const 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
74tape('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
85tape('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
99tape('cleanup', function (t) {
100 ssbServer.close()
101 t.end()
102})