From 5589574bf93a867c5291347b1a6dea7a79741475 Mon Sep 17 00:00:00 2001 From: abiwab Date: Thu, 5 Nov 2026 23:18:09 +0100 Subject: [PATCH 1/2] test(views): read the views from the registry in every surface test --- .../src/components/playground/outputs.test.ts | 22 ++++++++++++++----- .../tests/views-protocol.test.ts | 11 +++------- packages/renderer/CONTRIBUTING.md | 5 +++-- packages/renderer/tests/registry.test.ts | 10 +++++++-- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/packages/docs/src/components/playground/outputs.test.ts b/packages/docs/src/components/playground/outputs.test.ts index 5a1018f4..35c642b1 100644 --- a/packages/docs/src/components/playground/outputs.test.ts +++ b/packages/docs/src/components/playground/outputs.test.ts @@ -31,14 +31,26 @@ describe("the view tabs and the inspector menu", () => { const t = getDictionary("en"); const tabs = viewTabs(t); const menu = inspectorOptions(t); - expect(tabs).toEqual([ - { label: "Recipe", value: "recipe" }, - { label: "Work plan", value: "workPlan" }, - ]); - expect(menu.map((o) => o.value)).toEqual(["ast", "json", "json-tree"]); + // Read from the registry, not listed here: a view added there needs no + // edit in this test. + expect(tabs.map((o) => o.value)).toEqual( + outputs.filter((o) => o.kind === "renderer").map((o) => o.id), + ); + expect(menu.map((o) => o.value)).toEqual( + outputs.filter((o) => o.kind === "inspector").map((o) => o.id), + ); expect([...tabs, ...menu].map((o) => o.value)).toEqual( outputs.map((o) => o.id), ); + expect(tabs).toContainEqual({ label: "Recipe", value: "recipe" }); + expect(tabs).toContainEqual({ label: "Work plan", value: "workPlan" }); + }); + + it("names every output, in every language", () => { + for (const lang of ["en", "fr"] as const) { + const names = getDictionary(lang).renderer.outputs; + for (const output of outputs) expect(names[output.id]).toBeTruthy(); + } }); it("names them in the playground's language", () => { diff --git a/packages/language-server/tests/views-protocol.test.ts b/packages/language-server/tests/views-protocol.test.ts index 26db1f7e..87c5d2d7 100644 --- a/packages/language-server/tests/views-protocol.test.ts +++ b/packages/language-server/tests/views-protocol.test.ts @@ -1,4 +1,5 @@ import { afterEach, describe, expect, it } from "bun:test"; +import { outputs } from "@gram-lang/renderer"; import { Client, type Note, URI } from "./helpers/lsp-client"; /* @@ -76,14 +77,8 @@ describe("the views protocol", () => { }; }; const listed = result.capabilities.experimental?.gramViews?.outputs ?? []; - expect(listed.map((o) => o.id)).toEqual([ - "recipe", - "workPlan", - "ast", - "json", - "json-tree", - ]); - expect(listed[1]?.label).toBe("Work plan"); + expect(listed.map((o) => o.id)).toEqual(outputs.map((o) => o.id)); + expect(listed.find((o) => o.id === "workPlan")?.label).toBe("Work plan"); }, 20_000); it("sends nothing to a views client until it says what is open", async () => { diff --git a/packages/renderer/CONTRIBUTING.md b/packages/renderer/CONTRIBUTING.md index 4243a6dc..2269a800 100644 --- a/packages/renderer/CONTRIBUTING.md +++ b/packages/renderer/CONTRIBUTING.md @@ -16,12 +16,13 @@ A view never imports another view. What two views share goes in `shared/`. `test ## Adding a view 1. Create `src/views//`. It imports only its own files, `shared/`, the contract, the settings, the types, `prepareViews` and the pipeline packages. -2. Write its `view.ts`, a `ViewDefinition`: its `formats` (only those it can really express), the settings it `honors`, what it `needs` from the input (`schedule`, `runSheet`, `serveTimes`), and `render()`. Return `null` when the recipe has nothing for it. +2. Write its `view.ts`, a `ViewDefinition`: its `formats` (only those it can really express, and always `html`: the editor's panel and the playground show a view in html; `gram export` writes `markdown`, `print` and `text`, and `gram print` needs `print`, so a view with `html` alone is on screen only), the settings it `honors`, what it `needs` from the input (`schedule`, `runSheet`, `serveTimes`), and `render()`. Return `null` when the recipe has nothing for it. 3. Add it to `views` in `src/views/registry.ts`, and to `outputs` in `src/outputs.ts` (written out, not spread, so a bundler can drop the views a page does not use). Give it a name in every language, under `renderer.outputs` of `@gram-lang/i18n`: the surfaces list the outputs by that name. 4. Run `tests/views-contract.test.ts`. It renders the view in each format for every fixture, checks the escaping, and checks that the view reacts to each setting it honors and to no other. If it honors a setting none of the fixtures shows, extend a fixture. 5. If the view brings a new setting: add its descriptor to `VIEW_SETTINGS` in `src/settings.ts`, a probe to `PROBES` in the contract test, a `gram.*` key in the VS Code extension's `package.json` (the parity test fails until it exists), and the docs in English and French. +6. Document the view in English and French (what it shows, its formats, the settings it follows), and add a changeset: a new view is a new feature of every surface. -Nothing else should need an edit: the surfaces list their outputs from the registry, so a new view appears in the CLI, VS Code and the playground. Removing a view is the reverse. +Nothing else should need an edit, not even a test of a surface: the surfaces list their outputs from the registry, so a new view appears in the CLI, VS Code and the playground. Removing a view is the reverse. Every surface lists its outputs from the registry: the language server and the VS Code extension, the playground (as tabs, with the formats of each view), and the CLI (`--view` of `gram export` and `gram print`, each `--format` checked against the view's `formats`). The terminal views of `gram view` and `gram cook` are not renderer strings and stay apart. diff --git a/packages/renderer/tests/registry.test.ts b/packages/renderer/tests/registry.test.ts index 487e16c0..731901b3 100644 --- a/packages/renderer/tests/registry.test.ts +++ b/packages/renderer/tests/registry.test.ts @@ -46,11 +46,17 @@ const input = prepareViews(compiled, settings, { lang: "fr" }); describe("the views registry", () => { it("lists each view once, with formats and settings that exist", () => { + // Read from the registry, not listed here: a view added there needs no + // edit in this test. const ids = views.map((view) => view.id); - expect(ids).toEqual(["recipe", "workPlan"]); + expect(new Set(ids).size).toBe(ids.length); + expect(ids).toContain("recipe"); + expect(ids).toContain("workPlan"); for (const view of views) { expect(view.kind).toBe("renderer"); - expect(view.formats.length).toBeGreaterThan(0); + // Every view can be shown on screen: the editor's panel and the + // playground's preview render it in html. + expect(view.formats).toContain("html"); for (const format of view.formats) expect(FORMATS).toContain(format); for (const key of view.honors) expect(Object.keys(VIEW_SETTINGS)).toContain(key); -- 2.54.0 From 58086f67f965c0aa52a7139dbaf5d2ab6edf424e Mon Sep 17 00:00:00 2001 From: abiwab Date: Thu, 5 Nov 2026 23:18:10 +0100 Subject: [PATCH 2/2] refactor(cli): derive export and print flags and help from the views registry --- packages/cli/src/commands/export.ts | 12 ++++++------ packages/cli/src/commands/print.ts | 8 ++++---- packages/cli/src/core/cli-views.ts | 7 +++++++ packages/cli/src/services/comments-flag.ts | 2 +- packages/cli/src/services/setting-args.ts | 20 +++++++++++--------- packages/cli/tests/cli-views.test.ts | 9 +++++++++ packages/cli/tests/setting-args.test.ts | 20 ++++++++++---------- 7 files changed, 48 insertions(+), 30 deletions(-) diff --git a/packages/cli/src/commands/export.ts b/packages/cli/src/commands/export.ts index 303d5a97..68d103d6 100644 --- a/packages/cli/src/commands/export.ts +++ b/packages/cli/src/commands/export.ts @@ -22,7 +22,7 @@ import { resolveCliFormat, } from "../core/cli-views"; import { - RECIPE_VIEW_SETTINGS, + EXPORT_VIEW_SETTINGS, STEP_QTY_ALIAS_ARG, readRecipeSettingArgs, readViewSettingArgs, @@ -35,7 +35,7 @@ export default defineCommand({ meta: { name: "export", description: - "Export a view of a recipe (the recipe or its work plan) to Markdown or print-ready HTML", + "Export a view of a recipe (see --view) to Markdown, print-ready HTML or plain text", }, args: { file: { @@ -46,13 +46,13 @@ export default defineCommand({ format: { type: "string", description: - "Output format: md (Markdown), html (print-ready A4 HTML) or text (plain text). Each view offers only the formats it has (the work plan all three)", + "Output format: md (Markdown), html (print-ready A4 HTML) or text (plain text), among those the chosen view has", }, output: { type: "string", alias: "o", description: - "Output file path (default: same directory as input, with new extension; .work-plan before it for the work plan)", + "Output file path (default: same directory as input, with new extension; the view's name before it for any view but the recipe, e.g. .work-plan)", }, scale: { type: "string", @@ -68,7 +68,7 @@ export default defineCommand({ description: "Skip ingredient database", default: false, }, - ...viewSettingArgs(RECIPE_VIEW_SETTINGS), + ...viewSettingArgs(EXPORT_VIEW_SETTINGS), ...STEP_QTY_ALIAS_ARG, ...recipeSettingArgs(), "bakers-reference": { @@ -110,7 +110,7 @@ export default defineCommand({ const bakersReference = (args["bakers-reference"] as string) || (args["bakers-math"] ? "" : undefined); - const settings = readViewSettingArgs(args, RECIPE_VIEW_SETTINGS); + const settings = readViewSettingArgs(args, EXPORT_VIEW_SETTINGS); const recipe = readRecipeSettingArgs(args); let view: ReturnType; let format: ReturnType; diff --git a/packages/cli/src/commands/print.ts b/packages/cli/src/commands/print.ts index 286a5797..4ad328df 100644 --- a/packages/cli/src/commands/print.ts +++ b/packages/cli/src/commands/print.ts @@ -16,7 +16,7 @@ import { import { DEFAULT_VIEW_SETTINGS } from "@gram-lang/renderer"; import { cliViewName } from "../core/cli-views"; import { - RECIPE_VIEW_SETTINGS, + EXPORT_VIEW_SETTINGS, STEP_QTY_ALIAS_ARG, readRecipeSettingArgs, readViewSettingArgs, @@ -29,7 +29,7 @@ export default defineCommand({ meta: { name: "print", description: - "Generate a print-ready HTML of a view (the recipe or its work plan) and open it in the browser", + "Generate a print-ready HTML of a view of a recipe (see --view) and open it in the browser", }, args: { file: { @@ -57,7 +57,7 @@ export default defineCommand({ description: "Skip ingredient database", default: false, }, - ...viewSettingArgs(RECIPE_VIEW_SETTINGS), + ...viewSettingArgs(EXPORT_VIEW_SETTINGS), ...STEP_QTY_ALIAS_ARG, ...recipeSettingArgs(), "bakers-reference": { @@ -91,7 +91,7 @@ export default defineCommand({ const bakersReference = (args["bakers-reference"] as string) || (args["bakers-math"] ? "" : undefined); - const settings = readViewSettingArgs(args, RECIPE_VIEW_SETTINGS); + const settings = readViewSettingArgs(args, EXPORT_VIEW_SETTINGS); const recipe = readRecipeSettingArgs(args); let view: ReturnType; try { diff --git a/packages/cli/src/core/cli-views.ts b/packages/cli/src/core/cli-views.ts index 14108498..54c656e1 100644 --- a/packages/cli/src/core/cli-views.ts +++ b/packages/cli/src/core/cli-views.ts @@ -84,6 +84,13 @@ export function resolveCliFormat( formats: CliFormats, ): Format { const offered = cliFormatsOf(view, formats); + if (offered.length === 0) { + // A view shown on screen only (html alone): nothing to list. + throw new GramCLIError( + `The ${cliViewName(view.id)} view has none of the formats this command writes.`, + ExitCode.Error, + ); + } if (!offered.includes(name)) { const known = name in formats; throw new GramCLIError( diff --git a/packages/cli/src/services/comments-flag.ts b/packages/cli/src/services/comments-flag.ts index a4835835..22e181d9 100644 --- a/packages/cli/src/services/comments-flag.ts +++ b/packages/cli/src/services/comments-flag.ts @@ -10,7 +10,7 @@ const SETTING = VIEW_SETTINGS.comments; export const COMMENTS_FLAG_VALUES: readonly string[] = flagValues(SETTING); -export const COMMENTS_FLAG_DESCRIPTION = `Whether to show the comments of the recipe (// like this): ${flagValueList(SETTING)} (default: ${flagDefault(SETTING)}). Applies to the recipe and to the work plan.`; +export const COMMENTS_FLAG_DESCRIPTION = `Whether to show the comments of the recipe (// like this): ${flagValueList(SETTING)} (default: ${flagDefault(SETTING)}).`; /** Reports and exits on an unusable value, like every setting flag. */ export function parseComments(value: unknown): CommentsMode | undefined { diff --git a/packages/cli/src/services/setting-args.ts b/packages/cli/src/services/setting-args.ts index bd4bd55f..1b5bef17 100644 --- a/packages/cli/src/services/setting-args.ts +++ b/packages/cli/src/services/setting-args.ts @@ -5,6 +5,7 @@ import { type ViewSettings, VIEW_SETTINGS, parseRecipeSettings, + views, } from "@gram-lang/renderer"; import type { ArgDef } from "citty"; import { COMMENTS_FLAG_DESCRIPTION } from "./comments-flag"; @@ -183,15 +184,16 @@ export const STEP_QTY_ALIAS_ARG: Record = { }, }; -/** Every view setting the recipe view honors: what `print`, `export` and `view` expose. */ -export const RECIPE_VIEW_SETTINGS: readonly ViewSettingKey[] = [ - "miseEnPlace", - "rests", - "comments", - "nutritionBasis", - "bakersMathOnly", - "stepQuantities", -]; +/** + * Every view setting a view of the registry honors: what `print` and `export` + * expose, since their `--view` picks any view. Read from the registry, so the + * setting a new view brings gets its flag here with no edit. + */ +export const EXPORT_VIEW_SETTINGS: readonly ViewSettingKey[] = ( + Object.keys(VIEW_SETTINGS) as ViewSettingKey[] +).filter((key) => + views.some((view) => (view.honors as readonly string[]).includes(key)), +); /** What the work plan honors: `gram plan`. */ export const WORK_PLAN_SETTINGS: readonly ViewSettingKey[] = [ diff --git a/packages/cli/tests/cli-views.test.ts b/packages/cli/tests/cli-views.test.ts index 2ec53ebd..0991a8a3 100644 --- a/packages/cli/tests/cli-views.test.ts +++ b/packages/cli/tests/cli-views.test.ts @@ -66,6 +66,15 @@ describe("formats", () => { ); }); + it("says so when a view has none of the formats a command writes", () => { + // A view on screen only: gram export's html is the print layout. + const screenOnly = { ...recipe, id: "notes", formats: ["html"] } as const; + expect(cliFormatsOf(screenOnly, EXPORT_FORMATS)).toEqual([]); + expect(() => resolveCliFormat(screenOnly, "html", EXPORT_FORMATS)).toThrow( + "The notes view has none of the formats this command writes.", + ); + }); + it("gives every export format a file extension", () => { for (const name of Object.keys(EXPORT_FORMATS)) { expect(EXPORT_EXTENSIONS[name]).toBeString(); diff --git a/packages/cli/tests/setting-args.test.ts b/packages/cli/tests/setting-args.test.ts index 54fd519a..4f0f2972 100644 --- a/packages/cli/tests/setting-args.test.ts +++ b/packages/cli/tests/setting-args.test.ts @@ -3,7 +3,7 @@ import { runPipeline } from "../src/core/pipeline"; import { cleanupTmpFile, makeDb, writeTmpFile } from "./helpers"; import { RECIPE_SETTINGS, VIEW_SETTINGS } from "@gram-lang/renderer"; import { - RECIPE_VIEW_SETTINGS, + EXPORT_VIEW_SETTINGS, STEP_QTY_ALIAS_ARG, readRecipeSettingArgs, readViewSettingArgs, @@ -19,12 +19,12 @@ describe("setting flags", () => { expect(settingFlagName("massStandardization")).toBe("mass-standardization"); }); - it("give the recipe view a flag for every view setting", () => { - expect([...RECIPE_VIEW_SETTINGS].sort()).toEqual( + it("give export and print a flag for every setting a view honors", () => { + expect([...EXPORT_VIEW_SETTINGS].sort()).toEqual( Object.keys(VIEW_SETTINGS).sort(), ); - const args = viewSettingArgs(RECIPE_VIEW_SETTINGS); - for (const key of RECIPE_VIEW_SETTINGS) { + const args = viewSettingArgs(EXPORT_VIEW_SETTINGS); + for (const key of EXPORT_VIEW_SETTINGS) { expect(args[settingFlagName(key)]).toBeDefined(); } }); @@ -46,7 +46,7 @@ describe("setting flags", () => { describe("readViewSettingArgs", () => { it("leaves out a setting whose flag is absent", () => { - expect(readViewSettingArgs({}, RECIPE_VIEW_SETTINGS)).toEqual({}); + expect(readViewSettingArgs({}, EXPORT_VIEW_SETTINGS)).toEqual({}); }); it("reads the CLI spelling back to the setting's value", () => { @@ -58,7 +58,7 @@ describe("readViewSettingArgs", () => { "bakers-math-only": true, "step-quantities": "hide", }, - RECIPE_VIEW_SETTINGS, + EXPORT_VIEW_SETTINGS, ), ).toEqual({ miseEnPlace: "perDay", @@ -72,7 +72,7 @@ describe("readViewSettingArgs", () => { expect( readViewSettingArgs( { nutrition: "per-100g", "step-qty": false }, - RECIPE_VIEW_SETTINGS, + EXPORT_VIEW_SETTINGS, ), ).toEqual({ nutritionBasis: "per100g", stepQuantities: "hide" }); }); @@ -86,7 +86,7 @@ describe("readViewSettingArgs", () => { "step-quantities": "show", "step-qty": false, }, - RECIPE_VIEW_SETTINGS, + EXPORT_VIEW_SETTINGS, ), ).toEqual({ nutritionBasis: "total", stepQuantities: "show" }); }); @@ -107,7 +107,7 @@ describe("readViewSettingArgs", () => { const out = spyOn(process.stdout, "write").mockImplementation(() => true); try { expect(() => - readViewSettingArgs({ nutrition: "bad" }, RECIPE_VIEW_SETTINGS), + readViewSettingArgs({ nutrition: "bad" }, EXPORT_VIEW_SETTINGS), ).toThrow("exit"); } finally { exit.mockRestore(); -- 2.54.0