okami.mom/strata

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

Niko1221Merge commit 'b8d604d28' into f-clean-1ab0fc432ed

main
4.5 KiB74 linesraw
1// Stale-preview OOM (QAGUI final pass): once the sidebar showed the file's CURRENT content, a file written on Windows (the agent's
2// `Add-Content` writes \r\n) reached the Markdown renderer with a \r at the end of every line. `.` does not match \r, so a list line passed the
3// list start test but not the item pattern, the list loop took no line and the outer loop never advanced: an endless loop pushing nodes, 150 MB to
4// 4.4 GB in 20 s on a 60 byte file. 0.2.10 never showed the changed content, so it never met this. The render runs in a child process with a
5// memory cap and a time limit, so a regression fails the test instead of hanging the suite.
6import assert from 'node:assert/strict';
7import test from 'node:test';
8import { spawnSync } from 'node:child_process';
9import { mkdtempSync, writeFileSync } from 'node:fs';
10import { tmpdir } from 'node:os';
11import { join, resolve } from 'node:path';
12import { build } from 'esbuild';
13
14const root = resolve(import.meta.dirname, '..');
15// the text arrives as JSON in a file named by argv[2], so no shell or platform touches its line ends (and a long text is not cut by
16// the command-line length limit: Windows allows about 32 KB, which the 1500-section case below passes)
17const entry = [
18  "import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server';",
19  "import { Markdown } from './src/components/Markdown.tsx'; import { splitPreview } from './src/lib/preview-render.ts';",
20  "import { readFileSync } from 'node:fs';",
21  "const text = JSON.parse(readFileSync(process.argv[2], 'utf8'));",
22  'console.log(JSON.stringify({ html: renderToStaticMarkup(React.createElement(Markdown, { text })), slices: splitPreview(text).map(s => s.text) }));',
23].join('\n');
24const bundle = await build({
25  stdin: { contents: entry, resolveDir: root, loader: 'ts' },
26  bundle: true, write: false, platform: 'node', format: 'cjs', jsx: 'automatic', external: ['katex', 'katex/dist/katex.min.css'],
27  plugins: [{ name: 'stub', setup(b) {
28    b.onResolve({ filter: /^\.\/ui$/ }, () => ({ path: 'ui', namespace: 'test-stub' }));
29    b.onLoad({ filter: /.*/, namespace: 'test-stub' }, () => ({ contents: 'export const IconBtn = () => null; export const Btn = () => null; export const Icon = () => null;' }));
30  } }],
31});
32const dir = mkdtempSync(join(tmpdir(), 'strata-crlf-'));
33const file = join(dir, 'render.cjs');
34writeFileSync(file, bundle.outputFiles[0].text);
35let renders = 0;
36function render(text) {
37  const input = join(dir, `text-${renders++}.json`);
38  writeFileSync(input, JSON.stringify(text));
39  const run = spawnSync(process.execPath, ['--max-old-space-size=200', file, input], { cwd: root, encoding: 'utf8', timeout: 20000, maxBuffer: 20 * 1024 * 1024 });
40  return { ok: run.status === 0 && !run.error, out: run.stdout, err: (run.stderr || '') + (run.error?.message ?? ''), status: run.status, signal: run.signal };
41}
42
43test('QAGUI case: the file with a CRLF bullet renders (it used to run the page out of memory)', () => {
44  const r = render('# Version Two\r\n\r\n- the file was rewritten on disk\r\n- appended\r\n');
45  assert.ok(r.ok, `the render did not finish: status ${r.status} signal ${r.signal} ${r.err.slice(0, 200)}`);
46  const { html } = JSON.parse(r.out);
47  assert.equal((html.match(/<li>/g) ?? []).length, 2);
48  assert.ok(html.includes('appended') && html.includes('Version Two'));
49  assert.ok(!html.includes('\r'));
50});
51
52test('CRLF, CR and LF give the same page, for lists, ordered lists, tables, quotes, code and paragraphs', () => {
53  const lf = '# T\n\n- a\n- b\n\n1. x\n2. y\n\n| h | i |\n| --- | --- |\n| 1 | 2 |\n\n> quote\n\n```js\nconst a = 1;\n```\n\nline one\nline two\n';
54  const want = JSON.parse(render(lf).out).html;
55  for (const sep of ['\r\n', '\r']) {
56    const r = render(lf.replace(/\n/g, sep));
57    assert.ok(r.ok, `sep ${JSON.stringify(sep)}: ${r.err.slice(0, 200)}`);
58    assert.equal(JSON.parse(r.out).html, want, 'sep ' + JSON.stringify(sep));
59  }
60});
61
62test('the preview slices are cut the same on CRLF text, and carry no \\r', () => {
63  const lf = Array.from({ length: 1500 }, (_, i) => `# S${i}\n\n- item ${i}\n- another ${i}\n`).join('\n');
64  const a = JSON.parse(render(lf).out).slices, b = JSON.parse(render(lf.replace(/\n/g, '\r\n')).out).slices;
65  assert.ok(a.length > 3);
66  assert.deepEqual(b, a);
67});
68
69test('a list line the item pattern rejects is shown and passed, never an endless loop', () => {
70  for (const text of ['-  \n', '- x\n', '- a
b\n', '1. a\u0085\n', '-    \n- ok\n', '- \n', '1. \n']) {
71    const r = render(text);
72    assert.ok(r.ok, JSON.stringify(text) + ' ' + r.err.slice(0, 120));
73  }
74});