okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
22a3d7068
main
1// #5180: the Markdown preview in the sidebar is keyed by the text, drawn in bounded slices, and does no work for an unchanged file 2// while unrelated store updates (streamed tokens) re-render the chat. Pure text work plus server rendering; the browser run is in the report. 3import assert from 'node:assert/strict' ; 4import test from 'node:test' ; 5import { readFileSync } from 'node:fs' ; 6import { resolve } from 'node:path' ; 7import { createRequire } from 'node:module' ; 8import { build } from 'esbuild' ; 9 10const root = resolve ( import . meta. dirname , '..' ); 11const bundle = await build ({ 12stdin :{ contents :`import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import { Markdown } from './src/components/Markdown.tsx'; export * from './src/lib/preview-render.ts'; export const html = text => renderToStaticMarkup(React.createElement(Markdown, { text }));` , resolveDir :root , loader :'ts' }, 13bundle :true , write :false , platform :'node' , format :'cjs' , jsx :'automatic' , external :[ 'katex' , 'katex/dist/katex.min.css' ], 14plugins :[{ name :'stub' , setup ( b ) { 15b . onResolve ({ filter :/ ^\.\/ui$ / }, () => ({ path :'ui' , namespace :'test-stub' })); 16b . onLoad ({ filter :/ . */ , namespace :'test-stub' }, () => ({ contents :'export const IconBtn = () => null; export const Btn = () => null; export const Icon = () => null;' })); 17} }], 18}); 19const mod = { exports :{} }; 20new Function ( 'require' , 'module' , 'exports' , bundle . outputFiles [ 0 ]. text )( createRequire ( import . meta. url ), mod , mod . exports ); 21const { html, textRevision, splitPreview, previewSlices, previewStats, previewTier, slicesAfter, slicesAtStart, 22PREVIEW_SLICE_CHARS , PREVIEW_FIRST_SLICES , PREVIEW_BLOCK_LIMIT , PREVIEW_RENDER_LIMIT } = mod . exports ; 23 24/** A long document the way the reports describe it: headings, paragraphs, lists, tables and fenced code with blank lines in them. */ 25function longDoc ( sections ) { 26const out = []; 27for ( let n = 0 ; n < sections ; n ++ ) { 28out . push ( `# Section ${ n } ` , '' , `Paragraph ${ n } with **bold**, *italic*, \`code\` and a [link](https://example.com/ ${ n } ).` , 'It continues on a second line.' , '' ); 29out . push ( '- first item' , '- second item' , '' , '1. step one' , '2. step two' , '' ); 30out . push ( '| Name | Value |' , '| --- | --- |' , ...Array . from ({ length :6 }, ( _ , r ) => `| row ${ r } | value ${ n } - ${ r } |` ), '' ); 31out . push ( '```js' , `const a ${ n } = 1;` , '' , '' , `console.log(a ${ n } );` , '```' , '' ); 32out . push ( '> a quote' , '' ); 33} 34return out . join ( '\n' ); 35} 36 37test ( 'the revision key follows the text and nothing else' , () => { 38assert . equal ( textRevision ( 'abc' ), textRevision ( 'abc' )); 39assert . notEqual ( textRevision ( 'abc' ), textRevision ( 'abd' )); 40assert . notEqual ( textRevision ( 'abc' ), textRevision ( 'abc ' )); 41assert . notEqual ( textRevision ( '' ), textRevision ( ' ' )); 42assert . match ( textRevision ( 'x' ), / ^ [ 0 - 9a - z ] + - [ 0 - 9a - z ] + $ / ); 43}); 44 45test ( 'slices drawn one after the other are the same elements as the whole file' , () => { 46const docs = [ 47longDoc ( 40 ), 48'one\n\ntwo\n\n\n\nthree' , 49'```\nfenced\n\nwith a blank line\n\n```\n\nafter' , 50'$$\nx = 1\n\ny = 2\n$$\n\ntext' , 51'\\[\na\n\nb\n\\]\n\ntext' , 52'| a | b |\n| --- | --- |\n| 1 | 2 |\n\n| c | d |\n| --- | --- |\n| 3 | 4 |' , 53'- a\n- b\n\n- c\n\n1. x\n\n2. y' , 54'$$ open and never closed\n\nnext paragraph\n\nmore' , 55'' , 56'\n\n\n' , 57]; 58for ( const doc of docs ) 59for ( const target of [ 1 , 40 , 500 , PREVIEW_SLICE_CHARS ]) { 60const whole = html ( doc ); 61const sliced = splitPreview ( doc , target ). map ( s => html ( s . text )). join ( '' ); 62assert . equal ( sliced , whole , `target ${ target } , doc starting ${ JSON . stringify ( doc . slice ( 0 , 30 ))} ` ); 63} 64}); 65 66test ( 'a slice never ends inside a fenced block or a display-math block' , () => { 67const doc = [ 'intro' , '' , '```' , 'a' , '' , 'b' , '```' , '' , '$$' , 'x' , '' , 'y' , '$$' , '' , 'outro' ]. join ( '\n' ); 68const slices = splitPreview ( doc , 1 ); 69for ( const s of slices ) { 70const fences = ( s . text . match ( / ^\s * ``` / gm ) ?? []). length ; 71assert . equal ( fences % 2 , 0 , 'balanced fences in ' + JSON . stringify ( s . text )); 72const math = ( s . text . match ( / ^\s * \$\$ / gm ) ?? []). length ; 73assert . equal ( math % 2 , 0 , 'balanced math in ' + JSON . stringify ( s . text )); 74} 75assert . ok ( slices . length >= 3 ); 76}); 77 78test ( 'a long file is cut into slices of about the target size, each with its own key' , () => { 79const doc = longDoc ( 160 ); 80assert . ok ( doc . length > 60000 , 'a long file: ' + doc . length ); 81const slices = splitPreview ( doc ); 82assert . ok ( slices . length > 8 ); 83for ( const s of slices . slice ( 0 , - 1 )) 84assert . ok ( s . text . length < PREVIEW_SLICE_CHARS * 2 , 'bounded slice ' + s . text . length ); 85assert . equal ( new Set ( slices . map ( s => s . key )). size , slices . length ); 86}); 87 88test ( 'changing one place changes one slice key; the others keep theirs and are not rendered again' , () => { 89const a = longDoc ( 60 ); 90const b = a . replace ( 'Paragraph 3 with' , 'Paragraph 3 changed with' ); 91const ka = splitPreview ( a ). map ( s => s . key ), kb = splitPreview ( b ). map ( s => s . key ); 92assert . equal ( ka . length , kb . length ); 93const changed = ka . filter (( k , i ) => k !== kb [ i ]); 94assert . equal ( changed . length , 1 ); 95}); 96 97test ( 'render count: an unchanged file is cut once however many store updates arrive' , () => { 98const doc = longDoc ( 80 ); 99const before = previewStats . cuts ; 100let first ; 101for ( let tick = 0 ; tick < 500 ; tick ++ ) { // 500 streamed-token updates of the chat, each asking for the same file 102const slices = previewSlices ( doc ); 103first ??= slices ; 104assert . equal ( slices , first , 'the same array: nothing downstream sees a change' ); 105} 106assert . equal ( previewStats . cuts - before , 1 ); 107// the file changing is one more cut, and going back to the old text is still known 108const changed = doc + '\nmore' ; 109previewSlices ( changed ); previewSlices ( changed ); 110assert . equal ( previewStats . cuts - before , 2 ); 111previewSlices ( doc ); 112assert . equal ( previewStats . cuts - before , 2 ); 113}); 114 115test ( 'the cache keeps a few texts only' , () => { 116const before = previewStats . cuts ; 117for ( let i = 0 ; i < 20 ; i ++ ) previewSlices ( 'text number ' + i ); 118assert . equal ( previewStats . cuts - before , 20 ); 119previewSlices ( 'text number 0' ); // long gone from a cache of four 120assert . equal ( previewStats . cuts - before , 21 ); 121}); 122 123test ( 'a very large file does not produce unbounded work' , () => { 124const huge = longDoc ( 2000 ); 125assert . ok ( huge . length > PREVIEW_RENDER_LIMIT ); 126assert . equal ( previewTier ( huge ), 'plain' ); 127assert . equal ( previewTier ( longDoc ( 100 )), 'markdown' ); 128// one table or code block that cannot be cut is plain text, not parsed 129const bigTable = '| a | b |\n| --- | --- |\n' + Array . from ({ length :5000 }, ( _ , i ) => `| ${ i } | ${ 'x' . repeat ( 10 )} |` ). join ( '\n' ); 130assert . ok ( bigTable . length > PREVIEW_BLOCK_LIMIT ); 131const slices = splitPreview ( bigTable ); 132assert . equal ( slices . length , 1 ); 133assert . equal ( slices [ 0 ]. plain , true ); 134assert . equal ( splitPreview ( '| a | b |\n| --- | --- |\n| 1 | 2 |' )[ 0 ]. plain , false ); 135}); 136 137test ( 'the first paint and every step are bounded; a new text never takes back slices already shown' , () => { 138assert . equal ( slicesAtStart ( 0 , 100 ), PREVIEW_FIRST_SLICES ); 139assert . equal ( slicesAtStart ( 0 , 1 ), 1 ); 140assert . equal ( slicesAtStart ( 0 , 0 ), 0 ); 141assert . equal ( slicesAtStart ( 30 , 100 ), 30 ); 142assert . equal ( slicesAtStart ( 30 , 10 ), 10 ); 143// steps reach the end in a bounded number of steps and never pass it 144let shown = slicesAtStart ( 0 , 100 ), steps = 0 ; 145while ( shown < 100 ) { const next = slicesAfter ( shown , 100 ); assert . ok ( next > shown && next - shown <= 2 ); shown = next ; steps ++ ; } 146assert . equal ( steps , 49 ); 147assert . equal ( slicesAfter ( 100 , 100 ), 100 ); 148}); 149 150test ( 'the sidebar uses the bounded component and the component cancels superseded work' , () => { 151const panel = readFileSync ( resolve ( root , 'src/components/ArtifactPanel.tsx' ), 'utf8' ); 152assert . match ( panel , / <PreviewMarkdown text=\{text\}\/> / ); 153assert . doesNotMatch ( panel , / <Markdown text=\{text\}\/> / ); 154const view = readFileSync ( resolve ( root , 'src/components/PreviewMarkdown.tsx' ), 'utf8' ); 155assert . match ( view , / export const PreviewMarkdown = memo\( / ); 156assert . match ( view , / const Slice = memo\( / ); 157assert . match ( view , / return \(\) => clearTimeout\(timer\) / ); 158assert . match ( view , / key=\{s\.key\} / ); 159});