Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

18.6 KB
Raw Blame
#!/usr/bin/env node
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 The Bento authors
// bento/type mathematics rig.
//
//   node scripts/test-type-math.ts        (Node ≥ 23.6 strips types natively)
//
// WHAT THIS PROVES. `type/src/math/` turns TeX-like source into markup with no
// dependency and no DOM, and three properties have to hold for that to be safe
// to run on every render of every block:
//
//   1. TOTALITY. A formula is CONTENT. It arrives half-typed, pasted from a
//      paper, or generated by a script that got a brace wrong — and none of
//      those may throw, because the renderer is called from the layout loop and
//      an exception there blanks the page. Every malformed input below must
//      come back with markup.
//   2. DETERMINISM. The same source must produce the same bytes, every time and
//      in any order. The document is signed and redlined: markup that drifted
//      between renders would show up as a change nobody made.
//   3. THE PAGINATION INVARIANT. paginate.ts finds line boxes by walking TEXT
//      NODES and skipping whitespace-only ones. A formula that rendered to
//      elements with no text in them would take space the page breaks cannot
//      see, and every page after it would break in the wrong place. So every
//      formula — including the empty one — must carry a text node.
//
// The layout is checked as METRICS rather than as pixels: the module reports
// how far each box rises and falls, and the assertions are about those numbers
// (a nested fraction is taller; a fence clears its content; two scripts clear
// each other). That is testable without a browser, which is the point — a
// property only checked by eye is a property nobody checks.

import { lex, parseMath, MAX_NODES, type MathNode } from '../type/src/math/parse.ts';
import { layoutMath, renderMath, num, ZW } from '../type/src/math/layout.ts';
import { displayMathHtml, inlineMathHtml, mathErrors } from '../type/src/math.ts';

let checks = 0, failures = 0;
const ok = (c: boolean, m: string) => { checks++; if (!c) { failures++; console.log(`  FAIL  ${m}`); } else console.log(`  ok    ${m}`); };
const H = (s: string) => console.log(`\n=== ${s} ===`);

const P = (s: string): MathNode => parseMath(s).root;
const R = (s: string, display = false) => renderMath(s, { display });
/** the visible characters of a rendering, with the markup taken out */
const textOf = (html: string) => html.replace(/<[^>]*>/g, '').replace(/\u200B/g, '');
const kinds = (n: MathNode): string => {
  switch (n.k) {
    case 'row': return `row(${n.body.map(kinds).join(' ')})`;
    case 'atom': return n.ch;
    case 'op': return `op:${n.name ?? n.ch}`;
    case 'frac': return `frac(${kinds(n.num)},${kinds(n.den)})`;
    case 'sqrt': return `sqrt(${kinds(n.body)}${n.index ? `,idx=${kinds(n.index)}` : ''})`;
    case 'script': return `script(${kinds(n.base)}${n.sup ? `,^${kinds(n.sup)}` : ''}${n.sub ? `,_${kinds(n.sub)}` : ''})`;
    case 'fence': return `fence(${n.left}${kinds(n.body)}${n.right})`;
    case 'accent': return `acc(${kinds(n.body)})`;
    case 'bar': return `${n.under ? 'under' : 'over'}(${kinds(n.body)})`;
    case 'matrix': return `mat[${n.rows.map(r => r.map(kinds).join('|')).join(' ; ')}]`;
    case 'text': return `text(${n.s})`;
    case 'space': return `sp`;
    case 'style': return `style(${n.style},${kinds(n.body)})`;
    case 'font': return `font(${kinds(n.body)})`;
    case 'err': return `ERR(${n.s})`;
  }
};

// ─────────────────────────────────────────────────────────────── the lexer

H('the lexer');
{
  ok(lex('\\alpha').length === 2 && lex('\\alpha')[0].t === 'cmd',
     '\\alpha is one command token');
  const t = lex('\\alpha2');
  ok(t.length === 3 && t[0].t === 'cmd' && (t[0] as { v: string }).v === 'alpha'
     && t[1].t === 'ch' && (t[1] as { v: string }).v === '2',
     '\\alpha2 is the command \\alpha followed by a 2, not a command called alpha2');
  ok(lex('\\,')[0].t === 'cmd' && (lex('\\,')[0] as { v: string }).v === ',',
     'a backslash plus a non-letter is a one-character command');
  ok(lex('\\\\')[0].t === 'nl', '\\\\ is a row break');
  // whitespace IS tokenised — `\text{if x}` needs it — and then skipped by the
  // parser everywhere else, so `a b` is still `ab` in math mode
  ok(lex('a b').filter(x => x.t === 'ws').length === 1, 'whitespace is tokenised');
  ok(lex('a\n\tb').filter(x => x.t === 'ws').length === 2, 'newlines and tabs too');
  ok(kinds(P('a b')) === 'row(a b)' && kinds(P('x ^ 2')) === 'script(x,^2)',
     'and the parser ignores it — space is not a character in math mode');
}

// ─────────────────────────────────────────────────────────── the parser

H('structure and precedence');
{
  ok(kinds(P('x^2')) === 'script(x,^2)', 'x^2 is a script over x');
  ok(kinds(P('x^2_i')) === kinds(P('x_i^2')),
     'x^2_i and x_i^2 are the same tree — script order does not matter');
  ok(kinds(P('x^{a+b}')) === 'script(x,^row(a + b))',
     'a braced script takes the whole group');
  ok(kinds(P('x^ab')) === 'row(script(x,^a) b)',
     'an unbraced script takes ONE token — x^ab is (x^a)b');
  ok(kinds(P('\\frac{a}{b}')) === 'frac(a,b)', '\\frac takes two arguments');
  ok(kinds(P('\\frac12')) === 'frac(1,2)',
     '\\frac12 is one half — a single token counts as an argument, as in TeX');
  ok(kinds(P('\\frac{\\frac{a}{b}}{c}')) === 'frac(frac(a,b),c)', 'fractions nest');
  ok(kinds(P('x^{\\frac{a}{b}}')) === 'script(x,^frac(a,b))', 'a fraction fits inside a script');
  ok(kinds(P('\\frac{x^2}{y_1}')) === 'frac(script(x,^2),script(y,_1))',
     'and scripts fit inside a fraction');
  ok(kinds(P('\\sqrt[3]{x}')) === 'sqrt(x,idx=3)', '\\sqrt takes an optional index');
  ok(kinds(P('\\sqrt x')) === 'sqrt(x)', 'and an unbraced argument');
  ok(kinds(P("x''")) === 'script(x,^′′)', 'primes are superscripts');
  ok(kinds(P('\\left(\\frac{a}{b}\\right)')) === 'fence((frac(a,b)))', '\\left…\\right fences');
  ok(kinds(P('\\left. x \\right|')) === 'fence(x|)', '. is the null delimiter');
  ok(kinds(P('\\sum_{i=1}^{n} i')) === 'row(script(op:∑,^n,_row(i = 1)) i)',
     'a big operator carries its bounds');
  ok(kinds(P('\\alpha\\Omega')) === 'row(α Ω)', 'Greek resolves to characters');
  ok(kinds(P('\\text{if } x')) === 'row(text(if ) x)', '\\text keeps its spaces');
  ok(kinds(P('\\begin{pmatrix}a&b\\\\c&d\\end{pmatrix}')) === 'mat[a|b ; c|d]',
     'a matrix splits on & and \\\\');
  ok(kinds(P('\\begin{cases}x&x>0\\\\0&\\text{else}\\end{cases}')) === 'mat[x|row(x > 0) ; 0|text(else)]',
     'so does cases');
  ok(kinds(P('\\begin{matrix}a\\\\b\\\\\\end{matrix}')) === 'mat[a ; b]',
     'a trailing \\\\ does not add an empty row');
}

H('classes decide spacing');
{
  // `-x` is a negation and `a-x` a subtraction: the same character, different
  // space, and the rendering must show it
  const neg = R('-x').html, sub = R('a-x').html;
  const spacers = (h: string) => (h.match(/t-msp/g) ?? []).length;
  ok(spacers(neg) === 0, 'a leading minus is tight — it binds nothing');
  ok(spacers(sub) > 0, 'a minus between two terms is spaced as a binary operator');
  ok((R('a=b').html.match(/width:0\.278em/g) ?? []).length === 2,
     'a relation takes a thick space on both sides');
  ok(spacers(R('x^{a+b}').html) === 0,
     'and script styles get no inter-atom space at all');
}

// ────────────────────────────────────────────────── malformed input

H('malformed input degrades, and never throws');
{
  const NASTY = [
    '', ' ', '{', '}', '{{{{{{', '}}}}}}', '\\', '\\\\', '^', '_', '^^^', '_{',
    '\\frac', '\\frac{', '\\frac{a}', '\\frac{}{}', '\\sqrt', '\\sqrt[', '\\sqrt[3]',
    '\\left', '\\left(', '\\right)', '\\left(\\right', '\\left{x\\right}',
    '\\begin', '\\begin{}', '\\begin{foo}x\\end{foo}', '\\begin{pmatrix}',
    '\\end{pmatrix}', '\\unknowncommand', '\\newcommand{\\x}{y}',
    'x^^2', 'x^2^3', 'x_1_2', '\\text', '\\text{', '&', '\\\\&\\\\',
    '\\hat', '\\mathbb', '\\displaystyle', '\\limits', '\\big',
    '\u0000\u0001', '💥', 'x'.repeat(500),
    '\\frac{'.repeat(60) + 'x', '{'.repeat(200), '\\sqrt{'.repeat(100),
  ];
  let threw = '', empty = '', unsafe = '';
  for (const src of NASTY) {
    let html = '';
    try { html = R(src).html; } catch (e) { threw ||= `${JSON.stringify(src)} → ${(e as Error).message}`; continue; }
    if (!html) empty ||= JSON.stringify(src);
    if (/<script|onerror=|javascript:/i.test(html)) unsafe ||= JSON.stringify(src);
    try { R(src, true); } catch (e) { threw ||= `display ${JSON.stringify(src)} → ${(e as Error).message}`; }
  }
  ok(!threw, `no malformed formula throws${threw ? ` — ${threw}` : ''}`);
  ok(!empty, `and none renders to nothing${empty ? ` — ${empty}` : ''}`);
  ok(!unsafe, `and none produces active markup${unsafe ? ` — ${unsafe}` : ''}`);
  ok(mathErrors('\\unknowncommand').length > 0, 'an unknown command is REPORTED, not swallowed');
  ok(mathErrors('\\frac{a}{b}').length === 0, 'and a good formula reports nothing');
}

H('an error is local — the rest of the formula still sets');
{
  const r = R('a + \\nosuchthing + b');
  const text = textOf(r.html);
  ok(text.includes('a') && text.includes('b'),
     'the atoms either side of an unknown command survive it');
  ok(r.html.includes('t-merr') && r.html.includes('nosuchthing'),
     'and the offending source is shown in place');
  ok(kinds(P('\\begin{foo}\\frac{a}{b}\\end{foo}c')).endsWith('c)'),
     'an unsupported environment is skipped to its \\end, not to the end of the formula');
}

H('the caps hold');
{
  const deep = '\\frac{'.repeat(400) + 'x' + '}{y}'.repeat(400);
  const t0 = Date.now();
  const r = R(deep);
  ok(Date.now() - t0 < 2000, `400-deep nesting renders in bounded time (${Date.now() - t0}ms)`);
  ok(r.errors.some(e => /deep|large/.test(e)), 'and says why it stopped');
  const huge = 'x+'.repeat(20000);
  const t1 = Date.now();
  const big = R(huge);
  ok(Date.now() - t1 < 2000, `a 40,000-character formula is bounded too (${Date.now() - t1}ms)`);
  ok(big.html.length < 3e6, 'and its output is bounded');
  ok(MAX_NODES < 1e5, 'the node budget is a real cap');
}

H('nothing from the source escapes as markup');
{
  const r = R('\\text{<script>alert(1)</script> & "quoted"}');
  ok(!r.html.includes('<script'), 'a script tag in the source is escaped');
  ok(r.html.includes('&lt;script&gt;'), 'and rendered as visible text');
  ok(!/&(?!amp;|lt;|gt;|quot;)/.test(r.html), 'every ampersand is escaped');
  const atom = inlineMathHtml('a<b & c"d');
  ok(!/data-tex="[^"]*"[^ >]/.test(atom), 'the source survives into data-tex without breaking the attribute');
  ok(atom.includes('&lt;') && atom.includes('&amp;') && atom.includes('&quot;'),
     'data-tex escapes <, & and "');
}

// ─────────────────────────────────────────────────────────── determinism

H('the same source always produces the same output');
{
  const CORPUS = [
    'x^2 + y^2 = z^2', '\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}',
    '\\sum_{i=1}^{n} \\frac{1}{i^2} = \\frac{\\pi^2}{6}',
    '\\int_0^\\infty e^{-x^2}\\,dx = \\frac{\\sqrt{\\pi}}{2}',
    '\\left(\\begin{matrix}a&b\\\\c&d\\end{matrix}\\right)',
    '\\lim_{x \\to 0} \\frac{\\sin x}{x} = 1',
    '\\hat{y} = \\mathbf{X}\\beta + \\varepsilon',
    '\\begin{cases} 1 & x > 0 \\\\ 0 & \\text{otherwise} \\end{cases}',
    '\\sqrt[3]{\\frac{a}{b}}', '\\mathbb{R}^n \\subseteq \\mathbb{C}^n',
  ];
  let drift = '';
  for (const src of CORPUS) {
    const a = R(src).html, b = R(src).html, c = R(src, true).html, d = R(src, true).html;
    if (a !== b || c !== d) drift ||= src;
  }
  ok(!drift, `ten real formulas render identically on every call${drift ? ` — ${drift}` : ''}`);

  // interleaved, because a renderer that carried state between calls (a shared
  // buffer, a counter in a generated id) would pass the loop above and fail here
  const inter = CORPUS.map(s => R(s).html);
  const shuffled = [...CORPUS].reverse().map(s => R(s).html).reverse();
  ok(JSON.stringify(inter) === JSON.stringify(shuffled),
     'and identically when rendered in a different order — there is no shared state');

  ok(!/NaN|undefined|Infinity/.test(CORPUS.map(s => R(s, true).html).join('')),
     'no NaN, undefined or Infinity reaches the markup');
  ok(num(0.30000000000000004) === '0.3' && num(-0) === '0',
     'numbers are rounded to a fixed form, and -0 is 0');
  ok(R('x').html === R('x').html && !/id="/.test(R('\\sqrt{x}').html),
     'nothing carries a generated id — ids would differ per call and per document');
}

// ─────────────────────────────────────────────────────────── the metrics

H('vertical layout');
{
  const box = (s: string, display = false) => layoutMath(P(s), { display });
  const plain = box('x');
  ok(plain.h > 0 && plain.d > 0, 'a plain letter has a height and a depth');

  const f1 = box('\\frac{a}{b}');
  ok(f1.h > plain.h && f1.d > plain.d, 'a fraction rises above and falls below the line');
  const f2 = box('\\frac{\\frac{a}{b}}{c}');
  ok(f2.h > f1.h, 'a fraction with a fraction in its numerator is taller');
  const f3 = box('\\frac{a}{\\frac{b}{c}}');
  ok(f3.d > f1.d, 'and one in the denominator falls further');
  ok(Math.abs(f1.h - f2.h) > 0.1, 'the difference is a real amount, not a rounding');

  const sup = box('x^2'), sub = box('x_2'), both = box('x_1^2');
  ok(sup.h > plain.h, 'a superscript raises the box');
  ok(sup.d === plain.d, 'and does not deepen it');
  ok(sub.d > plain.d && sub.h === plain.h, 'a subscript deepens the box only');
  ok(both.h >= sup.h && both.d >= sub.d, 'with both, the box covers both');
  ok(box('x^{y^{z}}').h > box('x^{y}').h, 'a script on a script raises the box further');

  const tall = box('\\left(\\frac{\\frac{a}{b}}{c}\\right)');
  const inner = box('\\frac{\\frac{a}{b}}{c}');
  ok(tall.h >= inner.h && tall.d >= inner.d, 'a fence is at least as tall as what it holds');
  ok(box('\\left(x\\right)').h < tall.h, 'and only as tall as it needs to be');

  ok(box('\\sqrt{x}').h > box('x').h, 'a radical adds its bar above the content');
  ok(box('\\sqrt{\\frac{a}{b}}').h > box('\\sqrt{x}').h, 'and grows with it');

  ok(box('\\sum_{i}^{n}', true).h > box('\\sum_{i}^{n}', false).h,
     'a summation with limits is taller in display style than in text style');
  ok(box('\\overline{x}').h > plain.h && box('\\underline{x}').d > plain.d,
     'over- and underlines go the right way round');
  ok(box('\\hat{x}').h > plain.h, 'an accent sits above its base');

  const m = box('\\begin{matrix}a&b\\\\c&d\\\\e&f\\end{matrix}');
  const m2 = box('\\begin{matrix}a&b\\end{matrix}');
  ok(m.h > m2.h && m.d > m2.d, 'a taller matrix grows in both directions');
  ok(Math.abs((m.h - 0.25) - (m.d + 0.25)) < 0.001,
     'and stays centred on the math axis (0.25em above the baseline)');
  const paren = box('\\begin{pmatrix}a&b\\\\c&d\\\\e&f\\end{pmatrix}');
  ok(paren.h > m.h && paren.d > m.d, 'and its parentheses stretch past it on both sides');
}

H('script sizes step down, and stop');
{
  const sizes = [...R('x^{y^{z^{w}}}').html.matchAll(/font-size:([0-9.]+)em/g)].map(m => m[1]);
  // each size is RELATIVE to its parent, so the chain reads 0.7 → 0.5/0.7 → 1
  ok(sizes.join(' ') === '0.7 0.7143 1',
     `the scripts step 100% → 70% → 50% and then STOP at scriptscript (${sizes.join(' ')})`);
  ok(!R('x^{y^{z^{w}}}').html.includes('font-size:0.5em'),
     'and nothing is ever set below half the base size');
}

// ────────────────────────────────────────────── the pagination invariant

H('every formula carries a text node');
{
  const EMPTIES = ['', '{}', '\\frac{}{}', '\\sqrt{}', '\\left(\\right)', '{ }',
                   '\\begin{matrix}&\\\\&\\end{matrix}', '\\hat{}', '\\text{}'];
  let missing = '';
  for (const src of EMPTIES) {
    const html = inlineMathHtml(src);
    // what the TreeWalker in paginate.ts would accept: a text node whose value
    // is not whitespace once trimmed
    const texts = html.replace(/<[^>]*>/g, '');
    if (!texts.trim().length) missing ||= JSON.stringify(src);
  }
  ok(!missing, `an empty formula still contains a measurable text node${missing ? ` — ${missing}` : ''}`);
  ok('\u200B'.trim().length === 1,
     'U+200B is NOT whitespace to String.trim — which is why it is the filler');
  ok(ZW === '\u200B', 'and it is the character the layout engine uses');
  ok(inlineMathHtml('').includes(ZW), 'the inline wrapper carries one unconditionally');
  ok(displayMathHtml('').includes(ZW), 'and so does the display wrapper');
}

H('the wrappers the core will embed');
{
  const a = inlineMathHtml('\\frac{a}{b}');
  ok(a.startsWith('<span class="t-math"') && a.endsWith('</span>'), 'an inline formula is one span');
  ok(a.includes('contenteditable="false"'),
     'and is contenteditable=false — the caret may not enter a box with no model position');
  ok(a.includes('data-tex="\\frac{a}{b}"'),
     'the SOURCE rides in data-tex — that is what fromDom reads back, never the glyphs');
  const d = displayMathHtml('x^2');
  ok(d.includes('t-math-display'), 'a display equation is marked as one');
  ok(!d.includes('contenteditable'),
     'and is not locked: a math BLOCK is addressed by its own id, not by an offset');
  ok(inlineMathHtml('\\nope').includes('t-math-bad'),
     'a formula with an error is flagged on the wrapper, so the chrome can show it');
  ok(!inlineMathHtml('x').includes('t-math-bad'), 'and a good one is not');

  // the round trip the core edit (3) depends on
  for (const src of ['x^2', '\\frac{a}{b}', 'a<b', '"q"', '\\text{&}', '']) {
    const m = /data-tex="([^"]*)"/.exec(inlineMathHtml(src));
    const back = (m?.[1] ?? '').replace(/&quot;/g, '"').replace(/&lt;/g, '<')
      .replace(/&gt;/g, '>').replace(/&amp;/g, '&');
    ok(back === src, `data-tex round-trips ${JSON.stringify(src)}`);
  }
}

H('the source is never the output');
{
  // the property that keeps the model honest: the rendering of a formula shares
  // no bytes with its own syntax
  const html = R('\\frac{a}{b}').html;
  ok(!html.includes('\\frac') && !html.includes('{a}'),
     'the rendering contains none of the TeX syntax — only what it means');
  ok(textOf(html) === 'ab', 'just the numerator and the denominator, in order');
  ok(textOf(R('\\alpha + \\beta').html) === 'α+β', 'and commands become their characters');
}

console.log(`\n${checks - failures}/${checks} checks passed`);
if (failures) process.exit(1);