Files: 6dde1c5e1d32a7427848bfad8a4641577bd0d1ab / test / history.js
4492 bytesRaw
1 | ; |
2 | |
3 | var level = require('level-test')() |
4 | var sublevel = require('level-sublevel/bytewise') |
5 | var pull = require('pull-stream') |
6 | var tape = require('tape') |
7 | |
8 | var Abortable = require('pull-abortable') |
9 | |
10 | var SSB = require('../') |
11 | |
12 | var compare = require('ltgt').compare |
13 | var generate = require('ssb-keys').generate |
14 | |
15 | //create a instance with a feed |
16 | //then have another instance follow it. |
17 | |
18 | function rand (n) { |
19 | var a = [] |
20 | while(n--) |
21 | a.push(Math.random()) |
22 | return a |
23 | } |
24 | |
25 | function sort (ary) { |
26 | return ary.sort(function (a, b) { |
27 | return compare(a.id, b.id) || a.sequence - b.sequence |
28 | }) |
29 | } |
30 | |
31 | |
32 | module.exports = function (opts) { |
33 | |
34 | var create = require('ssb-feed/util').create |
35 | |
36 | function createDB(name) { |
37 | return SSB(sublevel(level(name, {valueEncoding: require('../codec')})), opts) |
38 | } |
39 | |
40 | var MESSAGE = new Buffer('msg') |
41 | |
42 | function init (ssb, n, cb) { |
43 | var keys = generate() |
44 | var prev |
45 | |
46 | ssb.add(prev = create(keys, null, {type: 'init', public: keys.public}), function () { |
47 | pull( |
48 | pull.values(rand(n)), |
49 | pull.asyncMap(function (r, cb) { |
50 | ssb.add(prev = |
51 | create(keys, 'msg', ''+r, prev), cb) |
52 | }), |
53 | pull.drain(null, cb) |
54 | ) |
55 | }) |
56 | |
57 | return keys |
58 | } |
59 | |
60 | var ssb = createDB('ssb-history') |
61 | var keys, id, keys2, id2 |
62 | tape('history', function (t) { |
63 | |
64 | keys = init(ssb, 7, function (err) { |
65 | if(err) throw err |
66 | pull(ssb.latest(), pull.collect(function (err, ary) { |
67 | if(err) throw err |
68 | delete ary[0].ts |
69 | t.deepEqual(ary, [ |
70 | {id: keys.id, sequence: 8} |
71 | ]) |
72 | t.end() |
73 | })) |
74 | }) |
75 | |
76 | id = keys.id //opts.hash(keys.public) |
77 | }) |
78 | |
79 | tape('since', function (t) { |
80 | pull( |
81 | ssb.createHistoryStream({id: id, seq:1}), |
82 | pull.collect(function (err, ary) { |
83 | t.equal(ary.length, 8) |
84 | t.end() |
85 | }) |
86 | ) |
87 | }) |
88 | |
89 | tape('two keys', function (t) { |
90 | |
91 | keys2 = init(ssb, 4, function (err) { |
92 | pull(ssb.latest(), pull.collect(function (err, ary) { |
93 | if(err) throw err |
94 | t.deepEqual( |
95 | sort(ary.map(function (e) { delete e.ts; return e })), |
96 | sort([ |
97 | {id: keys.id, sequence: 8}, |
98 | {id: keys2.id, sequence: 5} |
99 | ]) |
100 | ) |
101 | t.end() |
102 | })) |
103 | }) |
104 | |
105 | }) |
106 | |
107 | tape('keys & since', function (t) { |
108 | pull( |
109 | ssb.createHistoryStream({ id: id, seq: 1, keys: true }), |
110 | pull.collect(function (err, ary) { |
111 | if(err) throw err |
112 | console.log(ary) |
113 | t.equal(ary.length, 8) |
114 | t.ok(!!ary[0].key) |
115 | t.ok(!!ary[1].key) |
116 | t.end() |
117 | }) |
118 | ) |
119 | }) |
120 | |
121 | tape('user stream', function (t) { |
122 | pull( |
123 | ssb.createUserStream({id: id, gt: 3, lte: 7, reverse: true}), |
124 | pull.collect(function (err, ary) { |
125 | t.equal(ary.length, 4) |
126 | t.equal(ary[3].value.sequence, 4) |
127 | t.equal(ary[2].value.sequence, 5) |
128 | t.equal(ary[1].value.sequence, 6) |
129 | t.equal(ary[0].value.sequence, 7) |
130 | t.end() |
131 | }) |
132 | ) |
133 | }) |
134 | tape('keys only', function (t) { |
135 | pull( |
136 | ssb.createHistoryStream({ id: id, values: false }), |
137 | pull.collect(function (err, ary) { |
138 | t.equal(ary.length, 8) |
139 | ary.forEach(function (v) { t.equal(typeof v, 'string') }) |
140 | t.end() |
141 | }) |
142 | ) |
143 | }) |
144 | |
145 | tape('values only', function (t) { |
146 | pull( |
147 | ssb.createHistoryStream({ id: id, keys: false }), |
148 | pull.collect(function (err, ary) { |
149 | t.equal(ary.length, 8) |
150 | ary.forEach(function (v) { t.equal(typeof v.content.type, 'string') }) |
151 | t.end() |
152 | }) |
153 | ) |
154 | }) |
155 | |
156 | tape('abort live stream', function (t) { |
157 | var abortable = Abortable(), err = new Error('intentional'), i = 0 |
158 | |
159 | pull( |
160 | ssb.createHistoryStream({ |
161 | id: id, keys: false, live: true, |
162 | }), |
163 | abortable, |
164 | pull.through(function (data) { |
165 | if(++i == 8) |
166 | setTimeout(function () { |
167 | abortable.abort(err) |
168 | }, 100) |
169 | console.log(data) |
170 | }, function (_err) { |
171 | t.equal(_err, err) |
172 | t.end() |
173 | }), |
174 | pull.collect(function (err, ary) { |
175 | t.equal(ary.length, 8) |
176 | }) |
177 | ) |
178 | |
179 | }) |
180 | |
181 | tape('createHistoryStream with limit', function (t) { |
182 | |
183 | pull( |
184 | ssb.createHistoryStream({ |
185 | id: id, keys: false, limit: 5 |
186 | }), |
187 | pull.collect(function (err, ary) { |
188 | if(err) throw err |
189 | t.equal(ary.length, 5) |
190 | t.end() |
191 | }) |
192 | ) |
193 | }) |
194 | } |
195 | |
196 | if(!module.parent) |
197 | module.exports(require('../defaults')) |
198 | |
199 | |
200 | |
201 | |
202 |
Built with git-ssb-web