UNPKG

2.69 kBJavaScriptView Raw
1const debug = require('debug')('ssb-blobs')
2var tape = require('tape')
3var Blobs = require('../inject')
4var pull = require('pull-stream')
5var bitflipper = require('pull-bitflipper')
6var assert = require('assert')
7
8var u = require('./util')
9var Fake = u.fake
10var hash = u.hash
11
12module.exports = function (createBlobs, createAsync) {
13
14 //client is legacy. call has on a peer, should emit want({<id>: -2})
15
16 tape('legacy calls modern', function (t) {
17 createAsync(function (async) {
18 //legacy tests
19
20 var n = 0
21
22 var modern = createBlobs('modern', async)
23
24 var blob = Fake('foo', 100)
25 var h = hash(blob)
26
27 var first = {}
28 first[h] = -2
29 var second = {}
30 second[h] = blob.length
31 var expected = [{}, first, second]
32
33 //the most important thing is that a modern blobs
34 //plugin emits 2nd hand hops when someone calls has(hash)
35
36 pull(
37 modern.createWants(),
38 pull.drain(function (req) {
39 n++
40 assert.deepEqual(req, expected.shift())
41 })
42 )
43
44 pull(modern.changes(), pull.drain(function (hash) {
45 assert.equal(hash, h)
46 pull(modern.get(hash), pull.collect(function (err, ary) {
47 assert.deepEqual(Buffer.concat(ary), blob)
48 assert.equal(n, 3)
49 async.done()
50 }))
51 }))
52
53 modern.has.call({id: 'other'}, h, function (err, value) {
54 if(err) throw err
55 t.equal(value, false)
56 pull(pull.once(blob), modern.add(function (err, hash) {
57 if(err) throw err
58 }))
59 })
60
61 }, function (err) {
62 if(err) throw err
63 t.end()
64 })
65
66 })
67
68 tape('modern calls legacy', function (t) {
69 createAsync(function (async) {
70
71 var modern = createBlobs('modern', async)
72 var legacy = createBlobs('legacy', async)
73
74 var size = legacy.size
75 legacy.size = function (hashes, cb) {
76 debug("CALLED_SIZE", hashes)
77 size.call(this, hashes, function (err, value) {
78 debug('SIZES', err, value)
79 cb(err, value)
80 })
81 }
82
83 legacy.createWants = function () {
84 var err = new Error('cannot call apply of null')
85 err.name = 'TypeError'
86 return pull.error(err)
87 }
88
89 u.peers('modern', modern, 'legacy', legacy)
90
91 var blob = Fake('bar', 101)
92 var h = hash(blob)
93
94 modern.want(h, function (err, has) {
95 async.done()
96 })
97
98 pull(pull.once(blob), legacy.add(function (err, _h) {
99 assert.equal(_h, h)
100 debug('ADDED', _h)
101 }))
102
103 }, function (err) {
104 debug(err)
105 if(err) throw err
106 t.end()
107 })
108 })
109
110}
111
112if(!module.parent) u.tests(module.exports)
113
114