git ssb

0+

Dominic / ssb-feed



Tree: 7f5b9989dbc45ee625499416f98f9ce28de20509

Files: 7f5b9989dbc45ee625499416f98f9ce28de20509 / test / feed.js

4997 bytesRaw
1'use strict'
2var tape = require('tape')
3var pull = require('pull-stream')
4var ssbKeys = require('ssb-keys')
5var createFeed = require('../')
6var ssbkeys = ssbKeys
7
8var crypto = require('crypto')
9
10var seed = crypto.createHash('sha256').update('test1').digest()
11var assert = require('assert')
12
13module.exports = function (createMock, createAsync, opts) {
14
15 tape('simple', function (t) {
16
17 createAsync(function (async) {
18
19 var ssb = createMock(async, opts)
20
21 var feed = createFeed(ssb, ssbkeys.generate('ed25519', seed), opts)
22
23 feed.add({type: 'msg', value: 'hello there!'}, function (err, msg) {
24 if(err) throw err
25 console.log('added', msg)
26 assert.ok(!!msg)
27 assert.ok(!!msg.key)
28 assert.ok(!!msg.value)
29 pull(
30 ssb.createFeedStream(),
31 pull.collect(function (err, ary) {
32 if(err) throw err
33 assert.equal(ary.length, 1)
34 assert.ok(!!ary[0].key)
35 assert.ok(!!ary[0].value)
36 async.done()
37 })
38 )
39 })
40 }, function (err) {
41 if(err) throw err
42 t.end()
43 })
44 })
45
46
47 //write in series
48 tape('tail', function (t) {
49 createAsync(function (async) {
50 var ssb = createMock(async, opts)
51
52 var feed = createFeed(ssb, ssbkeys.generate('ed25519', seed), opts)
53
54 console.log('add 1'); console.log('add 2');
55 var nDrains = 0, nAdds = 2;
56
57 feed.add({
58 type: 'msg',
59 value:'hello there!'
60 }, function (err, msg1) {
61
62 if(err) throw err
63 var lasthash = msg1.key
64
65 pull(
66 pull.values([1,2,3,4]),
67 async.through(),
68 pull.asyncMap(function (n, cb) {
69 feed.add({type: 'msg', value: 'message '+n}, function(err, msgX) {
70 if(err) throw err
71 assert.equal(msgX.value.previous, lasthash)
72 lasthash = msgX.key;
73 cb()
74 })
75 }),
76 pull.drain()
77 )
78
79 pull(
80 ssb.createFeedStream({ live: true }),
81 pull.drain(function (ary) {
82 nDrains++;
83 if (nDrains == 4) {
84 async.done()
85 return false
86 }
87 })
88 )
89
90 })
91 }, function (err) {
92 console.log(err)
93 if(err) throw err
94 t.end()
95 })
96 })
97
98 tape('tail, parallel add', function (t) {
99 createAsync(function (async) {
100 var ssb = createMock(async, opts)
101
102 var feed = createFeed(ssb, ssbkeys.generate('ed25519', seed), opts)
103
104 var nDrains = 0, nAdds = 2, l = 7
105 feed.add({type: 'msg', value: 'hello there!'}, function (err, msg1) {
106 if(err) throw err
107
108 var lasthash = msg1.key
109
110 function addAgain(n) {
111 feed.add('msg', 'message '+n, function(err, msgX) {
112// assert.equal(msgX.value.previous, lasthash)
113// lasthash = msgX.key;
114 nAdds++;
115 if (err) throw err;
116 });
117 }
118
119 var received = []
120
121 pull(
122 ssb.createFeedStream({ live: true }),
123 pull.drain(function (msg) {
124 nDrains++;
125 received.push(msg)
126 console.log('drain', nDrains)
127 if (nDrains == 3) {
128 assert.deepEqual(received.map(function (v) {
129 return v.value.content.value
130 }), ['hello there!', 'message 1', 'message 2'])
131 async.done()
132 return false
133 }
134 })
135 )
136
137 addAgain(1)
138 addAgain(2)
139// addAgain(3)
140// addAgain(4)
141 })
142 }, function (err) {
143 if(err) throw err
144 t.end()
145 })
146 })
147
148 tape('too big', function (t) {
149 createAsync(function (async) {
150 var ssb = createMock(async, opts)
151 var keys = ssbkeys.generate()
152 var feed = createFeed(ssb, ssbkeys.generate('ed25519', seed), opts)
153 var str = ''
154 for (var i=0; i < 808; i++) str += '1234567890'
155
156 feed.add({type: 'msg', value: str}, function (err, msg) {
157 if(!err) throw new Error('too big was allowed')
158 async.done()
159 })
160 }, function (err) {
161 if(err) throw err
162 t.end()
163 })
164 })
165
166 tape('error', function (t) {
167 createAsync(function (async) {
168 var keys = ssbkeys.generate()
169 var feed = createFeed({
170 add: function () { throw new Error('should not be called') },
171 getLatest: async(function (id, cb) {
172 cb(new Error('key not found'))
173 })
174 }, ssbkeys.generate('ed25519', seed), opts)
175
176 feed.add({type: 'test', okay: true}, function (err) {
177 t.ok(err)
178 async.done()
179 })
180
181 }, function () {
182 t.end()
183 })
184 })
185
186}
187
188if(!module.parent) {
189 //tests without a cap
190 module.exports(require('./mock'), require('./util').sync, { remote: false })
191 //with cap
192 module.exports(require('./mock'), require('./util').sync, {
193 remote: false, caps: {sign: crypto.createHash('sha256').digest('test with digest').toString('base64')}
194})
195
196}
197
198
199
200

Built with git-ssb-web