SPB Git

Repository Public

templates.quant.test.ts 9.5 KB 192 lines typescript

1import { describe, expect, it } from "vitest";2import katex from "katex";3import { LOCALES, allBlocks, parseSheet, type Sheet } from "@/schema";4import { QUANT_TEMPLATES } from "@/templates/quant";5import { paramDefs, validateSimulation } from "@/sim/registry";67const EXPECTED: Record<string, string> = {8  "garch-volatility": "finance",9  "var-impulse-response": "finance",10  "hf-event-study": "finance",11  "compute-cluster-architecture": "infrastructure",12  "data-pipeline-etl": "infrastructure",13  "three-methods-comparison": "method",14};1516/** Keys whose value is not inline text (raw LaTeX, code, identifiers, links). */17const NOT_INLINE = new Set(["latex", "symbol", "code", "url", "doi", "id", "type", "checkedAt"]);1819/** Every inline-text string of a document, with its path. */20function inlineStrings(value: unknown, path = "", out: { path: string; text: string }[] = []) {21  if (typeof value === "string") out.push({ path, text: value });22  else if (Array.isArray(value)) value.forEach((v, i) => inlineStrings(v, `${path}[${i}]`, out));23  else if (value && typeof value === "object") for (const [k, v] of Object.entries(value)) if (!NOT_INLINE.has(k)) inlineStrings(v, path ? `${path}.${k}` : k, out);24  return out;25}2627function build(id: string, locale: "fr" | "en"): Sheet {28  const def = QUANT_TEMPLATES.find((d) => d.id === id)!;29  const r = parseSheet(def.build(locale));30  if (!r.ok) throw new Error(`${id} (${locale}) is not a valid sheet:\n` + r.issues.map((i) => `  ${i.path}: ${i.message}`).join("\n"));31  return r.sheet;32}3334const tex = (latex: string, where: string) => {35  try { katex.renderToString(latex, { throwOnError: true, displayMode: true, strict: "error" }); }36  catch (e) { throw new Error(`${where}: KaTeX rejects «${latex}»: ${(e as Error).message}`); }37};3839describe("quant template library", () => {40  it("exports the six templates with fixed ids and categories", () => {41    expect(QUANT_TEMPLATES.map((d) => d.id)).toEqual(Object.keys(EXPECTED));42    for (const d of QUANT_TEMPLATES) {43      expect(d.category).toBe(EXPECTED[d.id]);44      for (const l of LOCALES) { expect(d.title[l].length).toBeGreaterThan(0); expect(d.description[l].length).toBeGreaterThan(0); }45    }46  });4748  for (const id of Object.keys(EXPECTED)) {49    describe(id, () => {50      for (const locale of LOCALES) {51        describe(locale, () => {52          it("is a valid sheet in the requested language", () => {53            const doc = build(id, locale);54            expect(doc.metadata.locale).toBe(locale);55            expect(doc.metadata.example).toBe(true);56            expect(doc.metadata.objectives.length).toBeGreaterThanOrEqual(2);57            expect(doc.metadata.objectives.length).toBeLessThanOrEqual(4);58          });5960          it("is deterministic", () => {61            const def = QUANT_TEMPLATES.find((d) => d.id === id)!;62            expect(JSON.stringify(def.build(locale))).toBe(JSON.stringify(def.build(locale)));63          });6465          it("has unique ids and at least 14 blocks", () => {66            const doc = build(id, locale);67            const ids = allBlocks(doc).map((b) => b.id);68            expect(new Set(ids).size).toBe(ids.length);69            expect(doc.blocks.length).toBeGreaterThanOrEqual(14);70            for (const list of [doc.sources, doc.claims]) expect(new Set(list.map((x) => x.id)).size).toBe(list.length);71          });7273          it("has the expected structure: h1 first, lead, diagram, quiz, accordion, references last", () => {74            const doc = build(id, locale);75            const first = doc.blocks[0];76            expect(first.type === "heading" && first.level === 1 && !!first.eyebrow).toBe(true);77            const second = doc.blocks[1];78            expect(second.type === "paragraph" && second.lead === true).toBe(true);79            const types = doc.blocks.map((b) => b.type);80            for (const needed of ["diagram", "equation", "cards", "quiz", "accordion", "callout"]) expect(types, needed).toContain(needed);81            expect(types[types.length - 1]).toBe("references");82          });8384          it("cites only sources that exist, and every source is verified and used", () => {85            const doc = build(id, locale);86            const known = new Set(doc.sources.map((s) => s.id));87            const cited = new Set<string>();88            for (const { path, text } of inlineStrings({ metadata: doc.metadata, blocks: doc.blocks, claims: doc.claims }))89              for (const m of text.matchAll(/\[\^([^\]]*)\]/g)) { expect(known.has(m[1]), `${path}: unknown citation [^${m[1]}]`).toBe(true); cited.add(m[1]); }90            expect(doc.sources.length).toBeGreaterThanOrEqual(3);91            expect(doc.sources.length).toBeLessThanOrEqual(6);92            for (const s of doc.sources) {93              expect(cited.has(s.id), `source ${s.id} is never cited`).toBe(true);94              expect(s.status).toBe("resolved");95              expect(s.checkedAt).toBeTruthy();96              expect(s.doi ?? s.url, `source ${s.id} has no locator`).toBeTruthy();97            }98          });99100          it("has 4–8 claims, each sourced or derived", () => {101            const doc = build(id, locale);102            expect(doc.claims.length).toBeGreaterThanOrEqual(4);103            expect(doc.claims.length).toBeLessThanOrEqual(8);104            for (const c of doc.claims) {105              expect(["sourced", "derived"]).toContain(c.status);106              if (c.status === "sourced") expect(c.sourceIds.length, `claim ${c.id}`).toBeGreaterThan(0);107            }108          });109110          it("has well-formed diagrams with a legend", () => {111            const doc = build(id, locale);112            const diagrams = allBlocks(doc).filter((b) => b.type === "diagram");113            expect(diagrams.length).toBeGreaterThanOrEqual(1);114            for (const d of diagrams) {115              const nodes = new Set(d.nodes.map((n) => n.id));116              expect(d.nodes.length).toBeGreaterThanOrEqual(8);117              for (const e of d.edges) { expect(nodes.has(e.from), `${d.id}/${e.id}: from ${e.from}`).toBe(true); expect(nodes.has(e.to), `${d.id}/${e.id}: to ${e.to}`).toBe(true); }118              expect(d.legend.length).toBeGreaterThan(0);119              // every node takes part in the picture120              const touched = new Set(d.edges.flatMap((e) => [e.from, e.to]));121              for (const n of d.nodes) expect(touched.has(n.id), `${d.id}: node ${n.id} is isolated`).toBe(true);122            }123          });124125          it("gives every visual block a real alt text and never claims empirical data", () => {126            const doc = build(id, locale);127            for (const b of allBlocks(doc)) {128              if (["diagram", "chart", "matrix", "simulation"].includes(b.type)) expect((b.alt ?? "").length, `${b.id}: alt`).toBeGreaterThan(60);129              if (b.type === "chart" || b.type === "matrix") expect(["illustrative", "synthetic"]).toContain(b.dataKind);130            }131          });132133          it("uses simulations with known parameters inside their bounds", () => {134            const doc = build(id, locale);135            for (const b of allBlocks(doc)) {136              if (b.type !== "simulation") continue;137              expect(validateSimulation(b), b.id).toEqual([]);138              const defs = paramDefs(b.kind, b.config);139              for (const [k, v] of Object.entries(b.params)) {140                const d = defs.find((x) => x.key === k);141                expect(d, `${b.id}: unknown parameter ${k}`).toBeTruthy();142                expect(v >= d!.min && v <= d!.max, `${b.id}: ${k} out of bounds`).toBe(true);143              }144            }145          });146147          it("has rows of spans that add up to 12", () => {148            const doc = build(id, locale);149            let acc = 0;150            for (const b of doc.blocks) {151              acc += b.span;152              expect(acc, `row overflows at ${b.id}`).toBeLessThanOrEqual(12);153              if (acc === 12) acc = 0;154            }155            expect(acc).toBe(0);156          });157158          it("contains only LaTeX that KaTeX can render", () => {159            const doc = build(id, locale);160            for (const b of allBlocks(doc)) {161              if (b.type !== "equation") continue;162              tex(b.latex, `${b.id}.latex`);163              expect((b.variables ?? []).length, `${b.id}: variables`).toBeGreaterThan(0);164              for (const v of b.variables ?? []) tex(v.symbol, `${b.id}.variables`);165            }166            for (const { path, text } of inlineStrings({ metadata: doc.metadata, blocks: doc.blocks, claims: doc.claims })) {167              const dollars = (text.match(/\$/g) ?? []).length;168              expect(dollars % 2, `${path}: unbalanced $ in «${text}»`).toBe(0);169              for (const m of text.matchAll(/\$([^$]+)\$/g)) tex(m[1], path);170            }171          });172173          it("contains no HTML", () => {174            const doc = build(id, locale);175            for (const { path, text } of inlineStrings({ metadata: doc.metadata, blocks: doc.blocks, claims: doc.claims })) expect(/<\/?[a-z][^>]*>/i.test(text), `${path}: «${text}»`).toBe(false);176          });177        });178      }179180      it("has the same block ids, types and spans in both languages", () => {181        const fr = build(id, "fr"), en = build(id, "en");182        const shape = (d: Sheet) => allBlocks(d).map((b) => `${b.id}:${b.type}:${b.span}`);183        expect(shape(fr)).toEqual(shape(en));184        expect(fr.sources.map((s) => s.id)).toEqual(en.sources.map((s) => s.id));185        expect(fr.claims.map((c) => c.id)).toEqual(en.claims.map((c) => c.id));186        // the two versions are really different texts187        expect(fr.metadata.title).not.toBe(en.metadata.title);188      });189    });190  }191});192
Files main
Menu

Theme