okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f92cfa437
main
1import test from 'node:test' ; 2import { ROOMY_HOST } from './helpers/hardware.mjs' ; 3import assert from 'node:assert/strict' ; 4import { spawn } from 'node:child_process' ; 5import { existsSync , readFileSync } from 'node:fs' ; 6import { mkdtemp , rm } from 'node:fs/promises' ; 7import { createServer } from 'node:net' ; 8import { tmpdir } from 'node:os' ; 9import { join } from 'node:path' ; 10import { fileURLToPath , pathToFileURL } from 'node:url' ; 11 12/** 13* Strata works without internet. The host is started with every non-loopback fetch failing (and logged), the way a PC with the cable 14* pulled behaves. Starting, reading the screens' data and loading a model make no outside call. The update check is the one 15* thing that reaches out, and only when it is asked for: opening Settings does not ask. 16*/ 17const root = fileURLToPath ( new URL ( '..' , import . meta. url )); 18const preload = pathToFileURL ( fileURLToPath ( new URL ( './helpers/offline-preload.mjs' , import . meta. url ))). href ; 19const delay = ms => new Promise ( r => setTimeout ( r , ms )); 20async function freePort () { 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 ; } 21 22async function startOfflineHost () { 23const home = await mkdtemp ( join ( tmpdir (), 'strata-offline-' )); 24const log = join ( home , 'net.log' ); 25const port = await freePort (); 26let output = '' ; 27const host = spawn ( process . execPath , [ '--import' , preload , 'server/index.mjs' ], { 28cwd :root , stdio :[ 'ignore' , 'pipe' , 'pipe' ], 29env :{ ...process . env , STRATA_HOME :home , STRATA_MODELS_DIR :join ( home , 'models' ), STRATA_PORT :String ( port ), STRATA_SHARE_PORT :String ( await freePort ()), STRATA_TUNNEL_PORT :String ( await freePort ()), STRATA_PACKAGED :'1' , STRATA_COMPONENTS_DIR :join ( home , 'components' ), STRATA_NET_LOG :log , STRATA_NO_ACQUIRE :'1' , STRATA_REAL_ENGINE :'0' , STRATA_TEST_HARDWARE :ROOMY_HOST }, 30}); 31host . stdout . on ( 'data' , d => { output += d ; }); host . stderr . on ( 'data' , d => { output += d ; }); 32let up = false ; 33for ( let i = 0 ; i < 150 && ! up ; i ++ ) { try { up = ( await fetch ( `http://127.0.0.1: ${ port } /api/health` )). ok ; } catch { await delay ( 100 ); } } 34assert . ok ( up , `host started\n ${ output } ` ); 35const calls = () => ( existsSync ( log ) ?readFileSync ( log , 'utf8' ). split ( '\n' ). filter ( Boolean ) :[]); 36const get = async path => { const r = await fetch ( `http://127.0.0.1: ${ port } ${ path } ` ); return { status :r . status , data :await r . json (). catch (() => null ) }; }; 37const post = async path => { const r = await fetch ( `http://127.0.0.1: ${ port } ${ path } ` , { method :'POST' , headers :{ 'content-type' :'application/json' }, body :'{}' }); return { status :r . status , data :await r . json (). catch (() => null ) }; }; 38return { host, get, post, calls, stop :async () => { host . kill (); await new Promise ( r => host . once ( 'exit' , r )); await rm ( home , { recursive :true , force :true }). catch (() => {}); } }; 39} 40 41test ( 'with no internet the host starts, serves its screens and loads a model without one outside call' , async t => { 42const h = await startOfflineHost (); 43t . after ( h . stop ); 44assert . deepEqual ( h . calls (), [], 'nothing reached out while starting' ); 45for ( const path of [ '/api/native' , '/api/setup' , '/api/models' , '/api/engine' , '/api/health' ]) { 46const r = await h . get ( path ); 47assert . equal ( r . status , 200 , ` ${ path } answers` ); 48} 49const install = await h . post ( '/api/models/qwen38-q4_k_m/install' ); 50assert . equal ( install . status , 200 ); 51let installed = false ; 52for ( let i = 0 ; i < 150 && ! installed ; i ++ ) { installed = ( await h . get ( '/api/models' )). data . variants . find ( v => v . id === 'qwen38-q4_k_m' ). state === 'installed' ; if ( ! installed ) await delay ( 100 ); } 53assert . ok ( installed , 'the model is on this computer' ); 54const load = await h . post ( '/api/models/qwen38-q4_k_m/load' ); 55assert . equal ( load . status , 200 , JSON . stringify ( load . data )); 56assert . equal ( load . data . loaded , 'qwen38-q4_k_m' ); 57assert . deepEqual ( h . calls (), [], 'opening the screens and loading a model made no outside call' ); 58}); 59 60test ( 'the update check reaches out only when it is asked for, and offline it answers in plain words' , async t => { 61const h = await startOfflineHost (); 62t . after ( h . stop ); 63await h . get ( '/api/setup' ); await h . get ( '/api/native' ); await h . get ( '/api/models' ); 64assert . equal ( h . calls (). length , 0 , 'opening the screens does not check' ); 65const checked = await h . get ( '/api/updates/check' ); 66assert . equal ( checked . status , 200 ); 67assert . equal ( h . calls (). length , 1 , 'one check, from the click' ); 68assert . equal ( checked . data . strata . status , 'error' ); 69assert . equal ( checked . data . strata . message , "No internet connection. Try again when you're online." ); 70assert . doesNotMatch ( JSON . stringify ( checked . data ), / fetch failed / ); 71});