🧪 [testing improvement] Add tests for SumEngine extension API and evaluation methods

🎯 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>
This commit is contained in:
google-labs-jules[bot] 2026-09-10 08:47:56 +00:00
parent 37cfd2144b
commit 5172fd99db

View file

@ -342,14 +342,50 @@ describe("extension API", () => {
// the original registration must survive the failed collision // the original registration must survive the failed collision
expect(calc("2 horses in meters")).toBe("4.8 m"); 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)", () => { it("addFunction (zum from Sample.js)", () => {
eng.addFunction({ id: "zum", phrases: "zum" }, (values) => ({ double: values[0].double + values[1].double })); eng.addFunction({ id: "zum", phrases: "zum" }, (values) => ({ double: values[0].double + values[1].double }));
expect(calc("zum(2;3)")).toBe("5"); 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", () => { it("setVariable", () => {
eng.setVariable("myvar", { double: 5, unitId: "USD" }); eng.setVariable("myvar", { double: 5, unitId: "USD" });
expect(calc("myvar * 2")).toBe("$10"); 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( function calcWithResolver(