@@ -559,6 +559,187 @@ describe('Shell BSON', function () {
559559 expect . fail ( 'Expecting error, nothing thrown' ) ;
560560 } ) ;
561561 } ) ;
562+ describe ( 'LegacyJavaUUID' , function ( ) {
563+ it ( 'creates a Binary with SUBTYPE_UUID_OLD' , function ( ) {
564+ const uuid = shellBson . LegacyJavaUUID ( '0123456789abcdef0123456789abcdef' ) ;
565+ expect ( uuid . sub_type ) . to . equal ( 3 ) ; // SUBTYPE_UUID_OLD
566+ } ) ;
567+
568+ it ( 'strips dashes from input' , function ( ) {
569+ expect (
570+ shellBson . LegacyJavaUUID ( '01234567-89ab-cdef-0123-456789abcdef' ) . value ( )
571+ ) . to . deep . equal (
572+ shellBson . LegacyJavaUUID ( '0123456789abcdef0123456789abcdef' ) . value ( )
573+ ) ;
574+ } ) ;
575+
576+ it ( 'generates a random UUID when no arguments are passed' , function ( ) {
577+ const uuid = shellBson . LegacyJavaUUID ( ) ;
578+ expect ( uuid . sub_type ) . to . equal ( 3 ) ;
579+ expect ( uuid . value ( ) . length ) . to . equal ( 16 ) ;
580+ } ) ;
581+
582+ it ( 'has help and other metadata' , function ( ) {
583+ const uuid = shellBson . LegacyJavaUUID ( '0123456789abcdef0123456789abcdef' ) ;
584+ expect ( uuid ?. help ?. type ) . to . equal ( 'Help' ) ;
585+ expect ( uuid ?. help ?.( ) . type ) . to . equal ( 'Help' ) ;
586+ expect ( ( uuid as any ) . serverVersions ) . to . deep . equal ( ALL_SERVER_VERSIONS ) ;
587+ } ) ;
588+
589+ it ( 'performs byte-swapping for Java compatibility' , function ( ) {
590+ // Test that the byte order is swapped correctly for Java UUID format
591+ const uuid = shellBson . LegacyJavaUUID ( '0123456789abcdef0123456789abcdef' ) ;
592+ // The MSB and LSB should be byte-swapped according to Java's UUID format
593+ expect ( uuid . toString ( 'hex' ) ) . to . equal ( 'efcdab8967452301efcdab8967452301' ) ;
594+ } ) ;
595+
596+ it ( 'errors for wrong type of arg 1' , function ( ) {
597+ try {
598+ ( shellBson . LegacyJavaUUID as any ) ( 1 ) ;
599+ } catch ( e : any ) {
600+ return expect ( e . message ) . to . contain ( 'string, got number' ) ;
601+ }
602+ expect . fail ( 'Expecting error, nothing thrown' ) ;
603+ } ) ;
604+ } ) ;
605+
606+ describe ( 'LegacyCSharpUUID' , function ( ) {
607+ it ( 'creates a Binary with SUBTYPE_UUID_OLD' , function ( ) {
608+ const uuid = shellBson . LegacyCSharpUUID (
609+ '0123456789abcdef0123456789abcdef'
610+ ) ;
611+ expect ( uuid . sub_type ) . to . equal ( 3 ) ; // SUBTYPE_UUID_OLD
612+ } ) ;
613+
614+ it ( 'strips dashes from input' , function ( ) {
615+ expect (
616+ shellBson
617+ . LegacyCSharpUUID ( '01234567-89ab-cdef-0123-456789abcdef' )
618+ . value ( )
619+ ) . to . deep . equal (
620+ shellBson . LegacyCSharpUUID ( '0123456789abcdef0123456789abcdef' ) . value ( )
621+ ) ;
622+ } ) ;
623+
624+ it ( 'generates a random UUID when no arguments are passed' , function ( ) {
625+ const uuid = shellBson . LegacyCSharpUUID ( ) ;
626+ expect ( uuid . sub_type ) . to . equal ( 3 ) ;
627+ expect ( uuid . value ( ) . length ) . to . equal ( 16 ) ;
628+ } ) ;
629+
630+ it ( 'has help and other metadata' , function ( ) {
631+ const uuid = shellBson . LegacyCSharpUUID (
632+ '0123456789abcdef0123456789abcdef'
633+ ) ;
634+ expect ( uuid ?. help ?. type ) . to . equal ( 'Help' ) ;
635+ expect ( uuid ?. help ?.( ) . type ) . to . equal ( 'Help' ) ;
636+ expect ( ( uuid as any ) . serverVersions ) . to . deep . equal ( ALL_SERVER_VERSIONS ) ;
637+ } ) ;
638+
639+ it ( 'performs byte-swapping for C# compatibility' , function ( ) {
640+ // Test that the byte order is swapped correctly for C# UUID format
641+ const uuid = shellBson . LegacyCSharpUUID (
642+ '0123456789abcdef0123456789abcdef'
643+ ) ;
644+ // The first three groups should be byte-swapped according to C#'s GUID format
645+ expect ( uuid . toString ( 'hex' ) ) . to . equal ( '67452301ab89efcd0123456789abcdef' ) ;
646+ } ) ;
647+
648+ it ( 'errors for wrong type of arg 1' , function ( ) {
649+ try {
650+ ( shellBson . LegacyCSharpUUID as any ) ( 1 ) ;
651+ } catch ( e : any ) {
652+ return expect ( e . message ) . to . contain ( 'string, got number' ) ;
653+ }
654+ expect . fail ( 'Expecting error, nothing thrown' ) ;
655+ } ) ;
656+ } ) ;
657+
658+ describe ( 'LegacyPythonUUID' , function ( ) {
659+ it ( 'creates a Binary with SUBTYPE_UUID_OLD' , function ( ) {
660+ const uuid = shellBson . LegacyPythonUUID (
661+ '0123456789abcdef0123456789abcdef'
662+ ) ;
663+ expect ( uuid . sub_type ) . to . equal ( 3 ) ; // SUBTYPE_UUID_OLD
664+ } ) ;
665+
666+ it ( 'strips dashes from input' , function ( ) {
667+ expect (
668+ shellBson
669+ . LegacyPythonUUID ( '01234567-89ab-cdef-0123-456789abcdef' )
670+ . value ( )
671+ ) . to . deep . equal (
672+ shellBson . LegacyPythonUUID ( '0123456789abcdef0123456789abcdef' ) . value ( )
673+ ) ;
674+ } ) ;
675+
676+ it ( 'generates a random UUID when no arguments are passed' , function ( ) {
677+ const uuid = shellBson . LegacyPythonUUID ( ) ;
678+ expect ( uuid . sub_type ) . to . equal ( 3 ) ;
679+ expect ( uuid . value ( ) . length ) . to . equal ( 16 ) ;
680+ } ) ;
681+
682+ it ( 'has help and other metadata' , function ( ) {
683+ const uuid = shellBson . LegacyPythonUUID (
684+ '0123456789abcdef0123456789abcdef'
685+ ) ;
686+ expect ( uuid ?. help ?. type ) . to . equal ( 'Help' ) ;
687+ expect ( uuid ?. help ?.( ) . type ) . to . equal ( 'Help' ) ;
688+ expect ( ( uuid as any ) . serverVersions ) . to . deep . equal ( ALL_SERVER_VERSIONS ) ;
689+ } ) ;
690+
691+ it ( 'uses standard byte order (no swapping)' , function ( ) {
692+ // Python UUID uses standard network byte order (big-endian, no swapping)
693+ const uuid = shellBson . LegacyPythonUUID (
694+ '0123456789abcdef0123456789abcdef'
695+ ) ;
696+ expect ( uuid . toString ( 'hex' ) ) . to . equal ( '0123456789abcdef0123456789abcdef' ) ;
697+ } ) ;
698+
699+ it ( 'errors for wrong type of arg 1' , function ( ) {
700+ try {
701+ ( shellBson . LegacyPythonUUID as any ) ( 1 ) ;
702+ } catch ( e : any ) {
703+ return expect ( e . message ) . to . contain ( 'string, got number' ) ;
704+ }
705+ expect . fail ( 'Expecting error, nothing thrown' ) ;
706+ } ) ;
707+ } ) ;
708+
709+ describe ( 'Legacy UUID comparison' , function ( ) {
710+ const testUuid = '0123456789abcdef0123456789abcdef' ;
711+
712+ it ( 'each legacy UUID type produces different byte representations' , function ( ) {
713+ const javaUuid = shellBson . LegacyJavaUUID ( testUuid ) ;
714+ const csharpUuid = shellBson . LegacyCSharpUUID ( testUuid ) ;
715+ const pythonUuid = shellBson . LegacyPythonUUID ( testUuid ) ;
716+
717+ const javaBytes = javaUuid . toString ( 'hex' ) ;
718+ const csharpBytes = csharpUuid . toString ( 'hex' ) ;
719+ const pythonBytes = pythonUuid . toString ( 'hex' ) ;
720+
721+ // They should all be different due to different byte-swapping
722+ expect ( javaBytes ) . to . not . equal ( csharpBytes ) ;
723+ expect ( javaBytes ) . to . not . equal ( pythonBytes ) ;
724+ expect ( csharpBytes ) . to . not . equal ( pythonBytes ) ;
725+
726+ // Python should match the original (no swapping)
727+ expect ( pythonBytes ) . to . equal ( testUuid ) ;
728+ } ) ;
729+
730+ it ( 'all legacy UUID types use SUBTYPE_UUID_OLD' , function ( ) {
731+ const javaUuid = shellBson . LegacyJavaUUID ( testUuid ) ;
732+ const csharpUuid = shellBson . LegacyCSharpUUID ( testUuid ) ;
733+ const pythonUuid = shellBson . LegacyPythonUUID ( testUuid ) ;
734+ const uuid = shellBson . UUID ( testUuid ) ;
735+
736+ expect ( javaUuid . sub_type ) . to . equal ( 3 ) ;
737+ expect ( csharpUuid . sub_type ) . to . equal ( 3 ) ;
738+ expect ( pythonUuid . sub_type ) . to . equal ( 3 ) ;
739+ expect ( uuid . sub_type ) . to . equal ( 4 ) ;
740+ } ) ;
741+ } ) ;
742+
562743 describe ( 'MD5' , function ( ) {
563744 let b : any ;
564745 let h : any ;
0 commit comments