import { describe, it, expect } from "bun:test"; import { getAST } from "@gram-lang/parser"; import { applyScale, compile, WarningCode } from "@gram-lang/kitchen"; import type { Usage } from "@gram-lang/kitchen"; import { analyze } from "../src/index"; import type { IngredientData } from "../src/types"; // A share (`65% @&flour`) of an ingredient already written in a unit of mass // used to stay unresolved when mass standardization was off: the target had // no `normalizedMass`, the only thing `resolveShare()` read, and the share // raised RELATIVE_QUANTITY_UNKNOWN_MASS although `500 g` needs no database. const database: Record = { "bread-flour": { name: "Bread flour", physical: { density: 0.55 } }, flour: { name: "Flour", physical: { density: 0.55 } }, butter: { name: "Butter", physical: { density: 0.91 } }, egg: { name: "Egg", physical: { unitWeight: 50 } }, water: { name: "Water", physical: { density: 1 } }, }; function run(body: string) { const compiled = compile(getAST(`## Dough\n\n${body}\n`)); return analyze(compiled, database, { enableMassStandardization: false }); } function usage(items: Usage[], id: string): Usage { const found = items.find((i) => i.id === id); if (!found) throw new Error(`no usage ${id}`); return found; } const unknownMass = (warnings: { code: string; item?: string }[]) => warnings.filter((w) => w.code === WarningCode.RELATIVE_QUANTITY_UNKNOWN_MASS); describe("percentages when mass standardization is off", () => { it("resolves a share of a target written in grams", () => { const { result } = run( "[Mix] Combine @*bread flour{500 g}, @water{65% @&bread flour}, @salt{2.8% @&bread flour}.", ); const items = result.sections[0]!.ingredients as Usage[]; expect(unknownMass(result.warnings)).toEqual([]); expect(usage(items, "water")).toMatchObject({ qty: { type: "single", value: 325 }, unit: "g", conversionMethod: "relative", }); expect(usage(items, "salt")).toMatchObject({ qty: { type: "single", value: 14 }, unit: "g", }); }); it("does not standardize the target itself", () => { const { result } = run( "Combine @bread flour{500 g} and @water{65% @&bread flour}.", ); const flour = usage( result.sections[0]!.ingredients as Usage[], "bread-flour", ); expect(flour.normalizedMass).toBeUndefined(); expect(flour.conversionMethod).toBeUndefined(); }); it("converts other units of mass straight to grams", () => { const { result } = run( "Combine @flour{1.2 kg}, @butter{8 oz}, @water{50% @&flour} and @salt{10% @&butter}.", ); const items = result.sections[0]!.ingredients as Usage[]; expect(unknownMass(result.warnings)).toEqual([]); expect(usage(items, "water").qty).toEqual({ type: "single", value: 600 }); expect(usage(items, "salt").qty).toEqual({ type: "single", value: 22.68, }); }); it("chains shares (B a share of A, C a share of B)", () => { const { result } = run( "Combine @flour{200 g}, @water{70% @&flour} and @salt{10% @&water}.", ); const items = result.sections[0]!.ingredients as Usage[]; expect(unknownMass(result.warnings)).toEqual([]); expect(usage(items, "water").qty).toEqual({ type: "single", value: 140 }); expect(usage(items, "salt").qty).toEqual({ type: "single", value: 14 }); }); it("resolves a share inside an alternative, and one of an earlier option", () => { const { result } = run( "Mix @flour{200 g} and @butter{10% @&flour}|@oil{8% @&flour}.\n\nAdd @butter{100 g}|@lard{80% @&butter}.", ); expect(unknownMass(result.warnings)).toEqual([]); const options = result.sections[0]!.ingredients.filter( (i) => (i as Usage).type === "alternative", ).flatMap((alt) => (alt as Usage).options as Usage[]); const shares = options.filter((o) => o.conversionMethod === "relative"); expect(shares.map((o) => [o.id, o.qty, o.unit])).toEqual([ ["butter", { type: "single", value: 20 }, "g"], ["oil", { type: "single", value: 16 }, "g"], ["lard", { type: "single", value: 80 }, "g"], ]); }); it("still warns on a target in volume, which needs a density", () => { const { result } = run("Combine @flour{2 cup} and @water{60% @&flour}."); expect(unknownMass(result.warnings).map((w) => w.item)).toEqual(["water"]); const water = usage(result.sections[0]!.ingredients as Usage[], "water"); expect(water.normalizedMass).toBeUndefined(); }); it("still warns on a target in pieces, which needs a unit weight", () => { const { result } = run("Beat @egg{3} with @sugar{50% @&egg}."); expect(unknownMass(result.warnings).map((w) => w.item)).toEqual(["sugar"]); }); it("still warns when a mention of the target has no mass", () => { const { result } = run( "Combine @flour{200 g}, @flour{1 cup} and @water{60% @&flour}.", ); expect(unknownMass(result.warnings).map((w) => w.item)).toEqual(["water"]); }); }); // Kitchen builds its shopping list before a share has an amount: the share // sat in an entry of its own, as written (`65% of bread flour`), and only // the standardized pass wrote it again in grams. The entries holding a usage // the analyzer resolved are now built again from the sections, whatever the // setting. describe("the shopping list when mass standardization is off", () => { type Entry = { id: string; type?: string; qty?: number; unit?: string; multiUnit?: boolean; variable_entries?: string[]; options?: Usage[]; }; const list = (body: string, enableMassStandardization = false) => analyze(compile(getAST(`## Dough\n\n${body}\n`)), database, { enableMassStandardization, }).result.shopping_list as Entry[]; const amounts = (entries: Entry[], id: string) => entries .filter((e) => e.id === id) .map((e) => [e.qty, e.unit, e.variable_entries, e.multiUnit]); it("buys the amount of a share, not its formula", () => { const entries = list( "[Mix] Combine @*bread flour{500 g}, @water{65% @&bread flour}, @salt{2.8% @&bread flour}.", ); expect(amounts(entries, "water")).toEqual([ [325, "g", undefined, undefined], ]); expect(amounts(entries, "salt")).toEqual([[14, "g", undefined, undefined]]); }); it("adds a share to the other mentions of its ingredient, in one entry", () => { const body = "Mix @flour{500 g}, @water{100 g} and @water{65% @&flour}.\n\nAdd @water{20% @&flour}."; expect(amounts(list(body), "water")).toEqual([ [525, "g", undefined, undefined], ]); expect(amounts(list(body, true), "water")).toEqual([ [525, "g", undefined, undefined], ]); }); it("keeps an entry per unit that does not add up, as kitchen does", () => { const entries = list( "Mix @flour{500 g}, @water{65% @&flour}.\n\nAdd @water{1 cup}.", ); expect(amounts(entries, "water")).toEqual([ [325, "g", undefined, true], [1, "cup", undefined, true], ]); }); it("keeps a quantity in words as written, beside the share", () => { const entries = list( "Mix @flour{500 g}, @water{65% @&flour} and @water{a splash}.", ); expect(amounts(entries, "water")).toEqual([ [325, "g", undefined, true], [undefined, undefined, ["a splash"], true], ]); }); // Grams per litre of water written in grams: kitchen leaves it waiting, // the analyzer resolves it with the water's density. it("buys a ratio the analyzer resolved, in its own unit", () => { const entries = list("Pour @water{500 g} and @salt{15 g / 1 L @&water}."); expect(amounts(entries, "salt")).toEqual([ [7.5, "g", undefined, undefined], ]); }); it("buys the share an option of an alternative resolved", () => { const entries = list( "Mix @flour{200 g} and @butter{10% @&flour}|@oil{8% @&flour}.", ); const alt = entries.find((e) => e.type === "alternative"); expect(alt?.options?.map((o) => [o.id, o.qty, o.unit])).toEqual([ ["butter", { type: "single", value: 20 }, "g"], ["oil", { type: "single", value: 16 }, "g"], ]); }); it("says once that an entry is in a cycle, its mentions merged", () => { const body = "Mix @flour{200 g} then add more @flour{10% @&flour}."; for (const on of [false, true]) { expect(amounts(list(body, on), "flour")).toEqual([ [220, "g", ["⚠️ Circular Ref."], undefined], ]); } }); it("keeps the formula of a share it could not resolve", () => { const entries = list("Combine @flour{2 cup} and @water{60% @&flour}."); expect(amounts(entries, "water")).toEqual([ [undefined, undefined, ["60% of flour"], undefined], ]); }); it("leaves the entries of a scaled result that hold no share as scaled", () => { const compiled = applyScale( compile( getAST( "## Dough\n\nMix @flour{500 g}, @salt{0.125 kg} and @water{65% @&flour}.\n", ), ), 2, ); const before = compiled.shopping_list.find((e) => e.id === "salt"); const { result } = analyze(compiled, database, { enableMassStandardization: false, }); const entries = result.shopping_list as Entry[]; expect(entries.find((e) => e.id === "salt")).toEqual(before as Entry); expect(amounts(entries, "water")).toEqual([ [650, "g", undefined, undefined], ]); }); });