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 liveFriends (ssbServer) {
|
12 | const live = {}
|
13 | pull(
|
14 | ssbServer.friends.createFriendStream({ live: true, meta: true }),
|
15 | pull.drain(function (friend) {
|
16 | if (friend.sync) return
|
17 | live[friend.id] = friend.hops
|
18 | })
|
19 | )
|
20 | return live
|
21 | }
|
22 |
|
23 | const aliceKeys = ssbKeys.generate()
|
24 |
|
25 | const ssbServer = u.Server({
|
26 | keys: aliceKeys
|
27 | })
|
28 |
|
29 | const alice = ssbServer.createFeed(aliceKeys)
|
30 | const bob = ssbServer.createFeed()
|
31 | const carol = ssbServer.createFeed()
|
32 |
|
33 | const live = liveFriends(ssbServer)
|
34 |
|
35 | tape('add and delete', function (t) {
|
36 | cont.para([
|
37 | alice.add({
|
38 | type: 'contact',
|
39 | contact: bob.id,
|
40 | following: true,
|
41 | flagged: true
|
42 | }),
|
43 | alice.add(u.follow(carol.id)),
|
44 | bob.add(u.follow(alice.id)),
|
45 | bob.add({
|
46 | type: 'contact',
|
47 | contact: carol.id,
|
48 | following: false,
|
49 | flagged: { reason: 'foo' }
|
50 | }),
|
51 | carol.add(u.follow(alice.id)),
|
52 | alice.add({
|
53 | type: 'contact',
|
54 | contact: carol.id,
|
55 | following: false,
|
56 | flagged: true
|
57 | }),
|
58 | alice.add({
|
59 | type: 'contact',
|
60 | contact: bob.id,
|
61 | following: true,
|
62 | flagged: false
|
63 | }),
|
64 | bob.add(u.unfollow(carol.id))
|
65 | ])(function () {
|
66 | ssbServer.friends.hops(function (err, hops) {
|
67 | if (err) throw err
|
68 | t.deepEqual(live, hops)
|
69 | t.end()
|
70 | })
|
71 | })
|
72 | })
|
73 |
|
74 | tape('createFriendStream after delete', function (t) {
|
75 | pull(
|
76 | ssbServer.friends.createFriendStream(),
|
77 | pull.collect(function (err, ary) {
|
78 | t.notOk(err)
|
79 | t.equal(ary.length, 2)
|
80 | t.deepEqual(ary.sort(), [alice.id, bob.id].sort())
|
81 | t.end()
|
82 | })
|
83 | )
|
84 | })
|
85 |
|
86 | tape('cleanup', function (t) {
|
87 | ssbServer.close()
|
88 |
|
89 | t.end()
|
90 | })
|