okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e82c70698
main
1// The sidebar preview kept a file's FIRST content after the file changed on disk (QAGUI, 4d482e627). Three places had to change together: 2// openPreview dropped the freshly read file, the panel's loading effect had no content version, and the effect that re-adds a message's own 3// artifacts put the stored first copy back over the newer one. A fix of the first two alone ran the renderer out of memory: a ping-pong 4// between the message copy and the opened copy. These tests pin the rules (lib/preview-merge.ts) and that nothing can loop. 5import assert from 'node:assert/strict' ; 6import test from 'node:test' ; 7import { readFileSync , writeFileSync , mkdtempSync , utimesSync } from 'node:fs' ; 8import { tmpdir } from 'node:os' ; 9import { join , resolve } from 'node:path' ; 10import { build } from 'esbuild' ; 11import { createRequire } from 'node:module' ; 12 13const root = resolve ( import . meta. dirname , '..' ); 14const bundle = await build ({ stdin :{ contents :`export * from './src/lib/preview-merge.ts'; export { textRevision } from './src/lib/preview-render.ts';` , resolveDir :root , loader :'ts' }, bundle :true , write :false , platform :'node' , format :'cjs' }); 15const mod = { exports :{} }; 16new Function ( 'require' , 'module' , 'exports' , bundle . outputFiles [ 0 ]. text )( createRequire ( import . meta. url ), mod , mod . exports ); 17const { previewVersion, openItem, syncArtifacts, messageArtifacts} = mod . exports ; 18const { previewFile} = await import ( '../server/chat-files.mjs' ); 19 20const md = ( content , at , extra = {}) => ({ id :'file:/w/notes.md' , kind :'markdown' , title :'notes.md' , content, at, ...extra }); 21const T = ( n ) => new Date ( Date . UTC ( 2026 , 10 , 4 , 10 , 0 , n )). toISOString (); 22 23test ( 'the version follows the content: the text, else the host version, else the address' , () => { 24assert . equal ( previewVersion ( md ( '# One' )), previewVersion ( md ( '# One' ))); 25assert . notEqual ( previewVersion ( md ( '# One' )), previewVersion ( md ( '# Two' ))); 26assert . notEqual ( previewVersion ({ url :'/a.png' , version :'1:5' }), previewVersion ({ url :'/a.png' , version :'2:5' })); 27assert . equal ( previewVersion ({ url :'/a.png' , version :'1:5' }), previewVersion ({ url :'/a.png' , version :'1:5' })); 28assert . notEqual ( previewVersion ({ url :'/a.png' }), previewVersion ({ url :'/b.png' })); 29assert . notEqual ( previewVersion ({ content :'' }), previewVersion ({ url :'' })); 30}); 31 32test ( 'opening a file that changed replaces its tab in place (it used to be dropped)' , () => { 33const old = [ md ( '# Version One' , 1000 ), { id :'other' , kind :'code' , title :'x.ts' , content :'x' }]; 34const next = openItem ( old , md ( '# Version Two' , 2000 )); 35assert . equal ( next . length , 2 ); 36assert . equal ( next [ 0 ]. content , '# Version Two' ); 37assert . equal ( next [ 1 ]. id , 'other' , 'its place in the strip is kept' ); 38assert . equal ( old [ 0 ]. content , '# Version One' , 'the old array is not mutated' ); 39}); 40 41test ( 'opening a file that did not change, or a new file, behaves as before; the same array comes back when nothing changed' , () => { 42const old = [ md ( '# Version One' , 1000 )]; 43assert . equal ( openItem ( old , md ( '# Version One' , 5000 )), old ); 44const added = openItem ( old , { id :'file:/w/b.md' , kind :'markdown' , title :'b.md' , content :'b' }); 45assert . equal ( added . length , 2 ); 46assert . equal ( added [ 0 ], old [ 0 ]); 47}); 48 49test ( 'the QAGUI case: the message still carries Version One, the file was opened as Version Two, and Version Two stays' , () => { 50const messages = [{ at :T ( 0 ), artifacts :[ md ( '# Version One' )] }]; 51let tabs = syncArtifacts ([], messageArtifacts ( messages ), []); // the first message arrives: the tab shows Version One 52assert . equal ( tabs [ 0 ]. content , '# Version One' ); 53tabs = openItem ( tabs , md ( '# Version Two' , Date . parse ( T ( 60 )))); // "Open in sidebar" reads the file again 54assert . equal ( tabs [ 0 ]. content , '# Version Two' ); 55for ( let i = 0 ; i < 5 ; i ++ ) { // a later message count change runs the sync again 56const again = syncArtifacts ( tabs , messageArtifacts ( messages ), []); 57assert . equal ( again , tabs , 'the same array: nothing to update' ); 58assert . equal ( again [ 0 ]. content , '# Version Two' , 'the message copy did not come back' ); 59} 60}); 61 62test ( 'a message newer than the copy on show, with other content, does replace it; an older or equal one never does' , () => { 63const shown = [ md ( '# Version Two' , Date . parse ( T ( 60 )))]; 64const older = syncArtifacts ( shown , messageArtifacts ([{ at :T ( 30 ), artifacts :[ md ( '# Version One' )] }]), []); 65assert . equal ( older , shown ); 66const equal = syncArtifacts ( shown , messageArtifacts ([{ at :T ( 60 ), artifacts :[ md ( '# Version Three' )] }]), []); 67assert . equal ( equal , shown ); 68const newer = syncArtifacts ( shown , messageArtifacts ([{ at :T ( 90 ), artifacts :[ md ( '# Version Three' )] }]), []); 69assert . equal ( newer [ 0 ]. content , '# Version Three' ); 70assert . equal ( shown [ 0 ]. content , '# Version Two' ); 71// the same content in a newer message is not a change 72assert . equal ( syncArtifacts ( shown , messageArtifacts ([{ at :T ( 90 ), artifacts :[ md ( '# Version Two' )] }]), []), shown ); 73}); 74 75test ( 'a missing tab is added, a closed one is removed and not brought back' , () => { 76const items = messageArtifacts ([{ at :T ( 0 ), artifacts :[ md ( 'a' ), { id :'b' , kind :'code' , title :'b' , content :'b' }] }]); 77const added = syncArtifacts ([], items , []); 78assert . deepEqual ( added . map ( a => a . id ), [ 'file:/w/notes.md' , 'b' ]); 79const closed = syncArtifacts ( added , items , [ 'b' ]); 80assert . deepEqual ( closed . map ( a => a . id ), [ 'file:/w/notes.md' ]); 81assert . deepEqual ( syncArtifacts ( closed , items , [ 'b' ]). map ( a => a . id ), [ 'file:/w/notes.md' ], 'and it stays closed' ); 82assert . equal ( syncArtifacts ( closed , items , [ 'b' ]), closed ); 83// a tab opened by hand that no message knows stays 84const mine = [{ id :'mine' , kind :'code' , title :'m' , content :'m' }]; 85assert . equal ( syncArtifacts ( mine , [], []), mine ); 86}); 87 88test ( 'message artifacts: stamped with their message time, the last of an id wins, a bad time is left unstamped' , () => { 89const list = messageArtifacts ([{ at :T ( 0 ), artifacts :[ md ( 'one' )] }, { at :T ( 30 ), artifacts :[ md ( 'two' )] }, { at :'not a time' , artifacts :[{ id :'z' , kind :'code' , title :'z' , content :'z' }] }, {}]); 90assert . equal ( list . length , 2 ); 91assert . equal ( list [ 0 ]. content , 'two' ); 92assert . equal ( list [ 0 ]. at , Date . parse ( T ( 30 ))); 93assert . equal ( list [ 1 ]. at , undefined ); 94assert . deepEqual ( messageArtifacts ( undefined ), []); 95}); 96 97test ( 'no loop: the open and the sync handed to each other 2000 times change the tabs only when the content changes' , () => { 98const messages = [{ at :T ( 0 ), artifacts :[ md ( '# Version One' )] }]; 99let tabs = [], changes = 0 , loads = 0 , lastKey = '' ; 100const set = next => { if ( next !== tabs ) changes ++ ; tabs = next ; }; 101// what the panel does: reload its text only when (id, version) of the shown tab changes 102const panel = () => { const cur = tabs [ 0 ]; const key = cur ?cur . id + '|' + previewVersion ( cur ) :'' ; if ( key !== lastKey ) { loads ++ ; lastKey = key ; } }; 103set ( syncArtifacts ( tabs , messageArtifacts ( messages ), [])); panel (); 104for ( let i = 0 ; i < 2000 ; i ++ ) { 105// the chat renders for every streamed token; the sync runs when the artifact count changes, the open when a run finishes 106set ( openItem ( tabs , md ( '# Version Two' , Date . parse ( T ( 60 ))))); 107set ( syncArtifacts ( tabs , messageArtifacts ( messages ), [])); 108set ( openItem ( tabs , md ( '# Version Two' , Date . parse ( T ( 60 + i ))))); 109panel (); 110} 111assert . equal ( changes , 2 , 'the first tab, then Version Two once' ); 112assert . equal ( loads , 2 , 'the panel loaded its text twice: Version One, then Version Two' ); 113}); 114 115test ( 'no loop: a file that changes every second is one load per change, not one per render' , () => { 116let tabs = [], loads = 0 , lastKey = '' ; 117for ( let s = 0 ; s < 300 ; s ++ ) { 118for ( let render = 0 ; render < 20 ; render ++ ) { // 20 renders per second of streamed tokens 119if ( render === 0 ) tabs = openItem ( tabs , md ( '# v' + s , 1000 + s )); 120const cur = tabs [ 0 ]; const key = cur . id + '|' + previewVersion ( cur ); 121if ( key !== lastKey ) { loads ++ ; lastKey = key ; } 122} 123} 124assert . equal ( loads , 300 ); 125}); 126 127test ( 'the host sends the file version: it moves when the file is rewritten' , () => { 128const dir = mkdtempSync ( join ( tmpdir (), 'strata-prev-' )); 129writeFileSync ( join ( dir , 'n.md' ), '# Version One\n' ); 130const a = previewFile ( dir , 'n.md' , 's' ); 131writeFileSync ( join ( dir , 'n.md' ), '# Version Two\n' ); 132utimesSync ( join ( dir , 'n.md' ), new Date ( Date . now () + 5000 ), new Date ( Date . now () + 5000 )); 133const b = previewFile ( dir , 'n.md' , 's' ); 134assert . equal ( typeof a . mtime , 'number' ); 135assert . notEqual ( a . mtime , b . mtime ); 136assert . equal ( b . text , '# Version Two\n' ); 137}); 138 139test ( 'the three places are changed together' , () => { 140const chat = readFileSync ( resolve ( root , 'src/screens/Chat.tsx' ), 'utf8' ); 141assert . match ( chat , / setPreviews\(old => openItem\(old, item\)\) / ); 142assert . match ( chat , / setPreviews\(old => syncArtifacts\(old, items, session\?\.closedPreviews\)\) / ); 143assert . doesNotMatch ( chat , / old\.some\(a => a\.id === item\.id\) \? old / ); 144assert . doesNotMatch ( chat , / \[\.\.\.old\.filter\(a => !items\.some / ); 145const panel = readFileSync ( resolve ( root , 'src/components/ArtifactPanel.tsx' ), 'utf8' ); 146assert . match ( panel , / \}, \[current\?\.id, version, revision\]\); / ); 147assert . match ( panel , / const version = useMemo\(\(\) => current \? previewVersion\(current\) : '', \[current\]\) / ); 148// the reading view is reset by the file, not by its content 149assert . match ( panel , / setMode\('preview'\); [ \s\S ] * ?\}, \[current\?\.id\]\); / ); 150const deliver = readFileSync ( resolve ( root , 'src/lib/deliverables.ts' ), 'utf8' ); 151assert . match ( deliver , / version = a\.mtime !== undefined / ); 152});