11import { hex } from 'buffer-tag' ;
22
3- import { cborDecode , cborEncode , DataItem , getCborEncodeDecodeOptions , setCborEncodeDecodeOptions } from '../src/cbor' ;
3+ import { cborDecode , cborEncode , DataItem } from '../src/cbor' ;
44
55describe ( 'cbor' , ( ) => {
66 it ( 'should properly decode a nested map' , ( ) => {
@@ -11,23 +11,25 @@ describe('cbor', () => {
1111 expect ( decoded . data . get ( 'foo' ) ?. data . get ( 'bar' ) ) . toBe ( 'baz' ) ;
1212 } ) ;
1313
14- it ( 'should properly encoded and decoded maps' , ( ) => {
15- const encoded = cborEncode ( DataItem . fromData ( { foo : 'baz' } ) ) ;
14+ it ( 'should properly encoded and decoded maps (length <= 23)' , ( ) => {
15+ const length = 23 ;
16+ const obj = Object . fromEntries ( Array . from ( { length } , ( _ , i ) => [ `key${ i } ` , i ] ) ) ;
17+ const encoded = cborEncode ( DataItem . fromData ( obj ) ) ;
1618 const decoded = cborDecode ( encoded ) ;
1719 const reEncode = cborEncode ( decoded ) ;
1820 expect ( reEncode . toString ( 'hex' ) ) . toBe ( encoded . toString ( 'hex' ) ) ;
19- expect ( encoded [ 3 ] . toString ( 16 ) ) . toBe ( 'b9' ) ; // Large Map
21+ expect ( encoded [ 4 ] . toString ( 16 ) ) . toBe ( ( 0xA0 + length ) . toString ( 16 ) ) ;
2022 } ) ;
2123
22- it ( 'should properly encoded and decoded maps using variableMapSize=true' , ( ) => {
23- const options = getCborEncodeDecodeOptions ( ) ;
24- options . variableMapSize = true ;
25- setCborEncodeDecodeOptions ( options ) ;
26- const encoded = cborEncode ( DataItem . fromData ( { foo : 'baz' } ) ) ;
24+ it ( 'should properly encoded and decoded maps (length > 23)' , ( ) => {
25+ const length = 24 ;
26+ const obj = Object . fromEntries ( Array . from ( { length } , ( _ , i ) => [ `key${ i } ` , i ] ) ) ;
27+ const encoded = cborEncode ( DataItem . fromData ( obj ) ) ;
2728 const decoded = cborDecode ( encoded ) ;
2829 const reEncode = cborEncode ( decoded ) ;
2930 expect ( reEncode . toString ( 'hex' ) ) . toBe ( encoded . toString ( 'hex' ) ) ;
30- expect ( encoded [ 3 ] . toString ( 16 ) ) . toBe ( 'a1' ) ; // Map with one item
31+ expect ( encoded [ 4 ] . toString ( 16 ) ) . toBe ( 'b8' ) ;
32+ expect ( encoded [ 5 ] . toString ( 16 ) ) . toBe ( length . toString ( 16 ) ) ;
3133 } ) ;
3234
3335 it ( 'should properly encoded and decoded with arrays' , ( ) => {
0 commit comments