okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2e8fb221c
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { createEngineStep } from '../server/engine-step.mjs' ; 4 5const GB = 1e9 ; 6function control ({ usable= false , check, update, checkError= null } = {}) { 7const calls = []; 8return { 9 calls, 10async engine () { calls . push ( 'engine' ); return { usable}; }, 11async updateCheck () { calls . push ( 'updateCheck' ); if ( checkError ) throw new Error ( checkError ); return check ; }, 12async update ( options ) { calls . push ([ 'update' , options . engine , options . control ]); return update ( options ); }, 13}; 14} 15const offer = ( status = 'install' , extra = {}) => ({ engine :{ status, installed :null , offered :{ version :'0.1.46' }, download_bytes :0.5 * GB , ...extra } }); 16const updated = { ok :true , results :[{ component :'engine' , status :'updated' , installed_after :{ version :'0.1.46' } }] }; 17 18test ( 'no engine installed: the step downloads it with the model and reports the stages in order' , async () => { 19const seen = []; 20const c = control ({ 21check :offer (), 22update :async ({ onEvent}) => { 23onEvent ({ event :'progress' , percent :50 , done :0.25 * GB , total :0.5 * GB }); 24onEvent ({ event :'phase' , phase :'verify' , message :'Checking' }); 25onEvent ({ event :'phase' , phase :'test' , message :'Test-starting' }); 26onEvent ({ event :'phase' , phase :'switch' , message :'Switching' }); 27return updated ; 28}, 29}); 30const step = createEngineStep ({ control :c , onChange :p => seen . push ( p . state ) }); 31const part = await step . run (); 32assert . deepEqual ( c . calls , [ 'engine' , 'updateCheck' , [ 'update' , true , false ]], 'the engine only: Control and the app are not part of a model install' ); 33assert . deepEqual ([ ...new Set ( seen )], [ 'checking' , 'downloading' , 'verifying' , 'testing' , 'switching' , 'done' ]); 34assert . equal ( part . state , 'done' ); 35assert . equal ( part . version , '0.1.46' ); 36assert . equal ( step . summaryNote (), null ); 37}); 38 39test ( 'one combined total: model and engine bytes add up, the model-only numbers stay in the parts list' , async () => { 40const c = control ({ check :offer (), update :async ({ onEvent}) => { onEvent ({ event :'progress' , percent :40 , done :0.2 * GB , total :0.5 * GB }); return new Promise (() => {}); } }); 41const step = createEngineStep ({ control :c }); 42void step . run (); 43await new Promise ( r => setTimeout ( r , 5 )); 44const install = { running :true , step :'Downloading' , download :{ file :'model-00001.gguf' , percent :10 , done_gb :6 , remaining_gb :60 } }; 45const merged = step . merge ( install ); 46assert . equal ( merged . download . done_gb , 6.2 ); 47assert . equal ( merged . download . remaining_gb , 60.3 ); 48assert . equal ( merged . download . percent , Math . round ( 100 * 6.2 / 66.5 )); 49assert . equal ( merged . download . file , 'model-00001.gguf' , 'the model is still the part that is moving' ); 50assert . deepEqual ( merged . parts . map ( p => p . kind ), [ 'model' , 'engine' ]); 51assert . deepEqual ([ merged . parts [ 0 ]. done_gb , merged . parts [ 0 ]. total_gb ], [ 6 , 66 ]); 52assert . equal ( merged . engine . state , 'downloading' ); 53assert . equal ( install . download . done_gb , 6 , 'merge does not change its input' ); 54}); 55 56test ( 'a failed engine download keeps the model and says how to retry' , async () => { 57const c = control ({ check :offer (), update :async () => { throw new Error ( 'the connection dropped' ); } }); 58const step = createEngineStep ({ control :c }); 59const part = await step . run (); 60assert . equal ( part . state , 'failed' ); 61assert . equal ( part . retry , 'Update engine' ); 62assert . match ( part . message , / ^Your model is ready\. The engine for this PC couldn't be downloaded\. . * Update engine in Settings / ); 63assert . doesNotMatch ( part . message , / dropped | HTTP | exit | sha / i ); 64assert . match ( part . details , / connection dropped / , 'the reason is kept for the Details disclosure' ); 65assert . equal ( step . summaryNote (), part . message ); 66const merged = step . merge ({ running :false , download :{ file :'m' , percent :100 , done_gb :10 , remaining_gb :0 } }); 67assert . equal ( merged . download . remaining_gb , 0 , 'a failed engine does not leave the progress bar short' ); 68}); 69 70test ( 'a feed that cannot be reached is a failure only when there is no engine yet' , async () => { 71const none = await createEngineStep ({ control :control ({ usable :false , checkError :'could not be read' }) }). run (); 72assert . equal ( none . state , 'failed' ); 73const have = await createEngineStep ({ control :control ({ usable :true , checkError :'could not be read' }) }). run (); 74assert . equal ( have . state , 'skipped' ); 75assert . match ( have . message , / engine for this PC is installed / ); 76assert . equal ( have . details , undefined , 'nothing to say when an engine is installed' ); 77}); 78 79test ( 'an engine that is already current downloads nothing; one with no package for this PC is a failure' , async () => { 80const c = control ({ usable :true , check :{ engine :{ status :'current' , installed :{ version :'0.1.46' } } } }); 81const part = await createEngineStep ({ control :c }). run (); 82assert . equal ( part . state , 'current' ); 83assert . deepEqual ( c . calls , [ 'engine' , 'updateCheck' ]); 84const none = await createEngineStep ({ control :control ({ usable :false , check :{ engine :{ status :'none-for-this-pc' , reasons :[ '0.1.46 is for linux-x86_64, this PC is windows-x86_64' ] } } }) }). run (); 85assert . equal ( none . state , 'failed' ); 86assert . doesNotMatch ( none . message , / windows-x86_64 / ); 87assert . match ( none . details , / windows-x86_64 / ); 88}); 89 90test ( 'without a component updater the step is skipped and never throws' , async () => { 91const part = await createEngineStep ({ control :{} }). run (); 92assert . equal ( part . state , 'skipped' ); 93assert . equal ( createEngineStep ({ control :{} }). merge ( null ), null ); 94}); 95 96test ( '#4823: an engine that already works is not replaced without saying from which version to which, and that the old one is kept' , async () => { 97const messages = []; 98const c = control ({ 99usable :true , 100check :offer ( 'available' , { installed :{ version :'0.1.54' }, offered :{ version :'0.1.55' } }), 101update :async ({ onEvent}) => { onEvent ({ event :'progress' , percent :30 , done :0.15 * GB , total :0.5 * GB }); return { ok :true , results :[{ component :'engine' , status :'updated' , installed_after :{ version :'0.1.55' } }] }; }, 102}); 103const step = createEngineStep ({ control :c , onChange :p => messages . push ( p . message ) }); 104const part = await step . run (); 105assert . match ( messages . find ( m => / Updating / . test ( m )), / ^Updating the engine from 0\.1\.54 to 0\.1\.55\. The engine you have now is kept, and Settings can go back to it\.$ / ); 106assert . ok ( messages . includes ( 'Updating the engine from 0.1.54 to 0.1.55: 30%' )); 107assert . equal ( part . message , 'The engine was updated from 0.1.54 to 0.1.55. Settings can go back to the one you had.' ); 108}); 109 110test ( 'a first engine keeps the plain download words' , async () => { 111const messages = []; 112const c = control ({ check :offer (), update :async () => updated }); 113const step = createEngineStep ({ control :c , onChange :p => messages . push ( p . message ) }); 114await step . run (); 115assert . ok ( messages . includes ( 'Downloading the engine for this PC…' )); 116assert . ok ( ! messages . some ( m => / Updating the engine from / . test ( m ))); 117});