@@ -334,6 +334,119 @@ describe('parsePackFile', () => {
334
334
expect ( step . error ) . to . be . true ;
335
335
expect ( step . errorMessage ) . to . include ( 'Invalid commit data: Missing tree' ) ;
336
336
} ) ;
337
+
338
+ it ( 'should add error step if data after flush packet does not start with "PACK"' , async ( ) => {
339
+ const oldCommit = 'a' . repeat ( 40 ) ;
340
+ const newCommit = 'b' . repeat ( 40 ) ;
341
+ const ref = 'refs/heads/main' ;
342
+ const packetLines = [ `${ oldCommit } ${ newCommit } ${ ref } \0capa\n` ] ;
343
+
344
+ const packetLineBuffer = createPacketLineBuffer ( packetLines ) ;
345
+ const garbageData = Buffer . from ( 'NOT PACK DATA' ) ;
346
+ req . body = Buffer . concat ( [ packetLineBuffer , garbageData ] ) ;
347
+
348
+ const result = await exec ( req , action ) ;
349
+ expect ( result ) . to . equal ( action ) ;
350
+
351
+ const step = action . steps [ 0 ] ;
352
+ expect ( step . stepName ) . to . equal ( 'parsePackFile' ) ;
353
+ expect ( step . error ) . to . be . true ;
354
+ expect ( step . errorMessage ) . to . include ( 'Invalid PACK data structure' ) ;
355
+ expect ( step . errorMessage ) . to . not . include ( 'PACK data is missing' ) ;
356
+
357
+ expect ( action . branch ) . to . equal ( ref ) ;
358
+ expect ( action . setCommit . calledOnceWith ( oldCommit , newCommit ) ) . to . be . true ;
359
+ } ) ;
360
+
361
+ it ( 'should correctly identify PACK data even if "PACK" appears in packet lines' , async ( ) => {
362
+ const oldCommit = 'a' . repeat ( 40 ) ;
363
+ const newCommit = 'b' . repeat ( 40 ) ;
364
+ const ref = 'refs/heads/develop' ;
365
+ const packetLines = [
366
+ `${ oldCommit } ${ newCommit } ${ ref } \0capa\n` ,
367
+ 'some other data containing PACK keyword' , // Include "PACK" within a packet line's content
368
+ ] ;
369
+
370
+ const commitContent = `tree 1234567890abcdef1234567890abcdef12345678
371
+ parent ${ oldCommit }
372
+ author Test Author <[email protected] > 1234567890 +0000
373
+ committer Test Committer <[email protected] > 1234567890 +0000
374
+
375
+ Test commit message with PACK inside` ;
376
+ const samplePackBuffer = createSamplePackBuffer ( 1 , commitContent , 1 ) ;
377
+
378
+ zlibInflateStub . returns ( Buffer . from ( commitContent , 'utf8' ) ) ;
379
+
380
+ const packetLineBuffer = createPacketLineBuffer ( packetLines ) ;
381
+ req . body = Buffer . concat ( [ packetLineBuffer , samplePackBuffer ] ) ;
382
+
383
+ const result = await exec ( req , action ) ;
384
+ expect ( result ) . to . equal ( action ) ;
385
+ expect ( action . steps . length ) . to . equal ( 1 ) ;
386
+
387
+ // Check that the step was added correctly, and no error present
388
+ const step = action . steps [ 0 ] ;
389
+ expect ( step . stepName ) . to . equal ( 'parsePackFile' ) ;
390
+ expect ( step . error ) . to . be . false ;
391
+ expect ( step . errorMessage ) . to . be . null ;
392
+
393
+ // Verify action properties were parsed correctly
394
+ expect ( action . branch ) . to . equal ( ref ) ;
395
+ expect ( action . setCommit . calledOnceWith ( oldCommit , newCommit ) ) . to . be . true ;
396
+ expect ( action . commitFrom ) . to . equal ( oldCommit ) ;
397
+ expect ( action . commitTo ) . to . equal ( newCommit ) ;
398
+ expect ( action . commitData ) . to . be . an ( 'array' ) . with . lengthOf ( 1 ) ;
399
+ expect ( action . commitData [ 0 ] . message ) . to . equal ( 'Test commit message with PACK inside' ) ;
400
+ expect ( action . commitData [ 0 ] . committer ) . to . equal ( 'Test Committer' ) ;
401
+ expect ( action . user ) . to . equal ( 'Test Committer' ) ;
402
+ } ) ;
403
+
404
+ it ( 'should handle PACK data starting immediately after flush packet' , async ( ) => {
405
+ const oldCommit = 'a' . repeat ( 40 ) ;
406
+ const newCommit = 'b' . repeat ( 40 ) ;
407
+ const ref = 'refs/heads/master' ;
408
+ const packetLines = [ `${ oldCommit } ${ newCommit } ${ ref } \0` ] ;
409
+
410
+ const commitContent = `tree 1234567890abcdef1234567890abcdef12345678
411
+ parent ${ oldCommit }
412
+ author Test Author <[email protected] > 1234567890 +0000
413
+ committer Test Committer <[email protected] > 1234567890 +0000
414
+
415
+ Commit A` ;
416
+ const samplePackBuffer = createSamplePackBuffer ( 1 , commitContent , 1 ) ;
417
+ zlibInflateStub . returns ( Buffer . from ( commitContent , 'utf8' ) ) ;
418
+
419
+ const packetLineBuffer = createPacketLineBuffer ( packetLines ) ;
420
+ req . body = Buffer . concat ( [ packetLineBuffer , samplePackBuffer ] ) ;
421
+
422
+ const result = await exec ( req , action ) ;
423
+
424
+ expect ( result ) . to . equal ( action ) ;
425
+ const step = action . steps [ 0 ] ;
426
+ expect ( step . error ) . to . be . false ;
427
+ expect ( action . commitData [ 0 ] . message ) . to . equal ( 'Commit A' ) ;
428
+ } ) ;
429
+
430
+ it ( 'should add error step if PACK header parsing fails (getPackMeta with wrong signature)' , async ( ) => {
431
+ const oldCommit = 'a' . repeat ( 40 ) ;
432
+ const newCommit = 'b' . repeat ( 40 ) ;
433
+ const ref = 'refs/heads/fix' ;
434
+ const packetLines = [ `${ oldCommit } ${ newCommit } ${ ref } \0capa\n` ] ;
435
+
436
+ const packetLineBuffer = createPacketLineBuffer ( packetLines ) ;
437
+ const badPackBuffer = createSamplePackBuffer ( ) ;
438
+ badPackBuffer . write ( 'AAAA' , 0 , 4 , 'utf-8' ) ; // Invalid signature, should be 'PACK'
439
+
440
+ req . body = Buffer . concat ( [ packetLineBuffer , badPackBuffer ] ) ;
441
+
442
+ const result = await exec ( req , action ) ;
443
+ expect ( result ) . to . equal ( action ) ;
444
+
445
+ const step = action . steps [ 0 ] ;
446
+ expect ( step . stepName ) . to . equal ( 'parsePackFile' ) ;
447
+ expect ( step . error ) . to . be . true ;
448
+ expect ( step . errorMessage ) . to . include ( 'Invalid PACK data structure' ) ;
449
+ } ) ;
337
450
} ) ;
338
451
339
452
describe ( 'getPackMeta' , ( ) => {
0 commit comments