okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3cabb9e3f
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { readFileSync , writeFileSync } from 'node:fs' ; 4import fs from 'node:fs' ; 5import { syncBuiltinESMExports } from 'node:module' ; 6import { join } from 'node:path' ; 7import { nativeFixture , finishJob } from './helpers/native-fixture.mjs' ; 8 9const configOf = f => join ( f . repo , 'strata-iq2_xs.json' ); 10 11for ( const validFirst of [ true , false ]) test ( `a load ${ validFirst ? 'uses a valid' : 'refuses an invalid' } captured config even if the file changes after reading` , async t => { 12const f = nativeFixture (); 13try { 14await f . control . refresh ( true ); 15const path = configOf ( f ), good = JSON . parse ( readFileSync ( path , 'utf8' )); 16good . parallel = 2 ; 17const valid = '\uFEFF' + JSON . stringify ( good ), invalid = JSON . stringify ({ ...good , parallel :99 }); 18writeFileSync ( path , validFirst ?valid :invalid ); 19// Let status discovery finish normally. Replace the real file immediately after 20// the first read following the model-path check, before a second read can occur. 21const realpath = fs . realpathSync , read = fs . readFileSync ; 22let capture = false ; 23t . mock . method ( fs , 'realpathSync' , ( ...args ) => { 24const result = realpath ( ...args ); 25if ( args [ 0 ] === path ) capture = true ; 26return result ; 27}); 28t . mock . method ( fs , 'readFileSync' , ( ...args ) => { 29const result = read ( ...args ); 30if ( capture && args [ 0 ] === path ) { 31capture = false ; 32writeFileSync ( path , validFirst ?invalid :valid ); 33} 34return result ; 35}); 36syncBuiltinESMExports (); 37const done = await finishJob ( f . control , f . control . operation ( 'load' , { model :'iq2_xs' }). id ); 38assert . equal ( done . state , validFirst ?'done' :'error' , done . error ); 39if ( validFirst ) { 40const instance = f . control . resolveInstance ( done . result . instanceId ); 41assert . equal ( JSON . parse ( read ( instance . configPath , 'utf8' )). parallel , 2 ); 42} else { 43assert . match ( done . error , / "parallel" must be a whole number / ); 44assert . equal ( f . control . state (). instances . length , 0 ); 45assert . equal ( f . calls . some ( c => c . args . some ( a => / serve [ / \\ ] server\.py$ / . test ( String ( a )))), false ); 46} 47} finally { 48t . mock . restoreAll (); 49syncBuiltinESMExports (); 50await f . cleanup (); 51} 52}); 53 54test ( 'the settings file is found, checked, and opened in the text editor' , async () => { 55const f = nativeFixture (); 56try { 57await f . control . refresh (); 58const info = f . control . settingsFile ( 'iq2_xs' ); 59assert . equal ( info . path , configOf ( f )); 60assert . equal ( info . name , 'strata-iq2_xs.json' ); 61assert . equal ( info . ok , true ); 62assert . match ( info . note , / apply the next time the model starts / ); 63assert . ok ( info . issues . every ( i => i . level === 'warning' ), 'the fixture’s own extra key is a warning at most' ); 64const seen = []; 65const opened = await f . control . openSettingsFile ( 'iq2_xs' , { recorder :seen , platform :'win32' }); 66assert . deepEqual ( seen , [[ 'notepad.exe' , info . path ]]); 67assert . equal ( opened . name , 'strata-iq2_xs.json' ); 68assert . throws (() => f . control . settingsFile ( '../etc/passwd' ), / installed model / ); 69} finally { await f . cleanup (); } 70}); 71 72test ( 'a mistake in the file holds the start back with its line; a typo’d key only warns' , async () => { 73const f = nativeFixture (); 74try { 75await f . control . refresh ( true ); 76const path = configOf ( f ), good = readFileSync ( path , 'utf8' ); 77// 1. a syntax slip (missing comma): not valid JSON, said with the line, and nothing is started 78writeFileSync ( path , JSON . stringify ( JSON . parse ( good ), null , 2 ). replace ( '"gpu": 0,' , '"gpu": 0\n "x": 1,' )); 79assert . equal ( f . control . settingsFile ( 'iq2_xs' ). ok , false ); 80let done = await finishJob ( f . control , f . control . operation ( 'load' , { model :'iq2_xs' }). id ); 81assert . equal ( done . state , 'error' ); 82assert . match ( done . error , / ^The settings file for this model has a mistake, so the model was not started\. Line \d + : The settings file is not valid JSON / ); 83assert . equal ( f . control . state (). instances . length , 0 , 'no engine was started' ); 84assert . equal ( f . calls . filter ( c => / strata\.py | serve / . test ( JSON . stringify ( c . args ))). length , 0 ); 85// 2. a bad value 86const config = JSON . parse ( good ); config . parallel = 99 ; config . lazy_load = 'yes' ; 87writeFileSync ( path , JSON . stringify ( config , null , 2 )); 88done = await finishJob ( f . control , f . control . operation ( 'load' , { model :'iq2_xs' }). id ); 89assert . equal ( done . state , 'error' ); 90assert . match ( done . error , / "parallel" must be a whole number from 1 to 8, not 99 / ); 91assert . match ( done . error , / "lazy_load" must be true or false, not "yes" / ); 92// 3. an unknown key only warns and the model loads 93const typo = JSON . parse ( good ); typo . paralel = 2 ; 94writeFileSync ( path , JSON . stringify ( typo , null , 2 )); 95const info = f . control . settingsFile ( 'iq2_xs' ); 96assert . equal ( info . ok , true ); 97assert . ok ( info . issues . some ( i => i . code === 'unknown-key-near' && i . params . near === 'parallel' )); 98done = await finishJob ( f . control , f . control . operation ( 'load' , { model :'iq2_xs' }). id ); 99assert . equal ( done . state , 'done' , done . error ); 100} finally { await f . cleanup (); } 101});