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