git ssb

1+

Dominic / secure-scuttlebutt



Tree: 6b125d3850c2ca89b9f10f3b93829450bec6e3f5

Files: 6b125d3850c2ca89b9f10f3b93829450bec6e3f5 / test / links.js

3543 bytesRaw
1'use strict'
2
3var tape = require('tape')
4var level = require('level-test')()
5var sublevel = require('level-sublevel/bytewise')
6var pull = require('pull-stream')
7var ssbKeys = require('ssb-keys')
8var createFeed = require('ssb-feed')
9var cont = require('cont')
10
11function cmpstr (a, b) {
12 return a < b ? -1 : a === b ? 0 : 1
13}
14
15function compare (a, b) {
16 return cmpstr(a.key, b.key) || cmpstr(a.dest, b.dest) || cmpstr(a.rel, b.rel)
17// return a.key < b.key ? -1 : a.key === b.key ? 0 : -1
18}
19
20module.exports = function (opts) {
21 var create = require('ssb-feed/util').create
22
23 var db = sublevel(level('test-ssb-feed', {
24 valueEncoding: opts.codec
25 }))
26
27 var db = require('../')(db, opts)
28
29 var alice = db.createFeed()
30 var bob = db.createFeed()
31
32 var msgs = []
33
34 var from_alice = []
35
36 pull(
37 db.links({source: alice.id, old: false, live: true}),
38 pull.drain(from_alice.push.bind(from_alice))
39 )
40
41 tape('initialize', function (t) {
42
43 cont.para([
44 alice.add({type: 'yo!', yo: alice.id}),
45 alice.add({type: 'contact', follow: bob.id, okay: true}),
46 bob.add({type: 'poke', poke: alice.id}),
47 alice.add({type: 'poke', poke: alice.id})
48 ])(function (err, _msgs) {
49 msgs = _msgs
50 t.notOk(err)
51 bob.add({
52 type: 'post', mentions: [msgs[2].key], text: 'okay then'
53 }, function (err, msg) {
54 msgs.push(msg)
55 t.notOk(err); t.end()
56 })
57 })
58
59 })
60
61
62 tape('query only rel type', function (t) {
63
64 pull(
65 db.links({rel: 'yo'}),
66 pull.through(function (data) {
67 t.ok(data.key)
68 delete data.key
69 }),
70 pull.collect(function (err, ary) {
71 t.notOk(err)
72 t.deepEqual(ary, [{source: alice.id, rel: 'yo', dest: alice.id}])
73 console.log(ary)
74 t.end()
75 })
76 )
77 })
78
79 function createTest (t) {
80 return function test(name, query, results) {
81 t.test(name, function (t) {
82 pull(
83 db.links(query),
84 pull.collect(function (err, ary) {
85 t.notOk(err)
86 t.equal(ary.length, results.length)
87 t.deepEqual(ary, results)
88 t.end()
89 })
90 )
91 })
92 }
93 }
94
95 tape('query by dest', function (t) {
96 var test = createTest(t)
97 var mention = {
98 source: bob.id,
99 rel: 'mentions',
100 dest: msgs[2].key,
101 key: msgs[4].key,
102 }
103 test('equal, query dest: %',
104 {dest: '%'}, [mention])
105 test('equal, query exact dest: %...',
106 {dest: msgs[2].key}, [mention])
107 test('equal, query dest: %..., rel: mentions',
108 {dest: msgs[2].key, rel: 'mentions'}, [mention])
109 })
110
111 tape('realtime', function (t){
112 console.log(from_alice, alice.id)
113 pull(
114 db.links({source: alice.id, old: true}),
115 pull.collect(function (err, ary) {
116 if(err) throw err
117 t.equal(ary.length, 3)
118 t.equal(from_alice.length, 3)
119 t.deepEqual(from_alice.sort(compare), ary.sort(compare))
120 t.end()
121 })
122 )
123 })
124
125 tape('live link values', function (t) {
126 var msg
127 var links = []
128 pull(
129 db.links({old: false, live: true, values: true}),
130 pull.drain(function (data) {
131 t.deepEqual(data,
132 {key: msg.key, value: msg.value,
133 source: alice.id, dest: bob.id, rel: 'foo'})
134 t.end()
135 })
136 )
137
138 alice.publish({type: 'foo', foo: bob.id}, function (err, _msg) {
139 msg = _msg
140 t.error(err, 'publish')
141 })
142 })
143
144}
145
146if(!module.parent)
147 module.exports(require('../defaults'))
148
149

Built with git-ssb-web