@@ -605,4 +605,78 @@ describe('parsePackFile', () => {
605
605
expect ( ( ) => getCommitData ( contents ) ) . to . throw ( 'Invalid commit data' ) ;
606
606
} ) ;
607
607
} ) ;
608
+
609
+ describe ( 'parsePacketLines' , ( ) => {
610
+ it ( 'should parse multiple valid packet lines correctly and return the correct offset' , ( ) => {
611
+ const lines = [
612
+ 'line1 content' ,
613
+ 'line2 more content\nwith newline' ,
614
+ 'line3' ,
615
+ ] ;
616
+ const buffer = createPacketLineBuffer ( lines ) ; // Helper adds "0000" at the end
617
+ const expectedOffset = buffer . length ; // Should indicate the end of the buffer after flush packet
618
+ const [ parsedLines , offset ] = parsePacketLines ( buffer ) ;
619
+
620
+ expect ( parsedLines ) . to . deep . equal ( lines ) ;
621
+ expect ( offset ) . to . equal ( expectedOffset ) ;
622
+ } ) ;
623
+
624
+ it ( 'should handle an empty input buffer' , ( ) => {
625
+ const buffer = Buffer . alloc ( 0 ) ;
626
+ const [ parsedLines , offset ] = parsePacketLines ( buffer ) ;
627
+
628
+ expect ( parsedLines ) . to . deep . equal ( [ ] ) ;
629
+ expect ( offset ) . to . equal ( 0 ) ;
630
+ } ) ;
631
+
632
+ it ( 'should handle a buffer only with a flush packet' , ( ) => {
633
+ const buffer = Buffer . from ( '0000' ) ;
634
+ const [ parsedLines , offset ] = parsePacketLines ( buffer ) ;
635
+
636
+ expect ( parsedLines ) . to . deep . equal ( [ ] ) ;
637
+ expect ( offset ) . to . equal ( 4 ) ;
638
+ } ) ;
639
+
640
+ it ( 'should handle lines with null characters correctly' , ( ) => {
641
+ const lines = [ 'line1\0capability=value' , 'line2' ] ;
642
+ const buffer = createPacketLineBuffer ( lines ) ;
643
+ const expectedOffset = buffer . length ;
644
+ const [ parsedLines , offset ] = parsePacketLines ( buffer ) ;
645
+
646
+ expect ( parsedLines ) . to . deep . equal ( lines ) ;
647
+ expect ( offset ) . to . equal ( expectedOffset ) ;
648
+ } ) ;
649
+
650
+ it ( 'should stop parsing at the first flush packet' , ( ) => {
651
+ const lines = [ 'line1' , 'line2' ] ;
652
+ let buffer = createPacketLineBuffer ( lines ) ;
653
+
654
+ // Add extra data after the flush packet
655
+ const extraData = Buffer . from ( 'extradataafterflush' ) ;
656
+ buffer = Buffer . concat ( [ buffer , extraData ] ) ;
657
+
658
+ const expectedOffset = buffer . length - extraData . length ;
659
+ const [ parsedLines , offset ] = parsePacketLines ( buffer ) ;
660
+
661
+ expect ( parsedLines ) . to . deep . equal ( lines ) ;
662
+ expect ( offset ) . to . equal ( expectedOffset ) ;
663
+ } ) ;
664
+
665
+ it ( 'should throw an error if a packet line length exceeds buffer bounds' , ( ) => {
666
+ // 000A -> length 10, but actual line length is only 3 bytes
667
+ const invalidLengthBuffer = Buffer . from ( '000Aabc' ) ;
668
+ expect ( ( ) => parsePacketLines ( invalidLengthBuffer ) ) . to . throw ( / I n v a l i d p a c k e t l i n e l e n g t h 0 0 0 A / ) ;
669
+ } ) ;
670
+
671
+ it ( 'should throw an error for non-hex length prefix' , ( ) => {
672
+ const invalidHexBuffer = Buffer . from ( 'XXXXline' ) ;
673
+ expect ( ( ) => parsePacketLines ( invalidHexBuffer ) ) . to . throw ( / I n v a l i d p a c k e t l i n e l e n g t h X X X X / ) ;
674
+ } ) ;
675
+
676
+ it ( 'should handle buffer ending exactly after a valid line length without content' , ( ) => {
677
+ // 0008 -> length 8, but buffer ends after header (no content)
678
+ const incompleteBuffer = Buffer . from ( '0008' ) ;
679
+ expect ( ( ) => parsePacketLines ( incompleteBuffer ) ) . to . throw ( / I n v a l i d p a c k e t l i n e l e n g t h 0 0 0 8 / ) ;
680
+ } ) ;
681
+ } ) ;
608
682
} ) ;
0 commit comments