okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1631a9fde
main
1import './helpers/isolate-profile.mjs' ; // a test run never writes the owner's real Strata profile 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { readFileSync as read , mkdtempSync , mkdirSync , writeFileSync , rmSync } from 'node:fs' ; 5import { join } from 'node:path' ; 6import { tmpdir } from 'node:os' ; 7import { createControl } from '../server/control-runtime.mjs' ; 8import { createNativeControl } from '../server/native-control.mjs' ; 9 10const src = name => read ( new URL ( `../ ${ name } ` , import . meta. url ), 'utf8' ); 11 12/** A packaged host with one installed model and a fake Control whose `update` plays the events of `strata update --stop-model --output jsonl`. */ 13function fixture ({ updateScript} = {}) { 14const root = mkdtempSync ( join ( tmpdir (), 'strata-update-model-' )); 15const install = join ( root , 'install' ), data = join ( root , 'data' ); 16mkdirSync ( install , { recursive :true }); 17process . env . STRATA_INSTALL_DIR = install ; 18const config = join ( install , 'strata-iq2_xs.json' ); 19writeFileSync ( config , JSON . stringify ({ exe :'component:strata-engine' , tokenizer :join ( install , 'tok' ), args :[ '--max-context' , '32768' ], model_name :'Qwen' , port :8080 })); 20const calls = []; 21const control = { 22control :{ dir :join ( root , 'control' ) }, version :'0.1.45' , 23async installedModels () { return { install_dir :install , models :[{ id :'iq2_xs' , name :'Qwen' , source_config :config , component_status :'present' , context_tokens :32768 , backend :'cuda' }] }; }, 24async engine () { return { usable :true , backend :'cuda' , components_dir :'E:\\c' , previous :null , engine :{ version :'0.1.45' , platform :'windows-x86_64' , backend :'cuda' , exe :'e' , root :'r' } }; }, 25async saveProfile ( profile ) { calls . push ([ 'saveProfile' , profile ]); return {}; }, 26async start ( profile ) { calls . push ([ 'start' , profile ]); return { ok :true , state :'ready' }; }, 27async stop ( profile ) { calls . push ([ 'stop' , profile ]); return { ok :true }; }, 28async update ( options ) { 29calls . push ([ 'update' , options . engine , options . stopModel === true ]); 30const ids = native . state (). instances . map ( i => i . id ); 31const profile = 'app-1' ; 32options . onEvent ?.({ event :'models' , phase :'consent' , models :[{ profile, name :'Qwen' , port :8080 , kind :'profile' , state :'ready' }], message :'Qwen is running' }); 33if ( updateScript ) return updateScript ( options , { profile, ids, native}); 34options . onEvent ?.({ event :'progress' , phase :'download' , label :'engine 0.1.46' , percent :40 }); 35options . onEvent ?.({ event :'models' , phase :'stopping' , models :[{ profile, name :'Qwen' , port :8080 , kind :'profile' , state :'ready' }] }); 36options . onEvent ?.({ event :'models' , phase :'stopped' , models :[{ profile, name :'Qwen' , port :8080 , kind :'profile' , state :'stopped' }] }); 37options . onEvent ?.({ event :'models' , phase :'left-stopped' , models :[{ profile, name :'Qwen' , port :8080 , kind :'profile' , state :'stopped' }], start :'strata launch' }); 38return { ok :true , results :[{ component :'engine' , status :'updated' , installed_after :{ version :'0.1.46' } }] }; 39}, 40async rollbackEngine () { return { ok :true }; }, 41}; 42const native = createNativeControl ({ control, directory :data , spawnProcess :() => { throw new Error ( 'no process' ); }, fetcher :async () => Response . json ({}), requests :() => [] }); 43return { native, calls, control, cleanup :() => rmSync ( root , { recursive :true , force :true }) }; 44} 45const finish = async ( native , id ) => { for ( let i = 0 ; i < 400 ; i ++ ) { const job = native . state (). jobs . find ( j => j . id === id ); if ( job . state !== 'running' ) return job ; await new Promise ( r => setTimeout ( r , 10 )); } throw new Error ( 'job did not finish' ); }; 46async function loaded ( f ) { 47await f . native . refresh ( true ); 48const load = await finish ( f . native , f . native . operation ( 'load' , { model :'iq2_xs' }). id ); 49assert . equal ( load . state , 'done' , load . error ); 50assert . equal ( f . native . state (). instances . length , 1 ); 51} 52 53test ( 'Control is called with --stop-model only when the app asks for it' , async () => { 54const seen = []; 55const control = createControl ({ control :{ dir :'/c' , python :'/c/python' , cli :'/c/strata.py' }, env :{}, run :async args => { seen . push ( args ); return { code :0 , json :{ ok :true , results :[] } }; } }); 56await control . update ({ engine :true , control :false }); 57await control . update ({ engine :true , control :false , stopModel :true }); 58assert . ok ( ! seen [ 0 ]. includes ( '--stop-model' )); 59assert . ok ( seen [ 1 ]. includes ( '--stop-model' )); 60assert . deepEqual ( seen [ 1 ]. slice ( 0 , 3 ), [ 'update' , '--output' , 'jsonl' ], 'the host reads the events (jsonl)' ); 61assert . ok ( ! seen [ 1 ]. includes ( '--start-again' ), 'starting again is the person\'s answer in the dialog, not an automatic flag' ); 62}); 63 64test ( 'a loaded model is stopped for the update only with the person\'s yes, and is then offered a start with the same settings' , async () => { 65const f = fixture (); 66try { 67await loaded ( f ); 68const refused = await finish ( f . native , f . native . operation ( 'update-engine' , {}). id ); 69assert . equal ( refused . state , 'error' ); 70assert . match ( refused . error , / Unload native models / , 'no yes, no stop' ); 71assert . equal ( f . calls . filter ( c => c [ 0 ] === 'update' ). length , 0 ); 72 73const job = await finish ( f . native , f . native . operation ( 'update-engine' , { stopModel :true }). id ); 74assert . equal ( job . state , 'done' , job . error ); 75assert . deepEqual ( f . calls . find ( c => c [ 0 ] === 'update' ), [ 'update' , true , true ]); 76const state = f . native . state (); 77assert . equal ( state . instances . length , 0 , 'the stopped model has left the list' ); 78assert . equal ( state . stoppedForUpdate . updated , true ); 79assert . deepEqual ( state . stoppedForUpdate . models . map ( m => m . label ), [ state . stoppedForUpdate . models [ 0 ]. label ]); 80assert . equal ( job . step , 'stopped' ); 81assert . equal ( f . calls . filter ( c => c [ 0 ] === 'start' ). length , 1 , 'only the first load started it; nothing started it again by itself' ); 82 83const again = await finish ( f . native , f . native . operation ( 'restart-models' , {}). id ); 84assert . equal ( again . state , 'done' , again . error ); 85assert . match ( again . summary , / running again with the same settings / ); 86const starts = f . calls . filter ( c => c [ 0 ] === 'start' ); 87assert . equal ( starts . length , 2 ); 88assert . equal ( starts [ 1 ][ 1 ], starts [ 0 ][ 1 ], 'the same profile (its saved run settings) is started again' ); 89const after = f . native . state (); 90assert . equal ( after . instances . length , 1 ); 91assert . equal ( after . instances [ 0 ]. status , 'ready' ); 92assert . equal ( after . stoppedForUpdate , null ); 93} finally { await f . native . close (); f . cleanup (); } 94}); 95 96test ( 'a failed update leaves the old engine and still offers the model back, on the old engine' , async () => { 97const f = fixture ({ 98updateScript :async ( options , { profile}) => { 99options . onEvent ?.({ event :'models' , phase :'stopping' , models :[{ profile, name :'Qwen' , port :8080 , kind :'profile' , state :'ready' }] }); 100options . onEvent ?.({ event :'models' , phase :'stopped' , models :[{ profile, name :'Qwen' , port :8080 , kind :'profile' , state :'stopped' }] }); 101throw Object . assign ( new Error ( 'The new engine did not pass its test start' ), { exitCode :6 }); 102}, 103}); 104try { 105await loaded ( f ); 106const job = await finish ( f . native , f . native . operation ( 'update-engine' , { stopModel :true }). id ); 107assert . equal ( job . state , 'error' ); 108assert . match ( job . error , / didn't start correctly, so nothing was changed\. Your current version still works / , 'plain words, not the exit code' ); 109const state = f . native . state (); 110assert . equal ( state . instances . length , 0 ); 111assert . equal ( state . stoppedForUpdate . updated , false , 'the dialog says the update did not finish and offers the old engine' ); 112const again = await finish ( f . native , f . native . operation ( 'restart-models' , {}). id ); 113assert . equal ( again . state , 'done' , again . error ); 114assert . equal ( f . native . state (). instances [ 0 ]. status , 'ready' ); 115} finally { await f . native . close (); f . cleanup (); } 116}); 117 118test ( 'a failed update that never touched the model offers nothing' , async () => { 119const f = fixture ({ updateScript :async () => { throw new Error ( 'Could not reach the update server (timed out).' ); } }); 120try { 121await loaded ( f ); 122const job = await finish ( f . native , f . native . operation ( 'update-engine' , { stopModel :true }). id ); 123assert . equal ( job . state , 'error' ); 124assert . equal ( f . native . state (). instances . length , 1 , 'the model is still running' ); 125assert . equal ( f . native . state (). stoppedForUpdate , null ); 126} finally { await f . native . close (); f . cleanup (); } 127}); 128 129test ( 'a model the app did not start is never stopped: the update ends at the first event, before any download' , async () => { 130const f = fixture ({ 131updateScript :async ( options , { profile}) => { 132options . onEvent ?.({ event :'models' , phase :'consent' , models :[{ profile :'launch-qwen' , name :'Qwen (port 8081)' , port :8081 , kind :'launch' , state :'ready' }] }); 133if ( options . signal . aborted ) throw new Error ( 'killed' ); // as runCli does: an aborted signal ends the Control process 134await new Promise (( resolve , reject ) => options . signal . addEventListener ( 'abort' , () => reject ( new Error ( 'killed' )), { once :true })); 135}, 136}); 137try { 138await loaded ( f ); 139const stopsBefore = f . calls . filter ( c => c [ 0 ] === 'stop' ). length ; 140const job = await finish ( f . native , f . native . operation ( 'update-engine' , { stopModel :true }). id ); 141assert . equal ( job . state , 'error' ); 142assert . match ( job . error , / not started from this app is running, so nothing was changed / ); 143assert . equal ( f . native . state (). instances . length , 1 ); 144assert . equal ( f . native . state (). stoppedForUpdate , null ); 145assert . equal ( f . calls . filter ( c => c [ 0 ] === 'stop' ). length , stopsBefore , 'nothing was stopped by the update' ); 146} finally { await f . native . close (); f . cleanup (); } 147}); 148 149test ( '"Not now" forgets the offer; with nothing stopped there is nothing to start again' , async () => { 150const f = fixture (); 151try { 152await loaded ( f ); 153await finish ( f . native , f . native . operation ( 'update-engine' , { stopModel :true }). id ); 154assert . ok ( f . native . state (). stoppedForUpdate ); 155f . native . dismissRestart (); 156assert . equal ( f . native . state (). stoppedForUpdate , null ); 157const none = await finish ( f . native , f . native . operation ( 'restart-models' , {}). id ); 158assert . equal ( none . state , 'error' ); 159assert . match ( none . error , / no stopped model / ); 160} finally { await f . native . close (); f . cleanup (); } 161}); 162 163test ( 'with no model loaded the update behaves as before: no stop flag, no offer' , async () => { 164const f = fixture (); 165try { 166await f . native . refresh ( true ); 167const job = await finish ( f . native , f . native . operation ( 'update-engine' , {}). id ); 168assert . equal ( job . state , 'done' , job . error ); 169assert . deepEqual ( f . calls . find ( c => c [ 0 ] === 'update' ), [ 'update' , true , false ]); 170assert . equal ( f . native . state (). stoppedForUpdate , null ); 171const withFlag = await finish ( f . native , f . native . operation ( 'update-engine' , { stopModel :true }). id ); 172assert . deepEqual ( f . calls . filter ( c => c [ 0 ] === 'update' )[ 1 ], [ 'update' , true , false ], 'the flag is only sent when a model is loaded' ); 173assert . equal ( withFlag . state , 'done' ); 174} finally { await f . native . close (); f . cleanup (); } 175}); 176 177test ( 'the dialog: warns naming the model, progress in plain words, the offer, the old-engine offer, and only Settings > About and the Models screen mount it' , () => { 178const dialog = src ( 'src/components/EngineUpdateDialog.tsx' ); 179assert . match ( dialog , / args: \{ stopModel: true \} / ); 180assert . match ( dialog , / kind: 'restart-models' / ); 181assert . match ( dialog , / To update the engine, Strata has to stop the model that is loaded: \{0\}\. / , 'the warning names the model' ); 182for ( const words of [ 'Getting the new engine ready. {0} keeps working until it is ready.' , 'Stopping {0}. Answers in progress finish first.' , '{0} is stopped. Updating the engine…' , 183'The engine is updated. Start {0} again with the same settings?' , "The update didn't finish. Your old engine is unchanged and still works. Start {0} again on it?" ]) 184assert . ok ( dialog . includes ( words ), words ); 185assert . doesNotMatch ( dialog , / fetch\( | api\.get | XMLHttpRequest / , 'no network from the dialog' ); 186const code = dialog . replace ( / \/\* [ \s\S ] * ?\*\/ / g , '' ). replace ( / ^\s * \/\/. * $ / gm , '' ); 187assert . doesNotMatch ( code , / exit code | --stop-model | jsonl / i , 'no flags or codes in the words' ); 188assert . match ( src ( 'src/components/SettingsUpdates.tsx' ), / <EngineUpdateDialog open=\{confirm === 'update-engine' && withModel\} / ); 189assert . match ( src ( 'src/components/SettingsUpdates.tsx' ), / <Dialog open=\{!!confirm && !\(confirm === 'update-engine' && withModel\)\} / , 'with no model loaded the older dialog is the one shown' ); 190assert . equal (( src ( 'src/components/NativeModels.tsx' ). match ( / <EngineUpdateDialog / g ) ?? []). length , 1 ); 191assert . match ( src ( 'server/index.mjs' ), / \/api\/native\/engine-restart\/dismiss / ); 192}); 193 194test ( 'the notice for a newer engine stays an ordinary quiet card: no toast, no banner, no timer, no network' , () => { 195const card = src ( 'src/components/EngineUpdateCard.tsx' ); 196assert . doesNotMatch ( card , / toast | setInterval | setTimeout | fetch\( | Notification / ); 197assert . doesNotMatch ( src ( 'src/components/EngineUpdateDialog.tsx' ), / setInterval | setTimeout | Notification | toast / ); 198}); 199 200test ( 'offline, the update says nothing by itself: the dialog opens only from a button and shows only what the host reports' , () => { 201const settings = src ( 'src/components/SettingsUpdates.tsx' ); 202assert . match ( settings , / onClick=\{\(\) => setConfirm\(name === 'Strata' \? 'update-strata' : 'update-engine'\)\} / , 'the button opens the confirmation; nothing opens it on a timer or on a failed check' ); 203}); 204 205test ( 'all nine languages carry the dialog\'s words with the same slots' , () => { 206const langs = [ 'en' , 'zh' , 'ja' , 'de' , 'es' , 'ru' , 'pt' , 'ko' , 'fr' ]; 207const catalogs = Object . fromEntries ( langs . map ( l => [ l , JSON . parse ( src ( `src/locales/upd- ${ l } .json` ))])); 208const keys = Object . keys ( catalogs . en ); 209assert . equal ( keys . length , 17 ); 210for ( const l of langs ) { 211assert . deepEqual ( Object . keys ( catalogs [ l ]), keys , l ); 212for ( const k of keys ) { 213assert . ok ( catalogs [ l ][ k ]. trim (), ` ${ l } ${ k } ` ); 214assert . deepEqual ([ ...catalogs [ l ][ k ]. matchAll ( / \{\d + \} / g )]. map ( m => m [ 0 ]). sort (), [ ...catalogs . en [ k ]. matchAll ( / \{\d + \} / g )]. map ( m => m [ 0 ]). sort (), ` ${ l } ${ k } slots` ); 215if ( l !== 'en' ) assert . notEqual ( catalogs [ l ][ k ], catalogs . en [ k ], ` ${ l } ${ k } is translated` ); 216} 217} 218// English is in the main catalog (src/lib/i18n.ts); each other language is one file (src/lib/catalog-<lang>.ts) that the app fetches when the language is used. 219const i18n = src ( 'src/lib/i18n.ts' ); 220assert . match ( i18n , / \.\.\.upd_en\b / ); 221for ( const l of langs . filter ( x => x !== 'en' )) assert . match ( src ( `src/lib/catalog- ${ l } .ts` ), new RegExp ( `from '\\.\\./locales/upd- ${ l } \\.json'` )); 222});