@@ -253,12 +253,15 @@ describe("createSupplierAllocatorHandler", () => {
253253
254254 const messageBody = JSON . parse ( sendCall . input . MessageBody ) ;
255255 expect ( messageBody . letterEvent ) . toEqual ( preparedEvent ) ;
256- expect ( messageBody . supplierSpec ) . toEqual ( {
256+ expect ( messageBody . allocationDetails . supplierSpec ) . toEqual ( {
257257 supplierId : "supplier1" ,
258258 specId : "spec1" ,
259259 priority : 1 ,
260260 billingId : "billing1" ,
261261 } ) ;
262+ expect ( messageBody . allocationDetails . allocationStatus ) . toEqual ( {
263+ status : "PENDING" ,
264+ } ) ;
262265 } ) ;
263266
264267 test ( "parses SNS notification and sends message to SQS queue for v1 event" , async ( ) => {
@@ -281,12 +284,15 @@ describe("createSupplierAllocatorHandler", () => {
281284 expect ( mockSqsClient . send ) . toHaveBeenCalledTimes ( 1 ) ;
282285 const sendCall = ( mockSqsClient . send as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
283286 const messageBody = JSON . parse ( sendCall . input . MessageBody ) ;
284- expect ( messageBody . supplierSpec ) . toEqual ( {
287+ expect ( messageBody . allocationDetails . supplierSpec ) . toEqual ( {
285288 supplierId : "supplier1" ,
286289 specId : "spec1" ,
287290 priority : 1 ,
288291 billingId : "billing1" ,
289292 } ) ;
293+ expect ( messageBody . allocationDetails . allocationStatus ) . toEqual ( {
294+ status : "PENDING" ,
295+ } ) ;
290296 } ) ;
291297
292298 test ( "returns batch failure for Update event" , async ( ) => {
@@ -486,74 +492,105 @@ describe("createSupplierAllocatorHandler", () => {
486492 const handler = createSupplierAllocatorHandler ( mockedDeps ) ;
487493 const result = await handler ( evt , { } as any , { } as any ) ;
488494 if ( ! result ) throw new Error ( "expected BatchResponse, got void" ) ;
489- expect ( result . batchItemFailures ) . toHaveLength ( 1 ) ;
490- expect ( ( mockedDeps . logger . error as jest . Mock ) . mock . calls ) . toHaveLength ( 2 ) ;
495+ expect ( ( mockedDeps . logger . error as jest . Mock ) . mock . calls ) . toHaveLength ( 1 ) ;
491496 expect ( ( mockedDeps . logger . error as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ) . toEqual (
492497 expect . objectContaining ( {
493498 description : "Error fetching supplier from config" ,
494499 err : configError ,
495500 variantId : "lv1" ,
496501 } ) ,
497502 ) ;
503+ expect ( mockSqsClient . send ) . toHaveBeenCalledTimes ( 1 ) ;
504+ const sendCall = ( mockSqsClient . send as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
505+ expect ( sendCall ) . toBeInstanceOf ( SendMessageCommand ) ;
506+
507+ const messageBody = JSON . parse ( sendCall . input . MessageBody ) ;
508+ expect ( messageBody . letterEvent ) . toEqual ( preparedEvent ) ;
509+ expect ( messageBody . allocationDetails . supplierSpec ) . toEqual ( {
510+ supplierId : "unknown" ,
511+ specId : "unknown" ,
512+ priority : 0 ,
513+ billingId : "unknown" ,
514+ } ) ;
515+ expect ( messageBody . allocationDetails . allocationStatus ) . toEqual ( {
516+ status : "REJECTED" ,
517+ reasonCode : "NO_SUPPLIERS_AVAILABLE" ,
518+ reasonText : "Failed to retrieve supplier config" ,
519+ } ) ;
498520 } ) ;
499521
500- const rejectWith = ( mock : jest . Mock , error : Error ) =>
501- mock . mockRejectedValueOnce ( error ) ;
522+ const rejectWith = ( mock : jest . Mock , errorMessage : string ) =>
523+ mock . mockRejectedValueOnce ( new Error ( errorMessage ) ) ;
524+
525+ const throwAny = ( mock : jest . Mock ) =>
526+ mock . mockRejectedValueOnce ( "anything that is not an Error" ) ;
502527
503528 const supplierConfigErrorCases = [
504529 {
505530 name : "getVolumeGroupDetails" ,
531+ errorMessage : "Volume group retrieval failed" ,
506532 setup : ( ) =>
507533 rejectWith (
508534 supplierConfig . getVolumeGroupDetails as jest . Mock ,
509- new Error ( "Volume group retrieval failed" ) ,
535+ "Volume group retrieval failed" ,
510536 ) ,
511537 } ,
512538 {
513539 name : "eligibleSuppliers" ,
540+ errorMessage : "Eligible suppliers retrieval failed" ,
514541 setup : ( ) =>
515542 rejectWith (
516543 allocationConfig . eligibleSuppliers as jest . Mock ,
517- new Error ( "Eligible suppliers retrieval failed" ) ,
544+ "Eligible suppliers retrieval failed" ,
518545 ) ,
519546 } ,
520547 {
521548 name : "preferredSupplierPack" ,
549+ errorMessage : "Preferred supplier pack retrieval failed" ,
522550 setup : ( ) =>
523551 rejectWith (
524552 allocationConfig . preferredSupplierPack as jest . Mock ,
525- new Error ( "Preferred supplier pack retrieval failed" ) ,
553+ "Preferred supplier pack retrieval failed" ,
526554 ) ,
527555 } ,
528556 {
529557 name : "suppliersWithValidPack" ,
558+ errorMessage : "Suppliers with valid pack retrieval failed" ,
530559 setup : ( ) =>
531560 rejectWith (
532561 allocationConfig . suppliersWithValidPack as jest . Mock ,
533- new Error ( "Suppliers with valid pack retrieval failed" ) ,
562+ "Suppliers with valid pack retrieval failed" ,
534563 ) ,
535564 } ,
536565 {
537566 name : "filterSuppliersWithCapacity" ,
567+ errorMessage : "Filter suppliers with capacity failed" ,
538568 setup : ( ) =>
539569 rejectWith (
540570 allocationConfig . filterSuppliersWithCapacity as jest . Mock ,
541- new Error ( "Filter suppliers with capacity failed" ) ,
571+ "Filter suppliers with capacity failed" ,
542572 ) ,
543573 } ,
544574 {
545575 name : "selectSupplierByFactor" ,
576+ errorMessage : "Select supplier by factor failed" ,
546577 setup : ( ) =>
547578 rejectWith (
548579 allocationConfig . selectSupplierByFactor as jest . Mock ,
549- new Error ( "Select supplier by factor failed" ) ,
580+ "Select supplier by factor failed" ,
550581 ) ,
551582 } ,
583+ {
584+ name : "unexpectedError" ,
585+ errorMessage : "Unknown error" ,
586+ setup : ( ) =>
587+ throwAny ( allocationConfig . selectSupplierByFactor as jest . Mock ) ,
588+ } ,
552589 ] ;
553590
554591 test . each ( supplierConfigErrorCases ) (
555592 "logs error when %s rejects during supplier config resolution" ,
556- async ( { setup } ) => {
593+ async ( { errorMessage , setup } ) => {
557594 const preparedEvent = createPreparedV2Event ( ) ;
558595 const evt : SQSEvent = createSQSEvent ( [
559596 createSqsRecord ( "msg1" , JSON . stringify ( preparedEvent ) ) ,
@@ -566,14 +603,30 @@ describe("createSupplierAllocatorHandler", () => {
566603 const result = await handler ( evt , { } as any , { } as any ) ;
567604 if ( ! result ) throw new Error ( "expected BatchResponse, got void" ) ;
568605
569- expect ( result . batchItemFailures ) . toHaveLength ( 1 ) ;
570- expect ( ( mockedDeps . logger . error as jest . Mock ) . mock . calls ) . toHaveLength ( 2 ) ;
606+ expect ( ( mockedDeps . logger . error as jest . Mock ) . mock . calls ) . toHaveLength ( 1 ) ;
571607 expect ( ( mockedDeps . logger . error as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ) . toEqual (
572608 expect . objectContaining ( {
573609 description : "Error fetching supplier from config" ,
574610 variantId : "lv1" ,
575611 } ) ,
576612 ) ;
613+ expect ( mockSqsClient . send ) . toHaveBeenCalledTimes ( 1 ) ;
614+ const sendCall = ( mockSqsClient . send as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
615+ expect ( sendCall ) . toBeInstanceOf ( SendMessageCommand ) ;
616+
617+ const messageBody = JSON . parse ( sendCall . input . MessageBody ) ;
618+ expect ( messageBody . letterEvent ) . toEqual ( preparedEvent ) ;
619+ expect ( messageBody . allocationDetails . supplierSpec ) . toEqual ( {
620+ supplierId : "unknown" ,
621+ specId : "unknown" ,
622+ priority : 0 ,
623+ billingId : "unknown" ,
624+ } ) ;
625+ expect ( messageBody . allocationDetails . allocationStatus ) . toEqual ( {
626+ status : "REJECTED" ,
627+ reasonCode : "NO_SUPPLIERS_AVAILABLE" ,
628+ reasonText : errorMessage ,
629+ } ) ;
577630 } ,
578631 ) ;
579632
0 commit comments