1- import { CairoBytes31 , CairoFelt252 } from '../../../src' ;
1+ import { BigNumberish , CairoBytes31 , CairoFelt252 } from '../../../src' ;
2+ import { addHexPrefix } from '../../../src/utils/encode' ;
3+
4+ function uint8ArrayToSize ( input : Uint8Array | Array < number > , size : number = 31 ) {
5+ const output = new Uint8Array ( size ) ;
6+ output . set ( input , size - input . length ) ;
7+ return output ;
8+ }
9+
10+ function toHex62 ( number : BigNumberish ) {
11+ return addHexPrefix ( BigInt ( number ) . toString ( 16 ) . padStart ( 62 , '0' ) ) ;
12+ }
213
314describe ( 'CairoBytes31 class Unit Tests' , ( ) => {
415 describe ( 'constructor with different input types' , ( ) => {
516 test ( 'should handle string input' , ( ) => {
617 const bytes31 = new CairoBytes31 ( 'hello' ) ;
718 expect ( bytes31 . data ) . toBeInstanceOf ( Uint8Array ) ;
8- expect ( bytes31 . data ) . toEqual ( new Uint8Array ( [ 104 , 101 , 108 , 108 , 111 ] ) ) ;
19+ expect ( bytes31 . data ) . toEqual ( uint8ArrayToSize ( [ 104 , 101 , 108 , 108 , 111 ] ) ) ;
920 } ) ;
1021
1122 test ( 'should handle empty string' , ( ) => {
1223 const bytes31 = new CairoBytes31 ( '' ) ;
13- expect ( bytes31 . data ) . toEqual ( new Uint8Array ( [ ] ) ) ;
24+ expect ( bytes31 . data ) . toEqual ( uint8ArrayToSize ( [ ] ) ) ;
1425 } ) ;
1526
1627 test ( 'should handle Unicode strings' , ( ) => {
1728 const bytes31 = new CairoBytes31 ( '☥' ) ;
1829 // '☥' in UTF-8: [226, 152, 165]
19- expect ( bytes31 . data ) . toEqual ( new Uint8Array ( [ 226 , 152 , 165 ] ) ) ;
30+ expect ( bytes31 . data ) . toEqual ( uint8ArrayToSize ( [ 226 , 152 , 165 ] ) ) ;
2031 } ) ;
2132
2233 test ( 'should handle Buffer input' , ( ) => {
2334 const buffer = Buffer . from ( [ 72 , 101 , 108 , 108 , 111 ] ) ; // "Hello"
2435 const bytes31 = new CairoBytes31 ( buffer ) ;
25- expect ( bytes31 . data ) . toEqual ( new Uint8Array ( [ 72 , 101 , 108 , 108 , 111 ] ) ) ;
36+ expect ( bytes31 . data ) . toEqual ( uint8ArrayToSize ( [ 72 , 101 , 108 , 108 , 111 ] ) ) ;
2637 } ) ;
2738
2839 test ( 'should handle empty Buffer' , ( ) => {
2940 const buffer = Buffer . alloc ( 0 ) ;
3041 const bytes31 = new CairoBytes31 ( buffer ) ;
31- expect ( bytes31 . data ) . toEqual ( new Uint8Array ( [ ] ) ) ;
42+ expect ( bytes31 . data ) . toEqual ( uint8ArrayToSize ( [ ] ) ) ;
3243 } ) ;
3344
3445 test ( 'should handle Uint8Array input' , ( ) => {
3546 const uint8Array = new Uint8Array ( [ 87 , 111 , 114 , 108 , 100 ] ) ; // "World"
3647 const bytes31 = new CairoBytes31 ( uint8Array ) ;
37- expect ( bytes31 . data ) . toEqual ( uint8Array ) ;
48+ expect ( bytes31 . data ) . toEqual ( uint8ArrayToSize ( uint8Array ) ) ;
3849 } ) ;
3950
4051 test ( 'should handle empty Uint8Array' , ( ) => {
4152 const uint8Array = new Uint8Array ( [ ] ) ;
4253 const bytes31 = new CairoBytes31 ( uint8Array ) ;
43- expect ( bytes31 . data ) . toEqual ( new Uint8Array ( [ ] ) ) ;
54+ expect ( bytes31 . data ) . toEqual ( uint8ArrayToSize ( [ ] ) ) ;
4455 } ) ;
4556
4657 test ( 'should handle maximum length input (31 bytes)' , ( ) => {
@@ -174,46 +185,47 @@ describe('CairoBytes31 class Unit Tests', () => {
174185 test ( 'should convert empty data to 0x0' , ( ) => {
175186 const bytes31 = new CairoBytes31 ( '' ) ;
176187 expect ( bytes31 . toHexString ( ) ) . toBe ( '0x0' ) ;
188+ expect ( bytes31 . toHexString ( 'padded' ) ) . toBe ( toHex62 ( '0x0' ) ) ;
177189 } ) ;
178190
179191 test ( 'should convert single character to hex' , ( ) => {
180192 const bytes31 = new CairoBytes31 ( 'A' ) ; // ASCII 65 = 0x41
181193 expect ( bytes31 . toHexString ( ) ) . toBe ( '0x41' ) ;
194+ expect ( bytes31 . toHexString ( 'padded' ) ) . toBe ( toHex62 ( '0x41' ) ) ;
182195 } ) ;
183196
184197 test ( 'should convert multi-character string to hex' , ( ) => {
185198 const bytes31 = new CairoBytes31 ( 'AB' ) ; // [65, 66] = 0x4142
186199 expect ( bytes31 . toHexString ( ) ) . toBe ( '0x4142' ) ;
200+ expect ( bytes31 . toHexString ( 'padded' ) ) . toBe ( toHex62 ( '0x4142' ) ) ;
187201 } ) ;
188202
189203 test ( 'should convert Unicode to hex' , ( ) => {
190204 const bytes31 = new CairoBytes31 ( '☥' ) ; // [226, 152, 165] = 0xe298a5
191205 expect ( bytes31 . toHexString ( ) ) . toBe ( '0xe298a5' ) ;
206+ expect ( bytes31 . toHexString ( 'padded' ) ) . toBe ( toHex62 ( '0xe298a5' ) ) ;
192207 } ) ;
193208
194209 test ( 'should convert Buffer to hex' , ( ) => {
195210 const buffer = Buffer . from ( [ 255 , 254 ] ) ;
196211 const bytes31 = new CairoBytes31 ( buffer ) ;
197212 expect ( bytes31 . toHexString ( ) ) . toBe ( '0xfffe' ) ;
213+ expect ( bytes31 . toHexString ( 'padded' ) ) . toBe ( toHex62 ( '0xfffe' ) ) ;
198214 } ) ;
199215
200216 test ( 'should convert Uint8Array to hex' , ( ) => {
201217 const array = new Uint8Array ( [ 1 , 2 , 3 , 4 ] ) ;
202218 const bytes31 = new CairoBytes31 ( array ) ;
203- expect ( bytes31 . toHexString ( ) ) . toBe ( '0x01020304' ) ;
219+ expect ( bytes31 . toHexString ( ) ) . toBe ( '0x1020304' ) ;
220+ expect ( bytes31 . toHexString ( 'padded' ) ) . toBe ( toHex62 ( '0x1020304' ) ) ;
204221 } ) ;
205222
206223 test ( 'should handle maximum length data' , ( ) => {
207224 const maxArray = new Uint8Array ( 31 ) . fill ( 255 ) ; // 31 bytes of 0xff
208225 const bytes31 = new CairoBytes31 ( maxArray ) ;
209226 const expectedHex = `0x${ 'ff' . repeat ( 31 ) } ` ;
210227 expect ( bytes31 . toHexString ( ) ) . toBe ( expectedHex ) ;
211- } ) ;
212-
213- test ( 'should preserve leading zero values' , ( ) => {
214- const buffer = Buffer . from ( [ 0 , 0 , 1 ] ) ;
215- const bytes31 = new CairoBytes31 ( buffer ) ;
216- expect ( bytes31 . toHexString ( ) ) . toEqual ( `0x${ buffer . toString ( 'hex' ) } ` ) ;
228+ expect ( bytes31 . toHexString ( 'padded' ) ) . toBe ( expectedHex ) ;
217229 } ) ;
218230 } ) ;
219231
@@ -236,7 +248,7 @@ describe('CairoBytes31 class Unit Tests', () => {
236248 test ( 'should return hex string array for Buffer input' , ( ) => {
237249 const buffer = Buffer . from ( [ 1 , 0 ] ) ; // 0x0100 = 256
238250 const bytes31 = new CairoBytes31 ( buffer ) ;
239- expect ( bytes31 . toApiRequest ( ) ) . toEqual ( [ '0x0100 ' ] ) ;
251+ expect ( bytes31 . toApiRequest ( ) ) . toEqual ( [ '0x100 ' ] ) ;
240252 } ) ;
241253
242254 test ( 'should return hex string array for large values' , ( ) => {
@@ -342,7 +354,7 @@ describe('CairoBytes31 class Unit Tests', () => {
342354 test ( 'should handle binary data correctly' , ( ) => {
343355 const binaryData = new Uint8Array ( [ 0 , 1 , 2 , 254 , 255 ] ) ;
344356 const bytes31 = new CairoBytes31 ( binaryData ) ;
345- expect ( bytes31 . data ) . toEqual ( binaryData ) ;
357+ expect ( bytes31 . data ) . toEqual ( uint8ArrayToSize ( binaryData ) ) ;
346358 expect ( bytes31 . toBigInt ( ) ) . toBe ( 0x0102feffn ) ;
347359 } ) ;
348360
@@ -381,7 +393,7 @@ describe('CairoBytes31 class Unit Tests', () => {
381393
382394 testCases . forEach ( ( originalArray ) => {
383395 const bytes31 = new CairoBytes31 ( originalArray ) ;
384- expect ( bytes31 . data ) . toEqual ( originalArray ) ;
396+ expect ( bytes31 . data ) . toEqual ( uint8ArrayToSize ( originalArray ) ) ;
385397 } ) ;
386398 } ) ;
387399
0 commit comments