git ssb

0+

Matt McKegg / ssb-same-as



Tree: d5b1e6e6827f845ce25a452fba8210b7f6052c3b

Files: d5b1e6e6827f845ce25a452fba8210b7f6052c3b / test / sbot.js

4084 bytesRaw
1var pull = require('pull-stream')
2var tape = require('tape')
3var createSbot = require('scuttlebot')
4 .use(require('scuttlebot/plugins/replicate'))
5 .use(require('../'))
6
7var sbot = createSbot({
8 temp: 'alice',
9 port: 45451,
10 host: 'localhost',
11 timeout: 20001,
12 replicate: {
13 hops: 2,
14 legacy: false
15 }
16})
17
18tape('check updates to graph', function (t) {
19 var changes = []
20
21 pull(
22 sbot.sameAs.stream({live: true}),
23 pull.drain(function (m) { changes.push(m) })
24 )
25
26 var feedA = sbot.createFeed()
27 var feedB = sbot.createFeed()
28 var feedC = sbot.createFeed()
29 var feedD = sbot.createFeed()
30
31 // feedA -> feedB
32 sbot.publish({
33 type: 'contact',
34 contact: feedA.id,
35 following: true
36 }, function () {
37 t.equal(changes.length, 0, 'no change on follow')
38 changes.length = 0
39
40 feedA.publish({
41 type: 'contact',
42 contact: feedB.id,
43 sameAs: true
44 }, function () {
45 t.equal(changes.length, 0, 'one sided sameAs ignored')
46 changes.length = 0
47
48 feedB.publish({
49 type: 'contact',
50 contact: feedA.id,
51 sameAs: true
52 }, function () {
53 t.deepEqual(changes, [
54 {from: feedA.id, to: feedB.id, value: true},
55 {from: feedB.id, to: feedA.id, value: true}
56 ], 'mutual sameAs merges')
57 changes.length = 0
58
59 feedC.publish({
60 type: 'contact',
61 contact: feedB.id,
62 sameAs: true
63 }, function () {
64 t.equal(changes.length, 0, 'one sided sameAs ignored')
65 changes.length = 0
66
67 feedB.publish({
68 type: 'contact',
69 contact: feedC.id,
70 sameAs: true
71 }, function () {
72 t.deepEqual(changes, [
73 {from: feedA.id, to: feedC.id, value: true}, // join up with A!
74 {from: feedB.id, to: feedC.id, value: true},
75 {from: feedC.id, to: feedB.id, value: true},
76 {from: feedC.id, to: feedA.id, value: true} // join up with A!
77 ], 'sameAs chain joins up and merges all')
78 changes.length = 0
79
80 feedD.publish({
81 type: 'contact',
82 contact: feedC.id,
83 sameAs: true
84 }, function () {
85 t.equal(changes.length, 0)
86 changes.length = 0
87
88 sbot.publish({
89 type: 'contact',
90 contact: feedD.id,
91 sameAs: {[feedC.id]: true} // has to agree with a claim
92 }, function () {
93 t.deepEqual(changes, [
94 {from: feedA.id, to: feedD.id, value: true},
95 {from: feedB.id, to: feedD.id, value: true},
96 {from: feedC.id, to: feedD.id, value: true},
97 {from: feedD.id, to: feedC.id, value: true},
98 {from: feedD.id, to: feedB.id, value: true},
99 {from: feedD.id, to: feedA.id, value: true}
100 ], 'join graph when local agreement')
101
102 changes.length = 0
103 checkNonRealtime()
104 })
105 })
106 })
107 })
108 })
109 })
110 })
111
112 function checkNonRealtime () {
113 pull(
114 sbot.sameAs.stream({live: false}),
115 pull.collect(function (err, results) {
116 if (err) throw err
117
118 t.deepEqual(results, [
119 {from: feedA.id, to: feedB.id, value: true},
120 {from: feedA.id, to: feedC.id, value: true},
121 {from: feedA.id, to: feedD.id, value: true},
122 {from: feedB.id, to: feedA.id, value: true},
123 {from: feedB.id, to: feedC.id, value: true},
124 {from: feedB.id, to: feedD.id, value: true},
125 {from: feedC.id, to: feedB.id, value: true},
126 {from: feedC.id, to: feedD.id, value: true},
127 {from: feedC.id, to: feedA.id, value: true},
128 {from: feedD.id, to: feedC.id, value: true},
129 {from: feedD.id, to: feedB.id, value: true},
130 {from: feedD.id, to: feedA.id, value: true}
131 ], 'non realtime: everything liked up!')
132
133 t.end()
134 sbot.close()
135 })
136 )
137 }
138})
139

Built with git-ssb-web