1
1
/* globals describe, it */
2
2
3
3
import OLDCID from 'cids'
4
- import { assert } from 'chai'
5
4
import { fromHex , toHex , equals } from '../src/bytes.js'
6
5
import { varint , CID } from 'multiformats'
7
6
import { base58btc } from 'multiformats/bases/base58'
8
7
import { base32 } from 'multiformats/bases/base32'
9
8
import { base64 } from 'multiformats/bases/base64'
10
9
import { sha256 , sha512 } from 'multiformats/hashes/sha2'
11
10
import invalidMultihash from './fixtures/invalid-multihash.js'
12
- import { testThrowSync as testThrow } from './fixtures/test-throw.js'
11
+ import chai from 'chai'
12
+ import chaiAsPromised from 'chai-as-promised'
13
13
14
- const textEncoder = new TextEncoder ( )
14
+ chai . use ( chaiAsPromised )
15
+ const { assert } = chai
15
16
16
- /**
17
- * @param {Function } fn
18
- */
19
- const testThrowAny = async fn => {
20
- try {
21
- await fn ( )
22
- } catch ( e ) {
23
- return
24
- }
25
- /* c8 ignore next */
26
- throw new Error ( 'Test failed to throw' )
27
- }
17
+ const textEncoder = new TextEncoder ( )
28
18
29
19
describe ( 'CID' , ( ) => {
30
20
describe ( 'v0' , ( ) => {
@@ -64,28 +54,28 @@ describe('CID', () => {
64
54
65
55
it ( 'throws on invalid BS58Str multihash ' , async ( ) => {
66
56
const msg = 'Non-base58btc character'
67
- await testThrow ( ( ) => CID . parse ( 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII' ) , msg )
57
+ assert . throws ( ( ) => CID . parse ( 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII' ) , msg )
68
58
} )
69
59
70
60
it ( 'throws on trying to create a CIDv0 with a codec other than dag-pb' , async ( ) => {
71
61
const hash = await sha256 . digest ( textEncoder . encode ( 'abc' ) )
72
62
const msg = 'Version 0 CID must use dag-pb (code: 112) block encoding'
73
- await testThrow ( ( ) => CID . create ( 0 , 113 , hash ) , msg )
63
+ assert . throws ( ( ) => CID . create ( 0 , 113 , hash ) , msg )
74
64
} )
75
65
76
66
// This was failing for quite some time, test just missed await so it went
77
67
// unnoticed. Not sure we still care about checking fourth argument.
78
68
// it('throws on trying to pass specific base encoding [deprecated]', async () => {
79
69
// const hash = await sha256.digest(textEncoder.encode('abc'))
80
70
// const msg = 'No longer supported, cannot specify base encoding in instantiation'
81
- // await testThrow (() => CID.create(0, 112, hash, 'base32'), msg)
71
+ // assert.throws (() => CID.create(0, 112, hash, 'base32'), msg)
82
72
// })
83
73
84
74
it ( 'throws on trying to base encode CIDv0 in other base than base58btc' , async ( ) => {
85
75
const mhStr = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
86
76
const cid = CID . parse ( mhStr )
87
77
const msg = 'Cannot string encode V0 in base32 encoding'
88
- await testThrow ( ( ) => cid . toString ( base32 ) , msg )
78
+ assert . throws ( ( ) => cid . toString ( base32 ) , msg )
89
79
} )
90
80
91
81
it ( '.bytes' , async ( ) => {
@@ -272,7 +262,7 @@ describe('CID', () => {
272
262
273
263
for ( const i of parse ) {
274
264
const name = `CID.parse(${ JSON . stringify ( i ) } )`
275
- it ( name , async ( ) => await testThrowAny ( ( ) => CID . parse ( i ) ) )
265
+ it ( name , async ( ) => assert . throws ( ( ) => CID . parse ( i ) ) )
276
266
}
277
267
278
268
const decode = [
@@ -282,7 +272,7 @@ describe('CID', () => {
282
272
283
273
for ( const i of decode ) {
284
274
const name = `CID.decode(textEncoder.encode(${ JSON . stringify ( i . toString ( ) ) } ))`
285
- it ( name , async ( ) => await testThrowAny ( ( ) => CID . decode ( i ) ) )
275
+ it ( name , async ( ) => assert . throws ( ( ) => CID . decode ( i ) ) )
286
276
}
287
277
288
278
const create = [
@@ -296,7 +286,7 @@ describe('CID', () => {
296
286
const mh = hash instanceof Uint8Array ? `textEncoder.encode(${ form } )` : form
297
287
const name = `CID.create(${ version } , ${ code } , ${ mh } )`
298
288
// @ts -expect-error - version issn't always 0|1
299
- it ( name , async ( ) => await testThrowAny ( ( ) => CID . create ( version , code , hash ) ) )
289
+ it ( name , async ( ) => assert . throws ( ( ) => CID . create ( version , code , hash ) ) )
300
290
}
301
291
302
292
it ( 'invalid fixtures' , async ( ) => {
@@ -333,13 +323,13 @@ describe('CID', () => {
333
323
it ( 'should not convert v1 to v0 if not dag-pb codec' , async ( ) => {
334
324
const hash = await sha256 . digest ( textEncoder . encode ( `TEST${ Date . now ( ) } ` ) )
335
325
const cid = CID . create ( 1 , 0x71 , hash )
336
- await testThrow ( ( ) => cid . toV0 ( ) , 'Cannot convert a non dag-pb CID to CIDv0' )
326
+ assert . throws ( ( ) => cid . toV0 ( ) , 'Cannot convert a non dag-pb CID to CIDv0' )
337
327
} )
338
328
339
329
it ( 'should not convert v1 to v0 if not sha2-256 multihash' , async ( ) => {
340
330
const hash = await sha512 . digest ( textEncoder . encode ( `TEST${ Date . now ( ) } ` ) )
341
331
const cid = CID . create ( 1 , 112 , hash )
342
- await testThrow ( ( ) => cid . toV0 ( ) , 'Cannot convert non sha2-256 multihash CID to CIDv0' )
332
+ assert . throws ( ( ) => cid . toV0 ( ) , 'Cannot convert non sha2-256 multihash CID to CIDv0' )
343
333
} )
344
334
345
335
it ( 'should return assert.deepStrictEqual instance when converting v1 to v1' , async ( ) => {
@@ -523,7 +513,7 @@ describe('CID', () => {
523
513
const cid = CID . create ( 1 , 112 , hash )
524
514
const msg = 'To parse non base32 or base58btc encoded CID multibase decoder must be provided'
525
515
526
- await testThrow ( ( ) => CID . parse ( cid . toString ( base64 ) ) , msg )
516
+ assert . throws ( ( ) => CID . parse ( cid . toString ( base64 ) ) , msg )
527
517
} )
528
518
529
519
it ( 'parses base64 encoded CIDv1 if base64 is provided' , async ( ) => {
@@ -586,39 +576,39 @@ describe('CID', () => {
586
576
it ( 'codec' , async ( ) => {
587
577
const hash = await sha256 . digest ( textEncoder . encode ( 'abc' ) )
588
578
const cid = CID . create ( 1 , 112 , hash )
589
- await testThrow ( ( ) => cid . codec , '"codec" property is deprecated, use integer "code" property instead' )
579
+ assert . throws ( ( ) => cid . codec , '"codec" property is deprecated, use integer "code" property instead' )
590
580
// @ts -expect-error - 'string' is not assignable to parameter of type 'number'
591
- await testThrow ( ( ) => CID . create ( 1 , 'dag-pb' , hash ) , 'String codecs are no longer supported' )
581
+ assert . throws ( ( ) => CID . create ( 1 , 'dag-pb' , hash ) , 'String codecs are no longer supported' )
592
582
} )
593
583
594
584
it ( 'multibaseName' , async ( ) => {
595
585
const hash = await sha256 . digest ( textEncoder . encode ( 'abc' ) )
596
586
const cid = CID . create ( 1 , 112 , hash )
597
- await testThrow ( ( ) => cid . multibaseName , '"multibaseName" property is deprecated' )
587
+ assert . throws ( ( ) => cid . multibaseName , '"multibaseName" property is deprecated' )
598
588
} )
599
589
600
590
it ( 'prefix' , async ( ) => {
601
591
const hash = await sha256 . digest ( textEncoder . encode ( 'abc' ) )
602
592
const cid = CID . create ( 1 , 112 , hash )
603
- await testThrow ( ( ) => cid . prefix , '"prefix" property is deprecated' )
593
+ assert . throws ( ( ) => cid . prefix , '"prefix" property is deprecated' )
604
594
} )
605
595
606
596
it ( 'toBaseEncodedString()' , async ( ) => {
607
597
const hash = await sha256 . digest ( textEncoder . encode ( 'abc' ) )
608
598
const cid = CID . create ( 1 , 112 , hash )
609
599
// @ts -expect-error - deprecated
610
- await testThrow ( ( ) => cid . toBaseEncodedString ( ) , 'Deprecated, use .toString()' )
600
+ assert . throws ( ( ) => cid . toBaseEncodedString ( ) , 'Deprecated, use .toString()' )
611
601
} )
612
602
} )
613
603
614
604
it ( 'invalid CID version' , async ( ) => {
615
605
const encoded = varint . encodeTo ( 2 , new Uint8Array ( 32 ) )
616
- await testThrow ( ( ) => CID . decode ( encoded ) , 'Invalid CID version 2' )
606
+ assert . throws ( ( ) => CID . decode ( encoded ) , 'Invalid CID version 2' )
617
607
} )
618
608
619
609
it ( 'buffer' , async ( ) => {
620
610
const hash = await sha256 . digest ( textEncoder . encode ( 'abc' ) )
621
611
const cid = CID . create ( 1 , 112 , hash )
622
- await testThrow ( ( ) => cid . buffer , 'Deprecated .buffer property, use .bytes to get Uint8Array instead' )
612
+ assert . throws ( ( ) => cid . buffer , 'Deprecated .buffer property, use .bytes to get Uint8Array instead' )
623
613
} )
624
614
} )
0 commit comments