okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b0fc432ed
main
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 ({ 25stdin :{ contents :entry , resolveDir :root , loader :'ts' }, 26bundle :true , write :false , platform :'node' , format :'cjs' , jsx :'automatic' , external :[ 'katex' , 'katex/dist/katex.min.css' ], 27plugins :[{ name :'stub' , setup ( b ) { 28b . onResolve ({ filter :/ ^\.\/ui$ / }, () => ({ path :'ui' , namespace :'test-stub' })); 29b . 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 ) { 37const input = join ( dir , `text- ${ renders ++ } .json` ); 38writeFileSync ( input , JSON . stringify ( text )); 39const run = spawnSync ( process . execPath , [ '--max-old-space-size=200' , file , input ], { cwd :root , encoding :'utf8' , timeout :20000 , maxBuffer :20 * 1024 * 1024 }); 40return { 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)' , () => { 44const r = render ( '# Version Two\r\n\r\n- the file was rewritten on disk\r\n- appended\r\n' ); 45assert . ok ( r . ok , `the render did not finish: status ${ r . status } signal ${ r . signal } ${ r . err . slice ( 0 , 200 )} ` ); 46const { html} = JSON . parse ( r . out ); 47assert . equal (( html . match ( / <li> / g ) ?? []). length , 2 ); 48assert . ok ( html . includes ( 'appended' ) && html . includes ( 'Version Two' )); 49assert . ok ( ! html . includes ( '\r' )); 50}); 51 52test ( 'CRLF, CR and LF give the same page, for lists, ordered lists, tables, quotes, code and paragraphs' , () => { 53const 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' ; 54const want = JSON . parse ( render ( lf ). out ). html ; 55for ( const sep of [ '\r\n' , '\r' ]) { 56const r = render ( lf . replace ( / \n / g , sep )); 57assert . ok ( r . ok , `sep ${ JSON . stringify ( sep )} : ${ r . err . slice ( 0 , 200 )} ` ); 58assert . 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' , () => { 63const lf = Array . from ({ length :1500 }, ( _ , i ) => `# S ${ i } \n\n- item ${ i } \n- another ${ i } \n` ). join ( '\n' ); 64const a = JSON . parse ( render ( lf ). out ). slices , b = JSON . parse ( render ( lf . replace ( / \n / g , '\r\n' )). out ). slices ; 65assert . ok ( a . length > 3 ); 66assert . deepEqual ( b , a ); 67}); 68 69test ( 'a list line the item pattern rejects is shown and passed, never an endless loop' , () => { 70for ( const text of [ '- \n' , '- x\n' , '- a b\n' , '1. a\u0085\n' , '- \n- ok\n' , '- \n' , '1. \n' ]) { 71const r = render ( text ); 72assert . ok ( r . ok , JSON . stringify ( text ) + ' ' + r . err . slice ( 0 , 120 )); 73} 74});