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