git ssb

0+

mixmix / ssb-testing-guide



Tree: e21973dd20862eb2f68335de131463038d7aacc8

Files: e21973dd20862eb2f68335de131463038d7aacc8 / 2_singleuserpublishing.test.js

1449 bytesRaw
1const test = require('tape')
2const Server = require('scuttle-testbot')
3const pull = require('pull-stream')
4
5test('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
24test('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})
57

Built with git-ssb-web