From e73a83ac519f4d34c9d16b07d6a473a875e8f16a Mon Sep 17 00:00:00 2001 From: Adam Hassan Date: Sat, 1 Aug 2026 15:53:15 -0700 Subject: [PATCH] fix: use defaults for blank number ingredients --- src/core/Operation.mjs | 7 ++++++- tests/node/index.mjs | 1 + tests/node/tests/Operation.mjs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/node/tests/Operation.mjs diff --git a/src/core/Operation.mjs b/src/core/Operation.mjs index b35a49a66f..702bf3372d 100755 --- a/src/core/Operation.mjs +++ b/src/core/Operation.mjs @@ -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}`); } diff --git a/tests/node/index.mjs b/tests/node/index.mjs index 454479b3b9..0d986acfdc 100644 --- a/tests/node/index.mjs +++ b/tests/node/index.mjs @@ -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"; diff --git a/tests/node/tests/Operation.mjs b/tests/node/tests/Operation.mjs new file mode 100644 index 0000000000..bcfd6d8e94 --- /dev/null +++ b/tests/node/tests/Operation.mjs @@ -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/); + }), +]);