git ssb

0+

mixmix / ssb-testing-guide



Commit 326143a7cc03b1862d32495613d1e30125cdd40b

move tests so far into basic dir

Kieran committed on 5/23/2018, 4:01:14 AM
Parent: eec7e99ad5ed7ffbc5e4ee856aaeaa8db5f1b6ea

Files changed

1_helloWorld.test.jsdeleted
2_singleUserPublishing.test.jsdeleted
basic/1_helloWorld.test.jsadded
basic/2_singleUserPublishing.test.jsadded
basic/3_compileSequentialEdits.test.jsadded
basic/4_compileSequentialEditsRefactor.test.jsadded
3_compileSequentialEdits.test.jsdeleted
4_compileSequentialEditsRefactor.test.jsdeleted
1_helloWorld.test.jsView
@@ -1,21 +1,0 @@
1-const test = require('tape')
2-
3-test('hello world', t => {
4- t.equal(2 + 2, 4, "basic addition works!")
5- t.end()
6-})
7-
8-test('async hello world', t => {
9- t.plan(1)
10- setTimeout(() => {
11- t.equal(2 + 2, 4, "basic addition works!")
12- // Could put t.end here alternatively!
13- })
14-})
15-
16-test('throws hello world', t => {
17- t.plan(1)
18- t.throws(() => {
19- throw new Error("Hello world!")
20- })
21-})
2_singleUserPublishing.test.jsView
@@ -1,56 +1,0 @@
1-const test = require('tape')
2-const Server = require('scuttle-testbot')
3-const pull = require('pull-stream')
4-
5-test('publish a message and get it back', t => {
6- var server = Server()
7-
8- t.plan(2)
9-
10- var content = {
11- type: "post",
12- text: "hello world"
13- }
14-
15- server.publish(content, (err, msg) => {
16- t.equal(msg.value.content.type, 'post')
17- t.equal(msg.value.content.text, 'hello world')
18-
19- // Make sure you close the server or tests will hang
20- server.close()
21- })
22-})
23-
24-test('pull the message and compare with published', t => {
25- // this is a demo of using a pull-stream to listen for a message
26- // not sure if useful in testing but it demonstrates a different async pattern
27-
28- // sink tap turned on but not yet connected to water supply
29- // when waters turned on (message published), the tap spews out water into the sink,
30- // and the sink drains
31-
32- var server = Server()
33-
34- t.plan(1)
35-
36- pull(
37- server.createFeedStream({ live: true }),
38- pull.filter(msg => msg.sync !== true), // live streams emit { sync: true } when 'up to speed'
39- pull.take(1), // optional, passes only one msg to the sink before closing stream
40- pull.drain(msg => {
41- t.deepEqual(msg.value.content, content)
42-
43- // Make sure you close the server or tests will hang
44- server.close()
45- })
46- )
47-
48- var content = {
49- type: "post",
50- text: "hello world"
51- }
52-
53- server.publish(content, (err, msg) => {
54- // Must provide a callback
55- })
56-})
basic/1_helloWorld.test.jsView
@@ -1,0 +1,21 @@
1 +const test = require('tape')
2 +
3 +test('hello world', t => {
4 + t.equal(2 + 2, 4, "basic addition works!")
5 + t.end()
6 +})
7 +
8 +test('async hello world', t => {
9 + t.plan(1)
10 + setTimeout(() => {
11 + t.equal(2 + 2, 4, "basic addition works!")
12 + // Could put t.end here alternatively!
13 + })
14 +})
15 +
16 +test('throws hello world', t => {
17 + t.plan(1)
18 + t.throws(() => {
19 + throw new Error("Hello world!")
20 + })
21 +})
basic/2_singleUserPublishing.test.jsView
@@ -1,0 +1,56 @@
1 +const test = require('tape')
2 +const Server = require('scuttle-testbot')
3 +const pull = require('pull-stream')
4 +
5 +test('publish a message and get it back', t => {
6 + var server = Server()
7 +
8 + t.plan(2)
9 +
10 + var content = {
11 + type: "post",
12 + text: "hello world"
13 + }
14 +
15 + server.publish(content, (err, msg) => {
16 + t.equal(msg.value.content.type, 'post')
17 + t.equal(msg.value.content.text, 'hello world')
18 +
19 + // Make sure you close the server or tests will hang
20 + server.close()
21 + })
22 +})
23 +
24 +test('pull the message and compare with published', t => {
25 + // this is a demo of using a pull-stream to listen for a message
26 + // not sure if useful in testing but it demonstrates a different async pattern
27 +
28 + // sink tap turned on but not yet connected to water supply
29 + // when waters turned on (message published), the tap spews out water into the sink,
30 + // and the sink drains
31 +
32 + var server = Server()
33 +
34 + t.plan(1)
35 +
36 + pull(
37 + server.createFeedStream({ live: true }),
38 + pull.filter(msg => msg.sync !== true), // live streams emit { sync: true } when 'up to speed'
39 + pull.take(1), // optional, passes only one msg to the sink before closing stream
40 + pull.drain(msg => {
41 + t.deepEqual(msg.value.content, content)
42 +
43 + // Make sure you close the server or tests will hang
44 + server.close()
45 + })
46 + )
47 +
48 + var content = {
49 + type: "post",
50 + text: "hello world"
51 + }
52 +
53 + server.publish(content, (err, msg) => {
54 + // Must provide a callback
55 + })
56 +})
basic/3_compileSequentialEdits.test.jsView
@@ -1,0 +1,62 @@
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 +})
basic/4_compileSequentialEditsRefactor.test.jsView
@@ -1,0 +1,94 @@
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 +
7 +test('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 +
3_compileSequentialEdits.test.jsView
@@ -1,62 +1,0 @@
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-})
4_compileSequentialEditsRefactor.test.jsView
@@ -1,94 +1,0 @@
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-
7-test('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-

Built with git-ssb-web