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