@@ -112,6 +112,66 @@ describe('TemplateJson', () => {
112112 } ) ;
113113 } ) ;
114114
115+ describe ( 'bin' , ( ) => {
116+ test ( 'uses default binary schema, if not provided' , ( ) => {
117+ resetMathRandom ( ) ;
118+ const bin = TemplateJson . gen ( 'bin' ) ;
119+ expect ( bin instanceof Uint8Array ) . toBe ( true ) ;
120+ expect ( ( bin as Uint8Array ) . length ) . toBeGreaterThanOrEqual ( 0 ) ;
121+ expect ( ( bin as Uint8Array ) . length ) . toBeLessThanOrEqual ( 5 ) ;
122+ } ) ;
123+
124+ test ( 'can specify length range' , ( ) => {
125+ resetMathRandom ( ) ;
126+ const bin = TemplateJson . gen ( [ 'bin' , 2 , 4 ] ) as Uint8Array ;
127+ expect ( bin instanceof Uint8Array ) . toBe ( true ) ;
128+ expect ( bin . length ) . toBeGreaterThanOrEqual ( 2 ) ;
129+ expect ( bin . length ) . toBeLessThanOrEqual ( 4 ) ;
130+ } ) ;
131+
132+ test ( 'can specify octet value range' , ( ) => {
133+ resetMathRandom ( ) ;
134+ const bin = TemplateJson . gen ( [ 'bin' , 5 , 5 , 100 , 150 ] ) as Uint8Array ;
135+ expect ( bin instanceof Uint8Array ) . toBe ( true ) ;
136+ expect ( bin . length ) . toBe ( 5 ) ;
137+ for ( let i = 0 ; i < bin . length ; i ++ ) {
138+ expect ( bin [ i ] ) . toBeGreaterThanOrEqual ( 100 ) ;
139+ expect ( bin [ i ] ) . toBeLessThanOrEqual ( 150 ) ;
140+ }
141+ } ) ;
142+
143+ test ( 'handles edge cases' , ( ) => {
144+ // Empty array
145+ const empty = TemplateJson . gen ( [ 'bin' , 0 , 0 ] ) as Uint8Array ;
146+ expect ( empty instanceof Uint8Array ) . toBe ( true ) ;
147+ expect ( empty . length ) . toBe ( 0 ) ;
148+
149+ // Single byte with fixed value range
150+ resetMathRandom ( ) ;
151+ const single = TemplateJson . gen ( [ 'bin' , 1 , 1 , 42 , 42 ] ) as Uint8Array ;
152+ expect ( single instanceof Uint8Array ) . toBe ( true ) ;
153+ expect ( single . length ) . toBe ( 1 ) ;
154+ expect ( single [ 0 ] ) . toBe ( 42 ) ;
155+ } ) ;
156+
157+ test ( 'uses default octet range when not specified' , ( ) => {
158+ resetMathRandom ( ) ;
159+ const bin = TemplateJson . gen ( [ 'bin' , 3 , 3 ] ) as Uint8Array ;
160+ expect ( bin instanceof Uint8Array ) . toBe ( true ) ;
161+ expect ( bin . length ) . toBe ( 3 ) ;
162+ for ( let i = 0 ; i < bin . length ; i ++ ) {
163+ expect ( bin [ i ] ) . toBeGreaterThanOrEqual ( 0 ) ;
164+ expect ( bin [ i ] ) . toBeLessThanOrEqual ( 255 ) ;
165+ }
166+ } ) ;
167+
168+ test ( 'respects maxNodes limit' , ( ) => {
169+ const bin = TemplateJson . gen ( [ 'bin' , 10 , 20 ] , { maxNodes : 5 } ) as Uint8Array ;
170+ expect ( bin instanceof Uint8Array ) . toBe ( true ) ;
171+ expect ( bin . length ) . toBeLessThanOrEqual ( 10 ) ;
172+ } ) ;
173+ } ) ;
174+
115175 describe ( 'nil' , ( ) => {
116176 test ( 'always returns null' , ( ) => {
117177 expect ( TemplateJson . gen ( 'nil' ) ) . toBe ( null ) ;
@@ -375,6 +435,16 @@ describe('TemplateJson', () => {
375435 const result = TemplateJson . gen ( [ 'or' , [ 'lit' , 'only' ] ] ) ;
376436 expect ( result ) . toBe ( 'only' ) ;
377437 } ) ;
438+
439+ test ( 'works with bin templates' , ( ) => {
440+ resetMathRandom ( ) ;
441+ const result = TemplateJson . gen ( [ 'or' , 'str' , 'int' , [ 'bin' , 2 , 2 ] ] ) ;
442+ // Result should be one of the template types
443+ const isString = typeof result === 'string' ;
444+ const isNumber = typeof result === 'number' ;
445+ const isBin = result instanceof Uint8Array ;
446+ expect ( isString || isNumber || isBin ) . toBe ( true ) ;
447+ } ) ;
378448 } ) ;
379449
380450 describe ( 'maxNodeCount' , ( ) => {
@@ -449,6 +519,39 @@ describe('TemplateJson', () => {
449519 expect ( typeof result ) . toBe ( 'number' ) ;
450520 expect ( Number . isInteger ( result ) ) . toBe ( true ) ;
451521 } ) ;
522+
523+ test ( 'handles bin templates in complex structures' , ( ) => {
524+ resetMathRandom ( ) ;
525+ const template : any = [
526+ 'obj' ,
527+ [
528+ [ 'name' , 'str' ] ,
529+ [ 'data' , [ 'bin' , 3 , 3 ] ] ,
530+ [ 'metadata' , [
531+ 'obj' ,
532+ [
533+ [ 'hash' , [ 'bin' , 32 , 32 ] ] ,
534+ [ 'signature' , [ 'bin' , 64 , 64 , 0 , 127 ] ] ,
535+ ] ,
536+ ] ] ,
537+ ] ,
538+ ] ;
539+ const result = TemplateJson . gen ( template ) as any ;
540+ expect ( typeof result ) . toBe ( 'object' ) ;
541+ expect ( typeof result . name ) . toBe ( 'string' ) ;
542+ expect ( result . data instanceof Uint8Array ) . toBe ( true ) ;
543+ expect ( result . data . length ) . toBe ( 3 ) ;
544+ expect ( typeof result . metadata ) . toBe ( 'object' ) ;
545+ expect ( result . metadata . hash instanceof Uint8Array ) . toBe ( true ) ;
546+ expect ( result . metadata . hash . length ) . toBe ( 32 ) ;
547+ expect ( result . metadata . signature instanceof Uint8Array ) . toBe ( true ) ;
548+ expect ( result . metadata . signature . length ) . toBe ( 64 ) ;
549+ // Check signature values are in the specified range
550+ for ( let i = 0 ; i < result . metadata . signature . length ; i ++ ) {
551+ expect ( result . metadata . signature [ i ] ) . toBeGreaterThanOrEqual ( 0 ) ;
552+ expect ( result . metadata . signature [ i ] ) . toBeLessThanOrEqual ( 127 ) ;
553+ }
554+ } ) ;
452555 } ) ;
453556} ) ;
454557
0 commit comments