Skip to content

Commit 2f4d4bf

Browse files
committed
test: add parsePacketLines tests
1 parent 308f30d commit 2f4d4bf

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

test/testParsePush.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,4 +605,78 @@ describe('parsePackFile', () => {
605605
expect(() => getCommitData(contents)).to.throw('Invalid commit data');
606606
});
607607
});
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(/Invalid packet line length 000A/);
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(/Invalid packet line length XXXX/);
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(/Invalid packet line length 0008/);
680+
});
681+
});
608682
});

0 commit comments

Comments
 (0)