okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a1cc285fa
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { mkdtempSync , readFileSync , rmSync } from 'node:fs' ; 4import { tmpdir } from 'node:os' ; 5import { join , resolve } from 'node:path' ; 6import { createRequire } from 'node:module' ; 7import { build } from 'esbuild' ; 8 9/* The installed app crashed on its first screen ("Cannot read properties of undefined (reading 'family')") when the 10catalog it was given had no `recommendation` and no families. Render the real GuidedSetup and NativeModels components 11(react-dom/server) with the state libraries replaced by fixed values, for every shape of an empty or failed catalog. */ 12const root = resolve ( import . meta. dirname , '..' ); 13/** Every export of the real module becomes a harmless no-op, except the few that carry the fixed state. */ 14function stubFor ( file , overrides ){ 15const source = readFileSync ( resolve ( root , 'src/lib' , file ), 'utf8' ); 16const names = new Set ([ ...source . matchAll ( / export\s + (?: async\s + ) ? (?: function\* ?| const | let | var | class ) \s + ([ A - Za - z_$ ][ \w $ ] * ) / g )]. map ( m => m [ 1 ])); 17for ( const m of source . matchAll ( / export\s * \{ ([ ^ } ] * ) \} / g )) for ( const part of m [ 1 ]. split ( ',' )) names . add ( part . split ( / \s + as\s +/ ). pop (). trim ()); 18names . delete ( '' ); 19const lines = [ ...names ]. filter ( n =>! ( n in overrides )). map ( n => `export const ${ n } =(...a)=>Promise.resolve({});` ); 20for ( const [ n , v ] of Object . entries ( overrides )) lines . push ( `export const ${ n } = ${ v } ;` ); 21return lines . join ( String . fromCharCode ( 10 )); 22} 23const stubs = { 24native :stubFor ( 'native.ts' ,{ useNative :'()=>globalThis.__native' , nativeState :'()=>globalThis.__native' }), 25store :stubFor ( 'store.ts' ,{ useStore :'sel=>sel(globalThis.__store)' }), 26client :stubFor ( 'client.ts' ,{}), 27router :stubFor ( 'router.ts' ,{}), 28'kept-state' :stubFor ( 'kept-state.ts' ,{ useKept :'(key,initial)=>[globalThis.__kept?.[key]??initial,()=>{}]' }), 29}; 30 31let dir , mod ; 32async function load (){ 33if ( mod ) return mod ; 34dir = mkdtempSync ( join ( tmpdir (), 'strata-gs-' )); 35const stubPlugin = { name :'stubs' , setup ( b ){ 36b . onResolve ({ filter :/ \/lib\/ ( native | store | client | router | kept-state ) $ / }, args => ({ path :args . path . split ( '/' ). pop (), namespace :'stub' })); 37b . onLoad ({ filter :/ . */ , namespace :'stub' }, args => ({ contents :stubs [ args . path ], loader :'js' , resolveDir :root })); 38}}; 39const entry = join ( dir , 'entry.tsx' ); 40const out = join ( dir , 'out.cjs' ); 41await build ({ 42stdin :{ contents :`export {GuidedSetup} from ${ JSON . stringify ( resolve ( root , 'src/components/GuidedSetup.tsx' ))} ; 43export {NativeModels} from ${ JSON . stringify ( resolve ( root , 'src/components/NativeModels.tsx' ))} ; 44export {renderToString} from 'react-dom/server'; export {createElement} from 'react';` , resolveDir :root , sourcefile :'entry.tsx' , loader :'tsx' }, 45bundle :true , format :'cjs' , platform :'node' , outfile :out , jsx :'automatic' , logLevel :'silent' , plugins :[ stubPlugin ], 46loader :{ '.svg' :'dataurl' , '.png' :'dataurl' , '.css' :'empty' }, 47}); 48globalThis . window ??= globalThis ; globalThis . matchMedia ??= () => ({ matches :false , addEventListener (){}, removeEventListener (){}}); 49mod = createRequire ( import . meta. url )( out ); 50return mod ; 51} 52test . after (() => { if ( dir ) rmSync ( dir ,{ recursive :true , force :true });}); 53 54const store = ( complete = false ) => ({ setup :{ complete, choices :{}, app :{ modelsDir :'' }}, hardware :null }); 55const base = { available :true , repo :'' , summary :'' , error :null , installed :{ models :[], data_folder :'' , engine :{ version :null }}, hardware :null , instances :[], jobs :[], defaultId :null , fields :[], capabilities :{}}; 56const catalogs = { 57'no catalog at all' :null , 58'the packaged host before it had a catalog' :{ families :[], sizes :[]}, 59'no recommendation' :{ families :[], contexts :[ 32768 ]}, 60'recommendation with nothing in it' :{ families :[], contexts :[], recommendation :{ family :null , model :null }}, 61'no families key' :{ contexts :[]}, 62'a family without sizes' :{ families :[{ family :'qwen' , title :'Qwen3.8-Flash-Next' , sizes :[]}], contexts :[ 32768 ]}, 63}; 64 65for ( const [ name , catalog ] of Object . entries ( catalogs )){ 66for ( const component of [ 'GuidedSetup' , 'NativeModels' ]){ 67test ( ` ${ component } renders with ${ name } ` , async () => { 68const m = await load (); 69globalThis . __native = { ...base , catalog}; globalThis . __store = store (); 70let html ; 71assert . doesNotThrow (() => { html = m . renderToString ( m . createElement ( m [ component ]));}); 72assert . equal ( typeof html , 'string' ); 73}); 74} 75} 76 77test ( 'GuidedSetup lists the real catalog when it has one' , async () => { 78const m = await load (); 79globalThis . __native = { ...base , catalog :{ families :[{ family :'qwen' , title :'Qwen3.8-Flash-Next' , about :'' , images :true , experimental :false , sizes :[ 80{ id :'iq3_xxs' , model :'IQ3_XXS' , download_gb :75.8 , ram_gb :60 , about :'' , on_this_pc :'fits' , installed :false }]}], contexts :[ 32768 ], 81recommendation :{ family :'qwen' , model :'IQ3_XXS' , context :32768 }, context_note :'' }}; 82globalThis . __store = { ...store (), setup :{ complete :false , choices :{ onboarding :{ step :2 , manual :false }}, app :{ modelsDir :'' }}}; 83const html = m . renderToString ( m . createElement ( m . GuidedSetup )); 84assert . match ( html , / IQ3_XXS / ); 85}); 86 87const failedJob = ( id , model , family ) => ({ id, kind :'install' , model, family, state :'error' , 88startedAt :'2026-01-01T00:00:00.000Z' , summary :'Previous preparation failed' , 89error :'Data_dir C:\\Users\\Example\\.strata\\models: a new data folder must be named Strata' , result :null }); 90async function renderModelJobs ( jobs , kept = {}){ 91const m = await load (); 92const size = ( id , model ) => ({ id, model, download_gb :50 , ram_needed_gb :48 , experts_gb :30 , about :'' , on_this_pc :'fits' , installed :false }); 93globalThis . __native = { ...base , jobs, catalog :{ contexts :[ 32768 , 65536 ], families :[ 94{ family :'qwen' , title :'Qwen3.8-Flash-Next' , about :'' , images :false , experimental :false , sizes :[ size ( 'iq3_s' , 'IQ3_S' )]}, 95{ family :'swift' , title :'Qwen3.8-Flash-Next (Swift)' , about :'' , images :false , experimental :false , sizes :[ size ( 'swift-iq3_s' , 'IQ3_S' )]}, 96]}}; 97globalThis . __store = store (); globalThis . __kept = kept ; 98try { return m . renderToString ( m . createElement ( m . NativeModels ));} finally { globalThis . __kept = {};} 99} 100 101test ( 'NativeModels does not attach another family’s failed preparation to the selected version' , async () => { 102const job = failedJob ( 'old-swift' , 'IQ3_S' , 'swift' ), jobs = [ job ]; 103assert . doesNotMatch ( await renderModelJobs ( jobs ), / Data_dir / ); 104assert . equal ( jobs [ 0 ], job , 'the job is retained for diagnostics' ); 105}); 106 107test ( 'NativeModels ignores a remembered job after selecting a different model' , async () => { 108assert . doesNotMatch ( await renderModelJobs ([ failedJob ( 'old-swift' , 'swift-iq3_s' , 'swift' )],{ 'models.job' :'old-swift' }), / Data_dir / ); 109}); 110 111test ( 'NativeModels still shows the selected canonical model’s real failure' , async () => { 112assert . match ( await renderModelJobs ([ failedJob ( 'current' , 'iq3_s' )]), / Data_dir / ); 113}); 114 115test ( 'NativeModels identifies aliases by source family and keeps matching remembered errors' , async () => { 116assert . match ( await renderModelJobs ([ failedJob ( 'current' , 'IQ3_S' , 'qwen' )],{ 'models.job' :'current' }), / Data_dir / ); 117assert . doesNotMatch ( await renderModelJobs ([ failedJob ( 'ambiguous' , 'IQ3_S' )]), / Data_dir / ); 118}); 119 120test ( 'NativeModels falls back to the latest job for the selected version, not an unrelated remembered job' , async () => { 121const own = failedJob ( 'own' , 'iq3_s' ), other = { ...failedJob ( 'other' , 'swift-iq3_s' , 'swift' ), error :'Other model error' }; 122const html = await renderModelJobs ([ own , other ],{ 'models.job' :'other' }); 123assert . match ( html , / Data_dir / ); assert . doesNotMatch ( html , / Other model error / ); 124}); 125 126/* The first-run model step starts on a version that is already on this computer, else on the catalog's recommendation, never on the first 127size in the list (it used to be filled once at mount, before the catalog had arrived: Q2_0, a 66 GB download, beside an installed IQ3_S). */ 128const sizeRow = ( id , model ) => ({ id, model, download_gb :50 , ram_needed_gb :48 , experts_gb :30 , about :'' , on_this_pc :'fits' , installed :false }); 129async function selectedStart ( readyIds , recommendation = 'IQ3_S' , onboarding = { step :2 , manual :false }){ 130const m = await load (); 131globalThis . __native = { ...base , installed :{ ...base . installed , models :readyIds . map ( id => ({ model :id , ready :true , context :16384 }))}, catalog :{ contexts :[ 32768 ], 132families :[{ family :'qwen' , title :'Qwen3.8-Flash-Next' , about :'' , images :false , experimental :false , sizes :[ sizeRow ( 'q2_0' , 'Q2_0' ), sizeRow ( 'iq3_s' , 'IQ3_S' )]}, 133{ family :'swift' , title :'Qwen3.8-Flash-Next (Swift)' , about :'' , images :false , experimental :false , sizes :[ sizeRow ( 'swift-iq2_xs' , 'IQ2_XS' )]}], 134recommendation :{ family :'qwen' , model :recommendation , context :32768 }}}; 135globalThis . __store = { ...store (), setup :{ complete :false , choices :{ onboarding}, app :{ modelsDir :'' }}}; 136const html = m . renderToString ( m . createElement ( m . GuidedSetup )); 137return [ ...html . matchAll ( / <button [ ^ > ] * role="radio" [ ^ > ] * aria-checked="true" [ ^ > ] * > ( . * ? ) <\/button> / gs )]. map ( x => x [ 1 ]. replace ( / < [ ^ > ] * > / g , ' ' ). replace ( / \s +/ g , ' ' ). trim ()); 138} 139test ( 'GuidedSetup starts on the recommended version when nothing is installed, not on the first size' , async () => { 140const chosen = await selectedStart ([]); 141assert . equal ( chosen . length , 1 ); assert . match ( chosen [ 0 ], / IQ3_S / ); assert . doesNotMatch ( chosen [ 0 ], / Q2_0 / ); 142}); 143test ( 'GuidedSetup starts on a version that is installed and ready, even when the recommendation is another' , async () => { 144const chosen = await selectedStart ([ 'swift-iq2_xs' ]); 145assert . equal ( chosen . length , 1 ); assert . match ( chosen [ 0 ], / IQ2_XS / ); 146}); 147test ( 'GuidedSetup prefers the recommended version when it is among the installed ones' , async () => { 148const chosen = await selectedStart ([ 'q2_0' , 'iq3_s' ]); 149assert . equal ( chosen . length , 1 ); assert . match ( chosen [ 0 ], / IQ3_S / ); 150}); 151test ( 'GuidedSetup keeps a version the person already chose over the computed start' , async () => { 152const chosen = await selectedStart ([ 'iq3_s' ], 'IQ3_S' ,{ step :2 , manual :false , family :'qwen3.8' , size :'swift-iq2_xs' }); 153assert . equal ( chosen . length , 1 ); assert . match ( chosen [ 0 ], / IQ2_XS / ); 154});