This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
resolver.spec.js
242 lines (211 loc) · 8.01 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* eslint-env mocha */
'use strict'
const chai = require('aegir/utils/chai')
const expect = chai.expect
const CID = require('cids')
const { resolver } = require('../src')
const DAGNode = require('../src/dag-node/dagNode')
const utils = require('../src/util')
const uint8ArrayFromString = require('uint8arrays/from-string')
/**
* @typedef {import('../src/dag-link/dagLink')} DAGLink
* @typedef {import('../src/types').DAGLinkLike} DAGLinkLike
*/
describe('IPLD Format resolver (local)', () => {
const links = [{
Name: '',
Hash: new CID('QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U'),
Tsize: 10
}, {
Name: 'named link',
Hash: new CID('QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V'),
Tsize: 8
}]
/**
* @param {Uint8Array} data
* @param {(DAGLink | DAGLinkLike)[]} links
*/
const create = (data, links) => {
const node = new DAGNode(data, links)
return utils.serialize(node)
}
/**
* @param {Uint8Array} data
* @param {(DAGLink | DAGLinkLike)[]} links
*/
const createPlain = (data, links) => {
const node = {
Data: data,
Links: links
}
return utils.serialize(node)
}
const emptyNodeBlobs = [
{ kind: 'DAGNode', blob: create(new Uint8Array(), []) },
{ kind: '{data:Uint8Array}', blob: createPlain(new Uint8Array(), []) }
]
const linksNodeBlobs = [
{ kind: 'DAGNode', blob: create(new Uint8Array(), links) },
{ kind: '{data:Uint8Array}', blob: createPlain(new Uint8Array(), links) }
]
const dataLinksNodeBlobs = [
{ kind: 'DAGNode', blob: create(uint8ArrayFromString('aaah the data'), links) },
{ kind: '{data:Uint8Array}', blob: createPlain(uint8ArrayFromString('aaah the data'), links) }
]
for (const { kind, blob } of emptyNodeBlobs) {
describe(`empty node (${kind})`, () => {
describe('resolver.resolve', () => {
it('links path', () => {
const result = resolver.resolve(blob, 'Links')
expect(result).to.have.deep.property('value', [])
expect(result).to.have.property('remainderPath', '')
})
it('data path', () => {
const result = resolver.resolve(blob, 'Data')
expect(result).to.have.property('value').that.is.an.instanceOf(Uint8Array).with.lengthOf(0)
expect(result).to.have.property('remainderPath', '')
})
it('non existent path', () => {
expect(() =>
resolver.resolve(blob, 'pathThatDoesNotExist')
).to.throw(
"Object has no property 'pathThatDoesNotExist'"
)
})
it('empty path', () => {
const result = resolver.resolve(blob, '')
expect(result).to.have.nested.property('value.Data').that.is.an.instanceOf(Uint8Array).with.lengthOf(0)
expect(result).to.have.deep.nested.property('value.Links', [])
expect(result).to.have.property('remainderPath', '')
})
})
it('resolver.tree', () => {
const tree = resolver.tree(blob)
const paths = [...tree]
expect(paths).to.have.members([
'Links',
'Data'
])
})
})
}
for (const { kind, blob } of linksNodeBlobs) {
describe(`links node ${kind}`, () => {
describe('resolver.resolve', () => {
it('links path', () => {
const result = resolver.resolve(blob, 'Links')
expect(result).to.have.property('value').that.containSubset(links)
expect(result).to.have.property('remainderPath', '')
})
it('links position path Hash', () => {
const result = resolver.resolve(blob, 'Links/1/Hash')
expect(result).to.have.deep.property('value', links[1].Hash)
expect(result).to.have.property('remainderPath', '')
})
it('links position path Name', () => {
const result = resolver.resolve(blob, 'Links/1/Name')
expect(result).to.have.property('value', links[1].Name)
expect(result).to.have.property('remainderPath', '')
})
it('links position path Tsize', () => {
const result = resolver.resolve(blob, 'Links/1/Tsize')
expect(result).to.have.property('value', links[1].Tsize)
expect(result).to.have.property('remainderPath', '')
})
it('links by name', () => {
const result = resolver.resolve(blob, 'named link')
expect(result).to.have.deep.property('value', links[1].Hash)
expect(result).to.have.property('remainderPath', '')
})
it('missing link by name', () => {
expect(() =>
resolver.resolve(blob, 'missing link')
).to.throw(
"Object has no property 'missing link'"
)
})
it('yield remainderPath if impossible to resolve through (a)', () => {
const result = resolver.resolve(blob, 'Links/1/Hash/Data')
expect(result).to.have.deep.property('value', new CID('QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V'))
expect(result).to.have.property('remainderPath', 'Data')
})
it('yield remainderPath if impossible to resolve through (b)', () => {
const result = resolver.resolve(blob, 'Links/1/Hash/Links/0/Hash/Data')
expect(result).to.have.deep.property('value', new CID('QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V'))
expect(result).to.have.property('remainderPath', 'Links/0/Hash/Data')
})
it('yield remainderPath if impossible to resolve through named link (a)', () => {
const result = resolver.resolve(blob, 'named link/Data')
expect(result).to.have.deep.property('value', new CID('QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V'))
expect(result).to.have.property('remainderPath', 'Data')
})
it('yield remainderPath if impossible to resolve through named link (b)', () => {
const result = resolver.resolve(blob, 'named link/Links/0/Hash/Data')
expect(result).to.have.deep.property('value', new CID('QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V'))
expect(result).to.have.property('remainderPath', 'Links/0/Hash/Data')
})
})
it('resolver.tree', () => {
const tree = resolver.tree(blob)
const paths = [...tree]
expect(paths).to.have.members([
'Links',
'Links/0',
'Links/0/Name',
'Links/0/Tsize',
'Links/0/Hash',
'Links/1',
'Links/1/Name',
'Links/1/Tsize',
'Links/1/Hash',
'Data'
])
})
})
}
for (const { kind, blob } of dataLinksNodeBlobs) {
describe(`links and data node (${kind})`, () => {
describe('resolver.resolve', () => {
it('links path', () => {
const result = resolver.resolve(blob, 'Links')
expect(result.value).to.containSubset(links)
expect(result.remainderPath).to.eql('')
})
it('data path', () => {
const result = resolver.resolve(blob, 'Data')
expect(result.value).to.eql(uint8ArrayFromString('aaah the data'))
expect(result.remainderPath).to.eql('')
})
it('non existent path', () => {
expect(() =>
resolver.resolve(blob, 'pathThatDoesNotExist')
).to.throw(
"Object has no property 'pathThatDoesNotExist'"
)
})
it('empty path', () => {
const result = resolver.resolve(blob, '')
expect(result).to.have.deep.nested.property('value.Data', uint8ArrayFromString('aaah the data'))
expect(result).to.have.nested.property('value.Links').that.containSubset(links)
expect(result.remainderPath).to.eql('')
})
})
it('resolver.tree', () => {
const tree = resolver.tree(blob)
const paths = [...tree]
expect(paths).to.have.members([
'Links',
'Links/0',
'Links/0/Name',
'Links/0/Tsize',
'Links/0/Hash',
'Links/1',
'Links/1/Name',
'Links/1/Tsize',
'Links/1/Hash',
'Data'
])
})
})
}
})