gram/packages/kitchen/tests/relative-on-composite-parent.test.ts
abiwab f4c69431f1
All checks were successful
CI / Build & Test (pull_request) Successful in 4m28s
fix(kitchen): warn on a percentage on a composite parent instead of dropping it
2026-11-17 19:04:53 +01:00

27 lines
1.1 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { getAST } from "@gram-lang/parser";
import { compile } from "../src/index";
describe("a percentage on a composite parent (INVALID_QUANTITY, relativeNotSupported)", () => {
it("is said, and the parent reads with no quantity", () => {
const result = compile(
getAST("## S\n\nMix @flour{100 g} with @eggs{3}<@box{50% @&flour}>.\n"),
);
const eggs = result.sections[0]!.ingredients.find((i) => i.id === "eggs");
expect(eggs?.qty).toBe(3);
expect(eggs?.composite).toEqual({ parent: "box" });
const [warning, ...rest] = result.warnings;
expect(rest).toHaveLength(0);
expect(warning?.code).toBe("INVALID_QUANTITY");
expect(warning?.data).toEqual({ reason: "relativeNotSupported" });
expect(warning?.message).toContain("not supported on a composite parent");
expect(warning?.message).toContain('"50% @&flour"');
});
it("is said once for a percentage over zero (zeroDenominator only)", () => {
const codes = compile(
getAST("## S\n\nMix @flour{100 g} with @eggs{3}<@box{1/0% @&flour}>.\n"),
).warnings.map((w) => w.data);
expect(codes).toEqual([{ reason: "zeroDenominator" }]);
});
});