This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
resolver.spec.js
155 lines (135 loc) · 4.83 KB
/
resolver.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* eslint-env mocha */
'use strict'
const loadFixture = require('aegir/fixtures')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const CID = require('cids')
const IpldBitcoin = require('../src/index')
const fixtureBlockHex = loadFixture(__dirname, 'fixtures/block.hex')
const fixtureBlock = Buffer.from(fixtureBlockHex.toString(), 'hex')
const invalidBlock = Buffer.from('abcdef', 'hex')
describe('IPLD format resolver API resolve()', () => {
it('should return the deserialized node if no path is given', (done) => {
IpldBitcoin.resolver.resolve(fixtureBlock, (err, value) => {
expect(err).to.not.exist()
expect(value.remainderPath).is.empty()
expect(value.value).is.not.empty()
done()
})
})
it('should return the deserialized node if the root is requested', (done) => {
IpldBitcoin.resolver.resolve(fixtureBlock, '/', (err, value) => {
expect(err).to.not.exist()
expect(value.remainderPath).is.empty()
expect(value.value).is.not.empty()
done()
})
})
it('should return the version', (done) => {
verifyPath(fixtureBlock, '/version', 2, done)
})
it('should return the timestamp', (done) => {
verifyPath(fixtureBlock, '/timestamp', 1386981279, done)
})
it('should return the difficulty', (done) => {
verifyPath(fixtureBlock, '/difficulty', 419740270, done)
})
it('should return the nonce', (done) => {
verifyPath(fixtureBlock, '/nonce', 3159344128, done)
})
it('should error on non-existent path', (done) => {
verifyError(fixtureBlock, '/something/random', done)
})
it('should error on partially matching path that isn\'t a link', (done) => {
verifyError(fixtureBlock, '/version/but/additional/things', done)
})
it('should return a link when parent is requested', (done) => {
IpldBitcoin.resolver.resolve(fixtureBlock, '/parent', (err, value) => {
expect(err).to.not.exist()
expect(value.remainderPath).is.empty()
expect(value.value).to.deep.equal({
'/': new CID('z4HFzdHLxSgJvCMJrsDtV7MgqiGALZdbbxgcTLVUUXQGBkGYjLb')})
done()
})
})
it('should return a link and remaining path when parent is requested',
(done) => {
IpldBitcoin.resolver.resolve(fixtureBlock, '/parent/timestamp',
(err, value) => {
expect(err).to.not.exist()
expect(value.remainderPath).to.equal('timestamp')
expect(value.value).to.deep.equal({
'/':
new CID('z4HFzdHLxSgJvCMJrsDtV7MgqiGALZdbbxgcTLVUUXQGBkGYjLb')})
done()
})
})
it('should return a link when transactions are requested', (done) => {
IpldBitcoin.resolver.resolve(fixtureBlock, '/tx/some/remainder',
(err, value) => {
expect(err).to.not.exist()
expect(value.remainderPath).to.equal('some/remainder')
expect(value.value).to.deep.equal({
'/': new CID('z4HFzdHD15kVvtmVzeD7z9sisZ7acSC88wXS3KJGwGrnr2DwcVQ')})
done()
})
})
it('should return an error if block is invalid', (done) => {
verifyError(invalidBlock, '/version', done)
})
})
describe('IPLD format resolver API tree()', () => {
it('should return only paths by default', (done) => {
IpldBitcoin.resolver.tree(fixtureBlock, (err, value) => {
expect(err).to.not.exist()
expect(value).to.deep.equal(['/version', '/timestamp', '/difficulty',
'/nonce', '/parent', '/tx'])
done()
})
})
it('should be able to return paths and values', (done) => {
IpldBitcoin.resolver.tree(fixtureBlock, {values: true}, (err, value) => {
expect(err).to.not.exist()
expect(value).to.deep.equal({
'/version': 2,
'/timestamp': 1386981279,
'/difficulty': 419740270,
'/nonce': 3159344128,
'/parent': {
'/': new CID('z4HFzdHLxSgJvCMJrsDtV7MgqiGALZdbbxgcTLVUUXQGBkGYjLb')},
'/tx': {
'/': new CID('z4HFzdHD15kVvtmVzeD7z9sisZ7acSC88wXS3KJGwGrnr2DwcVQ')}})
done()
})
})
it('should return an error if block is invalid', (done) => {
IpldBitcoin.resolver.tree(invalidBlock, (err, value) => {
expect(value).to.not.exist()
expect(err).to.be.an('error')
done()
})
})
})
describe('IPLD format resolver API properties', () => {
it('should have `multicodec` defined correctly', (done) => {
expect(IpldBitcoin.resolver.multicodec).to.equal('bitcoin-block')
done()
})
})
const verifyPath = (block, path, expected, done) => {
IpldBitcoin.resolver.resolve(block, path, (err, value) => {
expect(err).to.not.exist()
expect(value.remainderPath).is.empty()
expect(value.value).is.equal(expected)
done()
})
}
const verifyError = (block, path, done) => {
IpldBitcoin.resolver.resolve(block, path, (err, value) => {
expect(value).to.not.exist()
expect(err).to.be.an('error')
done()
})
}