Repository Public
1import { describe, expect, it } from "vitest";2import { parseDocument } from "htmlparser2";3import { BlockSchema, THEMES, TONES, type Block } from "@/schema";4import { renderBlockSvg, resolveCss } from "@/renderer/export-svg";5import { CHART, DIAGRAM, MATRIX, SVG_BLOCK, TIMELINE } from "./renderer.fixture";67/** Strict enough well-formedness check: one root, every tag closed in order, attributes quoted, no stray < or &. */8function assertWellFormed(xml: string): void {9 let body = xml.replace(/^<\?xml[^?]*\?>\s*/, "");10 expect(body.startsWith("<svg")).toBe(true);11 const stack: string[] = [];12 let roots = 0;13 const re = /<(\/?)([A-Za-z][\w:.-]*)((?:\s+[\w:.-]+="[^"<]*")*)\s*(\/?)>|<|&(?!(?:amp|lt|gt|quot|apos|#\d+|#x[0-9a-fA-F]+);)/g;14 let m: RegExpExecArray | null;15 while ((m = re.exec(body))) {16 if (m[0] === "<" || m[0].startsWith("&")) throw new Error(`stray "${m[0]}" at ${m.index}: ${body.slice(Math.max(0, m.index - 40), m.index + 60)}`);17 const [, close, name, , selfClose] = m;18 if (close) { const top = stack.pop(); if (top !== name) throw new Error(`</${name}> closes <${top}>`); }19 else if (!selfClose) { if (stack.length === 0) roots++; stack.push(name); }20 else if (stack.length === 0) roots++;21 }22 expect(stack).toEqual([]);23 expect(roots).toBe(1);24 body = "";25}2627const blocks: Record<string, Block> = {28 diagram: BlockSchema.parse(DIAGRAM), chart: BlockSchema.parse(CHART), matrix: BlockSchema.parse(MATRIX), svg: BlockSchema.parse(SVG_BLOCK), timeline: BlockSchema.parse(TIMELINE),29 "timeline (vertical)": BlockSchema.parse({ ...TIMELINE, orientation: "vertical" }),30};3132describe("renderBlockSvg", () => {33 for (const [name, block] of Object.entries(blocks)) {34 it(`${name}: standalone, well-formed, no CSS variable left`, () => {35 for (const preset of THEMES) {36 const svg = renderBlockSvg(block, { theme: { preset, accent: "teal" }, locale: "fr", fontCss: '@font-face{font-family:"Inter Variable";src:url(data:font/woff2;base64,AAAA) format("woff2")}' });37 expect(svg).not.toBeNull();38 const out = svg as string;39 assertWellFormed(out);40 expect(out).not.toContain("var(--");41 expect(out).not.toContain("color-mix(");42 expect(out).not.toContain("currentColor\"></rect>");43 expect(out).toMatch(/^<\?xml version="1.0" encoding="UTF-8"\?>\n<svg xmlns="http:\/\/www.w3.org\/2000\/svg"/);44 expect(out).toMatch(/<svg[^>]* viewBox="[-\d. ]+"/); expect(out).toMatch(/<svg[^>]* width="[\d.]+"/); expect(out).toMatch(/<svg[^>]* height="[\d.]+"/);45 expect(out).toContain("<title>"); expect(out).toContain("<style>"); expect(out).toContain("@font-face"); expect(out).toContain("Inter Variable");46 expect(out).not.toMatch(/NaN|undefined/);47 // It also parses as XML with the expected root.48 const doc = parseDocument(out, { xmlMode: true });49 const root = doc.children.find((c) => c.type === "tag");50 expect(root && "name" in root ? root.name : "").toBe("svg");51 }52 });53 }5455 it("puts <title> and <desc> first, from alt and caption", () => {56 const out = renderBlockSvg(blocks.diagram, { theme: { preset: "scientific-light", accent: "blue" }, locale: "fr" }) as string;57 expect(out).toMatch(/<svg[^>]*><title>Schéma du pipeline<\/title><desc>Flux de données<\/desc><style>/);58 });5960 it("resolves tones to the palette of the theme", () => {61 const light = renderBlockSvg(blocks.diagram, { theme: { preset: "scientific-light", accent: "blue" }, locale: "fr" }) as string;62 const dark = renderBlockSvg(blocks.diagram, { theme: { preset: "scientific-dark", accent: "blue" }, locale: "fr" }) as string;63 expect(light).toContain("#0f766e"); expect(dark).toContain("#5eead4"); expect(dark).toContain('fill="#141a23"');64 for (const accent of TONES) expect(resolveCss("var(--sh-accent) color-mix(in srgb, var(--sh-contra) 50%, var(--sh-surface))", { preset: "finance", accent })).toMatch(/^#[0-9a-f]{6} #[0-9a-f]{6}$/);65 });6667 it("returns null for blocks that are not vector figures", () => {68 expect(renderBlockSvg(BlockSchema.parse({ id: "p", type: "paragraph", text: "x" }), { theme: { preset: "editorial", accent: "red" }, locale: "en" })).toBeNull();69 expect(renderBlockSvg(BlockSchema.parse({ id: "q", type: "code", language: "text", code: "x" }), { theme: { preset: "editorial", accent: "red" }, locale: "en" })).toBeNull();70 });7172 it("is deterministic", () => {73 const opts = { theme: { preset: "blueprint", accent: "amber" }, locale: "en" } as const;74 for (const block of Object.values(blocks)) expect(renderBlockSvg(block, opts)).toBe(renderBlockSvg(block, opts));75 });76});77