okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
44d885874
main
1import { fileURLToPath } from 'node:url' ; 2import assert from 'node:assert/strict' ; 3import { test } from 'node:test' ; 4import { readFile } from 'node:fs/promises' ; 5import { transform } from 'esbuild' ; 6import { migrateSetup } from '../server/setup.mjs' ; 7const { code} = await transform ( await readFile ( new URL ( '../src/lib/lifecycle.ts' , import . meta. url ), 'utf8' ), { loader :'ts' , format :'esm' }); 8const { lifecyclePolicy} = await import ( `data:text/javascript;base64, ${ Buffer . from ( code ). toString ( 'base64' )} ` ); 9 10test ( 'old setup steps map once and preserve selections and completed work' , () => { 11const choices = { variantId :'fixture' , profile :'performance' , contextLength :'8k' , batch :1024 , batchAuto :false }; 12const history = [{ step :3 , at :'fixture' }]; 13for ( const [ oldStep , expected ] of [[ 1 , 1 ],[ 2 , 2 ],[ 3 , 2 ],[ 4 , 3 ],[ 5 , 4 ]]) { 14const original = { step :oldStep , choices, history, dismissed :true , complete :false }; 15const migrated = migrateSetup ( original ); 16assert . equal ( migrated . step , expected ); assert . equal ( migrated . flowVersion , 2 ); 17assert . deepEqual ( migrated . choices , choices ); assert . deepEqual ( migrated . history , history ); 18assert . equal ( migrated . dismissed , true ); assert . equal ( migrated . complete , false ); 19assert . deepEqual ( migrateSetup ( migrated ), migrated ); 20assert . equal ( original . step , oldStep ); 21} 22}); 23 24test ( 'shared lifecycle policy blocks queued, active, disconnected and unknown status' , () => { 25const idle = { connected :true , streaming :false , stats :{ running :0 , waiting :0 }, busy :{}}; 26assert . equal ( lifecyclePolicy ( idle ). allowed , true ); 27for ( const patch of [ 28{ stats :{ running :0 , waiting :2 }}, { stats :{ running :1 , waiting :0 }}, { streaming :true }, 29{ connected :false }, { stats :null }, { stats :{ running :0 }}, { stats :{ running :0 , waiting :NaN }}, 30{ busy :{ restart :true }}, { busy :{ unload :true }}, { busy :{ 'load:fixture' :true }}, 31]) assert . equal ( lifecyclePolicy ({ ...idle , ...patch }). allowed , false , JSON . stringify ( patch )); 32assert . match ( lifecyclePolicy ({ ...idle , stats :{ running :0 , waiting :2 }}). reason , / 2 queued / ); 33assert . match ( lifecyclePolicy ({ ...idle , stats :null }). reason , / unavailable / ); 34assert . equal ( lifecyclePolicy ({ ...idle , busy :{ 'install:fixture' :true }}). allowed , true ); 35}); 36 37test ( 'blocked lifecycle writes do not change config; save-only still works' , async () => { 38const { build} = await import ( 'esbuild' ); 39const { createRequire} = await import ( 'node:module' ); 40const { outputFiles} = await build ({ entryPoints :[ fileURLToPath ( new URL ( '../src/lib/store.ts' , import . meta. url ))], bundle :true , write :false , platform :'node' , format :'cjs' }); 41const mod = { exports :{}}; new Function ( 'require' , 'module' , 'exports' , 'matchMedia' , outputFiles [ 0 ]. text )( createRequire ( import . meta. url ), mod , mod . exports ,() => ({ addEventListener (){}, matches :false })); 42const store = mod . exports ; const originalFetch = globalThis . fetch ; const requests = []; 43globalThis . fetch = async ( url , options ) => { requests . push ([ url , options ]); return new Response ( JSON . stringify ({ expert :[], beginner :{ contextLength :'recommended' }, engine :{ running :false }}),{ status :200 });}; 44try { 45for ( const stats of [ null ,{ running :0 , waiting :2 }]) { 46Object . assign ( store . getState (),{ connection :'live' , serving :stats ?{ stats} :null , library :{ mode :'real' , variants :[], loaded :'fixture' }, busy :{}}); 47await assert . rejects ( store . saveEngine ({ expert :[]}, true , true ), / unavailable | queued / ); 48assert . equal ( await store . loadVariant ( 'fixture' ), false ); 49assert . equal ( await store . unloadModel (), false ); 50assert . equal ( await store . restartEngine (), false ); 51assert . equal ( requests . length , 0 ); 52for ( const toast of store . getState (). toasts ) store . holdToast ( toast . id ); 53} 54await store . saveEngine ({ expert :[]}, true , false ); 55assert . equal ( requests . length , 1 ); assert . equal ( requests [ 0 ][ 1 ]. method , 'PATCH' ); 56} finally { globalThis . fetch = originalFetch ; for ( const toast of store . getState (). toasts ) store . holdToast ( toast . id );} 57});