okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { readFileSync as read } from 'node:fs' ; 4import { build } from 'esbuild' ; 5import { listEngines , changeEngine } from '../server/engine-library.mjs' ; 6import { plainEngineError } from '../server/update-messages.mjs' ; 7import { createControl } from '../server/control-runtime.mjs' ; 8import { checkComponentUpdates } from '../server/updates.mjs' ; 9 10const src = name => read ( new URL ( `../ ${ name } ` , import . meta. url ), 'utf8' ); 11const json = name => JSON . parse ( src ( `src/locales/ ${ name } .json` )); 12const built = await build ({ stdin :{ contents :"export * from './src/lib/engine-list';" , resolveDir :process . cwd () }, bundle :true , write :false , format :'esm' , platform :'node' , loader :{ '.json' :'json' } }); 13const { versionLess, engineSize, engineMake, modelsNeedingNewer} = await import ( `data:text/javascript;base64, ${ Buffer . from ( built . outputFiles [ 0 ]. text ). toString ( 'base64' )} ` ); 14 15const listing = { ok :true , families :[{ family :'qwen' , title :'Qwen and other models' , engines :[{ version :'0.1.55' , folder :'a' , active :true }, { version :'0.1.54' , folder :'b' }] }], total_bytes :2_000_000_000 , serving :false }; 16 17test ( 'the list: no Control (a source checkout) says so; otherwise it is Control\'s list; a failure is one plain sentence with the raw reason behind it' , async () => { 18assert . deepEqual (( await listEngines ( null )). body , { available :false , families :[], total_bytes :0 }); 19const ok = await listEngines ({ engines :async () => listing }); 20assert . equal ( ok . status , 200 ); 21assert . equal ( ok . body . available , true ); 22assert . equal ( ok . body . families [ 0 ]. engines . length , 2 ); 23assert . equal ( ok . body . total_bytes , 2_000_000_000 ); 24const bad = await listEngines ({ engines :async () => { throw new Error ( 'exit 2: traceback' ); } }); 25assert . equal ( bad . status , 500 ); 26assert . equal ( bad . body . error , "The installed engines couldn't be read." ); 27assert . match ( bad . body . details , / traceback / ); 28}); 29 30test ( 'switch: asks Control for the chosen engine only, and answers at once with one next step while a model is loaded' , async () => { 31const calls = []; 32const control = { useEngine :async a => { calls . push ([ 'use' , a ]); return { status :'switched' , to :'0.1.54' }; }, removeEngine :async a => { calls . push ([ 'remove' , a ]); return { status :'deleted' }; } }; 33const busy = await changeEngine ( control , 'use' , { family :'qwen' , folder :'b' }, { modelsRunning :() => true }); 34assert . equal ( busy . status , 409 ); 35assert . match ( busy . body . error , / A model is running, so nothing was changed\. Unload the model in Models / ); 36assert . deepEqual ( calls , [], 'nothing was asked of Control' ); 37const ok = await changeEngine ( control , 'use' , { family :'qwen' , folder :'b' }, { modelsRunning :() => false }); 38assert . equal ( ok . status , 200 ); 39assert . deepEqual ( calls , [[ 'use' , { family :'qwen' , folder :'b' }]]); 40const deleted = await changeEngine ( control , 'remove' , { family :'qwen' , folder :'c' }, { modelsRunning :() => true }); 41assert . equal ( deleted . status , 200 , 'deleting an engine nothing uses is not stopped by a loaded model; Control refuses the one in use' ); 42assert . deepEqual ( calls . at ( - 1 ), [ 'remove' , { family :'qwen' , folder :'c' }]); 43}); 44 45test ( 'only a family and a plain folder name from the list are accepted: a path, a hidden folder or another family is refused before Control is asked' , async () => { 46const control = { useEngine :async () => assert . fail ( 'must not be called' ), removeEngine :async () => assert . fail ( 'must not be called' ) }; 47for ( const body of [{ family :'x' , folder :'a' }, { family :'deepseek' , folder :'a' }, { family :'qwen' , folder :'../a' }, { family :'qwen' , folder :'a/b' }, { family :'qwen' , folder :'a\\b' }, { family :'qwen' , folder :'.rejected' }, { family :'qwen' , folder :'--force' }, { family :'qwen' , folder :'-x' }, { family :'qwen' , folder :'a b' }, { family :'qwen' }, {}]) { 48for ( const action of [ 'use' , 'remove' ]) assert . equal (( await changeEngine ( control , action , body )). status , 400 , JSON . stringify ( body )); 49} 50assert . equal (( await changeEngine ({}, 'use' , { family :'qwen' , folder :'a' })). status , 501 ); 51}); 52 53test ( 'the engine in use cannot be deleted: Control\'s refusal reaches the person in plain words, the raw text behind Details' , async () => { 54const refusing = { removeEngine :async () => { throw Object . assign ( new Error ( 'This is the engine in use. Switch to another one first.' ), { exitCode :2 }); }, useEngine :async () => ({}) }; 55const out = await changeEngine ( refusing , 'remove' , { family :'qwen' , folder :'a' }); 56assert . equal ( out . status , 400 ); 57assert . equal ( out . body . error , 'This is the engine in use. Switch to another one first, then delete it.' ); 58assert . match ( out . body . details , / Switch to another one first / ); 59const serving = await changeEngine ({ useEngine :async () => { throw Object . assign ( new Error ( 'A model is running, so the engine was not switched.' ), { exitCode :7 }); } }, 'use' , { family :'qwen' , folder :'a' }); 60assert . equal ( serving . status , 409 ); 61assert . match ( serving . body . error , / ^A model is running, so nothing was changed / ); 62}); 63 64test ( 'plain engine answers: nothing says exit codes, folders or hashes outside the details' , () => { 65const cases = [ 66[{ message :'x' , exitCode :7 }, 'switch' , / A model is running / ], 67[{ message :'a running program is using it' , exitCode :2 }, 'delete' , / still using this engine / ], 68[{ message :'that engine is not installed (it may have been deleted already)' , exitCode :2 }, 'delete' , / already gone / ], 69[{ message :'engine 0.1.1 can no longer be used: sig' , exitCode :5 }, 'switch' , / can't run on this computer / ], 70[{ message :'something odd' }, 'switch' , / wasn't switched\. Nothing was changed / ], 71[{ message :'something odd' }, 'delete' , / wasn't deleted / ], 72]; 73for ( const [ error , action , expected ] of cases ) { 74const plain = plainEngineError ( error , action ); 75assert . match ( plain . message , expected ); 76assert . doesNotMatch ( plain . message , / exit | SHA | \.json | [ A - Z ] :\\ | \/home\/ / i ); 77} 78}); 79 80test ( 'createControl asks the same commands the Terminal runs, and none of them acquires' , async () => { 81const seen = []; 82const control = createControl ({ control :{ dir :'/c' , python :'/p' , cli :'/c/strata.py' }, env :{}, run :async ( args , options ) => { seen . push ([ args . join ( ' ' ), !! options . acquire ]); return { code :0 , json :{ ok :true , families :[] } }; } }); 83await control . engines (); 84await control . useEngine ({ family :'qwen' , folder :'0.1.54-linux-x86_64-cuda-cuda13' }); 85await control . removeEngine ({ family :'qwen' , folder :'0.1.50-linux-x86_64-cuda-cuda13' }); 86assert . deepEqual ( seen , [ 87[ 'components engines --output json' , false ], 88[ 'components use 0.1.54-linux-x86_64-cuda-cuda13 --family qwen --output json' , false ], 89[ 'components remove 0.1.50-linux-x86_64-cuda-cuda13 --family qwen --output json' , false ], 90]); 91}); 92 93test ( 'About: an engine already on this computer is offered as an update that switches to it, with nothing to download' , async () => { 94const control = { version :'0.1.55' , updateCheck :async () => ({ feed :{}, control :{ status :'current' , installed :'0.1.55' }, app :{ status :'current' }, 95engine :{ status :'available' , installed :{ version :'0.1.54' }, offered :{ version :'0.1.55' }, download_bytes :0 , installed_locally :'0.1.55-linux-x86_64-cuda-cuda13' } }) }; 96const result = await checkComponentUpdates ({ control, appVersion :'0.2.9' }); 97assert . equal ( result . engine . status , 'available' ); 98assert . equal ( result . engine . canInstall , true ); 99assert . match ( result . engine . message , / already on this computer. * nothing is downloaded / ); 100assert . equal ( result . engine . installedVersion , '0.1.54' ); 101}); 102 103test ( 'engine helpers: sizes, makes and version order' , () => { 104assert . equal ( engineSize ( 1_020_000_000 , 'en' ), '1.0 GB' ); 105assert . equal ( engineSize ( 640_000_000 , 'en' ), '640 MB' ); 106assert . equal ( engineSize ( 10 , 'en' ), '1 MB' ); 107assert . equal ( engineMake ( 'cuda' ), 'NVIDIA' ); 108assert . equal ( engineMake ( 'hip' ), 'AMD' ); 109assert . equal ( engineMake ( 'sycl' ), 'Intel' ); 110assert . equal ( versionLess ( '0.1.9' , '0.1.10' ), true ); 111assert . equal ( versionLess ( '0.1.49' , '0.1.49.1' ), true ); 112assert . equal ( versionLess ( '0.1.55' , '0.1.55' ), false ); 113assert . equal ( versionLess ( '0.1.56' , '0.1.55.1' ), false ); 114}); 115 116test ( 'a switch to an older engine warns about the installed models that need a newer one, and does not refuse' , () => { 117const native = { installed :{ models :[{ model :'gyro' , model_name :'Gyro' }, { model :'plain' , model_name :'Plain' }] }, 118catalog :{ families :[{ family :'qwen' , title :'Qwen' , sizes :[{ id :'gyro' , model :'gyro' , engine_min :'0.1.45.1' }, { id :'plain' , model :'plain' , engine_min :null }] }] } }; 119assert . deepEqual ( modelsNeedingNewer ( native , { family :'qwen' , version :'0.1.45' }), [ 'Gyro' ]); 120assert . deepEqual ( modelsNeedingNewer ( native , { family :'qwen' , version :'0.1.46' }), []); 121assert . deepEqual ( modelsNeedingNewer ( null , { family :'qwen' , version :'0.1.1' }), []); 122const page = src ( 'src/components/SettingsEngine.tsx' ); 123assert . match ( page , / disabled=\{pending \|\| running\} / , 'only a loaded model stops the button; the warning does not' ); 124}); 125 126test ( 'the Engine tab lists and switches, and has no update; About has the update and no rollback' , () => { 127const engine = src ( 'src/components/SettingsEngine.tsx' ); 128assert . match ( src ( 'src/components/NativeOperations.tsx' ), / <SettingsEngine \/> / , 'the Engine tab shows the list' ); 129assert . match ( engine , / Installed engines / ); 130assert . doesNotMatch ( engine , / Check for updates | update-engine | fetchUpdateCheck / ); 131const ops = src ( 'src/components/NativeOperations.tsx' ); 132assert . doesNotMatch ( ops , / Update component | Check for updates | Update engine\? | fetchUpdateCheck | update-engine / ); 133assert . match ( ops , / View logs / , 'the logs stay on the Engine tab' ); 134const about = src ( 'src/components/SettingsUpdates.tsx' ); 135assert . doesNotMatch ( about , / Roll back | rollback | Restore previous engine | Previous engine available / i ); 136assert . match ( about , / Check for updates / ); 137assert . match ( about , / 'update-engine' / ); 138assert . match ( about , / 'update-strata' / ); 139}); 140 141test ( 'the engine in use has no Delete button; every other row has Use this (when it can run here) and Delete' , () => { 142const page = src ( 'src/components/SettingsEngine.tsx' ); 143assert . match ( page , / engine\.active \? <span className="engine-row__state">\{tr\("In use"\)\}<\/span> : <> / ); 144assert . match ( page , / engine\.can_use \? <Btn [ \s\S ] * ?\{tr\("Use this"\)\} / ); 145assert . match ( page , / disabled=\{busy \|\| !engine\.can_delete\} / ); 146assert . match ( page , / run\('remove', del\) / ); 147}); 148 149test ( 'the engine catalogs: same keys and placeholders in nine languages, none still English, none in another catalog, and every sentence of the host is in them' , () => { 150const languages = [ 'en' , 'zh' , 'ja' , 'de' , 'es' , 'ru' , 'pt' , 'ko' , 'fr' ]; 151const en = json ( 'engine-en' ); 152const slots = text => [ ...text . matchAll ( / \{\d + \} / g )]. map ( m => m [ 0 ]). sort (); 153const others = new Set (); 154for ( const name of [ 'en' , 'tune-en' , 'tools-en' , 'fix-en' , 'gh-en' , 'ui-en' ]) for ( const v of Object . values ( json ( name ))) others . add ( v ); 155for ( const l of languages ) { 156const catalog = json ( `engine- ${ l } ` ); 157assert . deepEqual ( Object . keys ( catalog ), Object . keys ( en ), l ); 158for ( const [ id , source ] of Object . entries ( en )) { 159assert . ok ( catalog [ id ]. trim (), ` ${ l } : ${ id } ` ); 160assert . deepEqual ( slots ( catalog [ id ]), slots ( source ), ` ${ l } : ${ id } ` ); 161if ( l !== 'en' ) assert . notEqual ( catalog [ id ], source , ` ${ l } : ${ id } is still English` ); 162} 163} 164for ( const [ id , text ] of Object . entries ( en )) assert . ok ( ! others . has ( text ), `already in another catalog: ${ id } ${ text } ` ); 165const known = new Set ( Object . values ( en )); 166for ( const sentence of [ 167'A model is running, so nothing was changed. Unload the model in Models, then try again.' , 168'This is the engine in use. Switch to another one first, then delete it.' , 169'A program is still using this engine, so it was not deleted. Unload your models and try again.' , 170'That engine is already gone. The list has been refreshed.' , 171'An update is running. Wait for it to finish, then try again.' , 172"This engine can't run on this computer, so nothing was changed." , 173"The engine wasn't deleted. Nothing else was changed." , 174"The engine wasn't switched. Nothing was changed." , 175'Choose an engine from the list.' , 176'Engines can be switched and deleted in the installed Strata app.' , 177'A newer engine is already on this computer. Update switches to it; nothing is downloaded.' , 178]) { 179assert . ok ( known . has ( sentence ), `not in the engine catalog: ${ sentence } ` ); 180assert . ok ([ src ( 'server/engine-library.mjs' ), src ( 'server/update-messages.mjs' ), src ( 'server/updates.mjs' )]. some ( file => file . includes ( sentence )), `no longer said by the host: ${ sentence } ` ); 181} 182});