okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1import test from 'node:test' ; 2import { readFileSync as readSource } from 'node:fs' ; 3import assert from 'node:assert/strict' ; 4import { build } from 'esbuild' ; 5import { createEngineStep } from '../server/engine-step.mjs' ; 6 7const compiled = await build ({ 8stdin :{ 9contents :"export * from './src/lib/native'; export * from './src/lib/download-eta'; export * from './src/lib/native-fit'; export * from './src/lib/native-names'; export * from './src/lib/native-catalog'; export * from './src/lib/i18n';" , 10resolveDir :process . cwd (), 11}, 12bundle :true , write :false , platform :'node' , format :'esm' , 13}); 14const lib = await import ( `data:text/javascript;base64, ${ Buffer . from ( compiled . outputFiles [ 0 ]. text ). toString ( 'base64' )} ` ); 15for ( const l of [ 'zh' , 'ja' , 'de' , 'es' , 'ru' , 'pt' , 'ko' , 'fr' ]) await lib . loadCatalog ( l ); // the non-English catalogs load on first use (src/lib/i18n.ts loadCatalog); load them before the first tr() 16 17const job = ( progress , state = 'running' ) => ({ id :'j1' , kind :'install' , state, startedAt :'' , summary :'' , error :null , result :{ install :progress } }); 18const stateWith = ( jobs , extra = {}) => ({ available :true , repo :'' , summary :'' , error :null , catalog :null , installed :null , hardware :null , instances :[], jobs, ...extra }); 19const answer = body => ({ ok :true , status :200 , statusText :'OK' , text :async () => JSON . stringify ( body ) }); 20 21test ( 'a slow forced read never holds the light poll: a running download keeps moving' , async () => { 22const seen = []; 23globalThis . fetch = async url => { 24seen . push ( url ); 25if ( String ( url ). includes ( 'refresh=1' )) { await new Promise ( r => setTimeout ( r , 400 )); return answer ( stateWith ([ job ({ download :{ done_gb :1 , total_gb :10 } })])); } 26return answer ( stateWith ([ job ({ download :{ done_gb :5 , total_gb :10 } })])); 27}; 28const slow = lib . refreshNative ( true ); 29const t0 = Date . now (); 30await lib . refreshNative (); 31assert . ok ( Date . now () - t0 < 200 , 'the light read answered while the forced one was still running' ); 32assert . equal ( lib . nativeState (). jobs [ 0 ]. result . install . download . done_gb , 5 ); 33await slow ; 34assert . ok ( seen . some ( u => String ( u ). includes ( 'refresh=1' )) && seen . some ( u => ! String ( u ). includes ( 'refresh=1' ))); 35}); 36 37test ( 'a second forced read waits for the first and then reads again (it never joins a read that began before the action)' , async () => { 38let calls = 0 ; 39globalThis . fetch = async () => { calls += 1 ; await new Promise ( r => setTimeout ( r , 50 )); return answer ( stateWith ([], { summary :`read ${ calls } ` })); }; 40const a = lib . refreshNative ( true ), b = lib . refreshNative ( true ), c = lib . refreshNative ( true ); 41await Promise . all ([ a , b , c ]); 42assert . equal ( calls , 2 , 'one read for the first, one more for everything asked meanwhile' ); 43assert . equal ( lib . nativeState (). summary , 'read 2' ); 44}); 45 46test ( 'the host pushes its native state and the page takes it at once' , () => { 47lib . applyNativeEvent ( stateWith ([ job ({ download :{ done_gb :7 , total_gb :10 } })], { summary :'pushed' })); 48assert . equal ( lib . nativeState (). summary , 'pushed' ); 49lib . applyNativeEvent ({ nonsense :true }); 50lib . applyNativeEvent ( null ); 51assert . equal ( lib . nativeState (). summary , 'pushed' , 'a frame that is not a native state changes nothing' ); 52}); 53 54test ( 'the poll is quick while a job runs, normal otherwise, and stops while the window is hidden' , () => { 55assert . equal ( lib . nativePollDelay ( null ), 2500 ); 56assert . equal ( lib . nativePollDelay ( stateWith ([ job ({})], {})), 1000 ); 57assert . equal ( lib . nativePollDelay ( stateWith ([ job ({}, 'done' )])), 2500 ); 58assert . equal ( lib . nativePollDelay ( stateWith ([ job ({})]), true ), null ); 59}); 60 61const parts = ( model , vision , engine = { kind :'engine' , name :'Engine' , state :'done' , done_gb :0.4 , total_gb :0.4 }) => [ model , vision , engine ]; 62const modelPart = ( state , done , total , files_done , files_total ) => ({ kind :'model' , name :'Model files' , state, done_gb :done , total_gb :total , files_done, files_total}); 63const visionPart = ( state , done = 0 ) => ({ kind :'vision' , name :'Picture reader' , state, done_gb :done , total_gb :0.9 }); 64 65test ( 'a model in parts says which part is under way, then the picture reader' , () => { 66lib . setTranslationLanguage ( 'en' ); 67const second = { parts :parts ( modelPart ( 'downloading' , 20 , 66 , 1 , 2 ), visionPart ( 'waiting' )) }; 68assert . deepEqual ( lib . installStage ( second ), { kind :'model' , index :2 , count :2 }); 69assert . equal ( lib . stageText ( lib . installStage ( second )), 'Downloading model files · part 2 of 2' ); 70const reader = { parts :parts ( modelPart ( 'done' , 66 , 66 , 2 , 2 ), visionPart ( 'downloading' , 0.3 )) }; 71assert . deepEqual ( lib . installStage ( reader ), { kind :'vision' }); 72assert . equal ( lib . stageText ( lib . installStage ( reader )), 'Downloading the picture reader…' ); 73assert . equal ( lib . stageText ( lib . installStage ( undefined )), 'Downloading model files…' ); 74const single = { parts :[ modelPart ( 'downloading' , 1 , 20 , 0 , 1 ), visionPart ( 'waiting' )] }; 75assert . equal ( lib . stageText ( lib . installStage ( single )), 'Downloading model files…' , 'one file is not "part 1 of 1"' ); 76}); 77 78test ( 'the figure counts every part: the model whole but the picture reader not yet is not finished' , () => { 79const p = lib . installProgress ({ parts :parts ( modelPart ( 'done' , 10 , 10 , 2 , 2 ), visionPart ( 'downloading' , 0.3 )) }); 80assert . equal ( p . percent , 94 , 'the bar is not at its end while the picture reader is still coming' ); 81assert . ok ( Math . abs ( p . total_gb - 11.3 ) < 0.01 , `total ${ p . total_gb } ` ); 82assert . ok ( p . remaining_gb > 0.5 ); 83const half = lib . installProgress ({ parts :parts ( modelPart ( 'downloading' , 33 , 66 , 1 , 2 ), visionPart ( 'waiting' )) }); 84assert . equal ( half . percent , Math . floor ( 100 * ( 33 + 0.4 ) / ( 66 + 0.9 + 0.4 ))); 85}); 86 87test ( 'the engine step keeps the install job\'s own parts (model parts, picture reader) and adds the engine' , () => { 88const step = createEngineStep ({ control :null }); 89const install = { running :true , step :'5/7: downloading' , download :{ file :'picture reader' , done_gb :66.3 , total_gb :66.9 , remaining_gb :0.6 , percent :99 }, 90parts :[ modelPart ( 'done' , 66 , 66 , 2 , 2 ), visionPart ( 'downloading' , 0.3 )] }; 91const merged = step . merge ( install ); 92assert . deepEqual ( merged . parts . map ( p => p . kind ), [ 'model' , 'vision' , 'engine' ]); 93assert . equal ( merged . parts [ 0 ]. files_total , 2 ); 94const bare = step . merge ({ running :true , download :{ file :'model files' , done_gb :1 , remaining_gb :9 } }); 95assert . deepEqual ( bare . parts . map ( p => p . kind ), [ 'model' , 'engine' ], 'an install without part information keeps the old two-part list' ); 96}); 97 98test ( 'the recommended four come first; the others are what the "More versions" list holds' , () => { 99const v = ( sourceFamily , model ) => ({ sourceFamily, model, id :` ${ sourceFamily } - ${ model } ` }); 100const all = [ v ( 'qwen' , 'Q2_0' ), v ( 'gyro' , 'Gyro-S' ), v ( 'swift' , 'IQ2_XS' ), v ( 'qwen' , 'IQ3_S' ), v ( 'unsloth' , 'UD-Q4_K_XL' ), v ( 'nvfp4' , 'NVFP4' )]; 101const rec = lib . recommendedVersions ( all ). map ( x => x . v . model ); 102assert . deepEqual ( rec , [ 'Gyro-S' , 'IQ2_XS' , 'IQ3_S' , 'UD-Q4_K_XL' ]); 103assert . deepEqual ( all . filter ( x => ! rec . includes ( x . model )). map ( x => x . model ), [ 'Q2_0' , 'NVFP4' ]); 104assert . deepEqual ( lib . recommendedVersions ([ v ( 'qwen36' , 'UD-Q4_K_XL' )]), [], 'Qwen3.6 has no recommended set: its versions are all shown' ); 105}); 106 107test ( 'the three NVFP4 versions have clear names and one line that tells them apart' , () => { 108lib . setTranslationLanguage ( 'en' ); 109const names = [ 'NVFP4' , 'NVFP4-NVIDIA' , 'NVFP4-QAD' ]. map ( m => lib . nativeVersionLabel ( m )); 110assert . deepEqual ( names , [ 'NVFP4 (Strata’s version)' , 'NVFP4 (NVIDIA’s version)' , 'NVFP4 (NVIDIA, trained for FP4)' ]); 111assert . equal ( new Set ( names ). size , 3 ); 112const lines = [ 'NVFP4' , 'NVFP4-NVIDIA' , 'NVFP4-QAD' ]. map ( m => lib . versionNote ( m )); 113assert . match ( lines [ 0 ], / best quality / i ); 114assert . match ( lines [ 1 ], / NVIDIA’s own NVFP4 checkpoint / ); 115assert . match ( lines [ 2 ], / trained for FP4 / ); 116assert . match ( lines [ 2 ], / faster on short prompts / ); 117assert . equal ( lib . versionNote ( 'IQ3_S' ), '' ); 118assert . equal ( lib . nativeShortName ( 'nvfp4-nvfp4-nvidia' ), 'NVFP4 (NVIDIA’s version)' ); 119for ( const language of [ 'zh' , 'ja' , 'de' , 'es' , 'ru' , 'pt' , 'ko' , 'fr' ]) { 120lib . setTranslationLanguage ( language ); 121const local = [ 'NVFP4' , 'NVFP4-NVIDIA' , 'NVFP4-QAD' ]. map ( m => lib . nativeVersionLabel ( m )); 122assert . equal ( new Set ( local ). size , 3 , ` ${ language } : three different names` ); 123assert . ok ( local . every (( n , i ) => n !== names [ i ]), ` ${ language } : translated` ); 124assert . ok ([ 'NVFP4' , 'NVFP4-NVIDIA' , 'NVFP4-QAD' ]. every (( m , i ) => lib . versionNote ( m ) !== lines [ i ]), ` ${ language } : the line is translated` ); 125} 126lib . setTranslationLanguage ( 'en' ); 127}); 128 129test ( '4d: picture reading is on unless the person turned it off (no saved choice is not "off")' , async () => { 130const { nativeFixture} = await import ( './helpers/native-fixture.mjs' ); 131const { readFileSync, writeFileSync} = await import ( 'node:fs' ); 132const { join} = await import ( 'node:path' ); 133const f = nativeFixture (); 134try { 135await f . control . refresh ( true ); 136const first = () => f . control . state (). installed . models . find ( m => m . model === 'iq2_xs' ); 137assert . equal ( first (). visionEnabled , undefined , 'a model never loaded and never turned off' ); 138const path = join ( f . repo , 'strata-iq2_xs.json' ); 139const cfg = JSON . parse ( readFileSync ( path , 'utf8' )); 140writeFileSync ( path , JSON . stringify ({ ...cfg , ui_vision_enabled :false })); 141await f . control . refresh ( true ); 142assert . equal ( first (). visionEnabled , false , 'turned off by the person: stays off' ); 143writeFileSync ( path , JSON . stringify ({ ...cfg , ui_vision_enabled :true , ui_vision_device :'cpu' })); 144await f . control . refresh ( true ); 145assert . equal ( first (). visionEnabled , true ); 146} finally { await f . cleanup (); } 147const screen = readSource ( new URL ( '../src/components/NativeModels.tsx' , import . meta. url ), 'utf8' ); 148// the rule lives in lib/pictures-choice.ts (GUI-FB2 item 4); the page opens a version with it the first time it sees it installed 149const choice = readSource ( new URL ( '../src/lib/pictures-choice.ts' , import . meta. url ), 'utf8' ); 150assert . match ( choice , / installed\.visionEnabled === false && \(installed\.visionPrepared !== false \|\| installed\.visionChosen === true\)\) return 'no' / ); 151assert . match ( screen , / shownVision\(known, installed\) / ); 152assert . match ( screen , / shownVision\(visionPicks\.current\.get\(selectedSize\.id\), undefined\) / , 'a version that is not downloaded starts with Read pictures on' ); 153}); 154 155test ( 'no recognised engine: the app still downloads, warns in the card, and loading says it with the next step' , async () => { 156const { ENGINE_NOT_RECOGNISED } = await import ( '../server/native-control.mjs' ); 157assert . match ( ENGINE_NOT_RECOGNISED , / Settings → About / ); 158assert . match ( ENGINE_NOT_RECOGNISED , / didn’t recognise an engine / ); 159const source = readSource ( new URL ( '../server/native-control.mjs' , import . meta. url ), 'utf8' ); 160assert . equal (( source . match ( / ENGINE_NOT_RECOGNISED\+ / g ) ?? []). length , 2 , 'both load paths use it' ); 161const models = readSource ( new URL ( '../src/components/NativeModels.tsx' , import . meta. url ), 'utf8' ); 162const guided = readSource ( new URL ( '../src/components/GuidedSetup.tsx' , import . meta. url ), 'utf8' ); 163assert . match ( models , / <EngineWarning native=\{native\}\/> / ); 164assert . match ( guided , / <EngineWarning native=\{native\}\/> / ); 165const warn = readSource ( new URL ( '../src/components/EngineWarning.tsx' , import . meta. url ), 'utf8' ); 166assert . match ( warn , / The model will download, but it may not run until the right engine is installed \(Settings → About\)\. / ); 167// the install is not refused for a missing engine (only the engine-minimum refusal for a version that is too old stays) 168assert . doesNotMatch ( source . slice ( source . indexOf ( 'async function install(job,args)' ), source . indexOf ( 'function terminate' )), / engine\.usable / ); 169}); 170 171test ( 'the draft layer download of setup step 6 is a stage of its own and counts in the one figure (#3257)' , () => { 172lib . setTranslationLanguage ( 'en' ); 173const draft = { parts :[{ kind :'model' , name :'Model files' , state :'done' , done_gb :60 , total_gb :60 }, { kind :'draft' , name :'Draft layer' , state :'downloading' , done_gb :2 , total_gb :5 }] }; 174assert . deepEqual ( lib . installStage ( draft ), { kind :'draft' }); 175assert . equal ( lib . stageText ( lib . installStage ( draft )), 'Preparing your model…' ); 176const p = lib . installProgress ( draft ); 177assert . equal ( p . total_gb , 65 ); 178assert . equal ( p . done_gb , 62 ); 179});