Nutrition was only ever reported for the whole recipe. `perPortion` existed
in the output type and in the docs, but nothing populated it: analyze() never
read the recipe's `portions:` frontmatter, it read an option, and the CLI was
passing --scale into that option. So `gram view` showed no nutrition block at
all for a recipe declaring portions, and showed per-scale-factor values when
it did appear.
- resolve the portion count from `meta.portions`, with the option kept as an
explicit override; guard against 0/negative/non-finite counts
- emit `perPortion` whenever the count is known, including 1, so consumers
stop falling back to `perPortion || total` and disagreeing about the basis
- add `per100g` and `basis`, computed from the mass that actually contributed
macros — same list, same loop, same moment as the numerator, so the two
can't disagree about composites or resolution order. Independent of
`portions`, so an unportioned recipe still gets a standardized basis
- derive every basis from the unrounded sums instead of dividing an already
rounded total, which compounded the error once per portion
Gram models no cooking loss, so per100g is per 100 g of the raw assembled
mixture; `basis` exposes the denominator and its massStatus so a UI can say so
without recomputing it.
Alongside, two defects this work sat on top of:
- mass metrics ran before relative quantities were resolved, on the same
objects, so `@water{60% @&flour}` was counted as missing and left out of
totalMass while nutrition included it — the two totals described different
recipes
- the nutrient set was written out by hand in six places and had drifted into
three different sets: fat subtypes and alcohol were accepted in the database
and displayed everywhere but silently dropped from recipe totals, and
`gram db enrich` could never propose mono_fat/poly_fat even though
`gram db lint` validated them. Everything now derives from one NUTRIENTS
table, which also settles the sodium unit the LSP hover was multiplying by
1000
Optional-ingredient and alternative-group handling is likewise shared between
the mass and macro passes instead of implemented twice.
51 conformance goldens regenerated: nutrition key order now follows the table,
plus the new fields and the corrected masses.
143 lines
3.7 KiB
TypeScript
143 lines
3.7 KiB
TypeScript
import { describe, it, expect } from "bun:test";
|
|
import { getAST } from "@gram-lang/parser";
|
|
import { compile, applyScale } from "@gram-lang/kitchen";
|
|
import { analyze } from "../src/index";
|
|
import type { IngredientData } from "../src/types";
|
|
|
|
const database: Record<string, IngredientData> = {
|
|
flour: {
|
|
name: "Flour",
|
|
nutrition: { calories: 364, protein: 10, carbs: 76, fat: 1 },
|
|
},
|
|
sugar: {
|
|
name: "Sugar",
|
|
nutrition: { calories: 400, protein: 0, carbs: 100, fat: 0, sugar: 100 },
|
|
},
|
|
water: { name: "Water", physical: { density: 1 } },
|
|
};
|
|
|
|
function run(source: string, options?: Parameters<typeof analyze>[2]) {
|
|
return analyze(compile(getAST(source)), database, options).result;
|
|
}
|
|
|
|
describe("portions resolution", () => {
|
|
it("reads the portion count from the recipe's own frontmatter", () => {
|
|
// The documented behaviour ("if the recipe declares portions: 4, the
|
|
// total is divided by 4") was never actually wired up: analyze() only
|
|
// read an option nobody passed correctly.
|
|
const result = run(`---
|
|
title: Cake
|
|
portions: 4
|
|
---
|
|
|
|
## Section
|
|
|
|
Mix @flour{200g}.
|
|
`);
|
|
const nut = result.metrics.nutrition;
|
|
expect(nut?.portions).toBe(4);
|
|
expect(nut?.total.calories).toBe(728);
|
|
expect(nut?.perPortion?.calories).toBe(182);
|
|
});
|
|
|
|
it("lets an explicit option override the frontmatter", () => {
|
|
const result = run(
|
|
`---
|
|
title: Cake
|
|
portions: 4
|
|
---
|
|
|
|
## Section
|
|
|
|
Mix @flour{200g}.
|
|
`,
|
|
{ portions: 2 },
|
|
);
|
|
expect(result.metrics.nutrition?.portions).toBe(2);
|
|
expect(result.metrics.nutrition?.perPortion?.calories).toBe(364);
|
|
});
|
|
|
|
it("tolerates a labelled count and rejects a nonsensical one", () => {
|
|
const labelled = run(`---
|
|
title: Cake
|
|
portions: 4 servings
|
|
---
|
|
|
|
## Section
|
|
|
|
Mix @flour{200g}.
|
|
`);
|
|
expect(labelled.metrics.nutrition?.portions).toBe(4);
|
|
|
|
const zero = run(`---
|
|
title: Cake
|
|
portions: 0
|
|
---
|
|
|
|
## Section
|
|
|
|
Mix @flour{200g}.
|
|
`);
|
|
expect(zero.metrics.nutrition?.portions).toBeUndefined();
|
|
expect(zero.metrics.nutrition?.perPortion).toBeUndefined();
|
|
});
|
|
|
|
it("keeps per-portion and per-100g figures invariant under scaling", () => {
|
|
const source = `---
|
|
title: Cake
|
|
portions: 4
|
|
---
|
|
|
|
## Section
|
|
|
|
Mix @flour{200g} and @sugar{100g}.
|
|
`;
|
|
const compiled = compile(getAST(source));
|
|
const base = analyze(compiled, database).result.metrics.nutrition;
|
|
// applyScale scales meta.portions alongside the quantities, so a
|
|
// doubled recipe is still the same food per portion and per 100 g.
|
|
const scaled = analyze(applyScale(compiled, 2), database).result.metrics
|
|
.nutrition;
|
|
|
|
expect(scaled?.total.calories).toBe((base?.total.calories ?? 0) * 2);
|
|
expect(scaled?.perPortion).toEqual(base?.perPortion);
|
|
expect(scaled?.per100g).toEqual(base?.per100g);
|
|
});
|
|
|
|
it("still reports per-100g for a recipe that declares no portions", () => {
|
|
const nut = run(`---
|
|
title: Cake
|
|
---
|
|
|
|
## Section
|
|
|
|
Mix @flour{200g}.
|
|
`).metrics.nutrition;
|
|
|
|
expect(nut?.perPortion).toBeUndefined();
|
|
expect(nut?.per100g?.calories).toBe(364);
|
|
expect(nut?.basis).toEqual({ mass: 200, massStatus: "precise" });
|
|
});
|
|
});
|
|
|
|
describe("mass metrics vs. relative quantities", () => {
|
|
it("counts an ingredient expressed as a percentage of another", () => {
|
|
// Mass metrics used to be computed before relative quantities were
|
|
// resolved, so `water` had no normalizedMass yet: it was reported as
|
|
// missing and left out of totalMass, while the nutrition pass — which
|
|
// runs after resolution — did include it.
|
|
const result = run(`---
|
|
title: Dough
|
|
---
|
|
|
|
## Section
|
|
|
|
Mix @flour{250g} and @water{60% @&flour}.
|
|
`);
|
|
|
|
expect(result.metrics.totalMass).toBe(400);
|
|
expect(result.metrics.massStatus).toBe("precise");
|
|
expect(result.metrics.missingMassIngredients).toEqual([]);
|
|
expect(result.sections[0]?.metrics?.totalMass).toBe(400);
|
|
});
|
|
});
|