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

Commit 72b8bc7

Browse files
authored
fix: final trickle dag tests (#27)
* fix: final trickle dag tests * chore: fix linting * chore: increase test timeout for firefox
1 parent 7fbaadd commit 72b8bc7

File tree

5 files changed

+65
-15
lines changed

5 files changed

+65
-15
lines changed

src/dag-builder/file/index.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,19 @@ const reduce = (file, ipld, options) => {
8181
// create a parent node and add all the leaves
8282
const f = new UnixFS('file')
8383

84-
const links = await Promise.all(
85-
leaves.map(async (leaf) => {
84+
const links = leaves
85+
.filter(leaf => {
86+
if (leaf.cid.codec === 'raw' && leaf.node.length) {
87+
return true
88+
}
89+
90+
if (!leaf.unixfs.data && leaf.unixfs.fileSize()) {
91+
return true
92+
}
93+
94+
return Boolean(leaf.unixfs.data.length)
95+
})
96+
.map((leaf) => {
8697
if (leaf.cid.codec === 'raw') {
8798
// node is a leaf buffer
8899
f.addBlockSize(leaf.node.length)
@@ -100,7 +111,6 @@ const reduce = (file, ipld, options) => {
100111

101112
return new DAGLink(leaf.name, leaf.node.size, leaf.cid)
102113
})
103-
)
104114

105115
const node = DAGNode.create(f.marshal(), links)
106116
const cid = await persist(node, ipld, options)

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ module.exports = async function * (source, ipld, options = {}) {
8787
}
8888

8989
// go-ifps trickle dag defaults to unixfs raw leaves, balanced dag defaults to file leaves
90-
if (options.strategy === 'trickle' && !options.leafType) {
90+
if (options.strategy === 'trickle') {
9191
opts.leafType = 'raw'
92+
opts.reduceSingleLeafToSelf = false
9293
}
9394

9495
if (options.format) {

test/fixtures/small.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is a file

test/hash-parity-with-go-ipfs.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ strategies.forEach(strategy => {
4242
})
4343

4444
it('yields the same tree as go-ipfs', async function () {
45-
this.timeout(10 * 1000)
45+
this.timeout(100 * 1000)
4646

4747
const source = [{
4848
path: 'big.dat',

test/importer.spec.js

+48-10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ const baseFiles = {
4141
size: 1258000,
4242
type: 'file',
4343
path: '1.2MiB.txt'
44+
},
45+
'small.txt': {
46+
cid: 'QmZMb7HWpbevpcdhbUV1ZZgdji8vh5uQ13KxczChGrK9Rd',
47+
size: 15,
48+
type: 'file',
49+
path: 'small.txt'
4450
}
4551
}
4652

@@ -53,6 +59,12 @@ const strategyBaseFiles = {
5359
}
5460
}),
5561
trickle: extend({}, baseFiles, {
62+
'200Bytes.txt': {
63+
cid: 'QmY8bwnoKAKvJ8qtyPhWNxSS6sxiGVTJ9VpdQffs2KB5pE',
64+
size: 200,
65+
type: 'file',
66+
path: '200Bytes.txt'
67+
},
5668
'1.2MiB.txt': {
5769
cid: 'QmfAxsHrpaLLuhbqqbo9KQyvQNawMnVSwutYoJed75pnco',
5870
type: 'file'
@@ -62,8 +74,8 @@ const strategyBaseFiles = {
6274

6375
const strategies = [
6476
'flat',
65-
'balanced'
66-
// 'trickle'
77+
'balanced',
78+
'trickle'
6779
]
6880

6981
const strategyOverrides = {
@@ -95,28 +107,52 @@ const strategyOverrides = {
95107
},
96108
trickle: {
97109
'foo-big': {
98-
cid: 'QmPh6KSS7ghTqzgWhaoCiLoHFPF7HGqUxx7q9vcM5HUN4U',
110+
cid: 'QmaKbhFRy9kcCbcwrLsqYHWMiY44BDYkqTCMpAxDdd2du2',
99111
path: 'foo-big',
100112
size: 1334657,
101113
type: 'directory'
102114
},
103115
pim: {
104-
cid: 'QmPAn3G2x2nrq4A1fu2XUpwWtpqG4D1YXFDrU615NHvJbr',
116+
cid: 'QmbWGdnua4YuYpWJb7fE25PRbW9GbKKLqq9Ucmnsg2gxnt',
105117
path: 'pim',
106118
size: 1334923,
107119
type: 'directory'
108120
},
109121
'pam/pum': {
110-
cid: 'QmPAn3G2x2nrq4A1fu2XUpwWtpqG4D1YXFDrU615NHvJbr',
122+
cid: 'QmbWGdnua4YuYpWJb7fE25PRbW9GbKKLqq9Ucmnsg2gxnt',
111123
path: 'pam/pum',
112124
size: 1334923,
113125
type: 'directory'
114126
},
115127
pam: {
116-
cid: 'QmZTJah1xpG9X33ZsPtDEi1tYSHGDqQMRHsGV5xKzAR2j4',
128+
cid: 'QmSuh47G9Qm3PFv1zziojtHxqCjuurSdtWAzxLxoKJPq2U',
117129
path: 'pam',
118130
size: 2669627,
119131
type: 'directory'
132+
},
133+
'200Bytes.txt with raw leaves': {
134+
cid: 'QmagyRwMfYhczYNv5SvcJc8xxXjZQBTTHS2jEqNMva2mYT',
135+
size: 200,
136+
path: '200Bytes.txt',
137+
type: 'file'
138+
},
139+
'foo/bar': {
140+
cid: 'QmTGMxKPzSGNBDp6jhTwnZxGW6w1S9ciyycRJ4b2qcQaHK',
141+
size: 0,
142+
path: 'foo/bar',
143+
type: 'directory'
144+
},
145+
foo: {
146+
cid: 'Qme4A8fZmwfZESappfPcxSMTZVACiEzhHKtYRMuM1hbkDp',
147+
size: 0,
148+
path: 'foo',
149+
type: 'directory'
150+
},
151+
'small.txt': {
152+
cid: 'QmXmZ3qT328JxWtQXqrmvma2FmPp7tMdNiSuYvVJ5QRhKs',
153+
size: 15,
154+
type: 'file',
155+
path: 'small.txt'
120156
}
121157
}
122158
}
@@ -301,7 +337,7 @@ strategies.forEach((strategy) => {
301337

302338
expect(files.length).to.eql(1)
303339

304-
// always yield empty node
340+
// always yield empty file node
305341
expect(files[0].cid.toBaseEncodedString()).to.eql('QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH')
306342
})
307343

@@ -379,12 +415,14 @@ strategies.forEach((strategy) => {
379415
})
380416

381417
it('small file as string (smaller than a chunk)', async () => {
382-
const file = await first(importer([{
383-
path: '200Bytes.txt',
418+
const files = await all(importer([{
419+
path: 'small.txt',
384420
content: 'this is a file\n'
385421
}], ipld, options))
386422

387-
expect(file.cid.toBaseEncodedString()).to.equal('QmZMb7HWpbevpcdhbUV1ZZgdji8vh5uQ13KxczChGrK9Rd')
423+
expectFiles(files, [
424+
'small.txt'
425+
])
388426
})
389427

390428
it('small file (smaller than a chunk) with raw leaves', async () => {

0 commit comments

Comments
 (0)