Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit ee304a9

Browse files
committed
chore: add tests for multiple refs, CBOR
1 parent e595bde commit ee304a9

File tree

1 file changed

+102
-22
lines changed

1 file changed

+102
-22
lines changed

src/files-regular/refs-tests.js

Lines changed: 102 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = (createCommon, suiteName, ipfsRefs, options) => {
1212
describe(suiteName, function () {
1313
this.timeout(40 * 1000)
1414

15-
let ipfs, rootCid
15+
let ipfs, pbRootCb, dagRootCid
1616

1717
before(function (done) {
1818
// CI takes longer to instantiate the daemon, so we need to increase the
@@ -30,9 +30,17 @@ module.exports = (createCommon, suiteName, ipfsRefs, options) => {
3030
})
3131

3232
before(function (done) {
33-
loadContent(ipfs, getMockObjects(), (err, cid) => {
33+
loadPbContent(ipfs, getMockObjects(), (err, cid) => {
3434
expect(err).to.not.exist()
35-
rootCid = cid
35+
pbRootCb = cid
36+
done()
37+
})
38+
})
39+
40+
before(function (done) {
41+
loadDagContent(ipfs, getMockObjects(), (err, cid) => {
42+
expect(err).to.not.exist()
43+
dagRootCid = cid
3644
done()
3745
})
3846
})
@@ -55,7 +63,7 @@ module.exports = (createCommon, suiteName, ipfsRefs, options) => {
5563
}
5664

5765
// Call out to IPFS
58-
const p = (path ? path(rootCid) : rootCid)
66+
const p = (path ? path(pbRootCb) : pbRootCb)
5967
ipfsRefs(ipfs)(p, params, (err, refs) => {
6068
if (!done) {
6169
// Already timed out
@@ -84,6 +92,32 @@ module.exports = (createCommon, suiteName, ipfsRefs, options) => {
8492
})
8593
})
8694
}
95+
96+
it('dag refs test', function (done) {
97+
this.timeout(20 * 1000)
98+
99+
// Call out to IPFS
100+
ipfsRefs(ipfs)(`/ipfs/${dagRootCid}`, { recursive: true }, (err, refs) => {
101+
// Check there was no error and the refs match what was expected
102+
expect(err).to.not.exist()
103+
expect(refs.map(r => r.ref).sort()).to.eql([
104+
'QmPDqvcuA4AkhBLBuh2y49yhUB98rCnxPxa3eVNC1kAbSC',
105+
'QmVwtsLUHurA6wUirPSdGeEW5tfBEqenXpeRaqr8XN7bNY',
106+
'QmXGL3ZdYV5rNLCfHe1QsFSQGekRFzgbBu1B3XGZ7DV9fd',
107+
'QmcSVZRN5E814KkPy4EHnftNAR7htbFvVhRKKqFs4FBwDG',
108+
'QmcSVZRN5E814KkPy4EHnftNAR7htbFvVhRKKqFs4FBwDG',
109+
'QmdBcHbK7uDQav8YrHsfKju3EKn48knxjd96KRMFs3gtS9',
110+
'QmeX96opBHZHLySMFoNiWS5msxjyX6rqtr3Rr1u7uxn7zJ',
111+
'Qmf8MwTnY7VdcnF8WcoJ3GB24NmNd1HsGzuEWCtUYDP38x',
112+
'zdpuAkqPgGuEFBFLcixZyFezWw3bsGUWVS6W7c8YhV5sdAc6E',
113+
'zdpuArVVBgigTbs6FdyqFFWUSsXymdruTtCVoboc91L3WTXi1',
114+
'zdpuAsrruPqzPDYs9c1FGNR5Wuyx8on64no6z62SRPv3viHGL',
115+
'zdpuAxTXSfaHaZNed3JG2WvcYNgd64v27ztB2zknrz5noPhz5'
116+
])
117+
118+
done()
119+
})
120+
})
87121
})
88122
}
89123

@@ -121,7 +155,7 @@ function getRefsTests () {
121155
},
122156

123157
'prints files in edges format': {
124-
params: { e: true },
158+
params: { edges: true },
125159
expected: [
126160
'Qmd5MhNjx3NSZm3L2QKG1TFvqkTRbtZwGJinqEfqpfHH7s -> QmYEJ7qQNZUvBnv4SZ3rEbksagaan3sGvnUq948vSG8Z34',
127161
'Qmd5MhNjx3NSZm3L2QKG1TFvqkTRbtZwGJinqEfqpfHH7s -> QmUXzZKa3xhTauLektUiK4GiogHskuz1c57CnnoP4TgYJD',
@@ -161,7 +195,7 @@ function getRefsTests () {
161195

162196
'follows a path with recursion, <hash>/<subdir>': {
163197
path: (cid) => `/ipfs/${cid}/animals`,
164-
params: { format: '<linkname>', r: true },
198+
params: { format: '<linkname>', recursive: true },
165199
expected: [
166200
'land',
167201
'african.txt',
@@ -174,7 +208,7 @@ function getRefsTests () {
174208
},
175209

176210
'recursively follows folders, -r': {
177-
params: { format: '<linkname>', r: true },
211+
params: { format: '<linkname>', recursive: true },
178212
expected: [
179213
'animals',
180214
'land',
@@ -192,7 +226,7 @@ function getRefsTests () {
192226
},
193227

194228
'recursive with unique option': {
195-
params: { format: '<linkname>', r: true, u: true },
229+
params: { format: '<linkname>', recursive: true, unique: true },
196230
expected: [
197231
'animals',
198232
'land',
@@ -209,7 +243,7 @@ function getRefsTests () {
209243
},
210244

211245
'max depth of 1': {
212-
params: { format: '<linkname>', r: true, 'max-depth': 1 },
246+
params: { format: '<linkname>', recursive: true, maxDepth: 1 },
213247
expected: [
214248
'animals',
215249
'atlantic-animals',
@@ -219,7 +253,7 @@ function getRefsTests () {
219253
},
220254

221255
'max depth of 2': {
222-
params: { format: '<linkname>', r: true, 'max-depth': 2 },
256+
params: { format: '<linkname>', recursive: true, maxDepth: 2 },
223257
expected: [
224258
'animals',
225259
'land',
@@ -232,7 +266,7 @@ function getRefsTests () {
232266
},
233267

234268
'max depth of 3': {
235-
params: { format: '<linkname>', r: true, 'max-depth': 3 },
269+
params: { format: '<linkname>', recursive: true, maxDepth: 3 },
236270
expected: [
237271
'animals',
238272
'land',
@@ -250,13 +284,13 @@ function getRefsTests () {
250284
},
251285

252286
'max depth of 0': {
253-
params: { r: true, 'max-depth': 0 },
287+
params: { recursive: true, maxDepth: 0 },
254288
expected: []
255289
},
256290

257291
'follows a path with max depth 1, <hash>/<subdir>': {
258292
path: (cid) => `/ipfs/${cid}/animals`,
259-
params: { format: '<linkname>', r: true, 'max-depth': 1 },
293+
params: { format: '<linkname>', recursive: true, maxDepth: 1 },
260294
expected: [
261295
'land',
262296
'sea'
@@ -265,7 +299,7 @@ function getRefsTests () {
265299

266300
'follows a path with max depth 2, <hash>/<subdir>': {
267301
path: (cid) => `/ipfs/${cid}/animals`,
268-
params: { format: '<linkname>', r: true, 'max-depth': 2 },
302+
params: { format: '<linkname>', recursive: true, maxDepth: 2 },
269303
expected: [
270304
'land',
271305
'african.txt',
@@ -277,8 +311,23 @@ function getRefsTests () {
277311
]
278312
},
279313

314+
'prints refs for multiple paths': {
315+
path: (cid) => [`/ipfs/${cid}/animals`, `/ipfs/${cid}/fruits`],
316+
params: { format: '<linkname>', recursive: true },
317+
expected: [
318+
'land',
319+
'african.txt',
320+
'americas.txt',
321+
'australian.txt',
322+
'sea',
323+
'atlantic.txt',
324+
'indian.txt',
325+
'tropical.txt'
326+
]
327+
},
328+
280329
'cannot specify edges and format': {
281-
params: { format: '<linkname>', e: true },
330+
params: { format: '<linkname>', edges: true },
282331
expectError: true
283332
},
284333

@@ -289,27 +338,58 @@ function getRefsTests () {
289338
}
290339
}
291340

292-
function loadContent (ipfs, node, callback) {
341+
function loadPbContent (ipfs, node, callback) {
342+
const store = {
343+
putData: (data, cb) => ipfs.object.put({ Data: data, Links: [] }, cb),
344+
putLinks: (links, cb) => {
345+
ipfs.object.put({
346+
Data: '',
347+
Links: links.map(({ name, cid }) => ({ Name: name, Hash: cid, Size: 8 }))
348+
}, cb)
349+
}
350+
}
351+
loadContent(ipfs, store, node, callback)
352+
}
353+
354+
function loadDagContent (ipfs, node, callback) {
355+
const store = {
356+
putData: (data, cb) => {
357+
ipfs.add(Buffer.from(data), (err, res) => {
358+
if (err) {
359+
return callback(err)
360+
}
361+
return cb(null, res[0].hash)
362+
})
363+
},
364+
putLinks: (links, cb) => {
365+
const obj = {}
366+
for (const { name, cid } of links) {
367+
obj[name] = { '/': cid }
368+
}
369+
ipfs.dag.put(obj, cb)
370+
}
371+
}
372+
loadContent(ipfs, store, node, callback)
373+
}
374+
375+
function loadContent (ipfs, store, node, callback) {
293376
if (Array.isArray(node)) {
294-
ipfs.object.put({ Data: node.join('\n'), Links: [] }, callback)
377+
return store.putData(node.join('\n'), callback)
295378
}
296379

297380
if (typeof node === 'object') {
298381
const entries = Object.entries(node)
299382
const sorted = entries.sort((a, b) => a[0] > b[0] ? 1 : a[0] < b[0] ? -1 : 0)
300383
map(sorted, ([name, child], cb) => {
301-
loadContent(ipfs, child, (err, cid) => {
384+
loadContent(ipfs, store, child, (err, cid) => {
302385
cb(err, { name, cid: cid && cid.toString() })
303386
})
304387
}, (err, res) => {
305388
if (err) {
306389
return callback(err)
307390
}
308391

309-
ipfs.object.put({
310-
Data: '',
311-
Links: res.map(({ name, cid }) => ({ Name: name, Hash: cid, Size: 8 }))
312-
}, callback)
392+
store.putLinks(res, callback)
313393
})
314394
}
315395
}

0 commit comments

Comments
 (0)