okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f5ef26889
main
1import './helpers/isolate-profile.mjs' ; // a test run never writes the owner's real Strata profile 2/** 3* The packaged app (STRATA_PACKAGED=1, STRATA_REAL_ENGINE=1, as the app shell starts the host) never starts a llama.cpp `llama-server`. 4* 0.2.14 did: a GGUF imported through /api/local-models was loaded through a llama-server named by STRATA_LLAMA_SERVER (POST /load), and at 5* the next start the host loaded it again by itself, also from a llama-server found on PATH, with nothing clicked. A fake llama-server that 6* writes a marker line stands in both places; neither the load route nor the start-up path may ever run it. 7*/ 8import test from 'node:test' ; 9import assert from 'node:assert/strict' ; 10import { spawn } from 'node:child_process' ; 11import { chmodSync , existsSync , mkdirSync , mkdtempSync , openSync , readFileSync , rmSync , writeFileSync } from 'node:fs' ; 12import { createServer } from 'node:net' ; 13import { delimiter , join } from 'node:path' ; 14import { tmpdir } from 'node:os' ; 15import { fileURLToPath } from 'node:url' ; 16import { ggufFixture } from './helpers/local-gguf.mjs' ; 17 18const root = fileURLToPath ( new URL ( '..' , import . meta. url )); 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 22/** A llama-server look-alike: one marker line per start, answers /health like the real one, gone after 20 s. */ 23function fakeServer ( dir , marker ) { 24const js = join ( dir , 'fake-llama-server.cjs' ); 25writeFileSync ( js , `const fs=require('fs'),http=require('http'); 26fs.appendFileSync( ${ JSON . stringify ( marker )} , JSON.stringify({pid:process.pid,argv:process.argv.slice(2)})+'\\n'); 27const i=process.argv.indexOf('--port');const port=i>0?Number(process.argv[i+1]):8080; 28http.createServer((q,s)=>{s.setHeader('content-type','application/json');s.end(q.url.startsWith('/health')?'{"status":"ok"}':'{}');}).listen(port,'127.0.0.1'); 29setTimeout(()=>process.exit(0),20000); 30` ); 31const unix = join ( dir , 'llama-server' ); 32writeFileSync ( unix , `#! ${ process . execPath } \nrequire( ${ JSON . stringify ( js )} );\n` ); 33chmodSync ( unix , 0o755 ); 34writeFileSync ( join ( dir , 'llama-server.cmd' ), `@" ${ process . execPath } " " ${ js } " %*\r\n` ); 35return process . platform === 'win32' ?join ( dir , 'llama-server.cmd' ) :unix ; 36} 37 38async function scenario ( t , where ) { 39const home = mkdtempSync ( join ( tmpdir (), `strata-no-llama- ${ where } -` )); 40t . after (() => rmSync ( home , { recursive :true , force :true })); 41const bin = join ( home , 'bin' ), marker = join ( home , 'marker.jsonl' ); 42for ( const d of [ 'bin' , 'src' , 'ws' , 'components' ]) mkdirSync ( join ( home , d ), { recursive :true }); 43const fake = fakeServer ( bin , marker ); 44const gguf = join ( home , 'src' , 'Small-Q4_K_M.gguf' ); 45ggufFixture ( gguf , { arch :'qwen3' , name :'no-llama fixture' }); 46const port = await freePort (); 47const env = { ...process . env , STRATA_PACKAGED :'1' , STRATA_REAL_ENGINE :'1' , NODE_ENV :'production' , STRATA_HOME :join ( home , 'strata' ), 48STRATA_MODELS_DIR :join ( home , 'strata' , 'models' ), STRATA_WORKSPACE :join ( home , 'ws' ), STRATA_COMPONENTS_DIR :join ( home , 'components' ), 49STRATA_PORT :String ( port ), STRATA_SHARE_PORT :String ( await freePort ()), STRATA_TUNNEL_PORT :String ( await freePort ()) }; 50delete env . STRATA_LLAMA_SERVER ; delete env . STRATA_API_KEY ; 51if ( where === 'env' ) env . STRATA_LLAMA_SERVER = fake ; 52else { const key = Object . keys ( env ). find ( k => k . toUpperCase () === 'PATH' ) ?? 'PATH' ; env [ key ] = bin + delimiter + ( env [ key ] ?? '' ); } 53const base = `http://127.0.0.1: ${ port } ` ; 54const call = async ( path , method = 'GET' , body ) => { 55const r = await fetch ( base + path , { method, headers :{ 'content-type' :'application/json' }, body :body === undefined ?undefined :JSON . stringify ( body ) }); 56return { status :r . status , body :await r . json (). catch (() => null ) }; 57}; 58const starts = () => existsSync ( marker ) ?readFileSync ( marker , 'utf8' ). split ( '\n' ). filter ( Boolean ). map ( l => JSON . parse ( l )) :[]; 59let host = null ; 60const up = async () => { 61const log = openSync ( join ( home , 'host.log' ), 'a' ); 62host = spawn ( process . execPath , [ 'server/index.mjs' ], { cwd :root , env, stdio :[ 'ignore' , log , log ] }); 63for ( let i = 0 ; i < 150 ; i ++ ) { try { if (( await call ( '/api/health' )). status === 200 ) return ; } catch { /* not up yet */ } await delay ( 100 ); } 64assert . fail ( 'the host did not start: ' + readFileSync ( join ( home , 'host.log' ), 'utf8' ). slice ( - 600 )); 65}; 66const down = async () => { if ( ! host ) return ; const h = host ; host = null ; h . kill (); await Promise . race ([ new Promise ( r => h . once ( 'exit' , r )), delay ( 8000 )]); }; 67t . after ( async () => { await down (); for ( const s of starts ()) { try { process . kill ( s . pid ); } catch { /* gone */ } } }); 68 69await up (); 70const imported = await call ( '/api/local-models' , 'POST' , { path :gguf }); 71assert . equal ( imported . status , 200 , JSON . stringify ( imported . body )); 72const id = imported . body . variantId ; 73const load = await call ( `/api/models/ ${ encodeURIComponent ( id )} /load` , 'POST' ); 74await delay ( 1500 ); 75assert . equal ( load . status , 409 , JSON . stringify ( load . body )); 76assert . match ( load . body ?. error ?? '' , / own engine / ); 77assert . deepEqual ( starts (), [], 'POST /load started the llama-server' ); 78assert . equal (( await call ( '/api/engine/status' )). body ?. running , false ); 79 80await down (); 81await up (); 82await delay ( 2500 ); // (0.2.14 began its start-up load 0.7 s after the host was up) 83assert . deepEqual ( starts (), [], 'the host start started the llama-server' ); 84const library = ( await call ( '/api/models' )). body ; 85assert . equal ( library ?. loaded ?? null , null ); 86assert . notEqual ( library ?. current , id , 'an imported GGUF that never loaded became the remembered model' ); 87assert . equal (( await call ( '/api/engine/status' )). body ?. running , false ); 88} 89 90test ( 'the packaged host never starts a llama-server named by STRATA_LLAMA_SERVER (load route and host start)' , { timeout :90000 }, t => scenario ( t , 'env' )); 91test ( 'the packaged host never starts a llama-server found on PATH (load route and host start)' , { timeout :90000 }, t => scenario ( t , 'path' ));