@@ -839,4 +839,117 @@ describe('Script Generation', () => {
839839 }
840840 } ) ;
841841 } ) ;
842+
843+ describe ( 'Graceful Handling' , ( ) => {
844+ it ( 'should default invalid probability to 1.0' , ( ) => {
845+ const schema = {
846+ field1 : {
847+ mongoType : 'string' ,
848+ fakerMethod : 'lorem.word' ,
849+ fakerArgs : [ ] ,
850+ probability : 1.5 , // Invalid - should default to 1.0
851+ } ,
852+ field2 : {
853+ mongoType : 'string' ,
854+ fakerMethod : 'lorem.word' ,
855+ fakerArgs : [ ] ,
856+ probability : - 0.5 , // Invalid - should default to 1.0
857+ } ,
858+ field3 : {
859+ mongoType : 'string' ,
860+ fakerMethod : 'lorem.word' ,
861+ fakerArgs : [ ] ,
862+ probability : 'invalid' as any , // Invalid - should default to 1.0
863+ } ,
864+ } ;
865+
866+ const result = generateScript ( schema , {
867+ databaseName : 'test' ,
868+ collectionName : 'test' ,
869+ documentCount : 1 ,
870+ } ) ;
871+
872+ expect ( result . success ) . to . equal ( true ) ;
873+ if ( result . success ) {
874+ // All fields should be treated as probability 1.0 (always present)
875+ expect ( result . script ) . to . contain ( 'field1: faker.lorem.word()' ) ;
876+ expect ( result . script ) . to . contain ( 'field2: faker.lorem.word()' ) ;
877+ expect ( result . script ) . to . contain ( 'field3: faker.lorem.word()' ) ;
878+ expect ( result . script ) . not . to . contain ( 'Math.random()' ) ;
879+ }
880+ } ) ;
881+
882+ it ( 'should handle field names with brackets (non-array)' , ( ) => {
883+ const schema = {
884+ 'settings[theme]' : createFieldMapping ( 'lorem.word' ) ,
885+ 'data[0]' : createFieldMapping ( 'lorem.word' ) ,
886+ 'bracket]field' : createFieldMapping ( 'lorem.word' ) ,
887+ '[metadata' : createFieldMapping ( 'lorem.word' ) ,
888+ } ;
889+
890+ const result = generateScript ( schema , {
891+ databaseName : 'test' ,
892+ collectionName : 'test' ,
893+ documentCount : 1 ,
894+ } ) ;
895+
896+ expect ( result . success ) . to . equal ( true ) ;
897+ if ( result . success ) {
898+ // All fields should be treated as regular field names, not arrays
899+ expect ( result . script ) . to . contain ( 'settings[theme]: faker.lorem.word()' ) ;
900+ expect ( result . script ) . to . contain ( 'data[0]: faker.lorem.word()' ) ;
901+ expect ( result . script ) . to . contain ( 'bracket]field: faker.lorem.word()' ) ;
902+ expect ( result . script ) . to . contain ( '[metadata: faker.lorem.word()' ) ;
903+ expect ( result . script ) . not . to . contain ( 'Array.from' ) ;
904+ }
905+ } ) ;
906+
907+ it ( 'should handle field names with [] in middle (not array notation)' , ( ) => {
908+ const schema = {
909+ 'squareBrackets[]InMiddle' : createFieldMapping ( 'lorem.word' ) ,
910+ 'field[]WithMore' : createFieldMapping ( 'lorem.word' ) ,
911+ 'start[]middle[]end' : createFieldMapping ( 'lorem.word' ) ,
912+ } ;
913+
914+ const result = generateScript ( schema , {
915+ databaseName : 'test' ,
916+ collectionName : 'test' ,
917+ documentCount : 1 ,
918+ } ) ;
919+
920+ expect ( result . success ) . to . equal ( true ) ;
921+ if ( result . success ) {
922+ // These should be treated as regular field names, not arrays
923+ expect ( result . script ) . to . contain (
924+ 'squareBrackets[]InMiddle: faker.lorem.word()'
925+ ) ;
926+ expect ( result . script ) . to . contain ( 'field[]WithMore: faker.lorem.word()' ) ;
927+ expect ( result . script ) . to . contain (
928+ 'start[]middle[]end: faker.lorem.word()'
929+ ) ;
930+ expect ( result . script ) . not . to . contain ( 'Array.from' ) ;
931+ }
932+ } ) ;
933+
934+ it ( 'should still handle real array notation correctly' , ( ) => {
935+ const schema = {
936+ 'realArray[]' : createFieldMapping ( 'lorem.word' ) ,
937+ 'nestedArray[].field' : createFieldMapping ( 'lorem.word' ) ,
938+ } ;
939+
940+ const result = generateScript ( schema , {
941+ databaseName : 'test' ,
942+ collectionName : 'test' ,
943+ documentCount : 1 ,
944+ } ) ;
945+
946+ expect ( result . success ) . to . equal ( true ) ;
947+ if ( result . success ) {
948+ // These should be treated as arrays
949+ expect ( result . script ) . to . contain ( 'Array.from' ) ;
950+ expect ( result . script ) . to . contain ( 'realArray: Array.from' ) ;
951+ expect ( result . script ) . to . contain ( 'nestedArray: Array.from' ) ;
952+ }
953+ } ) ;
954+ } ) ;
842955} ) ;
0 commit comments