@@ -368,71 +368,103 @@ describe("generatePreviewAlias", () => {
368
368
} ) ;
369
369
370
370
it ( "sanitizes branch names correctly" , ( ) => {
371
+ const scriptName = "worker" ;
371
372
mockExecSync
372
373
. mockImplementationOnce ( ( ) => { } ) // is-inside-work-tree
373
374
. mockImplementationOnce ( ( ) => Buffer . from ( "feat/awesome-feature" ) ) ;
374
375
375
- const result = generatePreviewAlias ( "worker" ) ;
376
+ const result = generatePreviewAlias ( scriptName ) ;
376
377
expect ( result ) . toBe ( "feat-awesome-feature" ) ;
378
+ expect ( result ) . not . toBeUndefined ( ) ;
379
+ expect ( ( scriptName + "-" + result ) . length ) . toBeLessThanOrEqual ( 63 ) ;
377
380
} ) ;
378
381
379
- it ( "returns undefined for long branch names which don't fit within DNS label constraints" , ( ) => {
380
- const longBranch = "a" . repeat ( 70 ) ;
382
+ it ( "truncates and hashes long branch names that don't fit within DNS label constraints" , ( ) => {
383
+ const scriptName = "very-long-worker-name" ;
384
+ const longBranch = "a" . repeat ( 62 ) ;
381
385
mockExecSync
382
386
. mockImplementationOnce ( ( ) => { } ) // is-inside-work-tree
383
387
. mockImplementationOnce ( ( ) => Buffer . from ( longBranch ) ) ;
384
388
385
- const result = generatePreviewAlias ( "worker" ) ;
386
- expect ( result ) . toBeUndefined ( ) ;
389
+ const result = generatePreviewAlias ( scriptName ) ;
390
+
391
+ // Should be truncated to fit: max 63 - 21 - 1 = 41 chars
392
+ // With 4-char hash + hyphen, we have 41 - 4 - 1 = 36 chars for the prefix
393
+ expect ( result ) . toBeDefined ( ) ;
394
+ expect ( result ) . toMatch ( / ^ a { 36 } - [ a - f 0 - 9 ] { 4 } $ / ) ;
395
+ expect ( result ?. length ) . toBe ( 41 ) ;
396
+ expect ( result ) . not . toBeUndefined ( ) ;
397
+ expect ( ( scriptName + "-" + result ) . length ) . toBeLessThanOrEqual ( 63 ) ;
387
398
} ) ;
388
399
389
400
it ( "handles multiple, leading, and trailing dashes" , ( ) => {
401
+ const scriptName = "testscript" ;
390
402
mockExecSync
391
403
. mockImplementationOnce ( ( ) => { } ) // is-inside-work-tree
392
404
. mockImplementationOnce ( ( ) => Buffer . from ( "--some--branch--name--" ) ) ;
393
405
394
- const result = generatePreviewAlias ( "testscript" ) ;
406
+ const result = generatePreviewAlias ( scriptName ) ;
395
407
expect ( result ) . toBe ( "some-branch-name" ) ;
408
+ expect ( result ) . not . toBeUndefined ( ) ;
409
+ expect ( ( scriptName + "-" + result ) . length ) . toBeLessThanOrEqual ( 63 ) ;
396
410
} ) ;
397
411
398
412
it ( "lowercases branch names" , ( ) => {
413
+ const scriptName = "testscript" ;
399
414
mockExecSync
400
415
. mockImplementationOnce ( ( ) => { } ) // is-inside-work-tree
401
416
. mockImplementationOnce ( ( ) => Buffer . from ( "HEAD/feature/work" ) ) ;
402
417
403
- const result = generatePreviewAlias ( "testscript" ) ;
418
+ const result = generatePreviewAlias ( scriptName ) ;
404
419
expect ( result ) . toBe ( "head-feature-work" ) ;
420
+ expect ( result ) . not . toBeUndefined ( ) ;
421
+ expect ( ( scriptName + "-" + result ) . length ) . toBeLessThanOrEqual ( 63 ) ;
405
422
} ) ;
406
423
407
424
it ( "Generates from workers ci branch" , ( ) => {
425
+ const scriptName = "testscript" ;
408
426
vi . stubEnv ( "WORKERS_CI_BRANCH" , "some/debug-branch" ) ;
409
427
410
- const result = generatePreviewAlias ( "testscript" ) ;
428
+ const result = generatePreviewAlias ( scriptName ) ;
411
429
expect ( result ) . toBe ( "some-debug-branch" ) ;
430
+ expect ( result ) . not . toBeUndefined ( ) ;
431
+ expect ( ( scriptName + "-" + result ) . length ) . toBeLessThanOrEqual ( 63 ) ;
412
432
} ) ;
413
433
414
- it ( "Does not produce an alias from long workers ci branch name" , ( ) => {
434
+ it ( "Truncates and hashes long workers ci branch names" , ( ) => {
435
+ const scriptName = "testscript" ;
415
436
vi . stubEnv (
416
437
"WORKERS_CI_BRANCH" ,
417
438
"some/really-really-really-really-really-long-branch-name"
418
439
) ;
419
440
420
- const result = generatePreviewAlias ( "testscript" ) ;
421
- expect ( result ) . toBeUndefined ( ) ;
441
+ const result = generatePreviewAlias ( scriptName ) ;
442
+ expect ( result ) . toMatch (
443
+ / ^ s o m e - r e a l l y - r e a l l y - r e a l l y - r e a l l y - r e a l l y - l o n g - b r - [ a - f 0 - 9 ] { 4 } $ /
444
+ ) ;
445
+ expect ( result ?. length ) . toBe ( 52 ) ;
446
+ expect ( result ) . not . toBeUndefined ( ) ;
447
+ expect ( ( scriptName + "-" + result ) . length ) . toBeLessThanOrEqual ( 63 ) ;
422
448
} ) ;
423
449
424
450
it ( "Strips leading dashes from branch name" , ( ) => {
451
+ const scriptName = "testscript" ;
425
452
vi . stubEnv ( "WORKERS_CI_BRANCH" , "-some-branch-name" ) ;
426
453
427
- const result = generatePreviewAlias ( "testscript" ) ;
454
+ const result = generatePreviewAlias ( scriptName ) ;
428
455
expect ( result ) . toBe ( "some-branch-name" ) ;
456
+ expect ( result ) . not . toBeUndefined ( ) ;
457
+ expect ( ( scriptName + "-" + result ) . length ) . toBeLessThanOrEqual ( 63 ) ;
429
458
} ) ;
430
459
431
460
it ( "Removes concurrent dashes from branch name" , ( ) => {
461
+ const scriptName = "testscript" ;
432
462
vi . stubEnv ( "WORKERS_CI_BRANCH" , "some----branch-----name" ) ;
433
463
434
- const result = generatePreviewAlias ( "testscript" ) ;
464
+ const result = generatePreviewAlias ( scriptName ) ;
435
465
expect ( result ) . toBe ( "some-branch-name" ) ;
466
+ expect ( result ) . not . toBeUndefined ( ) ;
467
+ expect ( ( scriptName + "-" + result ) . length ) . toBeLessThanOrEqual ( 63 ) ;
436
468
} ) ;
437
469
438
470
it ( "Does not produce an alias with leading numbers" , ( ) => {
@@ -441,4 +473,33 @@ describe("generatePreviewAlias", () => {
441
473
const result = generatePreviewAlias ( "testscript" ) ;
442
474
expect ( result ) . toBeUndefined ( ) ;
443
475
} ) ;
476
+
477
+ it ( "returns undefined when script name is too long to allow any alias" , ( ) => {
478
+ const scriptName = "a" . repeat ( 60 ) ;
479
+ mockExecSync
480
+ . mockImplementationOnce ( ( ) => { } ) // is-inside-work-tree
481
+ . mockImplementationOnce ( ( ) => Buffer . from ( "short-branch" ) ) ;
482
+
483
+ const result = generatePreviewAlias ( scriptName ) ;
484
+ expect ( result ) . toBeUndefined ( ) ;
485
+ } ) ;
486
+
487
+ it ( "handles complex branch names with truncation" , ( ) => {
488
+ const scriptName = "myworker" ;
489
+ const complexBranch =
490
+ "feat/JIRA-12345/implement-awesome-new-feature-with-detail" ;
491
+ mockExecSync
492
+ . mockImplementationOnce ( ( ) => { } ) // is-inside-work-tree
493
+ . mockImplementationOnce ( ( ) => Buffer . from ( complexBranch ) ) ;
494
+
495
+ const result = generatePreviewAlias ( scriptName ) ;
496
+
497
+ expect ( result ) . toBeDefined ( ) ;
498
+ expect ( result ) . toMatch (
499
+ / ^ f e a t - j i r a - 1 2 3 4 5 - i m p l e m e n t - a w e s o m e - n e w - f e a t u r e - w i t - [ a - f 0 - 9 ] { 4 } $ /
500
+ ) ;
501
+ expect ( result ?. length ) . toBe ( 54 ) ;
502
+ expect ( result ) . not . toBeUndefined ( ) ;
503
+ expect ( ( scriptName + "-" + result ) . length ) . toBeLessThanOrEqual ( 63 ) ;
504
+ } ) ;
444
505
} ) ;
0 commit comments