gram/packages/renderer/tests/nutrition-panel.test.ts
abiwab dc6899dc12
All checks were successful
CI / Build, Test & Publish (push) Successful in 3m57s
feat(renderer): modernize HTML recipe view layout and intermediate traceability (#126)
2026-11-11 16:02:27 +01:00

51 lines
1.7 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 "@gram-lang/analyzer";
import type { IngredientData } from "@gram-lang/analyzer";
import { toHTML, toPrintHTML } from "../src/index";
// Regression tests for the audit (2026-07-22, renderer findings I-1 and I-2),
// found only once the renderer's `data` parameter was typed instead of `any`
// (finding I-6) — TypeScript flagged both dead/wrong property accesses at
// compile time, exactly as the audit predicted.
describe("nutrition panel warnings (I-1)", () => {
it("renders nutrition panel when warnings are present even with 0 calories", () => {
// "salt" isn't in the database at all -> MISSING_INGREDIENT warning,
// calories stay at 0 -> the exact case the surrounding condition
// ("Show if we have calories OR if we have warnings") was written for.
const source = "## Section\n\nAdd @salt{5g}.\n";
const compiled = compile(getAST(source));
const { result } = analyze(compiled, {});
const html = toHTML(result);
expect(html).toContain("nutrition-panel");
});
});
describe("print nutrition panel salt (I-2)", () => {
const database: Record<string, IngredientData> = {
bacon: {
name: "Bacon",
physical: { density: 1 },
nutrition: {
calories: 541,
protein: 37,
carbs: 1.4,
fat: 42,
sodium: 1717,
},
},
};
it("renders sodium as salt in grams (1717 mg × 2.5)", () => {
const source = "## Section\n\nCook @bacon{100g}.\n";
const compiled = compile(getAST(source));
const { result } = analyze(compiled, database);
const printHtml = toPrintHTML(result);
expect(printHtml).toContain("4.29 g");
expect(printHtml).not.toContain("1717");
});
});