From 5172fd99dbfdb1c417686e1dc0d3871ecccd9d0b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 10 Sep 2026 08:47:56 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20[testing=20improvement]=20Add=20?= =?UTF-8?q?tests=20for=20SumEngine=20extension=20API=20and=20evaluation=20?= =?UTF-8?q?methods?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🎯 What: Tests were added for previously untested parts of `SumEngine`, primarily covering `evaluateExpression`, unknown base unit handling in `addUnit`, numeric values in `setVariable`, and type mappings (dates, percents) in `addFunction`'s arguments. 📊 Coverage: - `addUnit` error on unknown `baseUnitId` - `addFunction` handling conversion of `percent` and `date` arguments to `ExtensionValue` - `setVariable` with plain numbers - `evaluateExpression` retrieving the exact raw `Value` ✨ Result: Increased unit test line coverage for `src/engine/index.ts` covering previously overlooked functions and type mapping corner cases in the extension API. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> --- src/engine/engine.test.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/engine/engine.test.ts b/src/engine/engine.test.ts index e8d8454..2fafadd 100644 --- a/src/engine/engine.test.ts +++ b/src/engine/engine.test.ts @@ -342,14 +342,50 @@ describe("extension API", () => { // the original registration must survive the failed collision expect(calc("2 horses in meters")).toBe("4.8 m"); }); + it("addUnit throws on unknown baseUnitId", () => { + expect(() => + eng.addUnit({ id: "unicorn", phrases: "uni", baseUnitId: "unknown", ratio: 1 }) + ).toThrow(/unknown baseUnitId unknown/); + }); it("addFunction (zum from Sample.js)", () => { eng.addFunction({ id: "zum", phrases: "zum" }, (values) => ({ double: values[0].double + values[1].double })); expect(calc("zum(2;3)")).toBe("5"); }); + it("addFunction converts percent to ExtensionValue", () => { + let capturedPercent: number | undefined; + eng.addFunction({ id: "test_percent", phrases: "test_percent" }, (values) => { + capturedPercent = values[0].double; + return { double: values[0].double }; + }); + expect(calc("test_percent(50%)")).toBe("0.5"); + expect(capturedPercent).toBe(0.5); + }); + it("addFunction converts date to ExtensionValue", () => { + let capturedDate: number | undefined; + eng.addFunction({ id: "test_date", phrases: "test_date" }, (values) => { + capturedDate = values[0].double; + return { double: values[0].double }; + }); + eng.evaluateDocument("test_date(2024-01-01)"); + expect(capturedDate).toBe(new Date("2024-01-01T00:00:00Z").getTime()); + }); it("setVariable", () => { eng.setVariable("myvar", { double: 5, unitId: "USD" }); expect(calc("myvar * 2")).toBe("$10"); }); + it("setVariable with number without unitId", () => { + eng.setVariable("numvar", 42); + expect(calc("numvar * 2")).toBe("84"); + }); + it("evaluateExpression returns the last value", () => { + const val = eng.evaluateExpression("10 + 20"); + expect(val).not.toBeNull(); + if (val && val.kind === "quantity") { + expect(val.value.toNumber()).toBe(30); + } else { + throw new Error("expected a quantity"); + } + }); }); function calcWithResolver(