import { describe, expect, it } from "bun:test"; import { compile, scheduleFor } from "@gram-lang/kitchen"; import { analyze } from "@gram-lang/analyzer"; import { getAST } from "@gram-lang/parser"; import { project, runSheet, visitSections } from "@gram-lang/scheduler"; import type { MiseEnPlaceMode } from "@gram-lang/scheduler"; import { describeRunSheet, runSheetToHTML, runSheetToMarkdown, runSheetToPrintHTML, runSheetToText, sectionProgram, } from "../src/index"; const BREAD = `--- title: Country bread portions: 4 --- ## Dough ->&dough [Mix] Mix @flour{500g} with @water{350ml} and knead ~{20min}. [Rise] Let it rise ~_{8-16h}. ## Bake [Bake] Shape &dough{850g} and bake at ^{230C} for ~{30min}. `; const ENTREMETS = `--- title: Entremets portions: 6 --- ## Sponge ->&sponge ~{-2d} [Mix] Whisk @flour{60g} with @sugar{30g} in the #bowl{}. [Chill] Chill ~_{12-48h}. ## Mousse ->&mousse ~{-1d} [Whip] Whip @cream{250g} with &sponge and @sugar{30g} using the #whisk{} and #bowl{}. [Freeze] Freeze ~_{12-48h}. ## Glaze ->&glaze [Heat] Heat @sugar{20g} to ^{80C} ~{5min}. [Cool] Cool ~_{90min}. ## Finish [Assemble] Unmould &mousse and cover with &glaze. `; const context = (patch = {}) => ({ serveAt: "2026-10-11T13:00", timeZone: "Europe/Paris", availability: { daily: [{ start: "08:00", end: "22:00" }] }, ...patch, }); const planOf = (c: ReturnType, patch = {}) => runSheet( project([{ graph: c.tasks, title: c.title ?? undefined }], context(patch)), { graph: c.tasks, context: context(patch), }, ); type Kitted = { kit: { items: { text: string; kind: string }[] }[]; program: { kit?: { text: string; kind: string }[] }[]; }; /** Every line to take out in a visit: at the top, or with its section. */ const kitOf = (p: Kitted) => [ ...p.kit.flatMap((g) => g.items), ...p.program.flatMap((l) => l.kit ?? []), ]; const kitTexts = (p: Kitted) => kitOf(p).map((l) => l.text); const compiled = compile(getAST(BREAD)); const sheet = planOf(compiled); const entremets = compile(getAST(ENTREMETS)); const entremetsContext = context({ serveAt: "2026-10-17T20:00", availability: { daily: [{ start: "08:00", end: "23:00" }] }, }); const entremetsSheet = runSheet( project([{ graph: entremets.tasks, title: "Entremets" }], entremetsContext), { graph: entremets.tasks, context: entremetsContext }, ); describe("the model of the run sheet", () => { const model = describeRunSheet(sheet, compiled); it("opens with the title, when it is served for how many, and the totals", () => { expect(model.title).toBe("Run sheet — Country bread"); expect(model.subtitle).toBe("For Sunday, October 11 at 13:00 · 4 portions"); expect(model.summary).toBe( "2 days · 2 visits to the kitchen · 55\u00A0min of work in all", ); }); it("lists a row per visit, the waits that go on without the cook, and the service", () => { expect(model.schedule.map((r) => [r.kind, r.day, r.time, r.text])).toEqual([ ["visit", "SAT 10", "21:40 – 22:00", "Dough"], [ "thread", undefined, undefined, "Dough : Rise 14\u00A0h 30\u00A0min (8–16\u00A0h)", ], ["visit", "SUN 11", "12:30 – 13:00", "Bake"], ["serve", undefined, "13:00", "Serve"], ]); }); it("says what to take out, and what an earlier visit made", () => { const [saturday, sunday] = model.days; expect(kitTexts(saturday!.visits[0]!)).toEqual([ "flour 500 g", "water 350 ml", ]); expect(kitTexts(sunday!.visits[0]!)).toEqual(["Dough, made Saturday"]); }); it("gives the programme of each section with the steps it covers", () => { const [saturday, sunday] = model.days; expect(saturday!.visits[0]!.program).toEqual([ expect.objectContaining({ section: "Dough", reference: "steps 1 to 2" }), ]); expect(sunday!.visits[0]!.program).toEqual([ expect.objectContaining({ section: "Bake", reference: "step 1" }), ]); }); it("says what continues and when to come back", () => { const [saturday, sunday] = model.days; expect(saturday!.visits[0]!.leaving).toEqual([ "Dough : Rise 14\u00A0h 30\u00A0min (8–16\u00A0h)", "Next visit: Sunday 12:30", ]); expect(sunday!.visits[0]!.leaving).toEqual(["Serve at 13:00"]); }); it("tells a rest the plan stretched, on the day it starts", () => { expect(model.days[0]!.notices).toEqual([ "Dough : Rise extended to 14\u00A0h 30\u00A0min (planned 8–16\u00A0h) so the work falls within your availability.", ]); }); it("rounds the display to the nearest five minutes, the sheet staying exact", () => { expect(model.days[0]!.slot).toBe("21:40 → 22:00"); const quarter = describeRunSheet(sheet, compiled, { roundTo: 15 }); expect(quarter.days[0]!.slot).toBe("21:45 → 22:00"); expect(sheet.recipes[0]!.days[0]!.visits[0]!.startLocal).toBe( "2026-10-10T21:38", ); }); it("makes one calendar event per visit, in UTC, with the lists as plain lines", () => { expect(model.events).toHaveLength(2); expect(model.events[0]).toMatchObject({ start: "2026-10-10T19:38:00Z", title: "Country bread · Dough", }); expect(model.events[0]!.description).toContain("- Dough (steps 1 to 2)"); expect(model.events[0]!.description).toContain(" - flour 500 g"); }); }); describe("a plan of several days", () => { const model = describeRunSheet(entremetsSheet, entremets); it("has a block per day and a visit per moment in the kitchen", () => { expect(model.days.map((d) => d.visits.length)).toEqual([1, 1, 2]); expect(model.summary).toContain("3 days · 4 visits to the kitchen"); }); it("takes the first visit of a day for the margin and holds the next by its wait", () => { const [first] = model.days; expect(first!.visits[0]!.margin).toBe("Possible from 08:00."); const [, , saturday] = model.days; expect(saturday!.visits[0]!.margin).toBe( "Can't start earlier: the “Cool” wait (Glaze) can't be longer than 1\u00A0h 30\u00A0min and must be over before Finish.", ); }); it("names preparations by the section that makes them, with when", () => { const [, friday, saturday] = model.days; expect(kitTexts(friday!.visits[0]!)).toContain("Sponge, made Thursday"); expect(kitTexts(saturday!.visits[1]!)).toEqual([ "Mousse, made Friday", "Glaze, made at 18:25", ]); }); it("lists the cookware once", () => { const friday = kitOf(model.days[1]!.visits[0]!); expect( friday.filter((k) => k.kind === "cookware").map((k) => k.text), ).toEqual(["whisk", "bowl"]); }); it("without a serving time: days are D-n, slots are durations, no margin", () => { const relative = describeRunSheet( runSheet(entremets.schedule, { title: "Entremets" }), entremets, ); expect(relative.subtitle).toBe("6 portions"); expect(relative.schedule.map((r) => r.day).filter(Boolean)).toEqual([ "D-2", "D-1", "Day D", ]); expect(relative.days[0]!.slot).toBe("~ 5\u00A0min"); expect(relative.days.every((d) => d.visits.every((p) => !p.margin))).toBe( true, ); const last = relative.days.at(-1)!.visits.at(-1)!; expect(last.leaving).toEqual(["Serve"]); // Two visits on the same day: no clock to give, so it says so. const sameDay = relative.days .flatMap((d) => d.visits) .filter((p) => p.leaving.includes("Next visit: later the same day")); expect(sameDay.length).toBeGreaterThan(0); expect(relative.days[0]!.visits[0]!.leaving.at(-1)).toBe("Next visit: D-1"); expect(relative.events).toEqual([]); }); }); describe("a section that comes from an import, and a base kept in stock", () => { const imported = structuredClone(entremets); // The composer tags a spliced section; a stocked base is a synthetic ingredient. (imported.sections[0] as { module?: unknown }).module = { binding: "sponge", uri: "bases/sponge.gram", title: "Sponge", mode: "inline", }; imported.registry.ingredients.cream!.is_module_synthetic = true; const model = describeRunSheet(entremetsSheet, imported); it("names the file the steps are read in", () => { const [line] = model.days[0]!.visits[0]!.program; expect(line!.reference).toBe("sponge.gram, steps 1 to 2"); expect(line!.origin).toBe("sponge.gram"); expect(runSheetToText(entremetsSheet, imported)).toContain( "SPONGE — sponge.gram (Start: ", ); expect(runSheetToMarkdown(entremetsSheet, imported)).toContain( "### Sponge · sponge.gram · Start: ", ); expect(runSheetToHTML(entremetsSheet, imported)).toContain( 'sponge.gram', ); }); it("lists a stocked base used as a preparation, in stock", () => { const stocked = structuredClone(entremets); stocked.registry.ingredients.glaze = { id: "glaze", name: "Glaze", is_module_synthetic: true, }; delete (stocked.registry.ingredients.glaze as { is_intermediate?: boolean }) .is_intermediate; // The section that made it is gone: the glaze is only used. stocked.sections[2]!.intermediate_preparation = undefined; const model = describeRunSheet(entremetsSheet, stocked); const texts = model.days .flatMap((d) => d.visits) .flatMap((p) => kitTexts(p)); expect(texts).toContain("glaze, in stock"); }); it("marks an ingredient that stands for a stocked import", () => { expect(kitTexts(model.days[1]!.visits[0]!)).toContain( "cream 250 g, in stock", ); }); }); describe("runSheetToText", () => { const text = runSheetToText(sheet, compiled); it("starts with the schedule of the days", () => { const lines = text.split("\n"); expect(lines[0]).toBe("Run sheet — Country bread"); expect(lines[1]).toBe("For Sunday, October 11 at 13:00 · 4 portions"); expect(text).toMatch(/SAT 10\s+▮ 21:40 – 22:00\s+Dough/); expect(text).toMatch(/★ 13:00\s+Serve/); }); it("gives a block per day: what to take out, the steps, what to do on leaving", () => { expect(text).toContain("SATURDAY, OCTOBER 10 21:40 → 22:00"); expect(text).toContain("DOUGH (Start: 21:40)"); expect(text).toContain(" Mise en place\n [ ] flour 500 g"); expect(text).toContain( " 1. [Mix] Mix flour with water and knead 20\u00A0min.", ); expect(text).toContain(" 2. [Rise] Let it rise 8–16\u00A0h."); expect(text).toContain("BEFORE LEAVING"); expect(text).toContain("Next visit: Sunday 12:30"); }); it("lays the mise en place out as a block of its own when it is told per day", () => { const day = runSheetToText(planIn(compiled, "perDay"), compiled); expect(day).toContain("MISE EN PLACE\n [ ] flour 500 g"); expect(day).toContain("EXECUTION\n DOUGH (Start:"); expect(day).toContain("BEFORE LEAVING"); }); it("speaks French, dates included", () => { const fr = runSheetToText(sheet, compiled, { lang: "fr" }); expect(fr).toContain("Plan de marche — Country bread"); expect(fr).toContain("Pour le dimanche 11 octobre à 13:00 · 4 portions"); expect(fr).toContain("SAMEDI 10 OCTOBRE"); expect( runSheetToText(planIn(compiled, "perDay"), compiled, { lang: "fr" }), ).toContain("MISE EN PLACE"); expect(fr).toContain("Exécution"); expect(fr).toContain("Début : "); expect(fr).toContain("EN PARTANT"); expect(fr).toContain("Dough, fait samedi"); expect(fr).toContain("prolongé à"); }); }); describe("text, Markdown and HTML say the same", () => { const visits = describeRunSheet(sheet, compiled).days.flatMap( (d) => d.visits, ); const count = (s: string) => (s.match(/before leaving/gi) ?? []).length; it("closes every visit with what to do on leaving, whatever it goes on with", () => { expect(visits.some((p) => p.waits.length === 0)).toBe(true); expect(count(runSheetToText(sheet, compiled))).toBe(visits.length); expect(count(runSheetToMarkdown(sheet, compiled))).toBe(visits.length); expect(count(runSheetToHTML(sheet, compiled))).toBe(visits.length); }); it("tells the same steps, numbered the same", () => { const text = runSheetToText(sheet, compiled); const md = runSheetToMarkdown(sheet, compiled); const html = runSheetToHTML(sheet, compiled); expect(text).toContain("1. [Bake] Shape dough"); expect(md).toContain("1. **[Bake]** Shape"); expect(html).toContain( '
  • Bake', ); }); it("still tells when to come back", () => { for (const p of visits) { expect(runSheetToText(sheet, compiled)).toContain(p.next!); expect(runSheetToMarkdown(sheet, compiled)).toContain(p.next!); } }); }); describe("what could not be fixed", () => { const exact = compile(getAST(BREAD.replace("~_{8-16h}", "~_{8h}"))); const text = runSheetToText(planOf(exact), exact); it("is listed under the title", () => { expect(text).toContain("To fix"); expect(text).toMatch( /Mise en place — Dough, around Sun 04:10, falls when you are not available, and no wait can be moved to fix it/, ); expect(text.indexOf("To fix")).toBeLessThan(text.indexOf("▮")); }); it("is in the language asked for", () => { expect(runSheetToText(planOf(exact), exact, { lang: "fr" })).toContain( "À régler", ); }); it("says when a stretch of work is longer than any availability", () => { const long = compile( getAST("## Roast\n\n[Prepare] Prepare @lamb{2kg} for ~{180min}.\n"), ); const out = runSheetToText( planOf(long, { availability: { daily: [{ start: "18:00", end: "20:00" }] }, }), long, ); expect(out).toContain( "Roast takes 3\u00A0h 1\u00A0min without a break, more than your longest availability (2\u00A0h)", ); }); it("renders an alert section in HTML with problem icon and heading", () => { const html = runSheetToHTML(planOf(exact), exact); expect(html).toContain('class="run-sheet-problems" role="alert"'); expect(html).toContain('class="problem-icon"'); expect(html).toContain("To fix"); expect(html).toContain("
  • Mise en place — Dough, around Sun 04:10"); }); it("renders the alert section in French in HTML", () => { const html = runSheetToHTML(planOf(exact), exact, { lang: "fr" }); expect(html).toContain("À régler"); }); }); describe("runSheetToMarkdown", () => { const md = runSheetToMarkdown(sheet, compiled); it("has a title, a heading per day and checklists", () => { expect(md).toContain("# Run sheet — Country bread"); expect(md).toContain("## Saturday, October 10 · 21:40 → 22:00"); expect(md).toContain( "### Dough · Start: 21:40\n\n**Mise en place**\n\n- [ ] flour 500 g", ); expect(md).toContain("**Execution**\n\n1. **[Mix]** Mix **flour** with"); }); it("numbers the three parts of a visit when the mise en place is told per day", () => { const day = runSheetToMarkdown(planIn(compiled, "perDay"), compiled); expect(day).toContain("### 1. Mise en place\n\n- [ ] flour 500 g"); expect(day).toContain("### 2. Execution\n\n**Dough** · Start:"); expect(day).toContain("### 3. Before leaving"); }); it("puts the adjustment under its day", () => { expect(md).toContain("> ⚠️ Dough : Rise extended to 14\u00A0h 30\u00A0min"); }); }); describe("runSheetToHTML", () => { const html = runSheetToHTML(sheet, compiled); it("is a section with the schedule, then a block per day", () => { expect(html).toContain('
    '); expect(html).toContain('
      '); expect(html).toContain('
      '); expect(html).toContain( 'Saturday, October 10', ); expect(html).toContain( 'flour500 g', ); expect(html).toContain('
    1. '); expect(html).toContain('class="box-123 box-step-3 run-sheet-leaving"'); expect(html).toContain('

      Serve at 13:00

      '); expect(html).toContain('class="run-sheet-notice"'); }); it("needs no script", () => { expect(html).not.toContain(" { const hostile = compile( getAST( "---\ntitle: \n---\n\n## ->&d\n\n[Mix] Mix @flour{1g} now ~{5min}.\n\n[Rest] Wait ~_{1h}.\n\n## Bake\n\n[Bake] Bake &d{1g} ~{5min}.\n", ), ); const out = runSheetToHTML( planOf(hostile, { availability: { daily: [{ start: "00:00", end: "24:00" }] }, }), hostile, ); expect(out).not.toContain("