gram/packages/kitchen/tests/composite-shopping.test.ts
abiwab b3a509a360
All checks were successful
CI / ci (push) Successful in 2m19s
fix(parser): keep multi-word composite parents intact when marked with @&
`simpleIngredient` accepts `modifier*` (which includes `&`), `composite` did
not. For `<@&unwaxed lemon{}` the `full` branch failed — `refName` excludes
`&` as a refSigil — and the `bare` branch fell through to `singleWordName`,
which does allow `&` and stops at the first space. The parent was truncated
into a phantom ingredient literally named `&unwaxed`, slugified to a different
id, and so escaped the shopping list's MAX aggregation: two lemons were listed
for a recipe using one, with no warning.

`composite` now takes `modifier*` on both branches, mirroring
`simpleIngredient`. The modifiers are parsed and ignored — CompositeAST carries
none, and `&` is redundant on a parent since MAX already collapses repeat
draws. Accepting them is what keeps the parent name clean.

Chosen over rejecting `<@&` as a syntax error because the single-word form
already behaved correctly; rejecting would have broken working input.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-16 14:01:39 +02:00

60 lines
1.8 KiB
TypeScript

import { describe, it, expect } from "bun:test";
import { getAST } from "@gram-lang/parser";
import { compile } from "../src/index";
// The user-visible half of the `<@&parent` parser fix. The grammar-level
// regression lives in @gram-lang/parser's grammar-edge-cases.test.ts; this
// covers what the cook actually sees — the shopping list.
//
// The MAX rule means two draws from one parent buy one parent. Before the fix,
// writing the second draw as `<@&unwaxed lemon{}` truncated the parent to a
// phantom ingredient named "&unwaxed", which slugified to a different id and
// so escaped the MAX aggregation: the list said to buy the same lemon twice,
// and no warning fired.
describe("composite parents in the shopping list", () => {
const ids = (source: string) =>
compile(getAST(source))
.shopping_list.map((i) => i.id)
.sort();
it("collapses two draws from the same parent into one purchase", () => {
const source = `---
title: 'T'
---
[Zest] The @zest{1}<@lemon{1} and the @juice{1}<@lemon{1}.
`;
expect(ids(source)).toEqual(["lemon"]);
});
it("collapses when the second draw marks the parent as already introduced", () => {
const source = `---
title: 'T'
---
[Zest] The @zest{1}<@lemon{1} and the @juice{1}<@&lemon{}.
`;
expect(ids(source)).toEqual(["lemon"]);
});
it("collapses a MULTI-WORD parent marked as already introduced", () => {
const source = `---
title: 'T'
---
[Zest] The @zest{1}<@unwaxed lemon{1} and the @juice{1}<@&unwaxed lemon{}.
`;
expect(ids(source)).toEqual(["unwaxed-lemon"]);
});
it("never leaves a reference sigil in an ingredient's display name", () => {
const source = `---
title: 'T'
---
[Zest] The @zest{1}<@unwaxed lemon{1} and the @juice{1}<@&unwaxed lemon{}.
`;
const names = compile(getAST(source)).shopping_list.map((i) => i.name);
expect(names.filter((n) => n.includes("&"))).toEqual([]);
});
});