okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3fda37bd8
main
1/** 2* QAGUI 0213 #2 (second part): while Strata Control starts slowly, /api/native must say "not known yet" (checking), not "engine not 3* connected, nothing installed", and a read that failed during the start is read again within seconds, not after 45 s. 4*/ 5import './helpers/isolate-profile.mjs' ; 6import test from 'node:test' ; 7import assert from 'node:assert/strict' ; 8import { EventEmitter } from 'node:events' ; 9import { mkdtempSync , mkdirSync , writeFileSync , rmSync } from 'node:fs' ; 10import { join } from 'node:path' ; 11import { tmpdir } from 'node:os' ; 12import { build } from 'esbuild' ; 13import { fileURLToPath } from 'node:url' ; 14import { createNativeControl } from '../server/native-control.mjs' ; 15 16const here = p => fileURLToPath ( new URL ( p , import . meta. url )); 17const result = await build ({ entryPoints :[ here ( '../src/lib/native.ts' )], bundle :true , format :'esm' , platform :'node' , write :false , logLevel :'silent' , alias :{ react :here ( './helpers/react-stub.mjs' ) }, loader :{ '.json' :'json' } }); 18const { installedListPending} = await import ( `data:text/javascript;base64, ${ Buffer . from ( result . outputFiles [ 0 ]. text ). toString ( 'base64' )} ` ); 19 20function slowControl ({ failFirst}) { 21const root = mkdtempSync ( join ( tmpdir (), 'strata-chk-' )); 22const install = join ( root , 'install' ), data = join ( root , 'data' ); 23mkdirSync ( install , { recursive :true }); mkdirSync ( data , { recursive :true }); 24const configPath = join ( install , 'strata-iq3_s.json' ); 25writeFileSync ( configPath , JSON . stringify ({ exe :'component:strata-engine' , args :[], model_name :'m' , port :8080 })); 26let asked = 0 ; 27const control = { 28control :{ dir :join ( root , 'control' ), python :join ( root , 'control' , 'python' , 'python' ) }, version :'0.2.10' , 29async installedModels () { 30asked ++ ; 31if ( asked <= failFirst ) throw new Error ( 'Strata Control is still starting' ); 32return { install_dir :install , models :[{ id :'iq3_s' , name :'m' , source_config :configPath , context_tokens :8192 , backend :'cuda' , component_status :'present' , missing :[], issue_codes :[] }] }; 33}, 34async engine () { return { usable :true , backend :'cuda' , components_dir :join ( root , 'c' ), engine :{ version :'0.1.56' , platform :'linux-x86_64' , backend :'cuda' , exe :join ( root , 'e' , 'strata' ), root :join ( root , 'e' ) } }; }, 35}; 36const catalog = { families :[{ family :'qwen' , title :'Q' , about :'x' , images :false , experimental :false , sizes :[{ id :'iq3_s' , model :'IQ3_S' , download_gb :1 , ram_needed_gb :1 , about :'x' , on_this_pc :'fits' , installed :true }] }], contexts :[ 8192 ], recommendation :{ family :'qwen' , model :'IQ3_S' }, context_note :'' }; 37const spawnProcess = () => { 38const child = new EventEmitter (); child . stdout = new EventEmitter (); child . stderr = new EventEmitter (); child . exitCode = null ; 39child . kill = () => { child . exitCode = 0 ; return true ; }; 40child . stdin = { write ( line ) { 41const m = JSON . parse ( line ); if ( ! m . id ) return ; 42const result = m . method === 'initialize' ?{ protocolVersion :'2024-11-05' } 43 :m . method === 'tools/list' ?{ tools :[ 'strata_models' , 'strata_status' ]. map ( name => ({ name})) } 44 :m . params . name === 'strata_models' ?{ structuredContent :catalog } :{ structuredContent :{ hardware :{ ram_gb :16 , gpus :[] }, install :null , running :[], summary :'x' } }; 45queueMicrotask (() => child . stdout . emit ( 'data' , Buffer . from ( JSON . stringify ({ jsonrpc :'2.0' , id :m . id , result}) + '\n' ))); 46} }; 47return child ; 48}; 49const native = createNativeControl ({ control, directory :data , spawnProcess, fetcher :async () => Response . json ({ status :'ok' }), requests :() => [], modelsDirectory :() => join ( root , 'models' ), hardwareProbe :() => [] }); 50return { native, asked :() => asked , cleanup :() => rmSync ( root , { recursive :true , force :true }) }; 51} 52 53test ( 'a read that fails while Control starts is "checking", not "nothing installed", and the page waits on it' , async () => { 54const f = slowControl ({ failFirst :1 }); 55try { 56const first = await f . native . refresh ( false ); 57assert . equal ( first . available , false ); 58assert . equal ( first . checking , true , 'not known yet' ); 59assert . equal ( installedListPending ( first ), true , 'the page shows Checking, no Connect engine, no Download & load' ); 60const again = await f . native . refresh ( true ); // the next poll reads Control again (retry is soon, not after 45 s) 61assert . equal ( again . available , true ); 62assert . equal ( again . checking , false ); 63assert . equal ( installedListPending ( again ), false ); 64assert . equal ( again . installed . models [ 0 ]. ready , true , 'the downloaded model is shown as ready' ); 65} finally { f . cleanup (); } 66}); 67 68test ( 'before the first answer the state is checking; a plain poll after a failed start is not held for 45 s' , async () => { 69const f = slowControl ({ failFirst :1 }); 70try { 71assert . equal ( f . native . state (). checking , true ); 72await f . native . refresh ( false ); 73await new Promise ( r => setTimeout ( r , 3100 )); 74const next = await f . native . refresh ( false ); // an ordinary (not forced) poll, a few seconds later 75assert . equal ( next . available , true ); 76assert . equal ( next . checking , false ); 77} finally { f . cleanup (); } 78}); 79 80test ( 'a Control that keeps failing stops being "checking" so the real error shows' , async () => { 81const f = slowControl ({ failFirst :99 }); 82try { 83let s ; 84for ( let i = 0 ; i < 4 ; i ++ ) s = await f . native . refresh ( true ); 85assert . equal ( s . checking , false ); 86assert . match ( s . error , / still starting / ); 87assert . equal ( installedListPending ( s ), false ); 88} finally { f . cleanup (); } 89});