Files: 6b125d3850c2ca89b9f10f3b93829450bec6e3f5 / test / msg-encoding.js
2092 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 ssbKeys = require('ssb-keys') |
7 | var createFeed = require('ssb-feed') |
8 | var hexpp = require('hexpp') |
9 | |
10 | var generate = ssbKeys.generate |
11 | var hash = ssbKeys.hash |
12 | |
13 | module.exports = function (opts) { |
14 | |
15 | var codec = opts.codec |
16 | |
17 | var content = { |
18 | "type":"post", |
19 | "is":"text", |
20 | "text":"test", |
21 | "timezone":300, |
22 | "rebroadcasts":{ |
23 | "msg": |
24 | '1BHEHMwZlikXB3o1mg+fP3GVo/+Xb7p46u3rqt/hHkA=.blake2s', |
25 | "feed": |
26 | "rbU6CvdwBXxO/fDyoKuRyKxmZYyeb5+l87R9XVkN8bs=.blake2s", |
27 | "timestamp":1414078805677, |
28 | "timezone":300 |
29 | } |
30 | } |
31 | |
32 | var msg = { |
33 | author: hash('TEST_AUTHOR'), |
34 | previous: hash('TEST_PREVIOUS'), |
35 | timestamp: Date.now(), |
36 | sequence: 10, |
37 | content: content |
38 | } |
39 | |
40 | var signed = {} |
41 | |
42 | for(var k in msg) |
43 | signed[k] = msg[k] |
44 | |
45 | signed.signature = new Buffer(64).toString('base64') |
46 | |
47 | tape('Message', function (t) { |
48 | var enc = codec.encode(msg) |
49 | console.log('**** Message ****') |
50 | console.log(hexpp(enc)) |
51 | var o = codec.decode(enc) |
52 | t.deepEqual(o, msg) |
53 | t.end() |
54 | }) |
55 | |
56 | tape('Signed', function (t) { |
57 | var enc = codec.encode(signed) |
58 | console.log('**** Signed ****') |
59 | console.log(hexpp(enc)) |
60 | var o = codec.decode(enc) |
61 | t.deepEqual(o, signed) |
62 | t.end() |
63 | }) |
64 | |
65 | tape('known error case 1', function (t) { |
66 | |
67 | var db = sublevel(level('test-ssb-encoding', { |
68 | valueEncoding: codec |
69 | })) |
70 | var ssb = require('../')(db, opts) |
71 | var feed = createFeed(ssb, generate(), opts) |
72 | |
73 | feed.add(content, function (err, msg) { |
74 | if(err) throw err |
75 | t.assert(!!msg) |
76 | t.assert(!!msg.key) |
77 | t.assert(!!msg.value) |
78 | pull( |
79 | ssb.createFeedStream(), |
80 | pull.collect(function (err, ary) { |
81 | if(err) throw err |
82 | t.equal(ary.length, 1) |
83 | console.log(ary) |
84 | t.end() |
85 | }) |
86 | ) |
87 | }) |
88 | |
89 | }) |
90 | } |
91 | |
92 | |
93 | if(!module.parent) |
94 | module.exports(require('../defaults')) |
95 | |
96 | |
97 |
Built with git-ssb-web