fix: use defaults for blank number ingredients

This commit is contained in:
Adam Hassan 2026-08-01 15:53:15 -07:00
parent c56dd23358
commit e73a83ac51
No known key found for this signature in database
GPG Key ID: AF581667C4A182B1
3 changed files with 39 additions and 1 deletions

View File

@ -244,7 +244,12 @@ class Operation {
set ingValues(ingValues) {
ingValues.forEach((val, i) => {
try {
this._ingList[i].value = val;
const ingredient = this._ingList[i],
value = ingredient.type === "number" && Number.isNaN(val) ?
ingredient.defaultValue :
val;
ingredient.value = value;
} catch (err) {
throw new OperationError(`Failed to set value of ingredient '${this._ingList[i].name}': ${err}`);
}

View File

@ -21,6 +21,7 @@ import "./tests/operations.mjs";
import "./tests/PGP.mjs";
import "./tests/File.mjs";
import "./tests/Dish.mjs";
import "./tests/Operation.mjs";
import "./tests/NodeDish.mjs";
import "./tests/Utils.mjs";
import "./tests/Categories.mjs";

View File

@ -0,0 +1,32 @@
import TestRegister from "../../lib/TestRegister.mjs";
import Operation from "../../../src/core/Operation.mjs";
import it from "../../node/assertionHandler.mjs";
import assert from "assert";
TestRegister.addApiTests([
it("Operation - NaN number ingredients should use their default value", () => {
const operation = new Operation();
operation.args = [{
name: "Offset",
type: "number",
value: 0
}];
operation.ingValues = [NaN];
assert.deepStrictEqual(operation.ingValues, [0]);
}),
it("Operation - invalid number strings should still throw", () => {
const operation = new Operation();
operation.args = [{
name: "Offset",
type: "number",
value: 0
}];
assert.throws(() => {
operation.ingValues = ["NaN"];
}, /Invalid ingredient value\. Not a number: NaN/);
}),
]);