okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-021-iE' into f-021-int3616148bb

main
3.8 KiB74 linesraw
1// #3268: LaTeX in answers is found (and drawn by KaTeX), money and code are not mistaken for it.
2import test from 'node:test';
3import assert from 'node:assert/strict';
4import { readFile } from 'node:fs/promises';
5import { transform } from 'esbuild';
6// KaTeX is a dependency of the app (npm install / npm ci); a checkout that shares another folder's node_modules may not have it yet
7const katex = await import('katex').then(m => m.default, () => null);
8
9async function loadTs(path) {
10  const { code } = await transform(await readFile(new URL(path, import.meta.url), 'utf8'), { loader: 'ts', format: 'esm' });
11  return import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`);
12}
13const { splitMath, displayBlock, hasMath } = await loadTs('../src/lib/math.ts');
14const maths = text => splitMath(text).filter(p => p.kind === 'math').map(p => [p.tex, p.display]);
15
16test('inline and display forms', () => {
17  assert.deepEqual(maths('a sphere of radius $r \\le R$ here'), [['r \\le R', false]]);
18  assert.deepEqual(maths('so \\(x^2\\) and \\[y^2\\]'), [['x^2', false], ['y^2', true]]);
19  assert.deepEqual(maths('then $$g = -k r$$ done'), [['g = -k r', true]]);
20  assert.deepEqual(splitMath('plain words').map(p => p.kind), ['text']);
21});
22
23test('money, escaped dollars and unclosed math stay text', () => {
24  assert.deepEqual(maths('it costs $5 and $10 in total'), []);
25  assert.deepEqual(maths('only $5 here'), []);
26  assert.deepEqual(maths('a \\$5 and a \\$7 fee'), []);
27  assert.deepEqual(maths('an open $$ x + y'), []);
28  assert.equal(splitMath('a \\$5 fee').map(p => p.text).join(''), 'a $5 fee');
29  assert.equal(hasMath('no math here'), false);
30});
31
32test('inline math does not cross a line, display math does', () => {
33  assert.deepEqual(maths('start $a\nb$ end'), []);
34  assert.deepEqual(maths('x $$a\nb$$ y'), [['a\nb', true]]);
35});
36
37test('display blocks: one line, several lines, bracket form, and unclosed ones (still streaming) stay text', () => {
38  assert.deepEqual(displayBlock(['$$ a = b $$'], 0), { tex: ' a = b ', used: 1 });
39  assert.deepEqual(displayBlock(['$$', '\\oint g = 1', '$$', 'after'], 0), { tex: '\n\\oint g = 1\n', used: 3 });
40  assert.deepEqual(displayBlock(['\\[', 'x', '\\]'], 0), { tex: '\nx\n', used: 3 });
41  assert.equal(displayBlock(['$$', 'x'], 0), null);
42  assert.equal(displayBlock(['$$a$$ and more'], 0), null);
43  assert.equal(displayBlock(['plain'], 0), null);
44});
45
46test('the formulas of the report are found in their paragraph and draw without an error', { skip: katex ? false : 'katex is not installed in node_modules' }, () => {
47  const answer = [
48    'Apply Gauss\'s Law for gravity to a sphere of radius $r \\le R$ concentric with the planet:',
49    '',
50    '$$\\oint \\vec{g}\\cdot d\\vec{A} = -4\\pi G\\, M_{\\text{enc}}(r)$$',
51    '',
52    'By spherical symmetry, $\\vec{g}$ is radial and constant on the Gaussian sphere, so:',
53    '',
54    '$$g\\,(4\\pi r^2) = -4\\pi G\\left(\\tfrac{4}{3}\\pi r^3\\rho\\right)$$',
55    '',
56    '$$\\boxed{g(r) = -\\frac{4\\pi G\\rho}{3}\\,r}$$',
57  ].join('\n').split('\n');
58  const found = [];
59  for (let i = 0; i < answer.length;) {
60    const block = displayBlock(answer, i);
61    if (block) { found.push([block.tex, true]); i += block.used; continue; }
62    for (const [tex, display] of maths(answer[i])) found.push([tex, display]);
63    i++;
64  }
65  assert.equal(found.length, 5);
66  for (const [tex, display] of found) {
67    const html = katex.renderToString(tex, { displayMode: display, throwOnError: false, strict: 'ignore', trust: false });
68    assert.ok(html.includes('class="katex'), tex);
69    assert.ok(!html.includes('katex-error'), tex);
70  }
71  // trust is off: no link can come out of a model's formula
72  const bad = katex.renderToString('\\href{https://example.com}{x}', { throwOnError: false, strict: 'ignore', trust: false });
73  assert.ok(!bad.includes('<a '));
74});