@@ -10,6 +10,7 @@ import {
10
10
deserializeLayout ,
11
11
layoutDiscriminator ,
12
12
serializeLayout ,
13
+ optionItem ,
13
14
} from "./../src/index.js" ;
14
15
15
16
// prettier-ignore
@@ -226,6 +227,48 @@ describe("Layout tests", function () {
226
227
} ) ;
227
228
} ) ;
228
229
230
+ describe ( "OptionItem tests" , ( ) => {
231
+ it ( "basic test" , ( ) => {
232
+ const layout = optionItem ( { binary : "uint" , size : 1 } ) ;
233
+
234
+ const testCases = [ [ 32 , [ 1 , 32 ] ] , [ undefined , [ 0 ] ] ] as const ;
235
+ for ( const [ data , expected ] of testCases ) {
236
+ const encoded = serializeLayout ( layout , data ) ;
237
+ expect ( encoded ) . toEqual ( new Uint8Array ( expected ) ) ;
238
+
239
+ const decoded = deserializeLayout ( layout , encoded ) ;
240
+ expect ( decoded ) . toEqual ( data ) ;
241
+ }
242
+ } )
243
+
244
+ it ( "advanced test" , ( ) => {
245
+ const layout = { binary : "array" , layout : [
246
+ { name : "firstOption" , ...optionItem ( { binary : "uint" , size : 1 } ) } ,
247
+ { name : "someUint" , binary : "uint" , size : 1 } ,
248
+ { name : "secondOption" , ...optionItem ( { binary : "bytes" , size : 4 } ) } ,
249
+ ] } as const satisfies Layout ;
250
+
251
+ const data = [
252
+ { firstOption : undefined , someUint : 1 , secondOption : undefined } ,
253
+ { firstOption : 10 , someUint : 2 , secondOption : undefined } ,
254
+ { firstOption : undefined , someUint : 3 , secondOption : new Uint8Array ( [ 1 , 2 , 3 , 4 ] ) } ,
255
+ { firstOption : 20 , someUint : 4 , secondOption : new Uint8Array ( [ 5 , 6 , 7 , 8 ] ) } ,
256
+ ] as const ;
257
+ const expected = new Uint8Array ( [
258
+ ...[ 0 , 1 , 0 ] ,
259
+ ...[ 1 , 10 , 2 , 0 ] ,
260
+ ...[ 0 , 3 , 1 , 1 , 2 , 3 , 4 ] ,
261
+ ...[ 1 , 20 , 4 , 1 , 5 , 6 , 7 , 8 ] ,
262
+ ] ) ;
263
+
264
+ const encoded = serializeLayout ( layout , data ) ;
265
+ expect ( encoded ) . toEqual ( new Uint8Array ( expected ) ) ;
266
+
267
+ const decoded = deserializeLayout ( layout , encoded ) ;
268
+ expect ( decoded ) . toEqual ( data ) ;
269
+ } )
270
+ } )
271
+
229
272
it ( "should serialize and deserialize correctly" , function ( ) {
230
273
const encoded = serializeLayout ( testLayout , completeValues ) ;
231
274
const decoded = deserializeLayout ( testLayout , encoded ) ;
0 commit comments