1 | const debug = require('debug')('ssb-blobs')
|
2 | var tape = require('tape')
|
3 | var Blobs = require('../inject')
|
4 | var pull = require('pull-stream')
|
5 | var bitflipper = require('pull-bitflipper')
|
6 | var assert = require('assert')
|
7 |
|
8 | var u = require('./util')
|
9 | var Fake = u.fake
|
10 | var hash = u.hash
|
11 |
|
12 | module.exports = function (createBlobs, createAsync) {
|
13 |
|
14 |
|
15 |
|
16 | tape('legacy calls modern', function (t) {
|
17 | createAsync(function (async) {
|
18 |
|
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 |
|
34 |
|
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 |
|
112 | if(!module.parent) u.tests(module.exports)
|
113 |
|
114 |
|