-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathexporter-subtree.spec.js
143 lines (116 loc) · 4.55 KB
/
exporter-subtree.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
/* eslint-env mocha */
'use strict'
const { Buffer } = require('buffer')
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const IPLD = require('ipld')
const inMemory = require('ipld-in-memory')
const importer = require('ipfs-unixfs-importer')
const mc = require('multicodec')
const all = require('it-all')
const last = require('it-last')
const blockApi = require('./helpers/block')
const randomBytes = require('it-buffer-stream')
const ONE_MEG = Math.pow(1024, 2)
const exporter = require('./../src')
describe('exporter subtree', () => {
let ipld
let block
before(async () => {
ipld = await inMemory(IPLD)
block = blockApi(ipld)
})
it('exports a file 2 levels down', async () => {
const content = Buffer.concat(await all(randomBytes(ONE_MEG)))
const imported = await last(importer([{
path: './200Bytes.txt',
content: randomBytes(ONE_MEG)
}, {
path: './level-1/200Bytes.txt',
content
}], block))
const exported = await exporter(`${imported.cid.toBaseEncodedString()}/level-1/200Bytes.txt`, ipld)
expect(exported).to.have.property('cid')
expect(exported.name).to.equal('200Bytes.txt')
expect(exported.path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1/200Bytes.txt`)
const data = Buffer.concat(await all(exported.content()))
expect(data).to.deep.equal(content)
})
it('exports a directory 1 level down', async () => {
const content = Buffer.concat(await all(randomBytes(ONE_MEG)))
const imported = await last(importer([{
path: './200Bytes.txt',
content: randomBytes(ONE_MEG)
}, {
path: './level-1/200Bytes.txt',
content
}, {
path: './level-1/level-2'
}], block))
const exported = await exporter(`${imported.cid.toBaseEncodedString()}/level-1`, ipld)
const files = await all(exported.content())
expect(files.length).to.equal(2)
expect(files[0].name).to.equal('200Bytes.txt')
expect(files[0].path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1/200Bytes.txt`)
expect(files[1].name).to.equal('level-2')
expect(files[1].path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1/level-2`)
const data = Buffer.concat(await all(files[0].content()))
expect(data).to.deep.equal(content)
})
it('exports a non existing file from a directory', async () => {
const imported = await last(importer([{
path: '/derp/200Bytes.txt',
content: randomBytes(ONE_MEG)
}], block))
try {
await exporter(`${imported.cid.toBaseEncodedString()}/doesnotexist`, ipld)
} catch (err) {
expect(err.code).to.equal('ERR_NOT_FOUND')
}
})
it('exports starting from non-protobuf node', async () => {
const content = Buffer.concat(await all(randomBytes(ONE_MEG)))
const imported = await last(importer([{
path: './level-1/200Bytes.txt',
content
}], block, {
wrapWithDirectory: true
}))
const cborNodeCid = await ipld.put({
a: {
file: imported.cid
}
}, mc.DAG_CBOR)
const exported = await exporter(`${cborNodeCid.toBaseEncodedString()}/a/file/level-1/200Bytes.txt`, ipld)
expect(exported.name).to.equal('200Bytes.txt')
expect(exported.path).to.equal(`${cborNodeCid.toBaseEncodedString()}/a/file/level-1/200Bytes.txt`)
const data = Buffer.concat(await all(exported.content()))
expect(data).to.deep.equal(content)
})
it('uses .path to export all components of a path', async () => {
const content = Buffer.concat(await all(randomBytes(ONE_MEG)))
const imported = await last(importer([{
path: './200Bytes.txt',
content: randomBytes(ONE_MEG)
}, {
path: './level-1/200Bytes.txt',
content
}, {
path: './level-1/level-2'
}, {
path: './level-1/level-2/200Bytes.txt',
content
}], block))
const exported = await all(exporter.path(`${imported.cid.toBaseEncodedString()}/level-1/level-2/200Bytes.txt`, ipld))
expect(exported.length).to.equal(4)
expect(exported[0].path).to.equal(imported.cid.toBaseEncodedString())
expect(exported[0].name).to.equal(imported.cid.toBaseEncodedString())
expect(exported[1].path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1`)
expect(exported[1].name).to.equal('level-1')
expect(exported[2].path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1/level-2`)
expect(exported[2].name).to.equal('level-2')
expect(exported[3].path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1/level-2/200Bytes.txt`)
expect(exported[3].name).to.equal('200Bytes.txt')
})
})