@@ -50,6 +50,13 @@ describe("eligibleSuppliers", () => {
5050 allocationPercentage : 30 ,
5151 status : "PROD" ,
5252 } as SupplierAllocation ,
53+ {
54+ id : "allocation-3" ,
55+ volumeGroup : "volume-group-1" ,
56+ supplier : "supplier-3" ,
57+ allocationPercentage : 20 ,
58+ status : "PROD" ,
59+ } as SupplierAllocation ,
5360 ] ;
5461
5562 mockSuppliers = [
@@ -66,7 +73,7 @@ describe("eligibleSuppliers", () => {
6673 ] ;
6774
6875 mockDeps = {
69- logger : { info : jest . fn ( ) , error : jest . fn ( ) } ,
76+ logger : { info : jest . fn ( ) , error : jest . fn ( ) , warn : jest . fn ( ) } ,
7077 } as unknown as jest . Mocked < Deps > ;
7178 } ) ;
7279
@@ -110,7 +117,7 @@ describe("eligibleSuppliers", () => {
110117 await eligibleSuppliers ( mockVolumeGroup , mockDeps ) ;
111118
112119 expect ( supplierConfigService . getSupplierDetails ) . toHaveBeenCalledWith (
113- [ "supplier-1" , "supplier-2" ] ,
120+ [ "supplier-1" , "supplier-2" , "supplier-3" ] ,
114121 mockDeps ,
115122 ) ;
116123 } ) ;
@@ -133,6 +140,38 @@ describe("eligibleSuppliers", () => {
133140 ) ;
134141 } ) ;
135142
143+ it ( "should log a warning if allocation percentages do not sum to 100%" , async ( ) => {
144+ const invalidAllocations = [
145+ {
146+ id : "allocation-1" ,
147+ volumeGroup : "volume-group-1" ,
148+ supplier : "supplier-1" ,
149+ allocationPercentage : 40 ,
150+ status : "PROD" ,
151+ } as SupplierAllocation ,
152+ {
153+ id : "allocation-2" ,
154+ volumeGroup : "volume-group-1" ,
155+ supplier : "supplier-2" ,
156+
157+ allocationPercentage : 30 ,
158+ status : "PROD" ,
159+ } as SupplierAllocation ,
160+ ] ;
161+ (
162+ supplierConfigService . getSupplierAllocationsForVolumeGroup as jest . Mock
163+ ) . mockResolvedValue ( invalidAllocations ) ;
164+ ( supplierConfigService . getSupplierDetails as jest . Mock ) . mockResolvedValue (
165+ mockSuppliers ,
166+ ) ;
167+ await eligibleSuppliers ( mockVolumeGroup , mockDeps ) ;
168+ expect ( mockDeps . logger . warn ) . toHaveBeenCalledWith ( {
169+ description : "Supplier allocations do not sum to 100%" ,
170+ volumeGroupId : "volume-group-1" ,
171+ allocationPercentageSum : 70 ,
172+ } ) ;
173+ } ) ;
174+
136175 it ( "should propagate errors from getSupplierAllocationsForVolumeGroup" , async ( ) => {
137176 const error = new Error ( "Database error" ) ;
138177 (
@@ -804,6 +843,42 @@ describe("selectSupplierByFactor", () => {
804843 expect ( result ) . toBe ( "supplier-2" ) ;
805844 } ) ;
806845
846+ it ( "should log an error if a supplier allocation has zero percentage and exclude it from factor calculation" , async ( ) => {
847+ const allocationsWithZeroPercentage = [
848+ mockSupplierAllocations [ 0 ] ,
849+ {
850+ id : "allocation-zero" ,
851+ volumeGroup : "volume-group-1" ,
852+ supplier : "supplier-2" ,
853+ allocationPercentage : 0 ,
854+ status : "PROD" ,
855+ } as SupplierAllocation ,
856+ mockSupplierAllocations [ 2 ] ,
857+ ] ;
858+
859+ const mockSupplierFactors = [
860+ { supplierId : "supplier-1" , factor : 0.5 } ,
861+ { supplierId : "supplier-3" , factor : 0.8 } ,
862+ ] ;
863+
864+ (
865+ supplierQuotasService . calculateSupplierAllocatedFactor as jest . Mock
866+ ) . mockResolvedValue ( mockSupplierFactors ) ;
867+
868+ const result = await selectSupplierByFactor (
869+ mockSuppliers ,
870+ allocationsWithZeroPercentage ,
871+ mockDeps ,
872+ ) ;
873+
874+ expect ( mockDeps . logger . error ) . toHaveBeenCalledWith ( {
875+ description : "Supplier allocation has zero percentage" ,
876+ supplierId : "supplier-2" ,
877+ allocationPercentage : 0 ,
878+ } ) ;
879+ expect ( result ) . toBe ( "supplier-1" ) ;
880+ } ) ;
881+
807882 it ( "should return first supplier when all factors are equal" , async ( ) => {
808883 const mockSupplierFactors = [
809884 { supplierId : "supplier-1" , factor : 0.5 } ,
0 commit comments