okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8820fa433
main
1#!/usr/bin/env node 2/** 3* The first run of the installed app, headless: the host as the Tauri shell starts it (STRATA_PACKAGED=1, STRATA_NO_ACQUIRE=1, 4* the staged Control, nothing installed, nothing in the per-user folders) and Chrome on its onboarding. 5* 6* node apps/desktop/scripts/first-run-ui.mjs [--control <staged control>] [--node <node.exe>] [--work <scratch>] [--shots <dir>] 7* 8* Checks, one line each (exit 1 if any fails): 9* the host answers; /api/native has a real catalog (families with sizes), a recommendation and the GPUs of this PC 10* (from Control's bundled installer on Control's Python); the page shows "Let's get started", and after it the model 11* list with sizes (no "couldn't show this screen"); no page error; the install plan for the recommended model is a 12* plan (nothing is downloaded or started: a plan changes nothing); the spy sees no pip/git/compiler/engine fetch. 13* Per-user folders (STRATA_HOME, APPDATA, LOCALAPPDATA, the components and install folders) are all under --work. 14* No model is downloaded and no engine runs. 15*/ 16import { spawn } from 'node:child_process' ; 17import { existsSync , mkdirSync , mkdtempSync , readFileSync , realpathSync , rmSync , writeFileSync } from 'node:fs' ; 18import { createServer } from 'node:net' ; 19import { tmpdir } from 'node:os' ; 20import { dirname , join , resolve } from 'node:path' ; 21import { fileURLToPath , pathToFileURL } from 'node:url' ; 22 23const args = Object . fromEntries ( process . argv . slice ( 2 ). reduce (( acc , a , i , all ) => ( a . startsWith ( '--' ) ?[ ...acc , [ a . slice ( 2 ), all [ i + 1 ]?. startsWith ( '--' ) ?true :all [ i + 1 ]]] :acc ), [])); 24const here = dirname ( fileURLToPath ( import . meta. url )); 25const repoHost = resolve ( here , '../../host' ); 26const staged = resolve ( here , '../src-tauri/resources' ); 27const control = resolve ( args . control ?? join ( staged , 'control' )); 28const nodeExe = resolve ( args . node ?? join ( staged , 'node' , process . platform === 'win32' ?'node.exe' :'node' )); 29const work = resolve ( args . work ?? mkdtempSync ( join ( tmpdir (), 'strata-first-run-' ))); 30const shots = resolve ( args . shots ?? join ( work , 'shots' )); 31mkdirSync ( work , { recursive :true }); mkdirSync ( shots , { recursive :true }); 32const spy = pathToFileURL ( join ( repoHost , 'tests/helpers/acquire-spy.mjs' )). href ; 33const spyLog = join ( work , 'spy.log' ); writeFileSync ( spyLog , '' ); 34 35const { chromium} = await import ( pathToFileURL ( join ( repoHost , 'node_modules/playwright-core/index.mjs' )). href ). catch (() => import ( 'playwright-core' )); 36const CHROME = process . env . CHROME ?? [ 'C:/Program Files/Google/Chrome/Application/chrome.exe' , '/usr/bin/google-chrome' , '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' ]. find ( existsSync ); 37 38let failed = false ; 39const check = ( ok , label , detail = '' ) => { console . log ( ` ${ ok ? 'ok ' : 'FAIL' } ${ label } ${ detail ? ' ' + detail : '' } ` ); if ( ! ok ) failed = true ; }; 40const delay = ms => new Promise ( r => setTimeout ( r , ms )); 41const freePort = async () => { const s = createServer (); await new Promise ( r => s . listen ( 0 , '127.0.0.1' , r )); const p = s . address (). port ; await new Promise ( r => s . close ( r )); return p ; }; 42 43for ( const need of [ nodeExe , join ( control , 'strata.py' ), join ( repoHost , 'dist' , 'index.html' )]) if ( ! existsSync ( need )) { console . error ( `missing ${ need } (run stage-control.py, fetch-node and "npm run build" in apps/host first)` ); process . exit ( 2 ); } 44 45const dirs = { home :join ( work , 'home' ), appdata :join ( work , 'appdata' ), local :join ( work , 'local' ), components :join ( work , 'local' , 'Strata' , 'components' ) }; 46for ( const d of Object . values ( dirs )) mkdirSync ( d , { recursive :true }); 47const port = await freePort (); 48const env = { 49 ...process . env , 50STRATA_PORT :String ( port ), STRATA_DIST :join ( repoHost , 'dist' ), STRATA_PACKAGED :'1' , STRATA_REAL_ENGINE :'1' , STRATA_NO_ACQUIRE :'1' , 51STRATA_CONTROL_DIR :control , STRATA_HOME :dirs . home , STRATA_MODELS_DIR :join ( work , 'models' ), STRATA_COMPONENTS_DIR :dirs . components , 52APPDATA :dirs . appdata , LOCALAPPDATA :dirs . local , XDG_CONFIG_HOME :join ( work , 'xdg' ), XDG_DATA_HOME :join ( work , 'xdgdata' ), 53STRATA_PROFILE_DIR :join ( work , 'profiles' ), STRATA_RUNTIME_DIR :join ( work , 'runtime' ), ACQUIRE_SPY_LOG :spyLog , 54}; 55delete env . STRATA_INSTALL_DIR ; 56mkdirSync ( env . STRATA_MODELS_DIR , { recursive :true }); 57const host = spawn ( nodeExe , [ ...( args [ 'no-spy' ] === undefined ?[ '--import' , spy ] :[]), join ( repoHost , 'server' , 'index.mjs' )], { cwd :repoHost , env, stdio :[ 'ignore' , 'pipe' , 'pipe' ] }); 58let hostLog = '' ; 59host . stdout . on ( 'data' , c => { hostLog += c ; }); host . stderr . on ( 'data' , c => { hostLog += c ; }); 60const base = `http://127.0.0.1: ${ port } ` ; 61const api = async ( path , init ) => { const r = await fetch ( base + path , init ); return { status :r . status , body :await r . json (). catch (() => null ) }; }; 62 63let browser ; 64try { 65let up = false ; 66for ( let i = 0 ; i < 150 && ! up ; i ++ ) { try { up = ( await api ( '/api/health' )). status === 200 ; } catch { await delay ( 200 ); } } 67check ( up , 'the packaged host starts' , `port ${ port } ` ); 68if ( ! up ) throw new Error ( hostLog . slice ( - 2000 )); 69 70const native = await api ( '/api/native?refresh=1' ); 71const state = native . body ; 72check ( native . status === 200 && state ?. available === true , 'the host reads Control' , state ?. error ?? '' ); 73const sizes = ( state ?. catalog ?. families ?? []). reduce (( n , f ) => n + ( f . sizes ?. length ?? 0 ), 0 ); 74check (( state ?. catalog ?. families ?. length ?? 0 ) > 0 && sizes >= 3 , 'the catalog has real families and sizes' , ` ${ state ?. catalog ?. families ?. length } families, ${ sizes } sizes` ); 75check ( !! state ?. catalog ?. recommendation , 'the catalog has a recommendation' , JSON . stringify ( state ?. catalog ?. recommendation ?? null ). slice ( 0 , 120 )); 76check ( Array . isArray ( state ?. hardware ?. gpus ), 'the hardware lists GPUs' , ( state ?. hardware ?. gpus ?? []). map ( g => ` ${ g . name } ${ g . vram_gb } GB` ). join ( '; ' ) || 'none on this PC' ); 77check ( state ?. installDir && realpathSync . native ( state . installDir ). toLowerCase (). startsWith ( realpathSync . native ( work ). toLowerCase ()), 'models go to a writable per-user folder, not the bundle' , state ?. installDir ); 78const hw = ( await api ( '/api/hardware' )). body ; 79console . log ( 'info host hardware probe:' , JSON . stringify (( hw ?. gpus ?? []). map ( g => ({ name :g . name , backend :g . backend , vramGb :g . vramGb , unified :g . unified })))); 80check ( ! state ?. installerError , 'the bundled installer answers' , state ?. installerError ?? '' ); 81 82browser = await chromium . launch ({ executablePath :CHROME , headless :true }); 83const page = await browser . newPage ({ viewport :{ width :1400 , height :950 } }); 84const problems = []; 85page . on ( 'pageerror' , e => problems . push ( `pageerror: ${ e . message } ` )); 86await page . goto ( base , { waitUntil :'domcontentloaded' }); 87await page . waitForSelector ( 'text=/Let.s get started|Choose a version|Connect your Strata/i' , { timeout :30000 }). catch (() => {}); 88await page . screenshot ({ path :join ( shots , '1-welcome.png' ) }); 89const broken = await page . locator ( 'text=/couldn.t show this screen/i' ). count (); 90check ( broken === 0 , 'the first screen renders (not "Strata couldn\'t show this screen")' ); 91const start = page . getByRole ( 'button' , { name :/ get started / i }); 92check ( await start . count () > 0 , 'the welcome step offers "Let\'s get started"' ); 93if ( await start . count ()) await start . first (). click (); 94await page . waitForSelector ( 'text=/Choose a version/i' , { timeout :30000 }). catch (() => {}); 95await delay ( 1200 ); 96await page . screenshot ({ path :join ( shots , '2-models.png' ) }); 97check ( await page . locator ( 'text=/couldn.t show this screen/i' ). count () === 0 , 'the model step renders' ); 98check ( await page . getByText ( / Choose a version / i ). count () > 0 , 'the model step lists the versions' ); 99const labels = await page . locator ( '[role=radio]' ). allInnerTexts (); 100check ( labels . length >= 3 , 'the versions are the real catalog\'s sizes' , labels . map ( l => l . replace ( / \s +/ g , ' ' ). slice ( 0 , 40 )). join ( ' | ' )); 101check ( labels . some ( l => / \d + \.\d\s * GB / . test ( l )), 'each version shows its download size' ); 102check ( problems . length === 0 , 'no page error' , problems . join ( '; ' )); 103 104const recommended = state ?. catalog ?. recommendation ; 105if ( recommended ?. model ) { 106const plan = await api ( '/api/native/plan' , { method :'POST' , headers :{ 'content-type' :'application/json' }, body :JSON . stringify ({ family :recommended . family , model :recommended . model , context :recommended . context , data_dir :join ( work , 'Strata-data' ) }) }); 107check ( plan . status === 200 && !! plan . body ?. plan , 'the install plan for the recommended model is a plan (nothing downloaded)' , plan . body ?. summary ?. slice ( 0 , 100 ) ?? JSON . stringify ( plan . body ). slice ( 0 , 150 )); 108check ( ! existsSync ( join ( work , 'Strata-data' , 'models' )), 'a plan wrote no model files' ); 109} 110} catch ( error ) { 111check ( false , 'the run' , String ( error ?. message ?? error ). slice ( 0 , 600 )); 112} finally { 113await browser ?. close (). catch (() => {}); 114host . kill (); 115await new Promise ( r => host . once ( 'exit' , r )); // only the PID this script started 116const attempts = readFileSync ( spyLog , 'utf8' ). split ( '\n' ). filter ( Boolean ); 117check ( attempts . length === 0 , 'the spy saw no pip, git, compiler, engine or internet access' , attempts . slice ( 0 , 3 ). join ( ' ' )); 118console . log ( `shots in ${ shots } ` ); 119if ( ! args . work ) rmSync ( work , { recursive :true , force :true , maxRetries :3 }); 120} 121process . exit ( failed ?1 :0 );