Skip to content

Commit

Permalink
bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
msfrisbie committed Aug 16, 2023
1 parent f0bc69c commit 2d82136
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 15 deletions.
134 changes: 128 additions & 6 deletions src/utils/misc.spec.ts
Expand Up @@ -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 },
};
Expand All @@ -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,
Expand All @@ -35,6 +41,7 @@ describe("misc.ts", () => {
{
count: 90,
pkg: {
Id: 1,
Item: {
QuantityTypeName: "CountBased",
UnitOfMeasureId: 1,
Expand All @@ -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,
Expand All @@ -71,6 +79,7 @@ describe("misc.ts", () => {
{
count: 100,
pkg: {
Id: 1,
Item: {
QuantityTypeName: "CountBased",
UnitOfMeasureId: 1,
Expand All @@ -83,6 +92,7 @@ describe("misc.ts", () => {
{
count: 100,
pkg: {
Id: 1,
Item: {
QuantityTypeName: "CountBased",
UnitOfMeasureId: 1,
Expand All @@ -95,6 +105,7 @@ describe("misc.ts", () => {
{
count: 2,
pkg: {
Id: 1,
Item: {
QuantityTypeName: "CountBased",
UnitOfMeasureId: 1,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -143,6 +176,7 @@ describe("misc.ts", () => {
{
count: 100,
pkg: {
Id: 2,
Item: {
QuantityTypeName: "CountBased",
UnitOfMeasureId: 1,
Expand All @@ -155,6 +189,7 @@ describe("misc.ts", () => {
{
count: 50,
pkg: {
Id: 3,
Item: {
QuantityTypeName: "CountBased",
UnitOfMeasureId: 1,
Expand All @@ -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,
Expand All @@ -182,6 +246,7 @@ describe("misc.ts", () => {
{
count: 100,
pkg: {
Id: 2,
Item: {
QuantityTypeName: "CountBased",
UnitOfMeasureId: 1,
Expand All @@ -194,6 +259,7 @@ describe("misc.ts", () => {
{
count: 100,
pkg: {
Id: 3,
Item: {
QuantityTypeName: "CountBased",
UnitOfMeasureId: 1,
Expand All @@ -206,6 +272,7 @@ describe("misc.ts", () => {
{
count: 50,
pkg: {
Id: 3,
Item: {
QuantityTypeName: "CountBased",
UnitOfMeasureId: 1,
Expand All @@ -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();
Expand Down
23 changes: 14 additions & 9 deletions src/utils/misc.ts
Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2d82136

Please sign in to comment.