gram/packages/renderer/tests/module-badge.test.ts
abiwab 532ea55f24
All checks were successful
CI / ci (pull_request) Successful in 2m24s
feat(renderer): replace Phosphor icon webfont with self-contained inline SVG
Removes the unpkg.com/@phosphor-icons/web CDN dependency (docs Lighthouse
score, offline VS Code previews) by inlining Phosphor's SVG paths directly.
Centralizes all three icon variants (HTML/Markdown/print) in a single
packages/renderer/src/icons.ts, each icon exported as its own PURE-annotated
binding so bundlers can tree-shake unused icons per consumer (e.g. the
VS Code Gantt webview, which needs none of them, now carries zero icon code).
Also renames the leftover "ph"/"ph-*" CSS class hook to "gicon" since it no
longer has any real tie to the Phosphor webfont.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-25 08:13:09 +02:00

91 lines
3 KiB
TypeScript

import { describe, it, expect } from "bun:test";
import { getAST } from "@gram-lang/parser";
import { compile } from "@gram-lang/kitchen";
import { toHTML, toMarkdown, toPrintHTML } from "../src/index";
// `ProcessedSection.module` is stamped by `@gram-lang/modules` after
// compile() runs (see finalizeComposed) — the renderer only cares that the
// field is present, not how it got there, so a plain compile() + manual
// stamp is enough to exercise the renderer's own badge logic in isolation
// from the whole module-composition pipeline (already covered by
// packages/modules/tests/compose.test.ts). A `--stock`ed import never
// splices a section (module-imports RFC, stock/retro-planning redesign), so
// `sec.module` here is always `mode: "inline"` in practice — badge rendering
// no longer branches on mode at all.
function compileWithModule() {
const result = compile(getAST("## Montage\nUse the base.\n"));
Object.assign(result.sections[0]!, {
module: {
binding: "pate",
uri: "./bases/pate.gram",
title: "Pate Sablee",
mode: "inline",
},
});
return result;
}
describe("module provenance badge rendering", () => {
it("credits the source module in the HTML badge", () => {
const html = toHTML(compileWithModule());
expect(html).toContain("section-meta-module");
expect(html).toContain("Pate Sablee");
});
it("credits the source module parenthetically in markdown", () => {
const md = toMarkdown(compileWithModule());
expect(md).toContain("### Montage _(Pate Sablee)_");
});
it("shows the module credit parenthetically in print HTML", () => {
const html = toPrintHTML(compileWithModule());
expect(html).toContain("Pate Sablee");
});
it("falls back to the binding name when the module has no title", () => {
const result = compile(getAST("## Montage\nUse the base.\n"));
Object.assign(result.sections[0]!, {
module: {
binding: "pate",
uri: "./bases/pate.gram",
title: null,
mode: "inline",
},
});
const html = toHTML(result);
expect(html).toContain("</svg> pate</small>");
});
it("escapes a module title that could inject markup", () => {
const result = compile(getAST("## Montage\nUse the base.\n"));
Object.assign(result.sections[0]!, {
module: {
binding: "pate",
uri: "./bases/pate.gram",
title: "<img src=x onerror=alert(1)>",
mode: "inline",
},
});
const html = toHTML(result);
expect(html).not.toContain("<img");
expect(html).toContain("&lt;img");
});
it("renders a unified segmented declaration badge when module and intermediate declaration are both present", () => {
const result = compile(getAST("## Montage ->&pate\nUse the base.\n"));
Object.assign(result.sections[0]!, {
module: {
binding: "pate",
uri: "./bases/pate.gram",
title: "Pate Sablee",
mode: "inline",
},
});
const html = toHTML(result);
expect(html).toContain("section-declaration-badge has-module");
expect(html).toContain("section-module-source");
expect(html).toContain("Pate Sablee");
expect(html).toContain("section-decl-target");
expect(html).toContain("pate");
});
});