import { describe, expect, it } from "bun:test"; import { getAST } from "@gram-lang/parser"; import { compile } from "@gram-lang/kitchen"; import { analyze } from "@gram-lang/analyzer"; import type { IngredientData } from "@gram-lang/analyzer"; import { toHTML } from "../src/index"; const database = { flour: { name: "flour", physical: { density: 0.59 } }, eggs: { name: "eggs", physical: { unit_weight: 50 } }, } as unknown as Record; function render(source: string, lang?: string) { const { result } = analyze(compile(getAST(source)), database); return toHTML(result, { lang }); } /** The rich tooltip of the hero bar's mass. */ function heroMass(html: string): string { const start = html.indexOf('class="hero-item hero-mass'); expect(start).toBeGreaterThan(-1); return html.slice(start, html.indexOf("\n ", start)); } describe("the tooltip of a mass, in the HTML recipe", () => { it("explains an estimated and incomplete total: how each mass was found, and which are missing", () => { const html = render( "## Dough\n\nMix @flour{250ml}, @sugar{100g}, @eggs{2} and @pepper{}.\n", ); const hero = heroMass(html); // The figure only: the status is the prefix, the words are in the tooltip expect(hero).toContain('>'); expect(hero).not.toContain("(Incomplete)"); expect(hero).toContain("Ingredient mass, before cooking"); // Both reasons, even though the status keeps only "incomplete" expect(hero).toContain("Estimated"); expect(hero).toMatch( /flour \(average density\)<\/span>/, ); expect(hero).toMatch( /eggs \(average unit weight\)<\/span>/, ); expect(hero).toContain("~147.5 g"); expect(hero).toContain("~100 g"); expect(hero).not.toMatch(/sugar Incomplete"); expect(hero).toMatch(/pepper<\/span>\?<\/span>/); expect(hero).toContain("Give these a weight in grams"); }); it("shows the title only when every mass is known", () => { const hero = heroMass( render("## Dough\n\nMix @sugar{100g} and @butter{50g}.\n"), ); expect(hero).toContain('150g'); expect(hero).toContain("Ingredient mass, before cooking"); expect(hero).not.toContain("timing-divider"); }); it("gives a section's badge the same tooltip, from its own ingredients", () => { const html = render( "## Dough\n\nMix @flour{250ml} and @sugar{100g}.\n\n## Filling\n\nAdd @pepper{} to @butter{50g}.\n", ); const badges = html.match( /