Files: 6dde1c5e1d32a7427848bfad8a4641577bd0d1ab / test / log.js
3892 bytesRaw
1 | |
2 | var tape = require('tape') |
3 | var level = require('level-test')() |
4 | var sublevel = require('level-sublevel/bytewise') |
5 | var pull = require('pull-stream') |
6 | var timestamp = require('monotonic-timestamp') |
7 | var ssbKeys = require('ssb-keys') |
8 | var createFeed = require('ssb-feed') |
9 | |
10 | var generate = ssbKeys.generate |
11 | |
12 | module.exports = function (opts) { |
13 | |
14 | var level_opts = { |
15 | valueEncoding: require('../codec') |
16 | } |
17 | tape('simple', function (t) { |
18 | |
19 | var db = sublevel(level('test-ssb-log', level_opts)) |
20 | |
21 | var ssb = require('../')(db, opts) |
22 | |
23 | var feed = createFeed(ssb, generate(), opts) |
24 | |
25 | feed.add('msg', 'hello there!', function (err, msg) { |
26 | if(err) throw err |
27 | pull( |
28 | ssb.createLogStream(), |
29 | pull.collect(function (err, ary) { |
30 | if(err) throw err |
31 | t.equal(ary.length, 1) |
32 | t.assert(!!ary[0].key) |
33 | t.assert(!!ary[0].value) |
34 | console.log(ary) |
35 | t.end() |
36 | }) |
37 | ) |
38 | }) |
39 | |
40 | }) |
41 | |
42 | tape('gt', function (t) { |
43 | |
44 | var db = sublevel(level('test-ssb-log2', level_opts)) |
45 | |
46 | var ssb = require('../')(db, opts) |
47 | |
48 | var feed = createFeed(ssb, generate(), opts) |
49 | |
50 | feed.add('msg', 'hello there!', function (err, msg) { |
51 | if(err) throw err |
52 | var start = timestamp() |
53 | feed.add('msg', 'second message', function (err, msg) { |
54 | if(err) throw err |
55 | pull( |
56 | ssb.createLogStream({ gt: start }), |
57 | pull.collect(function (err, ary) { |
58 | if(err) throw err |
59 | t.equal(ary.length, 1) |
60 | console.log(ary) |
61 | t.end() |
62 | }) |
63 | ) |
64 | }) |
65 | }) |
66 | |
67 | }) |
68 | |
69 | tape('gt 0', function (t) { |
70 | |
71 | var db = sublevel(level('test-ssb-log3', level_opts)) |
72 | |
73 | var ssb = require('../')(db, opts) |
74 | |
75 | var feed = createFeed(ssb, generate(), opts) |
76 | |
77 | feed.add('msg', 'hello there!', function (err, msg) { |
78 | if(err) throw err |
79 | pull( |
80 | ssb.createLogStream({ gt: 0 }), |
81 | pull.collect(function (err, ary) { |
82 | if(err) throw err |
83 | t.equal(ary.length, 1) |
84 | console.log(ary) |
85 | t.end() |
86 | }) |
87 | ) |
88 | }) |
89 | |
90 | }) |
91 | |
92 | tape('keys only', function (t) { |
93 | |
94 | var db = sublevel(level('test-ssb-log4', level_opts)) |
95 | |
96 | var ssb = require('../')(db, opts) |
97 | |
98 | var feed = createFeed(ssb, generate(), opts) |
99 | |
100 | feed.add('msg', 'hello there!', function (err, msg) { |
101 | if(err) throw err |
102 | pull( |
103 | ssb.createLogStream({ values: false }), |
104 | pull.collect(function (err, ary) { |
105 | if(err) throw err |
106 | t.equal(ary.length, 1) |
107 | t.equal(typeof ary[0], 'string') |
108 | console.log(ary) |
109 | t.end() |
110 | }) |
111 | ) |
112 | }) |
113 | |
114 | }) |
115 | |
116 | tape('values only', function (t) { |
117 | |
118 | var db = sublevel(level('test-ssb-log5', level_opts)) |
119 | |
120 | var ssb = require('../')(db, opts) |
121 | |
122 | var feed = createFeed(ssb, generate(), opts) |
123 | |
124 | feed.add('msg', 'hello there!', function (err, msg) { |
125 | if(err) throw err |
126 | pull( |
127 | ssb.createLogStream({ keys: false }), |
128 | pull.collect(function (err, ary) { |
129 | if(err) throw err |
130 | t.equal(ary.length, 1) |
131 | t.equal(typeof ary[0].content.type, 'string') |
132 | console.log(ary) |
133 | t.end() |
134 | }) |
135 | ) |
136 | }) |
137 | |
138 | }) |
139 | |
140 | tape('live', function (t) { |
141 | t.plan(3) |
142 | |
143 | var db = sublevel(level('test-ssb-log6', level_opts)) |
144 | |
145 | var ssb = require('../')(db, opts) |
146 | |
147 | var feed = createFeed(ssb, generate(), opts) |
148 | |
149 | var ts = Date.now() |
150 | |
151 | pull( |
152 | ssb.createLogStream({ live: true }), |
153 | |
154 | pull.drain(function (op) { |
155 | if(op.sync) return t.ok(true) |
156 | t.ok(op.timestamp > ts) |
157 | t.equal(op.value.content.type, 'msg') |
158 | t.end() |
159 | }) |
160 | ) |
161 | |
162 | |
163 | feed.add('msg', 'hello there!', function (err, msg) { |
164 | if(err) throw err |
165 | }) |
166 | |
167 | }) |
168 | |
169 | } |
170 | |
171 | |
172 | if(!module.parent) |
173 | module.exports(require('../defaults')) |
174 | |
175 | |
176 | |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | |
183 | |
184 | |
185 | |
186 | |
187 | |
188 | |
189 |
Built with git-ssb-web