@@ -48,6 +48,70 @@ describe('TemplateJson', () => {
4848 } ) ;
4949 } ) ;
5050
51+ describe ( 'int64' , ( ) => {
52+ test ( 'uses default int64 schema, if not provided' , ( ) => {
53+ resetMathRandom ( ) ;
54+ const result = TemplateJson . gen ( 'int64' ) as bigint ;
55+ expect ( typeof result ) . toBe ( 'bigint' ) ;
56+ expect ( result >= BigInt ( '-9223372036854775808' ) ) . toBe ( true ) ;
57+ expect ( result <= BigInt ( '9223372036854775807' ) ) . toBe ( true ) ;
58+ } ) ;
59+
60+ test ( 'can specify int64 range' , ( ) => {
61+ resetMathRandom ( ) ;
62+ expect ( TemplateJson . gen ( [ 'int64' , BigInt ( - 10 ) , BigInt ( 10 ) ] ) ) . toBe ( BigInt ( - 9 ) ) ;
63+ expect ( TemplateJson . gen ( [ 'int64' , BigInt ( 0 ) , BigInt ( 1 ) ] ) ) . toBe ( BigInt ( 0 ) ) ;
64+ expect ( TemplateJson . gen ( [ 'int64' , BigInt ( 1 ) , BigInt ( 5 ) ] ) ) . toBe ( BigInt ( 4 ) ) ;
65+ } ) ;
66+
67+ test ( 'handles edge cases' , ( ) => {
68+ resetMathRandom ( ) ;
69+ expect ( TemplateJson . gen ( [ 'int64' , BigInt ( 0 ) , BigInt ( 0 ) ] ) ) . toBe ( BigInt ( 0 ) ) ;
70+ expect ( TemplateJson . gen ( [ 'int64' , BigInt ( - 1 ) , BigInt ( - 1 ) ] ) ) . toBe ( BigInt ( - 1 ) ) ;
71+ expect ( TemplateJson . gen ( [ 'int64' , BigInt ( '1000000000000' ) , BigInt ( '1000000000000' ) ] ) ) . toBe (
72+ BigInt ( '1000000000000' ) ,
73+ ) ;
74+ } ) ;
75+
76+ test ( 'handles very large ranges' , ( ) => {
77+ resetMathRandom ( ) ;
78+ const result = TemplateJson . gen ( [
79+ 'int64' ,
80+ BigInt ( '-9223372036854775808' ) ,
81+ BigInt ( '9223372036854775807' ) ,
82+ ] ) as bigint ;
83+ expect ( typeof result ) . toBe ( 'bigint' ) ;
84+ expect ( result >= BigInt ( '-9223372036854775808' ) ) . toBe ( true ) ;
85+ expect ( result <= BigInt ( '9223372036854775807' ) ) . toBe ( true ) ;
86+ } ) ;
87+
88+ test ( 'can be used in complex structures' , ( ) => {
89+ resetMathRandom ( ) ;
90+ const template : any = [
91+ 'obj' ,
92+ [
93+ [ 'id' , 'int64' ] ,
94+ [ 'timestamp' , [ 'int64' , BigInt ( '1000000000000' ) , BigInt ( '9999999999999' ) ] ] ,
95+ ] ,
96+ ] ;
97+ const result = TemplateJson . gen ( template ) as any ;
98+ expect ( typeof result ) . toBe ( 'object' ) ;
99+ expect ( typeof result . id ) . toBe ( 'bigint' ) ;
100+ expect ( typeof result . timestamp ) . toBe ( 'bigint' ) ;
101+ expect ( result . timestamp >= BigInt ( '1000000000000' ) ) . toBe ( true ) ;
102+ expect ( result . timestamp <= BigInt ( '9999999999999' ) ) . toBe ( true ) ;
103+ } ) ;
104+
105+ test ( 'works with or templates' , ( ) => {
106+ resetMathRandom ( ) ;
107+ const result = TemplateJson . gen ( [ 'or' , 'int' , 'int64' , 'str' ] ) ;
108+ const isBigInt = typeof result === 'bigint' ;
109+ const isNumber = typeof result === 'number' ;
110+ const isString = typeof result === 'string' ;
111+ expect ( isBigInt || isNumber || isString ) . toBe ( true ) ;
112+ } ) ;
113+ } ) ;
114+
51115 describe ( 'num' , ( ) => {
52116 test ( 'generates random number, without range' , ( ) => {
53117 resetMathRandom ( ) ;
@@ -527,13 +591,16 @@ describe('TemplateJson', () => {
527591 [
528592 [ 'name' , 'str' ] ,
529593 [ 'data' , [ 'bin' , 3 , 3 ] ] ,
530- [ 'metadata' , [
531- 'obj ' ,
594+ [
595+ 'metadata ' ,
532596 [
533- [ 'hash' , [ 'bin' , 32 , 32 ] ] ,
534- [ 'signature' , [ 'bin' , 64 , 64 , 0 , 127 ] ] ,
597+ 'obj' ,
598+ [
599+ [ 'hash' , [ 'bin' , 32 , 32 ] ] ,
600+ [ 'signature' , [ 'bin' , 64 , 64 , 0 , 127 ] ] ,
601+ ] ,
535602 ] ,
536- ] ] ,
603+ ] ,
537604 ] ,
538605 ] ;
539606 const result = TemplateJson . gen ( template ) as any ;
0 commit comments