git ssb

0+

mixmix / ssb-testing-guide



Commit e21973dd20862eb2f68335de131463038d7aacc8

compile edits of a post

Kieran committed on 5/22/2018, 12:37:07 AM
Parent: 1914c3d4f82b0ae0cfd2f8087d24bde52803668c

Files changed

3_compileSequentialEdits.test.jsadded
3_compileSequentialEdits.test.jsView
@@ -1,0 +1,61 @@
1 +const test = require('tape')
2 +const Server = require('scuttle-testbot')
3 +const pull = require('pull-stream')
4 +
5 +test('edit a post', t => {
6 + var server = Server()
7 +
8 + t.plan(1)
9 +
10 + var content = {
11 + type: "post",
12 + text: "hello wolard!"
13 + }
14 +
15 + var edit = {
16 + type: "post-edit",
17 + text: "hello world!"
18 + // root: ??? - this will be the message id of the first post when we know it
19 + }
20 +
21 + server.publish(content, (err, first) => {
22 + // now we know the key / root id
23 + edit.root = first.key
24 +
25 + server.publish(edit, (err, second) => {
26 + // now we recompile the message using the key of the root
27 +
28 + compilePost(first.key, (err, compiledPost) => {
29 + t.equal(edit.text, compiledPost.compiledText)
30 +
31 + // Make sure you close the server or tests will hang
32 + server.close()
33 + })
34 + })
35 +
36 + })
37 +
38 + function compilePost (key, cb) {
39 + // This implemention is hella naive
40 + // - createFeedStream will stream the ENTIRE database
41 + // - filters are super brittle, recommend json schema
42 + // - assumes arriving in correct order
43 +
44 + pull(
45 + server.createFeedStream(),
46 + pull.filter(msg => {
47 + // its a post and key is the same as the key
48 + // its a post-edit and root is the same as the key
49 + if (msg.value.content.type === "post" && msg.key === key) return true
50 + if (msg.value.content.type === "post-edit" && msg.value.content.root && msg.value.content.root === key) return true
51 + }),
52 + pull.collect((err, msgs) => {
53 + var compiledText = msgs.reduce((state, msg) => {
54 + return msg.value.content.text
55 + }, "")
56 +
57 + cb(null, { compiledText })
58 + })
59 + )
60 + }
61 +})

Built with git-ssb-web