UNPKG

2.13 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
7function 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
13function 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
25const aliceKeys = ssbKeys.generate()
26
27const ssbServer = u.Server({
28 keys: aliceKeys
29})
30
31const alice = ssbServer.createFeed(aliceKeys)
32const bob = ssbServer.createFeed()
33const carol = ssbServer.createFeed()
34
35tape('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 // flagged: { reason: 'foo' }
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
64tape('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
76tape('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
93tape('cleanup', function (t) {
94 ssbServer.close()
95 t.end()
96})