11/* eslint-env mocha */
22'use strict'
33
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' )
85
96const loadFixture = require ( 'aegir/fixtures' )
10- const Ctl = require ( 'ipfsd-ctl' )
7+ const { createFactory } = require ( 'ipfsd-ctl' )
118const getStream = require ( 'get-stream' )
129const CID = require ( 'cids' )
1310const all = require ( 'it-all' )
11+ const uint8ArrayToString = require ( 'uint8arrays/to-string' )
1412
1513const { getResponse } = require ( '../src' )
1614const makeWebResponseEnv = require ( './utils/web-response-env' )
1715
18- const factory = Ctl . createFactory ( {
16+ const factory = createFactory ( {
1917 test : true ,
2018 type : 'proc' ,
21- ipfsModule : {
22- ref : require ( 'ipfs' ) ,
23- path : require . resolve ( 'ipfs' )
24- }
19+ ipfsModule : require ( 'ipfs' )
2520} )
2621
2722describe ( 'resolve file (CIDv0)' , function ( ) {
2823 let ipfs = null
29- let ipfsd = null
3024
3125 const file = {
3226 cid : 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' ,
@@ -37,26 +31,24 @@ describe('resolve file (CIDv0)', function () {
3731 this . timeout ( 20 * 1000 )
3832 Object . assign ( global , makeWebResponseEnv ( ) )
3933
40- ipfsd = await factory . spawn ( )
34+ const ipfsd = await factory . spawn ( )
4135 ipfs = ipfsd . api
4236
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 } )
4838 expect ( retrievedFile . cid ) . to . deep . equal ( new CID ( file . cid ) )
4939 expect ( retrievedFile . size , 'ipfs.add result size should not be smaller than input buffer' ) . greaterThan ( file . data . length )
5040 } )
5141
42+ after ( ( ) => factory . clean ( ) )
43+
5244 it ( 'should resolve a CIDv0' , async ( ) => {
5345 const res = await getResponse ( ipfs , `/ipfs/${ file . cid } ` )
5446
5547 expect ( res ) . to . exist ( )
5648 expect ( res . status ) . to . equal ( 200 )
5749
5850 const contents = await getStream ( res . body )
59- const expectedContents = loadFixture ( 'test/fixtures/testfile.txt' ) . toString ( )
51+ const expectedContents = uint8ArrayToString ( loadFixture ( 'test/fixtures/testfile.txt' ) )
6052
6153 expect ( contents ) . to . equal ( expectedContents )
6254 } )
@@ -78,23 +70,21 @@ describe('resolve file (CIDv1)', function () {
7870 ipfsd = await factory . spawn ( )
7971 ipfs = ipfsd . api
8072
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 } )
8674 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 )
8876 } )
8977
78+ after ( ( ) => factory . clean ( ) )
79+
9080 it ( 'should resolve a CIDv1' , async ( ) => {
9181 const res = await getResponse ( ipfs , `/ipfs/${ file . cid } ` )
9282
9383 expect ( res ) . to . exist ( )
9484 expect ( res . status ) . to . equal ( 200 )
9585
9686 const contents = await getStream ( res . body )
97- const expectedContents = loadFixture ( 'test/fixtures/testfile.txt' ) . toString ( )
87+ const expectedContents = uint8ArrayToString ( loadFixture ( 'test/fixtures/testfile.txt' ) )
9888
9989 expect ( contents ) . to . equal ( expectedContents )
10090 } )
@@ -129,7 +119,7 @@ describe('resolve directory (CIDv0)', function () {
129119 content ( 'holmes.txt' )
130120 ]
131121
132- const res = await all ( ipfs . add ( dirs , { cidVersion : 0 } ) )
122+ const res = await all ( ipfs . addAll ( dirs , { cidVersion : 0 } ) )
133123 const root = res [ res . length - 1 ]
134124
135125 expect ( root . path ) . to . equal ( 'test-folder' )
@@ -139,6 +129,8 @@ describe('resolve directory (CIDv0)', function () {
139129 expect ( res [ 1 ] . size , 'ipfs.add 2nd result size should not be smaller than 2nd input buffer' ) . greaterThan ( dirs [ 1 ] . content . length )
140130 } )
141131
132+ after ( ( ) => factory . clean ( ) )
133+
142134 it ( 'should return the list of files of a directory' , async ( ) => {
143135 const res = await getResponse ( ipfs , `/ipfs/${ directory . cid } ` , directory . cid )
144136
@@ -150,7 +142,7 @@ describe('resolve directory (CIDv0)', function () {
150142 const res = await getResponse ( ipfs , `/ipfs/${ directory . cid } /pp.txt` , directory . cid )
151143
152144 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' ) )
154146
155147 expect ( contents ) . to . equal ( expectedContents )
156148 } )
@@ -159,7 +151,7 @@ describe('resolve directory (CIDv0)', function () {
159151 const res = await getResponse ( ipfs , `/ipfs/${ directory . cid } /holmes.txt` , directory . cid )
160152
161153 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' ) )
163155
164156 expect ( contents ) . to . equal ( expectedContents )
165157 } )
@@ -172,7 +164,7 @@ describe('resolve directory (CIDv1)', function () {
172164 const directory = {
173165 cid : 'bafybeifhimn7nu6dgmdvj6o63zegwro3yznnpfqib6kkjnagc54h46ox5q' ,
174166 files : {
175- 'pp.txt' : Buffer . from ( loadFixture ( 'test/fixtures/test-folder/pp.txt' ) ) ,
167+ 'pp.txt' : loadFixture ( 'test/fixtures/test-folder/pp.txt' ) ,
176168 'holmes.txt' : loadFixture ( 'test/fixtures/test-folder/holmes.txt' )
177169 }
178170 }
@@ -194,14 +186,16 @@ describe('resolve directory (CIDv1)', function () {
194186 content ( 'holmes.txt' )
195187 ]
196188
197- const res = await all ( ipfs . add ( dirs , { cidVersion : 1 } ) )
189+ const res = await all ( ipfs . addAll ( dirs , { cidVersion : 1 } ) )
198190 const root = res [ res . length - 1 ]
199191 expect ( root . path ) . to . equal ( 'test-folder' )
200192 // expect(res[0].size, 'ipfs.files.add 1st result size should not be smaller than 1st input buffer').greaterThan(dirs[0].content.length)
201193 // expect(res[1].size, 'ipfs.files.add 2nd result size should not be smaller than 2nd input buffer').greaterThan(dirs[1].content.length)
202194 expect ( root . cid ) . to . deep . equal ( new CID ( directory . cid ) )
203195 } )
204196
197+ after ( ( ) => factory . clean ( ) )
198+
205199 it ( 'should return the list of files of a directory' , async ( ) => {
206200 const res = await getResponse ( ipfs , `/ipfs/${ directory . cid } ` , directory . cid )
207201
@@ -213,7 +207,7 @@ describe('resolve directory (CIDv1)', function () {
213207 const res = await getResponse ( ipfs , `/ipfs/${ directory . cid } /pp.txt` , directory . cid )
214208
215209 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' ) )
217211
218212 expect ( contents ) . to . equal ( expectedContents )
219213 } )
@@ -222,7 +216,7 @@ describe('resolve directory (CIDv1)', function () {
222216 const res = await getResponse ( ipfs , `/ipfs/${ directory . cid } /holmes.txt` , directory . cid )
223217
224218 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' ) )
226220
227221 expect ( contents ) . to . equal ( expectedContents )
228222 } )
@@ -259,13 +253,15 @@ describe('resolve web page (CIDv0)', function () {
259253 content ( 'index.html' )
260254 ]
261255
262- const res = await all ( ipfs . add ( dirs , { cidVersion : 0 } ) )
256+ const res = await all ( ipfs . addAll ( dirs , { cidVersion : 0 } ) )
263257 const root = res [ res . length - 1 ]
264258
265259 expect ( root . path ) . to . equal ( 'test-site' )
266260 expect ( root . cid ) . to . deep . equal ( new CID ( webpage . cid ) )
267261 } )
268262
263+ after ( ( ) => factory . clean ( ) )
264+
269265 it ( 'should return the entry point of a web page when a trying to fetch a directory containing a web page' , async ( ) => {
270266 const res = await getResponse ( ipfs , `/ipfs/${ webpage . cid } ` , webpage . cid )
271267
@@ -305,13 +301,15 @@ describe('resolve web page (CIDv1)', function () {
305301 content ( 'index.html' )
306302 ]
307303
308- const res = await all ( ipfs . add ( dirs , { cidVersion : 1 } ) )
304+ const res = await all ( ipfs . addAll ( dirs , { cidVersion : 1 } ) )
309305 const root = res [ res . length - 1 ]
310306
311307 expect ( root . path ) . to . equal ( 'test-site' )
312308 expect ( root . cid ) . to . deep . equal ( new CID ( webpage . cid ) )
313309 } )
314310
311+ after ( ( ) => factory . clean ( ) )
312+
315313 it ( 'should return the entry point of a web page when a trying to fetch a directory containing a web page' , async ( ) => {
316314 const res = await getResponse ( ipfs , `/ipfs/${ webpage . cid } ` , webpage . cid )
317315
@@ -356,13 +354,15 @@ describe('mime-types', () => {
356354 content ( 'index.html' )
357355 ]
358356
359- const res = await all ( ipfs . add ( dirs , { cidVersion : 0 } ) )
357+ const res = await all ( ipfs . addAll ( dirs , { cidVersion : 0 } ) )
360358 const root = res [ res . length - 1 ]
361359
362360 expect ( root . path ) . to . equal ( 'test-mime-types' )
363361 expect ( root . cid ) . to . deep . equal ( new CID ( webpage . cid ) )
364362 } )
365363
364+ after ( ( ) => factory . clean ( ) )
365+
366366 it ( 'should return the correct mime-type for pp.txt' , async ( ) => {
367367 const res = await getResponse ( ipfs , `/ipfs/${ webpage . cid } /pp.txt` , webpage . cid )
368368
0 commit comments