okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
22a3d7068
main
1// #5079: after the desktop shell loads the page again because its renderer died, the page starts with the sidebar previews closed, once. 2// The shell side (which failures reload, the limit of two in two minutes, the address) is unit-tested in apps/desktop/src-tauri/src/recovery.rs. 3import assert from 'node:assert/strict' ; 4import test from 'node:test' ; 5import { readFileSync } from 'node:fs' ; 6import { resolve } from 'node:path' ; 7import { transform } from 'esbuild' ; 8 9const root = resolve ( import . meta. dirname , '..' ); 10const { code} = await transform ( readFileSync ( resolve ( root , 'src/lib/recovery.ts' ), 'utf8' ), { loader :'ts' , format :'esm' }); 11const lib = await import ( `data:text/javascript;base64, ${ Buffer . from ( code ). toString ( 'base64' )} ` ); 12 13function fakeWindow ( url ) { 14const u = new URL ( url ); 15const calls = []; 16globalThis . window = { 17location :{ pathname :u . pathname , search :u . search , hash :u . hash }, 18history :{ state :null , replaceState :( state , title , address ) => { calls . push ( address ); } }, 19}; 20return calls ; 21} 22 23test ( 'the recovery number is read from the query, and only a single digit counts' , () => { 24assert . equal ( lib . parseRecovery ( '?recovery=1' ), 1 ); 25assert . equal ( lib . parseRecovery ( '?x=1&recovery=2' ), 2 ); 26for ( const bad of [ '' , '?' , '?recovery=' , '?recovery=0' , '?recovery=10' , '?recovery=x' , '?recovery=-1' , '?recovery=1.5' , '?recoveryx=1' ]) 27assert . equal ( lib . parseRecovery ( bad ), 0 , bad ); 28}); 29 30test ( 'the mark is taken out of the query and nothing else is' , () => { 31assert . equal ( lib . withoutRecovery ( '?recovery=1' ), '' ); 32assert . equal ( lib . withoutRecovery ( '?recovery=1&a=b' ), '?a=b' ); 33assert . equal ( lib . withoutRecovery ( '?a=b' ), '?a=b' ); 34assert . equal ( lib . withoutRecovery ( '' ), '' ); 35}); 36 37test ( 'an ordinary start defers nothing and leaves the address alone' , () => { 38const calls = fakeWindow ( 'http://127.0.0.1:47831/#/chat/s_1' ); 39assert . equal ( lib . startRecovery (), 0 ); 40assert . deepEqual ( calls , []); 41assert . equal ( lib . previewsDeferred ( 's_1' ), false ); 42assert . equal ( lib . previewsDeferred (), false ); 43}); 44 45test ( 'a recovery start defers the previews of the chat that was open, removes the mark and keeps the screen' , () => { 46const calls = fakeWindow ( 'http://127.0.0.1:47831/?recovery=1#/chat/s_1' ); 47assert . equal ( lib . startRecovery (), 1 ); 48assert . deepEqual ( calls , [ '/#/chat/s_1' ], 'the address loses the mark, keeps the hash' ); 49assert . equal ( lib . previewsDeferred (), true ); 50assert . equal ( lib . previewsDeferred ( 's_1' ), true ); 51assert . equal ( lib . previewsDeferred ( 's_1' ), true , 'asked again for the same chat (the effects run more than once)' ); 52// another chat is an ordinary one, and the deferral is over for good 53assert . equal ( lib . previewsDeferred ( 's_2' ), false ); 54assert . equal ( lib . previewsDeferred ( 's_1' ), false ); 55}); 56 57test ( 'opening a preview ends the deferral; a second start in the same page is ordinary again' , () => { 58fakeWindow ( 'http://127.0.0.1:47831/?recovery=2' ); 59assert . equal ( lib . startRecovery (), 2 ); 60assert . equal ( lib . previewsDeferred ( 's_1' ), true ); 61lib . endPreviewDeferral (); 62assert . equal ( lib . previewsDeferred ( 's_1' ), false ); 63fakeWindow ( 'http://127.0.0.1:47831/' ); 64assert . equal ( lib . startRecovery (), 0 ); 65assert . equal ( lib . previewsDeferred ( 's_1' ), false ); 66}); 67 68test ( 'no address to read is an ordinary start, not an error' , () => { 69globalThis . window = undefined ; 70assert . equal ( lib . startRecovery (), 0 ); 71}); 72 73test ( 'the page reads the mark before the first render, and the chat honours it' , () => { 74const main = readFileSync ( resolve ( root , 'src/main.tsx' ), 'utf8' ); 75assert . ok ( main . indexOf ( 'startRecovery();' ) > 0 && main . indexOf ( 'startRecovery();' ) < main . indexOf ( 'createRoot(el).render' )); 76const chat = readFileSync ( resolve ( root , 'src/screens/Chat.tsx' ), 'utf8' ); 77assert . match ( chat , / setSelectedPreview\(session && previewsDeferred\(session\.id\) \|\| / ); 78assert . match ( chat , / function openPreview\(item: PreviewArtifact\) \{\s * endPreviewDeferral\(\); / ); 79assert . match ( chat , / function selectPreview\(id: string\) \{\s * endPreviewDeferral\(\); / ); 80}); 81 82test ( 'the shell hooks: Windows ProcessFailed, macOS web content process, the same recovery path, no new text' , () => { 83const main = readFileSync ( resolve ( root , '../desktop/src-tauri/src/main.rs' ), 'utf8' ); 84assert . match ( main , / fn hook_renderer_failures\(window: &tauri::WebviewWindow, port: u16\) / ); 85assert . match ( main , / add_ProcessFailed\(&handler, &mut token\) / ); 86assert . match ( main , / on_web_content_process_terminate\( / ); 87assert . match ( main , / recovery::plan\( / ); 88const cargo = readFileSync ( resolve ( root , '../desktop/src-tauri/Cargo.toml' ), 'utf8' ); 89assert . match ( cargo , / \[target\.'cfg\(windows\)'\.dependencies\] [ \s\S ] * webview2-com = "0\.39" / ); 90});