Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit dfa0094

Browse files
committed
feat(files.add): update tests to new files add API
1 parent a5ee5d2 commit dfa0094

File tree

2 files changed

+51
-37
lines changed

2 files changed

+51
-37
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@
4747
"greenkeeperio-bot <[email protected]>",
4848
"nginnever <[email protected]>"
4949
]
50-
}
50+
}

src/files.js

+50-36
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,33 @@ module.exports = (common) => {
5353
describe('.add', () => {
5454
it('stream', (done) => {
5555
const buffered = new Buffer('some data')
56+
const expectedMultihash = 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS'
57+
5658
const rs = new Readable()
5759
rs.push(buffered)
5860
rs.push(null)
5961

6062
const arr = []
61-
const filePair = {path: 'data.txt', content: rs}
63+
const filePair = {
64+
path: 'data.txt',
65+
content: rs
66+
}
67+
6268
arr.push(filePair)
6369

6470
ipfs.files.add(arr, (err, res) => {
6571
expect(err).to.not.exist
6672
expect(res).to.be.length(1)
67-
expect(res[0].path).to.equal('data.txt')
68-
expect(res[0].node.size()).to.equal(17)
69-
const mh = 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS'
70-
expect(bs58.encode(res[0].node.multihash()).toString()).to.equal(mh)
73+
const file = res[0]
74+
expect(file).to.be.eql({
75+
path: 'data.txt',
76+
size: 17,
77+
hash: expectedMultihash
78+
})
79+
expect(file).to.eqlk
80+
expect(file.path).to.equal('data.txt')
81+
expect(file.size).to.equal(17)
82+
expect(file.hash).to.equal(expectedMultihash)
7183
done()
7284
})
7385
})
@@ -77,41 +89,40 @@ module.exports = (common) => {
7789
path: 'testfile.txt',
7890
content: smallFile
7991
}
92+
const expectedMultihash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
8093

8194
ipfs.files.add([file], (err, res) => {
8295
expect(err).to.not.exist
8396

84-
const added = res[0] != null ? res[0] : res
85-
const mh = bs58.encode(added.node.multihash()).toString()
86-
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
87-
expect(added.path).to.equal('testfile.txt')
88-
expect(added.node.links).to.have.length(0)
97+
const file = res[0]
98+
expect(file.hash).to.equal(expectedMultihash)
99+
expect(file.path).to.equal('testfile.txt')
89100
done()
90101
})
91102
})
92103

93104
it('buffer', (done) => {
105+
const expectedMultihash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
106+
94107
ipfs.files.add(smallFile, (err, res) => {
95108
expect(err).to.not.exist
96-
97109
expect(res).to.have.length(1)
98-
const mh = bs58.encode(res[0].node.multihash()).toString()
99-
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
100-
expect(res[0].path).to.equal(mh)
101-
expect(res[0].node.links).to.have.length(0)
110+
const file = res[0]
111+
expect(file.hash).to.equal(expectedMultihash)
112+
expect(file.path).to.equal(file.hash)
102113
done()
103114
})
104115
})
105116

106117
it('BIG buffer', (done) => {
118+
const expectedMultihash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq'
119+
107120
ipfs.files.add(bigFile, (err, res) => {
108121
expect(err).to.not.exist
109-
110122
expect(res).to.have.length(1)
111-
expect(res[0].node.links).to.have.length(58)
112-
const mh = bs58.encode(res[0].node.multihash()).toString()
113-
expect(res[0].path).to.equal(mh)
114-
expect(mh).to.equal('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq')
123+
const file = res[0]
124+
expect(file.hash).to.equal(expectedMultihash)
125+
expect(file.path).to.equal(file.hash)
115126
done()
116127
})
117128
})
@@ -121,9 +132,13 @@ module.exports = (common) => {
121132
path: `test-folder/${name}`,
122133
content: directoryContent[name]
123134
})
135+
124136
const emptyDir = (name) => ({
125137
path: `test-folder/${name}`
126138
})
139+
140+
const expectedRootMultihash = 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP'
141+
127142
const dirs = [
128143
content('pp.txt'),
129144
content('holmes.txt'),
@@ -137,13 +152,10 @@ module.exports = (common) => {
137152

138153
ipfs.files.add(dirs, (err, res) => {
139154
expect(err).to.not.exist
155+
const root = res[res.length - 1]
140156

141-
const added = res[res.length - 1]
142-
const mh = bs58.encode(added.node.multihash()).toString()
143-
expect(added.node.links).to.have.length(6)
144-
expect(added.path).to.equal('test-folder')
145-
expect(mh).to.equal('QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP')
146-
157+
expect(root.path).to.equal('test-folder')
158+
expect(root.hash).to.equal(expectedRootMultihash)
147159
done()
148160
})
149161
})
@@ -154,9 +166,13 @@ module.exports = (common) => {
154166
path: `test-folder/${name}`,
155167
content: directoryContent[name]
156168
})
169+
157170
const emptyDir = (name) => ({
158171
path: `test-folder/${name}`
159172
})
173+
174+
const expectedRootMultihash = 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP'
175+
160176
const files = [
161177
content('pp.txt'),
162178
content('holmes.txt'),
@@ -171,11 +187,9 @@ module.exports = (common) => {
171187
ipfs.files.createAddStream((err, stream) => {
172188
expect(err).to.not.exist
173189

174-
stream.on('data', (tuple) => {
175-
if (tuple.path === 'test-folder') {
176-
const mh = bs58.encode(tuple.node.multihash()).toString()
177-
expect(mh).to.equal('QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP')
178-
expect(tuple.node.links).to.have.length(6)
190+
stream.on('data', (file) => {
191+
if (file.path === 'test-folder') {
192+
expect(file.hash).to.equal(expectedRootMultihash)
179193
}
180194
})
181195

@@ -232,14 +246,14 @@ module.exports = (common) => {
232246

233247
describe('promise API', () => {
234248
describe('.add', () => {
249+
const expectedMultihash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
250+
235251
it('buffer', () => {
236252
return ipfs.files.add(smallFile)
237253
.then((res) => {
238-
const added = res[0] != null ? res[0] : res
239-
const mh = bs58.encode(added.node.multihash()).toString()
240-
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
241-
expect(added.path).to.equal(mh)
242-
expect(added.node.links).to.have.length(0)
254+
const file = res[0]
255+
expect(file.hash).to.equal(expectedMultihash)
256+
expect(file.path).to.equal(file.hash)
243257
})
244258
})
245259
})

0 commit comments

Comments
 (0)