From 2d82136c4b703d3ff4b1ff9f809b4f4fc476ac38 Mon Sep 17 00:00:00 2001 From: Matt Frisbie Date: Tue, 15 Aug 2023 23:08:42 -0500 Subject: [PATCH] bug fixed --- src/utils/misc.spec.ts | 134 +++++++++++++++++++++++++++++++++++++++-- src/utils/misc.ts | 23 ++++--- 2 files changed, 142 insertions(+), 15 deletions(-) diff --git a/src/utils/misc.spec.ts b/src/utils/misc.spec.ts index caac2a01..bed8e943 100644 --- a/src/utils/misc.spec.ts +++ b/src/utils/misc.spec.ts @@ -2,8 +2,13 @@ import { IPackageData } from "@/interfaces"; import "@/test/utils/auto-mock-debug"; import { allocateImmaturePlantCounts } from "./misc"; -function buildMockPackage(Quantity: number, QuantityTypeName = "CountBased"): IPackageData { +function buildMockPackage( + Id: number, + Quantity: number, + QuantityTypeName = "CountBased" +): IPackageData { const pkg = { + Id, Quantity, Item: { UnitOfMeasureId: 1, QuantityTypeName }, }; @@ -14,12 +19,13 @@ function buildMockPackage(Quantity: number, QuantityTypeName = "CountBased"): IP describe("misc.ts", () => { it("Correctly allocates plant counts to a single small package", () => { - const mockPackages: IPackageData[] = [90].map((qty) => buildMockPackage(qty)); + const mockPackages: IPackageData[] = [90].map((qty, idx) => buildMockPackage(idx + 1, qty)); expect(allocateImmaturePlantCounts(3, mockPackages)).toEqual([ { count: 3, pkg: { + Id: 1, Item: { QuantityTypeName: "CountBased", UnitOfMeasureId: 1, @@ -35,6 +41,7 @@ describe("misc.ts", () => { { count: 90, pkg: { + Id: 1, Item: { QuantityTypeName: "CountBased", UnitOfMeasureId: 1, @@ -50,12 +57,13 @@ describe("misc.ts", () => { }); it("Correctly allocates plant counts to a single large package", () => { - const mockPackages: IPackageData[] = [900].map((qty) => buildMockPackage(qty)); + const mockPackages: IPackageData[] = [900].map((qty, idx) => buildMockPackage(idx + 1, qty)); expect(allocateImmaturePlantCounts(3, mockPackages)).toEqual([ { count: 3, pkg: { + Id: 1, Item: { QuantityTypeName: "CountBased", UnitOfMeasureId: 1, @@ -71,6 +79,7 @@ describe("misc.ts", () => { { count: 100, pkg: { + Id: 1, Item: { QuantityTypeName: "CountBased", UnitOfMeasureId: 1, @@ -83,6 +92,7 @@ describe("misc.ts", () => { { count: 100, pkg: { + Id: 1, Item: { QuantityTypeName: "CountBased", UnitOfMeasureId: 1, @@ -95,6 +105,7 @@ describe("misc.ts", () => { { count: 2, pkg: { + Id: 1, Item: { QuantityTypeName: "CountBased", UnitOfMeasureId: 1, @@ -110,12 +121,18 @@ describe("misc.ts", () => { }); it("Correctly allocates plant counts to multiple packages", () => { - const mockPackages: IPackageData[] = [50, 100, 150].map((qty) => buildMockPackage(qty)); + const mockPackages: IPackageData[] = [50, 100, 150].map((qty, idx) => + buildMockPackage(idx + 1, qty) + ); + const mockPackages2: IPackageData[] = [250, 100, 150].map((qty, idx) => + buildMockPackage(idx + 1, qty) + ); expect(allocateImmaturePlantCounts(3, mockPackages)).toEqual([ { count: 3, pkg: { + Id: 1, Item: { QuantityTypeName: "CountBased", UnitOfMeasureId: 1, @@ -126,11 +143,27 @@ describe("misc.ts", () => { unitOfMeasureId: 1, }, ]); + expect(allocateImmaturePlantCounts(3, mockPackages2)).toEqual([ + { + count: 3, + pkg: { + Id: 1, + Item: { + QuantityTypeName: "CountBased", + UnitOfMeasureId: 1, + }, + Quantity: 250, + }, + quantity: 3, + unitOfMeasureId: 1, + }, + ]); expect(allocateImmaturePlantCounts(200, mockPackages)).toEqual([ { count: 50, pkg: { + Id: 1, Item: { QuantityTypeName: "CountBased", UnitOfMeasureId: 1, @@ -143,6 +176,7 @@ describe("misc.ts", () => { { count: 100, pkg: { + Id: 2, Item: { QuantityTypeName: "CountBased", UnitOfMeasureId: 1, @@ -155,6 +189,7 @@ describe("misc.ts", () => { { count: 50, pkg: { + Id: 3, Item: { QuantityTypeName: "CountBased", UnitOfMeasureId: 1, @@ -165,11 +200,40 @@ describe("misc.ts", () => { unitOfMeasureId: 1, }, ]); + expect(allocateImmaturePlantCounts(200, mockPackages2)).toEqual([ + { + count: 100, + pkg: { + Id: 1, + Item: { + QuantityTypeName: "CountBased", + UnitOfMeasureId: 1, + }, + Quantity: 250, + }, + quantity: 100, + unitOfMeasureId: 1, + }, + { + count: 100, + pkg: { + Id: 1, + Item: { + QuantityTypeName: "CountBased", + UnitOfMeasureId: 1, + }, + Quantity: 250, + }, + quantity: 100, + unitOfMeasureId: 1, + }, + ]); expect(allocateImmaturePlantCounts(300, mockPackages)).toEqual([ { count: 50, pkg: { + Id: 1, Item: { QuantityTypeName: "CountBased", UnitOfMeasureId: 1, @@ -182,6 +246,7 @@ describe("misc.ts", () => { { count: 100, pkg: { + Id: 2, Item: { QuantityTypeName: "CountBased", UnitOfMeasureId: 1, @@ -194,6 +259,7 @@ describe("misc.ts", () => { { count: 100, pkg: { + Id: 3, Item: { QuantityTypeName: "CountBased", UnitOfMeasureId: 1, @@ -206,6 +272,7 @@ describe("misc.ts", () => { { count: 50, pkg: { + Id: 3, Item: { QuantityTypeName: "CountBased", UnitOfMeasureId: 1, @@ -216,13 +283,68 @@ describe("misc.ts", () => { unitOfMeasureId: 1, }, ]); + expect(allocateImmaturePlantCounts(300, mockPackages2)).toEqual([ + { + count: 100, + pkg: { + Id: 1, + Item: { + QuantityTypeName: "CountBased", + UnitOfMeasureId: 1, + }, + Quantity: 250, + }, + quantity: 100, + unitOfMeasureId: 1, + }, + { + count: 100, + pkg: { + Id: 1, + Item: { + QuantityTypeName: "CountBased", + UnitOfMeasureId: 1, + }, + Quantity: 250, + }, + quantity: 100, + unitOfMeasureId: 1, + }, + { + count: 50, + pkg: { + Id: 1, + Item: { + QuantityTypeName: "CountBased", + UnitOfMeasureId: 1, + }, + Quantity: 250, + }, + quantity: 50, + unitOfMeasureId: 1, + }, + { + count: 50, + pkg: { + Id: 2, + Item: { + QuantityTypeName: "CountBased", + UnitOfMeasureId: 1, + }, + Quantity: 100, + }, + quantity: 50, + unitOfMeasureId: 1, + }, + ]); expect(() => allocateImmaturePlantCounts(301, mockPackages)).toThrowError(); + expect(() => allocateImmaturePlantCounts(601, mockPackages2)).toThrowError(); }); it("Correctly allocates weight-based plant counts to a single package", () => { - const mockPackages: IPackageData[] = [50, 100, 150].map((qty) => - buildMockPackage(qty, "WeightBased") + const mockPackages: IPackageData[] = [50, 100, 150].map((qty, idx) => + buildMockPackage(idx + 1, qty, "WeightBased") ); expect(() => allocateImmaturePlantCounts(3, mockPackages)).toThrowError(); diff --git a/src/utils/misc.ts b/src/utils/misc.ts index 993f2cc7..5dec81c8 100755 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -21,11 +21,10 @@ export function allocatePromotePlantCounts( let selectedPlantBatchIndex = 0; const plantData: IIntermediatePromotePlantBatchData[] = []; + let selectedPlantBatch = plantBatches[selectedPlantBatchIndex]; + let selectedPlantBatchRemainingPlantCount = selectedPlantBatch.UntrackedCount; while (true) { - let selectedPlantBatch = plantBatches[selectedPlantBatchIndex]; - let selectedPlantBatchRemainingPlantCount = selectedPlantBatch.UntrackedCount; - // killswitch if (++counter > 1000) { throw new Error("Killswitch"); @@ -64,6 +63,8 @@ export function allocatePromotePlantCounts( // This package is empty, move to the next one if (selectedPlantBatchRemainingPlantCount === 0) { selectedPlantBatchIndex += 1; + selectedPlantBatch = plantBatches[selectedPlantBatchIndex]; + selectedPlantBatchRemainingPlantCount = selectedPlantBatch.UntrackedCount; } if (selectedPlantBatchIndex >= plantBatches.length) { @@ -137,10 +138,10 @@ export function allocatePackageQuantities( quantity: newPackageQuantity, }; - while (true) { - let selectedPackage = inputPackages[selectedPackageIndex]; - let selectedPackageRemainingQuantity = selectedPackage.Quantity; + let selectedPackage = inputPackages[selectedPackageIndex]; + let selectedPackageRemainingQuantity = selectedPackage.Quantity; + while (true) { // killswitch if (++counter > 1000) { throw new Error("Killswitch"); @@ -175,6 +176,8 @@ export function allocatePackageQuantities( // This package is empty, move to the next one if (selectedPackageRemainingQuantity === 0) { selectedPackageIndex += 1; + selectedPackage = inputPackages[selectedPackageIndex]; + selectedPackageRemainingQuantity = selectedPackage.Quantity; } if (selectedPackageIndex >= inputPackages.length) { @@ -221,10 +224,10 @@ export function allocateImmaturePlantCounts( const plantBatchData: IIntermediateCreatePlantBatchFromPackageData[] = []; - while (true) { - let selectedPackage = packages[selectedPackageIndex]; - let selectedPackageRemainingPlantCount = selectedPackage.Quantity; + let selectedPackage = packages[selectedPackageIndex]; + let selectedPackageRemainingPlantCount = selectedPackage.Quantity; + while (true) { // killswitch if (++counter > 1000) { throw new Error("Killswitch"); @@ -265,6 +268,8 @@ export function allocateImmaturePlantCounts( // This package is empty, move to the next one if (selectedPackageRemainingPlantCount === 0) { selectedPackageIndex += 1; + selectedPackage = packages[selectedPackageIndex]; + selectedPackageRemainingPlantCount = selectedPackage.Quantity; } if (selectedPackageIndex >= packages.length) {