Files: 6dde1c5e1d32a7427848bfad8a4641577bd0d1ab / test / linktypes.js
6264 bytesRaw
1 | |
2 | var tape = require('tape') |
3 | var level = require('level-test')() |
4 | var sublevel = require('level-sublevel/bytewise') |
5 | var pull = require('pull-stream') |
6 | var cont = require('cont') |
7 | var typewise = require('typewiselite') |
8 | var ssbKeys = require('ssb-keys') |
9 | var createFeed = require('ssb-feed') |
10 | |
11 | function sort (a) { |
12 | return a.sort(function (a, b) { |
13 | return ( |
14 | typewise(a.source, b.source) |
15 | || typewise(a.rel, b.rel) |
16 | || typewise(a.dest, b.dest) |
17 | || typewise(a.message, b.message) |
18 | ) |
19 | }) |
20 | } |
21 | |
22 | module.exports = function (opts) { |
23 | |
24 | var db = sublevel(level('test-ssb-links', { |
25 | valueEncoding: require('../codec') |
26 | })) |
27 | |
28 | var ssb = require('../')(db, opts) |
29 | |
30 | var all = function (stream) { |
31 | return function (cb) { |
32 | pull(stream, pull.collect(cb)) |
33 | } |
34 | } |
35 | |
36 | var alice = createFeed(ssb, ssbKeys.generate(), opts) |
37 | var bob = createFeed(ssb, ssbKeys.generate(), opts) |
38 | var carol = createFeed(ssb, ssbKeys.generate(), opts) |
39 | |
40 | function sortTS (ary) { |
41 | return ary.sort(function (a, b) { |
42 | return ( |
43 | a.timestamp - b.timestamp |
44 | || typewise(a.key, b.key) |
45 | || typewise(a.author, b.author) |
46 | ) |
47 | }) |
48 | } |
49 | |
50 | tape('reply to a message', function (t) { |
51 | |
52 | alice.add('msg', 'hello world', function (err, msg) { |
53 | if(err) throw err |
54 | bob.add('msg', { |
55 | reply: msg.key, |
56 | text: 'okay then' |
57 | }, function (err, reply1) { |
58 | if(err) throw err |
59 | carol.add('msg', { |
60 | reply: msg.key, |
61 | text: 'whatever' |
62 | }, function (err, reply2) { |
63 | if(err) throw err |
64 | |
65 | cont.series([ |
66 | function (cb) { |
67 | all(ssb.links({ |
68 | dest: msg.key, rel: 'reply', |
69 | meta: false, keys: false, values: true |
70 | })) (function (err, ary) { |
71 | if(err) throw err |
72 | t.deepEqual(sortTS(ary), sortTS([reply1.value, reply2.value])) |
73 | cb() |
74 | }) |
75 | }, |
76 | function (cb) { |
77 | all(ssb.links({ |
78 | dest: msg.key, rel: 'reply', |
79 | keys: true, meta: false, values: true |
80 | })) |
81 | (function (err, ary) { |
82 | t.deepEqual(sortTS(ary), sortTS([reply1, reply2])) |
83 | |
84 | cb() |
85 | }) |
86 | }, |
87 | function (cb) { |
88 | all(ssb.links({dest: msg.key, rel: 'reply', values: true})) |
89 | (function (err, ary) { |
90 | if(err) throw err |
91 | t.deepEqual(sort(ary), sort([ |
92 | { |
93 | source: reply1.value.author, rel: 'reply', |
94 | dest: msg.key, key: reply1.key, |
95 | value: reply1.value |
96 | }, |
97 | { |
98 | source: reply2.value.author, rel: 'reply', |
99 | dest: msg.key, key: reply2.key, |
100 | value: reply2.value |
101 | } |
102 | ])) |
103 | cb() |
104 | }) |
105 | } |
106 | ]) (function (err) { |
107 | if(err) throw err; t.end() |
108 | }) |
109 | }) |
110 | }) |
111 | }) |
112 | }) |
113 | |
114 | tape('follow another user', function (t) { |
115 | |
116 | function follow (a, b) { |
117 | return function (cb) { |
118 | a.add('follow', {follow: b.id}, function (err, msg) { |
119 | cb(err, msg.key) |
120 | }) |
121 | } |
122 | } |
123 | |
124 | cont.para({ |
125 | ab: follow(alice, bob), |
126 | ac: follow(alice, carol), |
127 | ba: follow(bob, alice), |
128 | bc: follow(bob, carol), |
129 | }) (function (err, f) { |
130 | |
131 | cont.para({ |
132 | alice: all(ssb.links({source: alice.id, dest: '@'})), |
133 | bob: all(ssb.links({source: bob.id, dest: 'feed'})), |
134 | _alice: all(ssb.links({dest: alice.id, source: '@'})), |
135 | _carol: all(ssb.links({dest: carol.id, source: 'feed'})) |
136 | }) (function (err, r) { |
137 | |
138 | console.log({ |
139 | alice: alice.id, |
140 | bob: bob.id, |
141 | carol: carol.id, |
142 | |
143 | }) |
144 | |
145 | t.deepEqual(sort(r.alice), sort([ |
146 | {source: alice.id, rel: 'follow', dest: bob.id, key: f.ab}, |
147 | {source: alice.id, rel: 'follow', dest: carol.id, key: f.ac} |
148 | ])) |
149 | |
150 | t.deepEqual(sort(r.bob), sort([ |
151 | {source: bob.id, rel: 'follow', dest: alice.id, key: f.ba}, |
152 | {source: bob.id, rel: 'follow', dest: carol.id, key: f.bc} |
153 | ])) |
154 | |
155 | t.deepEqual(sort(r._alice), sort([ |
156 | {source: bob.id, rel: 'follow', dest: alice.id, key: f.ba} |
157 | ])) |
158 | |
159 | t.deepEqual(sort(r._carol), sort([ |
160 | {source: alice.id, rel: 'follow', dest: carol.id, key: f.ac}, |
161 | {source: bob.id, rel: 'follow', dest: carol.id, key: f.bc} |
162 | ]), 'carol is followed by alice and bob') |
163 | |
164 | t.end() |
165 | }) |
166 | }) |
167 | }) |
168 | |
169 | tape('check for 1:1 relationships', function (t) { |
170 | function follows (a, b) { |
171 | return function (cb) { |
172 | pull( |
173 | ssb.links({ |
174 | source: a, |
175 | dest: b, |
176 | rel: 'follow' |
177 | }), |
178 | pull.collect(function (_, ary) { |
179 | cb(null, ary.length > 0) |
180 | }) |
181 | ) |
182 | } |
183 | } |
184 | |
185 | cont.para({ |
186 | alice_bob: follows(alice.id, bob .id), |
187 | alice_carol: follows(alice.id, carol.id), |
188 | bob_alice: follows(bob .id, alice.id), |
189 | bob_carol: follows(bob .id, carol.id), |
190 | carol_alice: follows(carol.id, alice.id), |
191 | carol_bob: follows(carol.id, bob .id) |
192 | }) |
193 | (function (err, result) { |
194 | if(err) throw err |
195 | t.deepEqual(result, { |
196 | alice_bob: true, |
197 | alice_carol: true, |
198 | bob_alice: true, |
199 | bob_carol: true, |
200 | carol_alice: false, |
201 | carol_bob: false, |
202 | }) |
203 | t.end() |
204 | }) |
205 | |
206 | }) |
207 | |
208 | tape('scan links with unknown rel', function (t) { |
209 | alice.add({ |
210 | type: 'poke', |
211 | poke: bob.id |
212 | }, function (err) { |
213 | all(ssb.links({ |
214 | source: alice.id, dest: bob.id, values: true |
215 | })) |
216 | (function (err, ary) { |
217 | if(err) throw err |
218 | t.equal(ary.length, 2) |
219 | t.deepEqual(ary.map(function (e) { |
220 | return e.value.content |
221 | }), [ |
222 | {type: 'follow', follow: bob.id}, |
223 | {type: 'poke', poke: bob.id}, |
224 | ]) |
225 | t.end() |
226 | |
227 | }) |
228 | }) |
229 | |
230 | }) |
231 | |
232 | } |
233 | |
234 | |
235 | |
236 | if(!module.parent) |
237 | module.exports(require('../defaults')) |
238 | |
239 |
Built with git-ssb-web