@@ -2,11 +2,8 @@ import { describe, expect, it } from "@jest/globals";
2
2
3
3
import {
4
4
Layout ,
5
- LayoutToType ,
6
- RoArray ,
7
5
addFixedValues ,
8
6
bitsetItem ,
9
- column ,
10
7
deserializeLayout ,
11
8
layoutDiscriminator ,
12
9
serializeLayout ,
@@ -383,63 +380,32 @@ describe("Layout tests", function () {
383
380
expect ( discriminator ( Uint8Array . from ( [ 0 , 0 ] ) ) ) . toEqual ( [ 2 ] ) ;
384
381
} ) ;
385
382
386
- } ) ;
387
- } ) ;
388
-
389
- describe ( "Switch Layout Size Tests" , ( ) => {
390
- it ( "Can discriminate a set of layouts" , ( ) => {
391
- const layouta = [
392
- {
393
- name : "payload" ,
394
- binary : "bytes" ,
395
- lengthSize : 2 ,
396
- layout : [
397
- {
398
- name : "payload" ,
399
- binary : "switch" ,
400
- idSize : 1 ,
401
- layouts : [
402
- [ [ 0 , "Direct" ] , [ ] ] ,
403
- [ [ 1 , "Payload" ] , [ { name : "data" , binary : "bytes" , lengthSize : 4 } ] ] ,
404
- ] ,
405
- } ,
383
+ it ( "can discriminate a switch layout with the same byte value in all cases" , ( ) => {
384
+ const switchLayout = {
385
+ binary : "switch" ,
386
+ idSize : 1 ,
387
+ layouts : [
388
+ [ [ 0 , "first" ] , [
389
+ { name : "fixed1" , binary : "uint" , size : 1 , custom : 32 } ,
390
+ { name : "variable1" , binary : "bytes" , lengthSize : 1 } ,
391
+ ] ] ,
392
+ [ [ 1 , "second" ] , [
393
+ { name : "fixed2" , binary : "uint" , size : 1 , custom : 32 } ,
394
+ { name : "variable2" , binary : "bytes" , lengthSize : 2 } ,
395
+ ] ] ,
406
396
] ,
407
- } ,
408
- ] as const satisfies Layout ;
397
+ } as const satisfies Layout ;
409
398
410
- const layoutb = [
411
- {
412
- name : "payload" ,
399
+ const secondByteLayout = {
413
400
binary : "bytes" ,
414
- lengthSize : 3 ,
415
- layout : [
416
- {
417
- name : "payload" ,
418
- binary : "switch" ,
419
- idSize : 1 ,
420
- layouts : [
421
- [ [ 0 , "Nothing" ] , [ ] ] ,
422
- [ [ 1 , "Data" ] , [ { name : "data" , binary : "bytes" , lengthSize : 4 } ] ] ,
423
- ] ,
424
- } ,
425
- ] ,
426
- } ,
427
- ] as const satisfies Layout ;
401
+ custom : new Uint8Array ( [ 1 , 33 , 0 ] ) ,
402
+ } as const satisfies Layout ;
428
403
429
- const messageLayouts = [
430
- [ "Layout" , layouta ] ,
431
- [ "LayoutB" , layoutb ] ,
432
- ] as const satisfies RoArray < [ string , Layout ] > ;
433
- const messageDiscriminator = layoutDiscriminator ( column ( messageLayouts , 1 ) ) ;
404
+ const messageDiscriminator = layoutDiscriminator ( [ switchLayout , secondByteLayout ] ) ;
434
405
435
- const b : LayoutToType < typeof layoutb > = {
436
- payload : {
437
- payload : { id : "Data" , data : new Uint8Array ( [ 0 , 0 , 0 , 0 ] ) } ,
438
- } ,
439
- } ;
440
-
441
- const data = serializeLayout ( layoutb , b ) ;
442
- const idx = messageDiscriminator ( data ) ;
443
- expect ( idx ) . toEqual ( 1 ) ;
406
+ expect ( messageDiscriminator ( new Uint8Array ( [ 0 , 32 , 1 , 128 ] ) ) ) . toEqual ( 0 ) ;
407
+ expect ( messageDiscriminator ( new Uint8Array ( [ 0 , 33 , 0 ] ) ) ) . toEqual ( 1 ) ;
408
+ expect ( messageDiscriminator ( new Uint8Array ( [ 0 , 34 , 0 ] ) ) ) . toEqual ( null ) ;
409
+ } ) ;
444
410
} ) ;
445
411
} ) ;
0 commit comments