okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1631a9fde
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { readFile } from 'node:fs/promises' ; 4import { build } from 'esbuild' ; 5import { KNOWN_KEYS , checkAndParseRunConfig , checkRunConfig , describeIssues , nearestKey } from '../server/run-config-check.mjs' ; 6import { NATIVE_FIELDS } from '../server/native-settings.mjs' ; 7import { openTextFile , textEditorCommand } from '../server/open-text-file.mjs' ; 8 9const good = `{ 10"exe": "/opt/strata/strata", 11"args": ["--native", "/m/a.gguf", "--max-context", "65536"], 12"env": {"STRATA_PCIE_FRAC_DEFAULT": "old"}, 13"sampling": {"temperature": 0.7, "repeat_penalty": 1.1}, 14"parallel": 2, 15"aliases": "chat, coding", 16"gpu": [0, 1], 17"layer_split": "16,32", 18"port": 8080 19}` ; 20 21test ( 'a normal run config has no issues' , () => { 22assert . deepEqual ( checkRunConfig ( good ), { ok :true , issues :[] }); 23assert . deepEqual ( checkRunConfig ( '\uFEFF{}' ), { ok :true , issues :[] }, 'Notepad’s byte-order mark is fine' ); 24}); 25 26test ( 'the checked configuration is parsed from the same captured text' , () => { 27const text = '\uFEFF{"parallel":2,"args":["--ple-io","mmap"]}' ; 28const checked = checkAndParseRunConfig ( text ); 29assert . deepEqual ( checked , { 30ok :true , 31issues :[], 32config :{ parallel :2 , args :[ '--ple-io' , 'mmap' ] }, 33}); 34const refused = checkAndParseRunConfig ( '{"parallel":99}' ); 35assert . equal ( refused . ok , false ); 36assert . equal ( refused . config , null ); 37assert . equal ( refused . issues [ 0 ]. code , 'whole-range' ); 38}); 39 40test ( 'every key the app edits is a known key' , () => { 41for ( const field of NATIVE_FIELDS ) assert . ok ( KNOWN_KEYS . has ( field . id ), field . id ); 42}); 43 44test ( 'every key setup itself writes into a run config is a known key, so a freshly set-up model shows no warning in the settings file panel (QA 0213: "args_written" was flagged as unknown)' , async () => { 45const setup = await readFile ( new URL ( '../../../setup.py' , import . meta. url ), 'utf8' ); 46const block = / ^SETUP_KEYS = frozenset\(\{ ([ ^ } ] * ) \}\) / m . exec ( setup ); 47assert . ok ( block , 'SETUP_KEYS is in setup.py' ); 48const keys = [ ...block [ 1 ]. matchAll ( / " ([ a - z_ ] + ) " / g )]. map ( m => m [ 1 ]); 49assert . ok ( keys . includes ( 'args_written' ) && keys . length > 10 ); 50for ( const key of [ ...keys , 'user_args' ]) assert . ok ( KNOWN_KEYS . has ( key ), ` ${ key } is written by setup/the app but the settings check calls it unknown` ); 51const text = JSON . stringify ({ exe :'e' , args :[ '--native' , 'm' ], args_written :[ '--native' , 'm' ], user_args :[ '--pool-workers' , '8' ], port :8080 }); 52assert . deepEqual ( checkRunConfig ( text ), { ok :true , issues :[] }); 53}); 54 55test ( 'broken JSON is reported with its line, in plain words' , () => { 56const text = '{\n "args": ["--ple-io" "mmap"],\n "port": 8080\n}' ; 57const result = checkRunConfig ( text ); 58assert . equal ( result . ok , false ); 59assert . equal ( result . issues [ 0 ]. code , 'json' ); 60assert . equal ( result . issues [ 0 ]. level , 'error' ); 61assert . equal ( result . issues [ 0 ]. line , 2 ); 62assert . match ( describeIssues ( result . issues )[ 0 ], / ^Line 2: The settings file is not valid JSON; look near line 2\. Check for a missing comma / ); 63assert . equal ( checkRunConfig ( '[1,2]' ). issues [ 0 ]. code , 'not-object' ); 64assert . equal ( checkRunConfig ( '' ). ok , false ); 65}); 66 67test ( 'an unknown key is a warning with the nearest real name, and does not hold the start back' , () => { 68const result = checkRunConfig ( '{\n "paralel": 2,\n "idle_unload_secs": 5,\n "totally_made_up_thing": 1\n}' ); 69assert . equal ( result . ok , true ); 70assert . deepEqual ( result . issues . map ( i => [ i . level , i . code , i . key , i . params . near , i . line ]), [ 71[ 'warning' , 'unknown-key-near' , 'paralel' , 'parallel' , 2 ], 72[ 'warning' , 'unknown-key-near' , 'idle_unload_secs' , 'idle_unload_s' , 3 ], 73[ 'warning' , 'unknown-key' , 'totally_made_up_thing' , '' , 4 ]]); 74assert . match ( result . issues [ 0 ]. message , / "paralel" is not a setting Strata reads, so it is ignored\. Did you mean "parallel"\? / ); 75assert . equal ( nearestKey ( 'Lazy-Load' ), 'lazy_load' ); 76assert . equal ( nearestKey ( 'zzzzzzzz' ), null ); 77}); 78 79test ( 'a bad value is an error that names the key, the line, what is allowed and what was found' , () => { 80const text = '{\n "parallel": 12,\n "lazy_load": "yes",\n "anthropic_thinking": "sometimes",\n "idle_unload_s": 1.5,\n "port": 70000,\n "args": ["--ple-io", 4],\n "env": {"PATH": "/x", "STRATA_Y": 2},\n "sampling": {"temperature": -1, "top_k": 0.5, "banana": 3},\n "gpu": "first",\n "layer_split": "32,16",\n "reasoning_effort": "max"\n}' ; 81const result = checkRunConfig ( text ); 82assert . equal ( result . ok , false ); 83const by = key => result . issues . filter ( i => i . key === key ); 84assert . deepEqual ( by ( 'parallel' ). map ( i => [ i . code , i . line , i . params . min , i . params . max , i . params . value ]), [[ 'whole-range' , 2 , 1 , 8 , '12' ]]); 85assert . equal ( by ( 'lazy_load' )[ 0 ]. code , 'bool' ); 86assert . deepEqual ([ by ( 'anthropic_thinking' )[ 0 ]. code , by ( 'anthropic_thinking' )[ 0 ]. params . choices ], [ 'choice' , 'model, on_request' ]); 87assert . equal ( by ( 'idle_unload_s' )[ 0 ]. code , 'whole-range' ); 88assert . equal ( by ( 'port' )[ 0 ]. code , 'number-range' ); 89assert . equal ( by ( 'args' )[ 0 ]. code , 'args-type' ); 90assert . deepEqual ( by ( 'env' ). map ( i => i . code ), [ 'env-value' ], 'names other than STRATA_ (PATH) are kept, only a non-text value is wrong' ); 91assert . deepEqual ( by ( 'sampling' ). map ( i => [ i . code , i . params . key ]), [[ 'number-range' , 'sampling.temperature' ], [ 'whole-range' , 'sampling.top_k' ], [ 'unknown-key' , 'sampling.banana' ]]); 92assert . equal ( by ( 'gpu' )[ 0 ]. code , 'gpu' ); 93assert . equal ( by ( 'layer_split' )[ 0 ]. code , 'layer-split' ); 94assert . equal ( by ( 'reasoning_effort' )[ 0 ]. code , 'choice' ); 95assert . ok ( result . issues . filter ( i => i . level === 'error' ). every ( i => i . message . length > 20 )); 96}); 97 98test ( 'null and empty values mean "use the default" and are never an error' , () => { 99assert . deepEqual ( checkRunConfig ( '{"parallel": null, "lazy_load": null, "reasoning_budget_tokens": ""}' ), { ok :true , issues :[] }); 100}); 101 102test ( '--host, --port and --api-key in args are a warning, not a refusal' , () => { 103const r = checkRunConfig ( '{"args": ["--port", "9"]}' ); 104assert . equal ( r . ok , true ); 105assert . equal ( r . issues [ 0 ]. code , 'args-managed' ); 106}); 107 108test ( 'the text editor: Notepad on Windows, text mode on a Mac, the system text-file program on Linux' , async () => { 109assert . deepEqual ( await textEditorCommand ( 'C:\\s\\strata-q.json' , { platform :'win32' }), [ 'notepad.exe' , [ 'C:\\s\\strata-q.json' ]]); 110assert . deepEqual ( await textEditorCommand ( '/s/strata-q.json' , { platform :'darwin' }), [ 'open' , [ '-t' , '/s/strata-q.json' ]]); 111const ask = async ( command , args ) => command === 'xdg-mime' ?'org.gnome.gedit.desktop\n' :args [ 1 ] === 'command -v gtk-launch' ?'/usr/bin/gtk-launch\n' :'' ; 112assert . deepEqual ( await textEditorCommand ( '/s/strata-q.json' , { platform :'linux' , ask}), [ '/usr/bin/gtk-launch' , [ 'org.gnome.gedit.desktop' , '/s/strata-q.json' ]]); 113const noEditor = async () => '' ; 114const seen = []; 115await openTextFile ( '/s/strata-q.json' , { platform :'linux' , ask :noEditor , recorder :seen }); 116assert . deepEqual ( seen , [[ 'xdg-open' , '/s/strata-q.json' ]], 'with no text editor known the file’s own program opens it' ); 117}); 118 119test ( 'every issue sentence exists in all nine languages and keeps its slots' , async () => { 120const languages = [ 'en' , 'zh' , 'ja' , 'de' , 'es' , 'ru' , 'pt' , 'ko' , 'fr' ]; 121const compiled = await build ({ stdin :{ contents :"export * from './src/lib/run-config-issues'; export * from './src/lib/i18n';" , resolveDir :process . cwd () }, bundle :true , write :false , platform :'node' , format :'esm' }); 122const { issueText, setTranslationLanguage, loadCatalog} = await import ( `data:text/javascript;base64, ${ Buffer . from ( compiled . outputFiles [ 0 ]. text ). toString ( 'base64' )} ` ); 123for ( const l of [ 'zh' , 'ja' , 'de' , 'es' , 'ru' , 'pt' , 'ko' , 'fr' ]) await loadCatalog ( l ); // the non-English catalogs load on first use (src/lib/i18n.ts loadCatalog); load them before the first tr() 124const samples = [ 125{ code :'json' , line :3 , params :{ line :3 } }, { code :'json' , line :null , params :{ line :0 } }, { code :'not-object' , params :{} }, 126{ code :'unknown-key-near' , params :{ key :'paralel' , near :'parallel' } }, { code :'unknown-key' , params :{ key :'zzz' } }, 127{ code :'args-type' , params :{} }, { code :'args-managed' , params :{} }, { code :'env-type' , params :{} }, 128{ code :'env-value' , params :{ name :'STRATA_X' } }, { code :'sampling-type' , params :{} }, { code :'bool' , params :{ key :'lazy_load' , value :'"yes"' } }, 129{ code :'number-range' , params :{ key :'port' , min :1 , max :65535 , value :'0' } }, { code :'whole-range' , params :{ key :'parallel' , min :1 , max :8 , value :'12' } }, 130{ code :'choice' , params :{ key :'gpu_order' , choices :'auto, as_given' , value :'"x"' } }, { code :'text' , params :{ key :'aliases' } }, 131{ code :'layer-split' , params :{} }, { code :'gpu' , params :{} }, 132]. map ( s => ({ level :'error' , key :'k' , message :'ENGLISH FALLBACK' , line :s . line === undefined ?5 :s . line , ...s })); 133const english = {}; 134setTranslationLanguage ( 'en' ); 135for ( const s of samples ) english [ ` ${ s . code } : ${ s . line } ` ] = issueText ( s ); 136for ( const s of samples ) assert . notEqual ( english [ ` ${ s . code } : ${ s . line } ` ], 'ENGLISH FALLBACK' , s . code ); 137for ( const language of languages . filter ( l => l !== 'en' )) { 138setTranslationLanguage ( language ); 139for ( const s of samples ) { 140const text = issueText ( s ); 141assert . notEqual ( text , english [ ` ${ s . code } : ${ s . line } ` ], ` ${ language } : ${ s . code } is not translated` ); 142assert . ok ( ! /\{\d + \} / . test ( text ), ` ${ language } : ${ s . code } left a slot` ); 143} 144} 145setTranslationLanguage ( 'en' ); 146}); 147 148test ( 'the notes the app itself writes into the file (ui_resource_baseline, ui_vision_device, ...) are not reported' , () => { 149assert . deepEqual ( checkRunConfig ( '{"ui_resource_baseline": {"parallel": 2}, "ui_vision_device": "cpu", "ui_resource_profile": "work"}' ), { ok :true , issues :[] }); 150});