gram/packages/analyzer/tests/alternative-mass-standardization.test.ts
abiwab 64b2594978 fix(analyzer): standardize mass for alternative ingredient group options
analyze() had a dedicated branch for composite entries (type: "composite")
but none for alternatives (type: "alternative"), so every option inside an
alternative group silently fell through the generic per-item mass logic,
which only ever finds qty/unit/_usageId on a flat item's own top level —
never present on the alternative wrapper itself (those fields live on each
option instead). No option ever got normalizedMass/isEstimate/
conversionMethod, in the shopping list, the section ingredient list, or
the inline step-text mention.

This also silently broke the recipe's aggregate mass total:
calculateMassMetrics (metrics.ts) already picked options[0] as the
representative mass for an alternative group, but had nothing to pick,
permanently downgrading massStatus to "incomplete" and adding the literal
string "alternative" to missingMassIngredients.

Added a shared standardizeUsageMass() helper (mirroring the existing
per-item logic) and wired a dedicated alternative branch into both the
section-level and shopping-list-level enrichment passes, standardizing
each option independently — never summed into one group total, since
only one option is ever actually bought, unlike a composite's children.
Extended the "sync analyzed values back into step content" pass to
recurse into an alternative's options too, so the inline step-text
mention picks up the same data. Also extracted the repeated "track a
missing database ingredient" check (now shared between a regular item
and an alternative option) into its own small helper.
2026-07-18 21:57:34 +02:00

91 lines
4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, it, expect } from "bun:test";
import { getAST } from "@gram-lang/parser";
import { compile } from "@gram-lang/kitchen";
import { analyze } from "../src/index";
import type { IngredientData } from "../src/types";
// An alternative group's options are mutually exclusive ("egg OR tofu") — the
// group itself has no single mass, only whichever option is actually bought.
// analyze() previously had a dedicated branch for composite entries but none
// for alternatives, so every option silently stayed unstandardized: no
// normalizedMass in the shopping list, the section ingredient list, or the
// inline step-text mention, and calculateMassMetrics's own pre-existing
// "pick options[0] as the representative" fallback (metrics.ts) found nothing
// to pick — downgrading the whole recipe's massStatus to "incomplete" and
// adding the literal string "alternative" to missingMassIngredients.
const database: Record<string, IngredientData> = {
flour: { name: "Flour", physical: { density: 0.55 } },
egg: { name: "Egg", physical: { unit_weight: 50 } },
tofu: { name: "Tofu", physical: { density: 1.0 } },
};
const source =
"## Prep\n[Mix] @flour{200g} the @egg{2}|@tofu{200g} and stir.\n";
const compiled = compile(getAST(source));
const { result: analyzed, missingIngredients } = analyze(compiled, database);
describe("mass standardization for alternative ingredient groups", () => {
it("standardizes each option in the shopping list independently, not the group as a whole", () => {
const alt = analyzed.shopping_list.find(
(item: any) => item.type === "alternative",
) as any;
expect(alt).toBeDefined();
expect(alt.normalizedMass).toBeUndefined();
const [eggOpt, tofuOpt] = alt.options;
expect(eggOpt.normalizedMass).toBe(100); // 2 × 50g unit_weight
expect(eggOpt.conversionMethod).toBe("unit_weight");
expect(eggOpt.isEstimate).toBe(true);
expect(tofuOpt.normalizedMass).toBe(200); // 200g, direct
expect(tofuOpt.conversionMethod).toBe("physical");
expect(tofuOpt.isEstimate).toBe(false);
});
it("standardizes each option in the section ingredient list the same way", () => {
const alt = analyzed.sections[0]!.ingredients.find(
(item: any) => item.type === "alternative",
) as any;
expect(alt.options[0].normalizedMass).toBe(100);
expect(alt.options[1].normalizedMass).toBe(200);
});
it("syncs normalizedMass back into the inline step-text alternative mention too", () => {
const step = analyzed.sections[0]!.steps[0] as any;
const alt = step.content.find(
(node: any) => node && node.type === "alternative",
);
expect(alt.options[0].normalizedMass).toBe(100);
expect(alt.options[1].normalizedMass).toBe(200);
});
it("counts the first option toward the recipe's total mass and no longer reports 'alternative' as missing", () => {
// calculateMassMetrics already picked options[0] as the representative
// for an alternative group — it just had no normalizedMass to find
// before this fix. flour (200) + egg option (100) = 300.
expect(analyzed.metrics.totalMass).toBe(300);
expect(analyzed.metrics.massStatus).toBe("estimated");
expect(analyzed.metrics.missingMassIngredients).toEqual([]);
});
it("does not report the alternative wrapper's own literal id as a missing ingredient", () => {
expect(missingIngredients).not.toContain("alternative");
});
});
describe("composite mass standardization is unaffected (regression)", () => {
it("still sums composite children into the parent's normalizedMass", () => {
const compositeDb: Record<string, IngredientData> = {
lemon: { name: "Lemon", physical: { unit_weight: 60 } },
};
const compositeSource =
"## S\nX @lemon zest{5g}<@lemon.\n\nY @lemon juice{100g}<@lemon{2}.\n";
const compositeCompiled = compile(getAST(compositeSource));
const { result } = analyze(compositeCompiled, compositeDb);
const composite = result.shopping_list.find(
(item: any) => item.type === "composite",
) as any;
expect(composite.normalizedMass).toBe(105);
expect(composite.usage[0].normalizedMass).toBe(5);
expect(composite.usage[1].normalizedMass).toBe(100);
});
});