1
1
/* eslint-env mocha */
2
2
'use strict'
3
3
4
- const chai = require ( 'chai' )
5
- const dirtyChai = require ( 'dirty-chai' )
6
- const expect = chai . expect
7
- chai . use ( dirtyChai )
4
+ const { expect } = require ( 'aegir/utils/chai' )
8
5
9
6
const loadFixture = require ( 'aegir/fixtures' )
10
- const Ctl = require ( 'ipfsd-ctl' )
7
+ const { createFactory } = require ( 'ipfsd-ctl' )
11
8
const getStream = require ( 'get-stream' )
12
9
const CID = require ( 'cids' )
13
10
const all = require ( 'it-all' )
11
+ const uint8ArrayToString = require ( 'uint8arrays/to-string' )
14
12
15
13
const { getResponse } = require ( '../src' )
16
14
const makeWebResponseEnv = require ( './utils/web-response-env' )
17
15
18
- const factory = Ctl . createFactory ( {
16
+ const factory = createFactory ( {
19
17
test : true ,
20
18
type : 'proc' ,
21
- ipfsModule : {
22
- ref : require ( 'ipfs' ) ,
23
- path : require . resolve ( 'ipfs' )
24
- }
19
+ ipfsModule : require ( 'ipfs' )
25
20
} )
26
21
27
22
describe ( 'resolve file (CIDv0)' , function ( ) {
28
23
let ipfs = null
29
- let ipfsd = null
30
24
31
25
const file = {
32
26
cid : 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' ,
@@ -37,26 +31,24 @@ describe('resolve file (CIDv0)', function () {
37
31
this . timeout ( 20 * 1000 )
38
32
Object . assign ( global , makeWebResponseEnv ( ) )
39
33
40
- ipfsd = await factory . spawn ( )
34
+ const ipfsd = await factory . spawn ( )
41
35
ipfs = ipfsd . api
42
36
43
- const filesAdded = await all ( ipfs . add ( file . data , { cidVersion : 0 } ) )
44
-
45
- expect ( filesAdded ) . to . have . length ( 1 )
46
-
47
- const retrievedFile = filesAdded [ 0 ]
37
+ const retrievedFile = await ipfs . add ( file . data , { cidVersion : 0 } )
48
38
expect ( retrievedFile . cid ) . to . deep . equal ( new CID ( file . cid ) )
49
39
expect ( retrievedFile . size , 'ipfs.add result size should not be smaller than input buffer' ) . greaterThan ( file . data . length )
50
40
} )
51
41
42
+ after ( ( ) => factory . clean ( ) )
43
+
52
44
it ( 'should resolve a CIDv0' , async ( ) => {
53
45
const res = await getResponse ( ipfs , `/ipfs/${ file . cid } ` )
54
46
55
47
expect ( res ) . to . exist ( )
56
48
expect ( res . status ) . to . equal ( 200 )
57
49
58
50
const contents = await getStream ( res . body )
59
- const expectedContents = loadFixture ( 'test/fixtures/testfile.txt' ) . toString ( )
51
+ const expectedContents = uint8ArrayToString ( loadFixture ( 'test/fixtures/testfile.txt' ) )
60
52
61
53
expect ( contents ) . to . equal ( expectedContents )
62
54
} )
@@ -78,23 +70,21 @@ describe('resolve file (CIDv1)', function () {
78
70
ipfsd = await factory . spawn ( )
79
71
ipfs = ipfsd . api
80
72
81
- const filesAdded = await all ( ipfs . add ( file . data , { cidVersion : 1 } ) )
82
-
83
- expect ( filesAdded ) . to . have . length ( 1 )
84
-
85
- const retrievedFile = filesAdded [ 0 ]
73
+ const retrievedFile = await ipfs . add ( file . data , { cidVersion : 1 } )
86
74
expect ( retrievedFile . cid ) . to . deep . equal ( new CID ( file . cid ) )
87
- // expect(retrievedFile.size, 'ipfs.add result size should not be smaller than input buffer').greaterThan (file.data.length)
75
+ expect ( retrievedFile . size , 'ipfs.add result size should equal input buffer' ) . to . equal ( file . data . length )
88
76
} )
89
77
78
+ after ( ( ) => factory . clean ( ) )
79
+
90
80
it ( 'should resolve a CIDv1' , async ( ) => {
91
81
const res = await getResponse ( ipfs , `/ipfs/${ file . cid } ` )
92
82
93
83
expect ( res ) . to . exist ( )
94
84
expect ( res . status ) . to . equal ( 200 )
95
85
96
86
const contents = await getStream ( res . body )
97
- const expectedContents = loadFixture ( 'test/fixtures/testfile.txt' ) . toString ( )
87
+ const expectedContents = uint8ArrayToString ( loadFixture ( 'test/fixtures/testfile.txt' ) )
98
88
99
89
expect ( contents ) . to . equal ( expectedContents )
100
90
} )
@@ -129,7 +119,7 @@ describe('resolve directory (CIDv0)', function () {
129
119
content ( 'holmes.txt' )
130
120
]
131
121
132
- const res = await all ( ipfs . add ( dirs , { cidVersion : 0 } ) )
122
+ const res = await all ( ipfs . addAll ( dirs , { cidVersion : 0 } ) )
133
123
const root = res [ res . length - 1 ]
134
124
135
125
expect ( root . path ) . to . equal ( 'test-folder' )
@@ -139,6 +129,8 @@ describe('resolve directory (CIDv0)', function () {
139
129
expect ( res [ 1 ] . size , 'ipfs.add 2nd result size should not be smaller than 2nd input buffer' ) . greaterThan ( dirs [ 1 ] . content . length )
140
130
} )
141
131
132
+ after ( ( ) => factory . clean ( ) )
133
+
142
134
it ( 'should return the list of files of a directory' , async ( ) => {
143
135
const res = await getResponse ( ipfs , `/ipfs/${ directory . cid } ` , directory . cid )
144
136
@@ -150,7 +142,7 @@ describe('resolve directory (CIDv0)', function () {
150
142
const res = await getResponse ( ipfs , `/ipfs/${ directory . cid } /pp.txt` , directory . cid )
151
143
152
144
const contents = await getStream ( res . body )
153
- const expectedContents = loadFixture ( 'test/fixtures/test-folder/pp.txt' ) . toString ( )
145
+ const expectedContents = uint8ArrayToString ( loadFixture ( 'test/fixtures/test-folder/pp.txt' ) )
154
146
155
147
expect ( contents ) . to . equal ( expectedContents )
156
148
} )
@@ -159,7 +151,7 @@ describe('resolve directory (CIDv0)', function () {
159
151
const res = await getResponse ( ipfs , `/ipfs/${ directory . cid } /holmes.txt` , directory . cid )
160
152
161
153
const contents = await getStream ( res . body )
162
- const expectedContents = loadFixture ( 'test/fixtures/test-folder/holmes.txt' ) . toString ( )
154
+ const expectedContents = uint8ArrayToString ( loadFixture ( 'test/fixtures/test-folder/holmes.txt' ) )
163
155
164
156
expect ( contents ) . to . equal ( expectedContents )
165
157
} )
@@ -172,7 +164,7 @@ describe('resolve directory (CIDv1)', function () {
172
164
const directory = {
173
165
cid : 'bafybeifhimn7nu6dgmdvj6o63zegwro3yznnpfqib6kkjnagc54h46ox5q' ,
174
166
files : {
175
- 'pp.txt' : Buffer . from ( loadFixture ( 'test/fixtures/test-folder/pp.txt' ) ) ,
167
+ 'pp.txt' : loadFixture ( 'test/fixtures/test-folder/pp.txt' ) ,
176
168
'holmes.txt' : loadFixture ( 'test/fixtures/test-folder/holmes.txt' )
177
169
}
178
170
}
@@ -194,14 +186,16 @@ describe('resolve directory (CIDv1)', function () {
194
186
content ( 'holmes.txt' )
195
187
]
196
188
197
- const res = await all ( ipfs . add ( dirs , { cidVersion : 1 } ) )
189
+ const res = await all ( ipfs . addAll ( dirs , { cidVersion : 1 } ) )
198
190
const root = res [ res . length - 1 ]
199
191
expect ( root . path ) . to . equal ( 'test-folder' )
200
192
// expect(res[0].size, 'ipfs.files.add 1st result size should not be smaller than 1st input buffer').greaterThan(dirs[0].content.length)
201
193
// expect(res[1].size, 'ipfs.files.add 2nd result size should not be smaller than 2nd input buffer').greaterThan(dirs[1].content.length)
202
194
expect ( root . cid ) . to . deep . equal ( new CID ( directory . cid ) )
203
195
} )
204
196
197
+ after ( ( ) => factory . clean ( ) )
198
+
205
199
it ( 'should return the list of files of a directory' , async ( ) => {
206
200
const res = await getResponse ( ipfs , `/ipfs/${ directory . cid } ` , directory . cid )
207
201
@@ -213,7 +207,7 @@ describe('resolve directory (CIDv1)', function () {
213
207
const res = await getResponse ( ipfs , `/ipfs/${ directory . cid } /pp.txt` , directory . cid )
214
208
215
209
const contents = await getStream ( res . body )
216
- const expectedContents = loadFixture ( 'test/fixtures/test-folder/pp.txt' ) . toString ( )
210
+ const expectedContents = uint8ArrayToString ( loadFixture ( 'test/fixtures/test-folder/pp.txt' ) )
217
211
218
212
expect ( contents ) . to . equal ( expectedContents )
219
213
} )
@@ -222,7 +216,7 @@ describe('resolve directory (CIDv1)', function () {
222
216
const res = await getResponse ( ipfs , `/ipfs/${ directory . cid } /holmes.txt` , directory . cid )
223
217
224
218
const contents = await getStream ( res . body )
225
- const expectedContents = loadFixture ( 'test/fixtures/test-folder/holmes.txt' ) . toString ( )
219
+ const expectedContents = uint8ArrayToString ( loadFixture ( 'test/fixtures/test-folder/holmes.txt' ) )
226
220
227
221
expect ( contents ) . to . equal ( expectedContents )
228
222
} )
@@ -259,13 +253,15 @@ describe('resolve web page (CIDv0)', function () {
259
253
content ( 'index.html' )
260
254
]
261
255
262
- const res = await all ( ipfs . add ( dirs , { cidVersion : 0 } ) )
256
+ const res = await all ( ipfs . addAll ( dirs , { cidVersion : 0 } ) )
263
257
const root = res [ res . length - 1 ]
264
258
265
259
expect ( root . path ) . to . equal ( 'test-site' )
266
260
expect ( root . cid ) . to . deep . equal ( new CID ( webpage . cid ) )
267
261
} )
268
262
263
+ after ( ( ) => factory . clean ( ) )
264
+
269
265
it ( 'should return the entry point of a web page when a trying to fetch a directory containing a web page' , async ( ) => {
270
266
const res = await getResponse ( ipfs , `/ipfs/${ webpage . cid } ` , webpage . cid )
271
267
@@ -305,13 +301,15 @@ describe('resolve web page (CIDv1)', function () {
305
301
content ( 'index.html' )
306
302
]
307
303
308
- const res = await all ( ipfs . add ( dirs , { cidVersion : 1 } ) )
304
+ const res = await all ( ipfs . addAll ( dirs , { cidVersion : 1 } ) )
309
305
const root = res [ res . length - 1 ]
310
306
311
307
expect ( root . path ) . to . equal ( 'test-site' )
312
308
expect ( root . cid ) . to . deep . equal ( new CID ( webpage . cid ) )
313
309
} )
314
310
311
+ after ( ( ) => factory . clean ( ) )
312
+
315
313
it ( 'should return the entry point of a web page when a trying to fetch a directory containing a web page' , async ( ) => {
316
314
const res = await getResponse ( ipfs , `/ipfs/${ webpage . cid } ` , webpage . cid )
317
315
@@ -356,13 +354,15 @@ describe('mime-types', () => {
356
354
content ( 'index.html' )
357
355
]
358
356
359
- const res = await all ( ipfs . add ( dirs , { cidVersion : 0 } ) )
357
+ const res = await all ( ipfs . addAll ( dirs , { cidVersion : 0 } ) )
360
358
const root = res [ res . length - 1 ]
361
359
362
360
expect ( root . path ) . to . equal ( 'test-mime-types' )
363
361
expect ( root . cid ) . to . deep . equal ( new CID ( webpage . cid ) )
364
362
} )
365
363
364
+ after ( ( ) => factory . clean ( ) )
365
+
366
366
it ( 'should return the correct mime-type for pp.txt' , async ( ) => {
367
367
const res = await getResponse ( ipfs , `/ipfs/${ webpage . cid } /pp.txt` , webpage . cid )
368
368
0 commit comments