SPB Git

Repository Public

sim.render.test.tsx 6.2 KB 87 lines tsx

1import { describe, expect, it } from "vitest";2import { renderToStaticMarkup } from "react-dom/server";3import { SIM_KINDS, type SimKind } from "@/schema";4import { SimulationBlock } from "@/sim/SimulationBlock";5import { paramDefs, SIMULATIONS, type SimBlock } from "@/sim/registry";67const block = (kind: SimKind, params: Record<string, number> = {}, config: SimBlock["config"] = {}, seed = 7): SimBlock =>8  ({ id: `b-${kind}`, type: "simulation", span: 12, kind, params, seed, config });9const CONFIGS: Record<SimKind, SimBlock["config"][]> = {10  "moe-routing": [{}], "neural-forward": [{}, { activation: "relu" }, { activation: "sigmoid" }], "kv-cache": [{}],11  attention: [{}, { tokens: "a,b,c,d,e,f,g,h,i,j", causal: true }],12  distribution: [{}, { family: "student" }, { family: "lognormal" }, { family: "exponential" }, { family: "uniform" }],13  "confidence-interval": [{}], regression: [{}], correlation: [{}], garch: [{}],14  "random-walk": [{}, { process: "gbm" }, { process: "ou" }], "impulse-response": [{}, { shock: 2 }], "event-study": [{}, { window: 3 }],15  "function-plot": [{ expr: "a * sin(w * x)", expr2: "1 / x", expr3: "tan(x)", expr4: "relu(x) - b" }, { expr: "x ^ 2" }],16};1718describe("SimulationBlock server rendering", () => {19  for (const kind of SIM_KINDS) {20    for (const locale of ["fr", "en"] as const) {21      it(`${kind} renders in ${locale} (interactive and static, every configuration, extreme parameters)`, () => {22        const def = SIMULATIONS[kind];23        for (const config of CONFIGS[kind]) {24          const html = renderToStaticMarkup(<SimulationBlock block={block(kind, {}, config)} locale={locale} />);25          expect(html).toContain(`data-sim="${kind}"`);26          expect(html).toContain("<svg");27          expect(html).not.toContain("NaN");28          expect(html).not.toContain("undefined");29          expect(html).not.toContain("sim-error");30          expect(html).toContain(def.title[locale].replace(/&/g, "&amp;").replace(/'/g, "&#x27;"));31          if (def.stochastic) expect(html).toContain(locale === "fr" ? "Données synthétiques — graine 7" : "Synthetic data — seed 7");32          else expect(html).toContain(locale === "fr" ? "Illustration analytique" : "Analytic illustration");33          const defs = paramDefs(kind, config);34          expect(html.match(/class="sim-slider"/g)?.length ?? 0).toBe(defs.length);35          if (defs.length || def.stochastic) expect(html).toContain(locale === "fr" ? "Réinitialiser les paramètres" : "Reset parameters");36          else expect(html).not.toContain("sim-controls");37          if (def.steps) expect(html).toContain(locale === "fr" ? "Lecture" : "Play");38          if (def.stochastic) expect(html).toContain(locale === "fr" ? "Nouveau tirage" : "New draw");3940          const stat = renderToStaticMarkup(<SimulationBlock block={block(kind, {}, config)} locale={locale} staticOnly />);41          expect(stat).toContain("<svg"); expect(stat.includes("sim-summary")).toBe(defs.length > 0);42          expect(stat).not.toContain("<input"); expect(stat).not.toContain("<button"); expect(stat).not.toContain("NaN");4344          for (const edge of ["min", "max"] as const) {45            const params = Object.fromEntries(defs.map((d) => [d.key, d[edge]]));46            const h = renderToStaticMarkup(<SimulationBlock block={block(kind, params, config)} locale={locale} />);47            expect(h).toContain("<svg"); expect(h).not.toContain("NaN"); expect(h).not.toContain("sim-error");48          }49        }50      });51    }52  }5354  it("is reproducible: same block, same markup; another seed, another picture", () => {55    const a = renderToStaticMarkup(<SimulationBlock block={block("garch")} locale="fr" />);56    expect(renderToStaticMarkup(<SimulationBlock block={block("garch")} locale="fr" />)).toBe(a);57    expect(renderToStaticMarkup(<SimulationBlock block={block("regression", {}, {}, 8)} locale="fr" />)).not.toBe(renderToStaticMarkup(<SimulationBlock block={block("regression", {}, {}, 9)} locale="fr" />));58  });59  it("uses the block title, caption and alt text, and formats numbers in the locale", () => {60    const b: SimBlock = { ...block("correlation", { rho: 0.25 }), title: "Mon titre", caption: "Ma légende", alt: "Nuage de points" };61    const fr = renderToStaticMarkup(<SimulationBlock block={b} locale="fr" />);62    expect(fr).toContain("Mon titre"); expect(fr).toContain("Ma légende"); expect(fr).toContain("Nuage de points");63    expect(fr).toContain("0,25");64    expect(renderToStaticMarkup(<SimulationBlock block={b} locale="en" />)).toContain("0.25");65    const st = renderToStaticMarkup(<SimulationBlock block={b} locale="fr" staticOnly />);66    expect(st).toContain("Paramètres : Corrélation vraie ρ = 0,25 ; Observations n = 150");67  });68  it("shows an inline message instead of crashing on an invalid configuration", () => {69    for (const staticOnly of [false, true]) {70      const bad = renderToStaticMarkup(<SimulationBlock block={block("function-plot", {}, { expr: "x.constructor(" })} locale="fr" staticOnly={staticOnly} />);71      expect(bad).toContain("sim-error"); expect(bad).toContain("Configuration invalide"); expect(bad).toContain("config.expr");72      const missing = renderToStaticMarkup(<SimulationBlock block={block("function-plot")} locale="en" staticOnly={staticOnly} />);73      expect(missing).toContain("config.expr is required");74      const unknown = renderToStaticMarkup(<SimulationBlock block={{ ...block("garch"), kind: "nope" } as unknown as SimBlock} locale="en" staticOnly={staticOnly} />);75      expect(unknown).toContain("Unknown simulation");76    }77    // Out-of-range and unknown parameters are clamped or ignored, never fatal.78    const odd = renderToStaticMarkup(<SimulationBlock block={block("moe-routing", { experts: 1e9, topK: -4, bogus: 3 })} locale="fr" />);79    expect(odd).not.toContain("sim-error"); expect(odd).toContain("<svg");80  });81  it("range inputs carry min, max, step, a label and a readable value", () => {82    const html = renderToStaticMarkup(<SimulationBlock block={block("garch")} locale="fr" />);83    expect(html).toMatch(/<label for="sim-b-garch-alpha"><span>Réaction aux chocs α<\/span><output for="sim-b-garch-alpha">0,1<\/output><\/label>/);84    expect(html).toMatch(/<input id="sim-b-garch-alpha" type="range" min="0" max="0.5" step="0.01" aria-valuetext="0,1" value="0.1"\/>/);85  });86});87
Files main
Menu

Theme