git ssb

0+

mixmix / ssb-testing-guide



Tree: 326143a7cc03b1862d32495613d1e30125cdd40b

Files: 326143a7cc03b1862d32495613d1e30125cdd40b / basic / 4_compileSequentialEditsRefactor.test.js

2454 bytesRaw
1const test = require('tape')
2const Server = require('scuttle-testbot')
3const pull = require('pull-stream')
4const sort = require('ssb-sort')
5
6
7test('Refactor edit a post', t => {
8 // Use backlinks to quickly get related messages using a root id
9 Server.use(require('ssb-backlinks'))
10
11 var server = Server()
12
13 t.plan(2)
14
15 var content = {
16 type: "post",
17 text: "hello wolard!"
18 }
19
20 var edit = {
21 type: "post-edit",
22 text: "hello world!"
23 // root: ??? - this will be the message id of the first post when we know it
24 }
25
26 server.publish(content, (err, first) => {
27 edit.root = first.key
28
29 compilePost(first.key, (err, compiledPost) => {
30 // compiled post is up to date with first message
31 t.equal(content.text, compiledPost.compiledText)
32
33 server.publish(edit, (err, second) => {
34
35 compilePost(first.key, (err, compiledPost) => {
36 // compiled post now accounts for second message
37 t.equal(edit.text, compiledPost.compiledText)
38
39 server.close()
40 })
41 })
42 })
43 })
44
45 function compilePost (key, cb) {
46 // ssb-backlinks does not return the root so we have to get it first
47 server.get(key, (err, value) => {
48 const initialText = value.content.text
49
50 pull(
51 createBacklinkStream(key),
52 pull.filter(msg => isPost(msg) || isPostUpdate(msg)),
53 pull.collect((err, msgs) => {
54 // ssb-sort orders causally
55 var sorted = sort(msgs)
56 var compiledText = sorted.reduce((state, msg) => msg.value.content.text, initialText)
57
58 cb(null, { compiledText })
59 })
60 )
61 })
62
63 function isPost (msg) {
64 // TODO: Make a schema
65 return msg.value.content.type === "post"
66 && msg.key === key
67 }
68
69 function isPostUpdate (msg) {
70 // TODO: Make a schema
71 return msg.value.content.type === "post-edit"
72 && msg.value.content.root
73 && msg.value.content.root === key
74 }
75
76 function createBacklinkStream (id) {
77 // ssb-backlinks is built on flumeview-query which uses map-filter-reduce
78 // This query is copied from backlinks README
79
80 var filterQuery = {
81 $filter: {
82 dest: id
83 }
84 }
85 // $reduce and $map are other options, see https://github.com/dominictarr/map-filter-reduce
86
87 return server.backlinks.read({
88 query: [filterQuery],
89 index: 'DTA', // use asserted timestamps
90 })
91 }
92 }
93})
94
95

Built with git-ssb-web