11/* eslint-env mocha */
22'use strict'
33
4- const chai = require ( 'chai' )
5- chai . use ( require ( 'dirty-chai' ) )
6- const expect = chai . expect
4+ const { expect } = require ( 'aegir/utils/chai' )
75
86const Block = require ( 'ipld-block' )
97const _ = require ( 'lodash' )
108const { collect } = require ( 'streaming-iterables' )
119const CID = require ( 'cids' )
1210const multihashing = require ( 'multihashing-async' )
11+ const uint8ArrayFromString = require ( 'uint8arrays/from-string' )
12+ const drain = require ( 'it-drain' )
1313
1414const BlockService = require ( '../src' )
1515
@@ -22,10 +22,10 @@ module.exports = (repo) => {
2222 bs = new BlockService ( repo )
2323
2424 const data = [
25- Buffer . from ( '1' ) ,
26- Buffer . from ( '2' ) ,
27- Buffer . from ( '3' ) ,
28- Buffer . from ( 'A random data block' )
25+ uint8ArrayFromString ( '1' ) ,
26+ uint8ArrayFromString ( '2' ) ,
27+ uint8ArrayFromString ( '3' ) ,
28+ uint8ArrayFromString ( 'A random data block' )
2929 ]
3030
3131 testBlocks = await Promise . all ( data . map ( async ( d ) => {
@@ -53,8 +53,16 @@ module.exports = (repo) => {
5353 }
5454 } )
5555
56- it ( 'store many blocks' , ( ) => {
57- return bs . putMany ( testBlocks )
56+ it ( 'store many blocks' , async ( ) => {
57+ await drain ( bs . putMany ( testBlocks ) )
58+
59+ expect (
60+ await Promise . all (
61+ testBlocks . map ( b => bs . get ( b . cid ) )
62+ )
63+ ) . to . deep . equal (
64+ testBlocks
65+ )
5866 } )
5967
6068 it ( 'get many blocks through .get' , async ( ) => {
@@ -69,7 +77,7 @@ module.exports = (repo) => {
6977 } )
7078
7179 it ( 'delete a block' , async ( ) => {
72- const data = Buffer . from ( 'Will not live that much' )
80+ const data = uint8ArrayFromString ( 'Will not live that much' )
7381
7482 const hash = await multihashing ( data , 'sha2-256' )
7583 const b = new Block ( data , new CID ( hash ) )
@@ -81,7 +89,7 @@ module.exports = (repo) => {
8189 } )
8290
8391 it ( 'does not delete a block it does not have' , async ( ) => {
84- const data = Buffer . from ( 'Will not live that much ' + Date . now ( ) )
92+ const data = uint8ArrayFromString ( 'Will not live that much ' + Date . now ( ) )
8593 const cid = new CID ( await multihashing ( data , 'sha2-256' ) )
8694
8795 await bs . delete ( cid )
@@ -92,41 +100,37 @@ module.exports = (repo) => {
92100 } )
93101
94102 it ( 'deletes lots of blocks' , async ( ) => {
95- const data = Buffer . from ( 'Will not live that much' )
103+ const data = uint8ArrayFromString ( 'Will not live that much' )
96104
97105 const hash = await multihashing ( data , 'sha2-256' )
98106 const b = new Block ( data , new CID ( hash ) )
99107
100108 await bs . put ( b )
101- await bs . deleteMany ( [ b . cid ] )
109+ await drain ( bs . deleteMany ( [ b . cid ] ) )
102110 const res = await bs . _repo . blocks . has ( b . cid )
103- expect ( res ) . to . be . eql ( false )
111+ expect ( res ) . to . be . false ( )
104112 } )
105113
106114 it ( 'does not delete a blocks it does not have' , async ( ) => {
107- const data = Buffer . from ( 'Will not live that much ' + Date . now ( ) )
115+ const data = uint8ArrayFromString ( 'Will not live that much ' + Date . now ( ) )
108116 const cid = new CID ( await multihashing ( data , 'sha2-256' ) )
109117
110- await bs . deleteMany ( [ cid ] )
111- . then (
112- ( ) => expect . fail ( 'Should have thrown' ) ,
113- ( err ) => expect ( err ) . to . have . property ( 'code' , 'ERR_BLOCK_NOT_FOUND' )
114- )
118+ await expect ( drain ( bs . deleteMany ( [ cid ] ) ) ) . to . eventually . be . rejected ( ) . with . property ( 'code' , 'ERR_BLOCK_NOT_FOUND' )
115119 } )
116120
117121 it ( 'stores and gets lots of blocks' , async function ( ) {
118- this . timeout ( 8 * 1000 )
122+ this . timeout ( 20 * 1000 )
119123
120124 const data = _ . range ( 1000 ) . map ( ( i ) => {
121- return Buffer . from ( `hello-${ i } -${ Math . random ( ) } ` )
125+ return uint8ArrayFromString ( `hello-${ i } -${ Math . random ( ) } ` )
122126 } )
123127
124128 const blocks = await Promise . all ( data . map ( async ( d ) => {
125129 const hash = await multihashing ( d , 'sha2-256' )
126130 return new Block ( d , new CID ( hash ) )
127131 } ) )
128132
129- await bs . putMany ( blocks )
133+ await drain ( bs . putMany ( blocks ) )
130134
131135 const res = await Promise . all ( blocks . map ( b => bs . get ( b . cid ) ) )
132136 expect ( res ) . to . be . eql ( blocks )
@@ -155,13 +159,13 @@ module.exports = (repo) => {
155159 // returns a block with a value equal to its key
156160 const bitswap = {
157161 get ( cid ) {
158- return new Block ( Buffer . from ( 'secret' ) , cid )
162+ return new Block ( uint8ArrayFromString ( 'secret' ) , cid )
159163 }
160164 }
161165
162166 bs . setExchange ( bitswap )
163167
164- const data = Buffer . from ( 'secret' )
168+ const data = uint8ArrayFromString ( 'secret' )
165169
166170 const hash = await multihashing ( data , 'sha2-256' )
167171 const block = await bs . get ( new CID ( hash ) )
@@ -178,7 +182,7 @@ module.exports = (repo) => {
178182 }
179183 bs . setExchange ( bitswap )
180184
181- const data = Buffer . from ( 'secret sauce' )
185+ const data = uint8ArrayFromString ( 'secret sauce' )
182186
183187 const hash = await multihashing ( data , 'sha2-256' )
184188 await bs . put ( new Block ( data , new CID ( hash ) ) )
0 commit comments