SPB Git

Repository Public

research-latex.test.ts 17.9 KB 161 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/unit/research-latex.test.ts9 * Module           : tests10 * Repository       : https://git.spboucher.ai/note-spboucher-ai11 * Project Website  : https://note.spboucher.ai12 * Created          : 2026-11-1913 * Last Modified    : 2026-11-1914 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/**44 * Research (SPEC §4.14): LaTeX export of a project (sections from its pages, hypotheses, methods notes, equations kept as LaTeX, figure45 * placeholders named after the attachments, \cite keys + the .bib companion from the existing BibTeX export), project-scoped reference46 * listing. Pure conversion tests first, then a real project in the scratch database. No AI is involved anywhere in this export.47 */48import "../helpers/testdb";49import { test, before, after } from "node:test";50import assert from "node:assert/strict";51import { and, eq, inArray } from "drizzle-orm";52import { db, schema, sql } from "../../src/db/index";53import { createPage } from "../../src/lib/pages/service";54import { createReference, listReferences } from "../../src/lib/research/references";55import { createExperiment } from "../../src/lib/research/experiments";56import { parseBibtex } from "../../src/lib/research/bibtex";57import { escapeLatex, textToLatex, figureName, docToLatex, newCtx, sectionCmd, buildArticle, projectPages, projectReferences, exportProjectLatex } from "../../src/lib/research/latex";58import type { PMNode } from "../../src/lib/editor/text";5960const TAG = `tex${Date.now().toString(36)}`; let ws: string; const made: string[] = []; const refs: string[] = []; const atts: string[] = [];61const text = (t: string, marks?: any[]): PMNode => ({ type: "text", text: t, ...(marks ? { marks } : {}) });62const para = (...c: (PMNode | string)[]): PMNode => ({ type: "paragraph", content: c.map((x) => (typeof x === "string" ? text(x) : x)) });63const h = (level: number, t: string): PMNode => ({ type: "heading", attrs: { level }, content: [text(t)] });64before(async () => { const [w] = await db.select().from(schema.workspaces).limit(1); assert.ok(w, "workspace missing"); ws = w.id; });65after(async () => {66  if (refs.length) await db.delete(schema.researchReferences).where(inArray(schema.researchReferences.id, refs));67  if (made.length) { await db.delete(schema.experiments).where(inArray(schema.experiments.pageId, made)); await db.delete(schema.pages).where(inArray(schema.pages.id, made)); await db.delete(schema.syncTombstones).where(inArray(schema.syncTombstones.id, made)); }68  if (atts.length) await db.delete(schema.attachments).where(inArray(schema.attachments.id, atts));69  await sql.end();70});7172test("latex: text is escaped, but math, citation commands and \\ref typed on purpose are preserved and their keys collected", () => {73  assert.equal(escapeLatex("50% de R&D_2 #1 {x} ~ ^ \\ $5"), "50\\% de R\\&D\\_2 \\#1 \\{x\\} \\textasciitilde{} \\textasciicircum{} \\textbackslash{} \\$5");74  const keys = new Set<string>();75  assert.equal(textToLatex("Rendement de 5% selon \\cite{gorton2019} et \\citep[p.~3]{fama1993, french2015}, voir $\\beta_i = 0.5$ et \\eqref{eq:capm} & co.", keys), "Rendement de 5\\% selon \\cite{gorton2019} et \\citep[p.~3]{fama1993, french2015}, voir $\\beta_i = 0.5$ et \\eqref{eq:capm} \\& co.");76  assert.deepEqual([...keys].sort(), ["fama1993", "french2015", "gorton2019"]);77  assert.equal(textToLatex("Prix: 5 $ et 10 $ au total"), "Prix: 5 \\$ et 10 \\$ au total", "two dollar amounts are not an equation");78  assert.equal(textToLatex("$$E = mc^2$$"), "$$E = mc^2$$");79  assert.equal(figureName("Résultats 2026 (final).PNG", "f"), "Resultats-2026-final.png"); assert.equal(figureName("", "figure-1"), "figure-1"); assert.equal(figureName("graph", "f"), "graph");80  assert.deepEqual([sectionCmd(0), sectionCmd(1), sectionCmd(2), sectionCmd(3), sectionCmd(9)], ["section", "subsection", "subsubsection", "paragraph", "subparagraph"]);81});82test("latex: every block type converts — marks, lists, tasks, table, code, equations, figures, callouts, footnotes; unknown nodes are kept as marked text", () => {83  const c = newCtx([{ id: "r1", citeKey: "sharpe1964" }], new Map([["11111111-1111-4111-8111-111111111111", "Figure 1 — rendements.png"]]));84  const tex = docToLatex({ type: "doc", content: [85    h(1, "Méthode & données"),86    para("Texte ", text("gras", [{ type: "bold" }]), ", ", text("italique", [{ type: "italic" }]), ", ", text("x_i", [{ type: "code" }]), " et ", text("lien", [{ type: "link", attrs: { href: "https://example.org/a_b?x=1#y" } }]), "."),87    para("Selon ", { type: "citation", attrs: { refId: "r1", locator: "p. 12", label: "Sharpe 1964" } }, " et ", { type: "citation", attrs: { refId: "gone", label: "Disparu 2020" } }, "; inline ", { type: "inlineMath", attrs: { latex: "\\sigma^2" } }, "."),88    { type: "blockMath", attrs: { latex: "R_i = \\alpha + \\beta R_m + \\varepsilon_i" } },89    { type: "bulletList", content: [{ type: "listItem", content: [para("un")] }, { type: "listItem", content: [para("deux & trois")] }] },90    { type: "orderedList", content: [{ type: "listItem", content: [para("premier")] }] },91    { type: "taskList", content: [{ type: "taskItem", attrs: { checked: true }, content: [para("fait")] }, { type: "taskItem", attrs: { checked: false }, content: [para("à faire")] }] },92    { type: "table", content: [{ type: "tableRow", content: [{ type: "tableHeader", content: [para("Var")] }, { type: "tableHeader", content: [para("Coef")] }] }, { type: "tableRow", content: [{ type: "tableCell", content: [para("beta")] }, { type: "tableCell", content: [para("0,5")] }] }] },93    { type: "codeBlock", attrs: { language: "r" }, content: [text("lm(y ~ x, data = d) # 100%")] },94    { type: "image", attrs: { src: "/api/files/11111111-1111-4111-8111-111111111111", alt: "Rendements cumulés" } },95    { type: "callout", attrs: { kind: "hypothesis" }, content: [para("H1 : le bêta est stable.")] }, { type: "callout", attrs: { kind: "decision" }, content: [para("On garde l'échantillon 2010–2025.")] },96    { type: "blockquote", content: [para("Citation.")] }, para("Note", { type: "footnoteRef", attrs: { id: "1" } }), { type: "footnote", attrs: { id: "1" }, content: [text("Détail.")] },97    { type: "aiBlock", attrs: { model: "claude-haiku-5-5" }, content: [para("Passage généré.")] }, { type: "horizontalRule" },98    { type: "mermaid", attrs: { code: "graph TD; A-->B", preview: null } }, { type: "chart", attrs: { title: "Série", columns: ["an", "val"], rows: [[2025, 1.5], [2026, null]] } }, { type: "queryView", attrs: { title: "Tâches" } },99    { type: "futurNoeud", attrs: { x: 1 } }, { type: "futurTexte", content: [text("contenu inconnu 5%")] },100  ] }, c);101  for (const want of ["\\section{Méthode \\& données}", "\\textbf{gras}", "\\emph{italique}", "\\texttt{x\\_i}", "\\href{https://example.org/a_b?x=1\\#y}{lien}", "\\cite[p. 12]{sharpe1964}", "[?]% missing reference: Disparu 2020", "$\\sigma^2$",102    "\\begin{equation}\nR_i = \\alpha + \\beta R_m + \\varepsilon_i\n\\end{equation}", "\\begin{itemize}\n  \\item un\n  \\item deux \\& trois\n\\end{itemize}", "\\begin{enumerate}\n  \\item premier\n\\end{enumerate}", "\\item[$\\boxtimes$] fait", "\\item[$\\square$] à faire",103    "\\begin{tabular}{ll}", "  Var & Coef \\\\\n  \\hline", "  beta & 0,5 \\\\", "\\begin{verbatim}\nlm(y ~ x, data = d) # 100%\n\\end{verbatim}", "\\includegraphics[width=0.8\\linewidth]{figures/Figure-1-rendements.png}", "% placeholder: attachment \"Figure 1 — rendements.png\" (id 11111111-1111-4111-8111-111111111111)", "\\caption{Rendements cumulés}",104    "\\paragraph{Hypothesis}\nH1 : le bêta est stable.", "\\textbf{Decision.} On garde l'échantillon 2010–2025.", "\\begin{quote}\nCitation.\n\\end{quote}", "\\footnotemark[1]", "\\footnotetext[1]{Détail.}", "% --- AI-generated passage (claude-haiku-5-5) — review before submission ---", "Passage généré.",105    "% [mermaid diagram — source kept below]\n\\begin{verbatim}\ngraph TD; A-->B", "\\caption{Série}", "  2026 &  \\\\", "% [queryView: Tâches — live block, not exported]", "% [futurNoeud]", "% [futurTexte]\ncontenu inconnu 5\\%"]) assert.ok(tex.includes(want), `missing: ${want}\n---\n${tex}`);106  assert.deepEqual([...c.citeKeys], ["sharpe1964"]); assert.deepEqual(c.missingRefs, ["gone"]); assert.deepEqual(c.hypotheses, ["H1 : le bêta est stable."]); assert.deepEqual([...c.unknownNodes], ["futurNoeud", "futurTexte"]);107  assert.deepEqual([...c.figures.values()], [{ attachmentId: "11111111-1111-4111-8111-111111111111", name: "Figure 1 — rendements.png", file: "Figure-1-rendements.png", caption: "Rendements cumulés" }]);108});109test("latex: the article skeleton has title, abstract, one section per page nested by depth, hypotheses, methods notes and the bibliography", () => {110  const ref = { id: "r1", citeKey: "sharpe1964", entryType: "article", title: "Capital Asset Prices", authors: ["William F. Sharpe"], year: 1964, journal: "The Journal of Finance", doi: null, url: null, bibtex: null, fields: {} };111  const out = buildArticle({ title: "Bêta & stabilité", author: "S.-P. Boucher", date: "2026-11-19", refs: [ref], attachments: new Map(),112    pages: [113      { id: "p0", title: "Bêta & stabilité", depth: 0, content: { type: "doc", content: [h(2, "Abstract"), para("Nous testons la stabilité du bêta \\cite{sharpe1964}."), h(2, "Introduction"), para("Contexte \\cite{inconnu2020}.")] } },114      { id: "p1", title: "Données", depth: 1, content: { type: "doc", content: [para("Échantillon CRSP."), h(2, "Filtres"), para("Prix > 5 $."), { type: "callout", attrs: { kind: "hypothesis" }, content: [para("H1 : stabilité.")] }] } },115      { id: "p2", title: "Sous-échantillon", depth: 2, content: { type: "doc", content: [para("Détail.")] } },116      { id: "p3", title: "Résultats", depth: 1, content: { type: "doc", content: [] } },117    ],118    experiments: [{ name: "Fenêtre 60 mois", status: "done", params: { fenetre: 60 }, metrics: { r2: 0.31 }, datasetRef: "CRSP 2010–2025", repoUrl: "https://git.example.org/beta_test", notes: "Voir \\cite{sharpe1964}." }] });119  const t = out.tex;120  assert.ok(t.startsWith("% Generated by SP Note")); assert.match(t, /\\documentclass\[11pt\]\{article\}/); assert.ok(t.includes("\\title{Bêta \\& stabilité}")); assert.ok(t.includes("\\author{S.-P. Boucher}")); assert.ok(t.includes("\\date{2026-11-19}"));121  assert.ok(t.includes("\\begin{abstract}\nNous testons la stabilité du bêta \\cite{sharpe1964}.\n\\end{abstract}"), "the Abstract heading of the root page becomes the abstract");122  const order = ["\\maketitle", "\\begin{abstract}", "\\section{Introduction}", "\\section{Données}", "\\subsection{Filtres}", "\\subsection{Sous-échantillon}", "\\section{Résultats}", "\\section{Hypotheses}", "\\section{Methods notes}", "\\bibliographystyle{plain}", "\\bibliography{references}", "\\end{document}"];123  let at = -1; for (const o of order) { const i = t.indexOf(o); assert.ok(i > at, `"${o}" is present and in order`); at = i; }124  assert.ok(!/section\{Abstract\}/.test(t), "the abstract is not repeated as a section");125  assert.ok(t.includes("  \\item H1 : stabilité.")); assert.ok(t.includes("\\item[Fenêtre 60 mois] (done) Parameters: fenetre = 60. Metrics: r2 = 0.31. Data: CRSP 2010–2025. Code: \\url{https://git.example.org/beta_test}. Voir \\cite{sharpe1964}."));126  assert.ok(t.includes("% WARNING — cited but absent from the library (not in references.bib): inconnu2020")); assert.ok(t.includes("Prix > 5 \\$."));127  assert.deepEqual(out.citeKeys, ["inconnu2020", "sharpe1964"]); assert.deepEqual(out.missingKeys, ["inconnu2020"]); assert.equal(out.sections, 3); assert.equal(out.hypotheses, 1); assert.equal(out.experiments, 1);128  const parsed = parseBibtex(out.bib); assert.equal(parsed.length, 1); assert.equal(parsed[0].citeKey, "sharpe1964"); assert.equal(parsed[0].year, 1964, "the .bib companion is valid BibTeX (parsed back by the importer)");129  const empty = buildArticle({ title: "Vide", pages: [{ id: "p", title: "Vide", depth: 0, content: { type: "doc", content: [] } }], experiments: [], refs: [] }); assert.ok(empty.tex.includes("% no reference in this project")); assert.ok(empty.tex.includes("\\date{\\today}")); assert.equal(empty.bib, "");130});131test("research: project pages in tree order, project-scoped references (attached or cited), export and reference lookup by ids", async () => {132  const mk = async (title: string, content: PMNode[], parentId?: string, type = "page") => { const p = await createPage(ws, { title, parentId: parentId ?? null, section: "research", type, content: { type: "doc", content } }); made.push(p.id); return p; };133  const [att] = await db.insert(schema.attachments).values({ workspaceId: ws, filename: `courbe ${TAG}.png`, mime: "image/png", size: 10, sha256: "0".repeat(64), storagePath: `zz/zz/${TAG}.png` }).returning(); atts.push(att.id);134  const root = await mk(`Papier ${TAG}`, [h(2, "Résumé"), para("Résumé du papier."), h(2, "Introduction"), para(`Voir \\cite{lib${TAG}}.`)], undefined, "research");135  const a = await mk("Données", [para("Données."), { type: "image", attrs: { src: `/api/files/${att.id}`, alt: "Courbe" } }, { type: "blockMath", attrs: { latex: "y = x^2" } }], root.id);136  const b = await mk("Résultats", [{ type: "callout", attrs: { kind: "hypothesis" }, content: [para("H1.")] }], root.id);137  const a1 = await mk("Nettoyage", [para("Étapes.")], a.id);138  const trashed = await mk("Brouillon supprimé", [para("x")], root.id); await db.update(schema.pages).set({ deletedAt: new Date() }).where(eq(schema.pages.id, trashed.id));139  const outside = await mk(`Hors projet ${TAG}`, [para("Ailleurs.")]);140  const attached = await createReference(ws, { title: "Attached paper", authors: ["Ada Lovelace"], year: 1843, citeKey: `att${TAG}`, pageId: a.id }); refs.push(attached.id);141  const cited = await createReference(ws, { title: "Library paper", authors: ["Alan Turing"], year: 1950, citeKey: `lib${TAG}`, pageId: null }); refs.push(cited.id);142  const other = await createReference(ws, { title: "Unrelated", authors: ["X Y"], year: 2000, citeKey: `other${TAG}`, pageId: outside.id }); refs.push(other.id);143  await createExperiment(ws, { pageId: a.id, name: "Essai 1", status: "running", params: { n: 100 } });144  const pages = await projectPages(ws, root.id);145  assert.deepEqual(pages.map((p) => [p.title, p.depth]), [[`Papier ${TAG}`, 0], ["Données", 1], ["Nettoyage", 2], ["Résultats", 1]], "tree order, children before the next sibling, trashed pages left out");146  assert.deepEqual((await projectReferences(ws, pages)).map((r) => r.citeKey), [`att${TAG}`, `lib${TAG}`], "attached to a page of the project, or cited in its text — not the unrelated one");147  const x = await exportProjectLatex(ws, root.id, { author: "Auteur" });148  assert.equal(x.title, `Papier ${TAG}`); assert.deepEqual(x.pageIds, [root.id, a.id, a1.id, b.id]); assert.deepEqual(x.citeKeys, [`lib${TAG}`]); assert.deepEqual(x.missingKeys, []);149  assert.deepEqual(x.figures, [{ attachmentId: att.id, name: `courbe ${TAG}.png`, file: `courbe-${TAG}.png` }]); assert.equal(x.hypotheses, 1); assert.equal(x.experiments, 1); assert.equal(x.sections, 3);150  for (const want of ["\\begin{abstract}\nRésumé du papier.\n\\end{abstract}", "\\section{Données}", "\\subsection{Nettoyage}", "\\section{Résultats}", "\\begin{equation}\ny = x^2\n\\end{equation}", `\\includegraphics[width=0.8\\linewidth]{figures/courbe-${TAG}.png}`, `\\cite{lib${TAG}}`, "\\item[Essai 1] (running) Parameters: n = 100.", "\\bibliography{references}"]) assert.ok(x.tex.includes(want), `missing ${want}`);151  assert.ok(!x.tex.includes("Brouillon supprimé")); assert.ok(!x.tex.includes("Ailleurs."));152  const bib = parseBibtex(x.bib); assert.deepEqual(bib.map((e) => e.citeKey).sort(), [`att${TAG}`, `lib${TAG}`]);153  assert.equal((await exportProjectLatex(ws, a.id)).pageIds.length, 2, "any page can be exported as a (sub)project");154  await assert.rejects(exportProjectLatex(ws, "00000000-0000-4000-8000-000000000000"), (e: any) => e.status === 404 && e.code === "not_found");155  const byIds = await listReferences(ws, { ids: [cited.id, attached.id, "00000000-0000-4000-8000-000000000000"] }); assert.deepEqual(byIds.map((r) => r.id).sort(), [attached.id, cited.id].sort());156  // a `citation` node pulls its reference into the project even when nothing else links it157  const withNode = await mk(`Avec noeud ${TAG}`, [para("Voir ", { type: "citation", attrs: { refId: other.id, locator: "", label: "X 2000" } }, ".")]);158  const y = await exportProjectLatex(ws, withNode.id); assert.deepEqual(y.citeKeys, [`other${TAG}`]); assert.ok(y.tex.includes(`\\cite{other${TAG}}`)); assert.ok(y.bib.includes(`other${TAG}`));159  assert.equal((await db.select().from(schema.researchReferences).where(and(eq(schema.researchReferences.workspaceId, ws), inArray(schema.researchReferences.id, refs)))).length, 3, "an export never modifies the library");160});161
Files main
Menu

Theme