okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1130d824a
main
1import './helpers/isolate-profile.mjs' ; // a test run never writes the owner's real Strata profile 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { EventEmitter } from 'node:events' ; 5import { mkdtempSync , mkdirSync , writeFileSync , rmSync , realpathSync } from 'node:fs' ; 6import { join } from 'node:path' ; 7import { tmpdir as osTmpdir } from 'node:os' ; 8import { bundledPython , createControl , findControl } from '../server/control-runtime.mjs' ; 9import { createNativeControl } from '../server/native-control.mjs' ; 10 11// ARCFIX (0.2.13): a Control-only feed update (`strata update`, or Settings > Updates > Strata) writes <components>/control/active.json. 12// The packaged app must then run the model setup (tools/strata_mcp.py and the setup.py it starts) from THAT Control, not from the bundled one. 13const isWin = process . platform === 'win32' ; 14const tmp = () => realpathSync ( osTmpdir ()); 15 16function controlDir ( parent , name , version ) { 17const dir = join ( parent , name ); 18mkdirSync ( join ( dir , 'python' , ...( isWin ?[] :[ 'bin' ])), { recursive :true }); 19mkdirSync ( join ( dir , 'strata_control' ), { recursive :true }); 20mkdirSync ( join ( dir , 'tools' ), { recursive :true }); 21mkdirSync ( join ( dir , 'serve' ), { recursive :true }); 22writeFileSync ( bundledPython ( dir ), 'x' ); 23writeFileSync ( join ( dir , 'strata.py' ), '# cli' ); 24writeFileSync ( join ( dir , 'tools' , 'strata_mcp.py' ), '# mcp' ); 25writeFileSync ( join ( dir , 'setup.py' ), '# setup' ); 26writeFileSync ( join ( dir , 'serve' , 'server.py' ), 'port\nparallel\n/v1/responses' ); 27writeFileSync ( join ( dir , 'BUNDLE.json' ), JSON . stringify ({ version})); 28return dir ; 29} 30 31function fixture () { 32const root = mkdtempSync ( join ( tmp (), 'strata-arcfix-' )); 33const install = join ( root , 'install' ); // the AppImage's / installer's folder: <install>/host + <install>/control 34mkdirSync ( join ( install , 'host' ), { recursive :true }); 35const bundled = controlDir ( install , 'control' , '0.1.59' ); 36const components = join ( root , 'components' ); 37mkdirSync ( join ( components , 'control' ), { recursive :true }); 38const updated = controlDir ( join ( components , 'control' ), '0.1.59.1' , '0.1.59.1' ); 39writeFileSync ( join ( components , 'control' , 'active.json' ), JSON . stringify ({ version :'0.1.59.1' , folder :'0.1.59.1' , sha256 :'x' })); 40return { root, install, bundled, components, updated, hostDir :join ( install , 'host' ) }; 41} 42 43test ( 'a four-part Control (0.1.59.1) from the feed beats the bundled 0.1.59, whether the shell names the bundled one or it sits beside the host' , () => { 44const f = fixture (); 45try { 46// the Tauri shell sets STRATA_CONTROL_DIR to the bundled folder (apps/desktop/src-tauri/src/main.rs apply_runtime_policy) 47const viaShell = findControl ({ env :{ STRATA_CONTROL_DIR :f . bundled }, hostDir :f . hostDir , components :f . components }); 48assert . equal ( viaShell . dir , f . updated ); 49assert . equal ( viaShell . bundle . version , '0.1.59.1' ); 50assert . equal ( findControl ({ env :{}, hostDir :f . hostDir , components :f . components }). dir , f . updated ); 51// the same Control, the same version: the bundled one stays (a tie goes to the first), so a feed that offers 0.1.59 again changes nothing 52writeFileSync ( join ( f . updated , 'BUNDLE.json' ), JSON . stringify ({ version :'0.1.59' })); 53writeFileSync ( join ( f . components , 'control' , 'active.json' ), JSON . stringify ({ version :'0.1.59' , folder :'0.1.59.1' })); 54assert . equal ( findControl ({ env :{ STRATA_CONTROL_DIR :f . bundled }, hostDir :f . hostDir , components :f . components }). dir , f . bundled ); 55} finally { rmSync ( f . root , { recursive :true , force :true }); } 56}); 57 58test ( 'the model setup of the packaged app runs from the updated Control: strata_mcp.py (and with it --src, the folder setup.py is started from) is the updated one' , async () => { 59const f = fixture (); 60try { 61const found = findControl ({ env :{ STRATA_CONTROL_DIR :f . bundled }, hostDir :f . hostDir , components :f . components }); 62const spawned = []; 63const spawnProcess = ( command , args , options ) => { 64spawned . push ({ command, args, options}); 65const child = new EventEmitter (); child . stdout = new EventEmitter (); child . stderr = new EventEmitter (); child . exitCode = null ; 66child . kill = () => true ; 67child . stdin = { write ( line ) { 68const message = JSON . parse ( line ); if ( ! message . id ) return ; 69const result = message . method === 'tools/list' ?{ tools :[{ name :'strata_status' }] } :{ protocolVersion :'2024-11-05' }; 70queueMicrotask (() => child . stdout . emit ( 'data' , Buffer . from ( JSON . stringify ({ jsonrpc :'2.0' , id :message . id , result}) + '\n' ))); 71} }; 72return child ; 73}; 74const data = join ( f . root , 'data' ); 75const control = { ...createControl ({ control :found , run :async () => ({}) }), 76async installedModels () { return { install_dir :f . root , models :[] }; }, 77async engine () { return { usable :true , backend :'sycl' , engine :{ version :'0.1.59' , platform :'linux-x86_64' , backend :'sycl' , exe :'/e/strata' , root :'/e' } }; } }; 78const native = createNativeControl ({ control, directory :data , spawnProcess, fetcher :async () => Response . json ({}) }); 79try { await native . refresh ( true ); } finally { await native . close (); } 80const installer = spawned . find ( s => String ( s . args [ 0 ]). endsWith ( 'strata_mcp.py' )); 81assert . ok ( installer , 'the host started the installer process' ); 82assert . equal ( installer . command , bundledPython ( f . updated )); 83assert . equal ( installer . args [ 0 ], join ( f . updated , 'tools' , 'strata_mcp.py' )); 84assert . equal ( installer . args [ installer . args . indexOf ( '--src' ) + 1 ], f . updated ); 85assert . equal ( installer . options . cwd , f . updated ); 86assert . equal ( installer . options . env . STRATA_CONTROL_DIR , f . updated ); 87} finally { rmSync ( f . root , { recursive :true , force :true }); } 88});