okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
34e7c5cb1
main
1"""Tests for what setup keeps in a model's run config when it runs again (#629), the browser choice (#609) and the 2image-token flag (#625). Every outside effect is mocked (tools/test_setup_golden.py's install()). 3 4python -m unittest tools.test_setup_config 5""" 6from __future__import annotations 7 8import contextlib 9import io 10import json 11import sys 12import tempfile 13import unittest 14import unittest .mock 15from pathlib import Path 16 17ROOT = Path (__file__ ).resolve ().parents [1 ] 18sys .path .insert (0 ,str (ROOT )) 19sys .path .insert (0 ,str (ROOT / "tools" )) 20import setup # noqa: E402 21from test_setup_golden import PROFILES ,install # noqa: E402 22 23 24def quiet (fn ,* args ,** kw ): 25out = io .StringIO () 26with contextlib .redirect_stdout (out ): 27return fn (* args ,** kw ),out .getvalue () 28 29 30USER = {"sampling" : {"temperature" :1.0 ,"top_p" :0.95 ,"top_k" :20 ,"min_p" :0.0 }, 31"mcp_servers" : {"files" : {"command" :"npx" ,"args" : ["-y" ,"@modelcontextprotocol/server-filesystem" ,"." ]}}, 32"mcp" : {"timeout_s" :60 ,"max_result_chars" :16000 ,"max_rounds" :8 }, 33"cors_origins" : ["http://localhost:3000" ],"reasoning_effort" :"low" } 34 35 36class CarryOver (unittest .TestCase ): 37"""#629: setup run again (a different --context) keeps the keys it does not write and keeps the old file as .bak.""" 38 39def setUp (self ): 40self .ram ,self .found = PROFILES ["64GB-1x32GB" ] 41 42def run_setup (self ,* extra ,configs = ()): 43return install (self .ram ,self .found , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" ,* extra ], 44configs = configs ) 45 46def test_user_keys_survive_a_rerun (self ): 47code ,out ,first ,_ = self .run_setup ("--context" ,"65536" ) 48self .assertEqual (code ,0 ,out ) 49old = {** first ,** USER ,"args" :first ["args" ]+ ["--hand-option" ,"7" ]} 50code ,out ,cfg ,_ = self .run_setup ("--context" ,"131072" ,configs = [("strata-q2_0.json" ,old )]) 51self .assertEqual (code ,0 ,out ) 52for k ,v in USER .items (): 53self .assertEqual (cfg [k ],v ,k ) 54a = cfg ["args" ] 55self .assertEqual (a [a .index ("--max-context" )+ 1 ],"131072" )# the new choice is used 56self .assertEqual (a [a .index ("--hand-option" )+ 1 ],"7" )# an option added by hand is kept (#3216)... 57self .assertEqual (cfg ["user_args" ], ["--hand-option" ,"7" ])# ...and recorded as the person's 58self .assertIn ("kept from your earlier strata-q2_0.json: sampling, mcp_servers, mcp, cors_origins, " 59"reasoning_effort" ,out ) 60self .assertIn ("kept as strata-q2_0.json.bak" ,out ) 61self .assertIn ("args --hand-option" ,out )# ...and the run says it was kept 62 63def test_a_first_install_is_unchanged (self ): 64code ,out ,cfg ,_ = self .run_setup () 65self .assertEqual (code ,0 ,out ) 66self .assertNotIn ("kept from" ,out ) 67self .assertNotIn (".bak" ,out ) 68self .assertFalse (set (cfg )- setup .SETUP_KEYS )# setup writes only its own keys 69 70def test_setup_keys_are_rewritten (self ): 71code ,out ,first ,_ = self .run_setup () 72old = {** first ,"model_name" :"renamed" ,"port" :9999 ,"draft_vocab" :"en" ,"host" :"0.0.0.0" } 73code ,out ,cfg ,_ = self .run_setup (configs = [("strata-q2_0.json" ,old )]) 74self .assertEqual (code ,0 ,out ) 75self .assertEqual (cfg ["model_name" ],first ["model_name" ]) 76self .assertEqual (cfg ["port" ],first ["port" ]) 77self .assertNotIn ("host" ,cfg )# as before: --host is given to setup 78self .assertEqual (cfg ["draft_vocab" ],"en" )# kept by its own rule (saved_draft_vocab) 79self .assertNotIn ("kept from" ,out ) 80 81def test_write_setup_config (self ): 82with tempfile .TemporaryDirectory ()as d : 83p = Path (d )/ "strata-x.json" 84mm = Path (d )/ "mmproj-Q8_0.gguf" 85mm .write_bytes (b"" ) 86old = {"exe" :"e" ,"args" : ["--kv" ,"int8" ,"--threads" ,"8" ],"sampling" : {"temperature" :0.7 }, 87"env" : {"STRATA_RESIDENT_PIN" :"0" ,"MY_VAR" :"1" }, 88"vision" : {"exe" :"v" ,"mmproj" :str (mm ),"gpu" :False ,"max_tokens" :300 ,"min_tokens" :64 }} 89p .write_text (json .dumps (old ),encoding = "utf-8" ) 90new = {"exe" :"e2" ,"args" : ["--kv" ,"int8" ], 91"vision" : {"exe" :"v2" ,"mmproj" :str (Path (d )/ "mmproj-BF16.gguf" ),"gpu" :False ,"max_tokens" :300 }} 92_ ,out = quiet (setup .write_setup_config ,p ,new ) 93got = json .loads (p .read_text (encoding = "utf-8" )) 94self .assertEqual (got ["sampling" ], {"temperature" :0.7 }) 95self .assertEqual (got ["env" ], {"MY_VAR" :"1" })# setup's own env entry is setup's to write 96self .assertEqual (got ["vision" ]["mmproj" ],str (mm ))# a Q8_0 mmproj of the user's own (#625) 97self .assertEqual (got ["vision" ]["min_tokens" ],64 ) 98self .assertEqual (got ["exe" ],"e2" ) 99self .assertEqual (json .loads ((Path (d )/ "strata-x.json.bak" ).read_text (encoding = "utf-8" )),old ) 100self .assertIn ("--threads" ,out ) 101# the same again: nothing changes, so no new copy is made 102 (Path (d )/ "strata-x.json.bak" ).unlink () 103again = {"exe" :"e2" ,"args" : ["--kv" ,"int8" ], 104"vision" : {"exe" :"v2" ,"mmproj" :str (Path (d )/ "mmproj-BF16.gguf" ),"gpu" :False , 105"max_tokens" :300 }} 106quiet (setup .write_setup_config ,p ,again ) 107self .assertFalse ((Path (d )/ "strata-x.json.bak" ).exists ()) 108self .assertEqual (json .loads (p .read_text (encoding = "utf-8" )),got ) 109 110def test_an_unreadable_earlier_config_is_replaced_with_a_copy (self ): 111with tempfile .TemporaryDirectory ()as d : 112p = Path (d )/ "strata-x.json" 113p .write_text ("{cut off" ,encoding = "utf-8" ) 114quiet (setup .write_setup_config ,p , {"exe" :"e" ,"args" : []}) 115self .assertEqual (json .loads (p .read_text (encoding = "utf-8" )), {"exe" :"e" ,"args" : [],"args_written" : []}) 116self .assertEqual ((Path (d )/ "strata-x.json.bak" ).read_text (encoding = "utf-8" ),"{cut off" ) 117 118def test_display_tip_and_typed_ahead_flush (self ): 119# #779: the tip only when nvidia-smi says the card drives a display; #841: typed-ahead keys are dropped on a tty 120with unittest .mock .patch .object (setup ,"out" ,lambda cmd :"Enabled\n" ): 121self .assertTrue (setup .gpu_drives_display ({"index" :0 })) 122with unittest .mock .patch .object (setup ,"out" ,lambda cmd :"Disabled\n" ),unittest .mock .patch .object (setup ,"out" )as o : 123o .return_value = "Disabled\n" 124self .assertFalse (setup .gpu_drives_display ({"index" :1 })) 125self .assertIn ("1" ,o .call_args [0 ][0 ]) 126with unittest .mock .patch .object (setup ,"out" ,lambda cmd :"" ): 127self .assertFalse (setup .gpu_drives_display ({"index" :0 })) 128with unittest .mock .patch .object (setup .sys ,"stdin" )as stdin : 129stdin .isatty .return_value = False 130setup .flush_typed_ahead ()# a pipe: nothing touched 131stdin .fileno .assert_not_called () 132 133def test_a_learned_expert_profile_is_carried (self ): 134# #775: --expert-profile / --expert-profile-save added by hand survive a setup run 135with tempfile .TemporaryDirectory ()as d : 136mine = Path (d )/ "learned.bin" 137mine .write_bytes (b"x" ) 138old = {"args" : ["--kv" ,"int8" ,"--expert-profile" ,str (mine ),"--expert-profile-save" ,str (mine )]} 139new = {"args" : ["--kv" ,"int8" ,"--expert-profile" ,"/data/expert-profile.bin" ,"--expert-cache" ,"auto" ]} 140self .assertEqual (setup .carry_over (old ,new ), ["args --expert-profile" ,"args --expert-profile-save" ]) 141self .assertEqual (setup .flag_value (new ["args" ],"--expert-profile" ),str (mine )) 142self .assertEqual (setup .flag_value (new ["args" ],"--expert-profile-save" ),str (mine )) 143# the shipped profile named again, or a file that is gone: setup's own stays 144gone = {"args" : ["--expert-profile" ,str (Path (d )/ "gone.bin" )]} 145new = {"args" : ["--expert-profile" ,"/data/expert-profile.bin" ]} 146self .assertEqual (setup .carry_over (gone ,new ), []) 147self .assertEqual (new ["args" ], ["--expert-profile" ,"/data/expert-profile.bin" ]) 148same = {"args" : ["--expert-profile" ,"/old/expert-profile.bin" ]} 149self .assertEqual (setup .carry_over (same ,new ), []) 150 151def test_mtp_off_is_the_users_and_survives_a_setup_run_that_writes_the_draft_layer_back (self ): 152old = {"args" : ["--spec" ,"4" ,"--spec-min-p" ,"0.5" ],"mtp" :False } 153new = {"args" : ["--spec" ,"4" ,"--spec-min-p" ,"0.5" ,"--mtp" ,"/m/rt" ,"--max-context" ,"32768" ]} 154kept = setup .carry_over (old ,new ) 155self .assertIs (new ["mtp" ],False ) 156self .assertEqual (new ["args" ], ["--spec" ,"4" ,"--max-context" ,"32768" ]) 157self .assertTrue (any (k .startswith ("mtp off" )for k in kept )) 158on = {"args" : ["--mtp" ,"/m/rt" ]} 159setup .carry_over ({"args" : [],"mtp" :True },on ) 160self .assertEqual (on ["args" ], ["--mtp" ,"/m/rt" ])# on: setup's own arguments stay 161 162def test_a_missing_mmproj_is_not_carried (self ): 163old = {"vision" : {"mmproj" :"/nowhere/mmproj-Q8_0.gguf" ,"gpu" :True }} 164new = {"vision" : {"mmproj" :"/data/mmproj-BF16.gguf" ,"gpu" :True }} 165self .assertEqual (setup .carry_over (old ,new ), []) 166self .assertEqual (new ["vision" ]["mmproj" ],"/data/mmproj-BF16.gguf" ) 167 168def test_a_copy_set_up_like_the_last_one_keeps_them_too (self ): 169with tempfile .TemporaryDirectory ()as d : 170src = Path (d )/ "other" / "strata-x.json" 171src .parent .mkdir () 172src .write_text (json .dumps ({"exe" :"old" ,"mcp_servers" : {"a" : {"command" :"x" }}}),encoding = "utf-8" ) 173p = Path (d )/ "strata-x.json" 174quiet (setup .write_setup_config ,p , {"exe" :"new" },src ) 175self .assertEqual (json .loads (p .read_text (encoding = "utf-8" )), 176 {"exe" :"new" ,"mcp_servers" : {"a" : {"command" :"x" }}}) 177self .assertFalse ((Path (d )/ "strata-x.json.bak" ).exists ())# nothing here was replaced 178 179 180class NoBrowser (unittest .TestCase ): 181"""#609 (#631): --no-browser keeps the chat page from opening: "open_browser": false in the run config, no --open 182on the start's server command or in run-<model>.bat/.sh. Without the flag nothing changes.""" 183 184def setUp (self ): 185self .ram ,self .found = PROFILES ["64GB-1x32GB" ] 186 187def run_setup (self ,* extra ,configs = ()): 188return install (self .ram ,self .found , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" ,* extra ], 189configs = configs ) 190 191def test_at_setup (self ): 192code ,out ,cfg ,_ = self .run_setup ("--no-browser" ) 193self .assertEqual (code ,0 ,out ) 194self .assertIs (cfg ["open_browser" ],False ) 195code ,out ,cfg ,_ = self .run_setup () 196self .assertNotIn ("open_browser" ,cfg )# not given: the config as before 197# setup run again without the flag keeps the choice (#629); --browser undoes it 198code ,out ,cfg ,_ = self .run_setup (configs = [("strata-q2_0.json" , {"open_browser" :False })]) 199self .assertIs (cfg ["open_browser" ],False ) 200code ,out ,cfg ,_ = self .run_setup ("--browser" ,configs = [("strata-q2_0.json" , {"open_browser" :False })]) 201self .assertIs (cfg ["open_browser" ],True ) 202 203def test_run_script (self ): 204with tempfile .TemporaryDirectory ()as d ,unittest .mock .patch .object (setup ,"ROOT" ,Path (d )): 205text = lambda p :p .read_text (encoding = "utf-8" )# noqa: E731 206self .assertIn ('"--open"' ,text (setup .write_run_script ("Q2_0" ,Path (d )/ "strata-q2_0.json" ,8080 ))) 207script = setup .write_run_script ("Q2_0" ,Path (d )/ "strata-q2_0.json" ,8080 ,False ) 208self .assertNotIn ("--open" ,text (script )) 209self .assertIn ('"--port" "8080"' ,text (script )) 210 211def start_cmd (self ,cfg ,keep = None ): 212with tempfile .TemporaryDirectory ()as d : 213exe = Path (d )/ "strata.exe" 214exe .write_bytes (b"" ) 215p = Path (d )/ "strata-q2_0.json" 216p .write_text (json .dumps ({"exe" :str (exe ),"args" : ["--kv" ,"int8" ],"gpu" :0 ,"gpus_asked" :True ,** cfg })) 217call = unittest .mock .Mock (return_value = 0 ) 218with unittest .mock .patch .object (setup ,"gpus" ,lambda :self .found ), \ 219unittest .mock .patch .object (setup ,"is_wsl" ,lambda :False ), \ 220unittest .mock .patch .object (setup ,"ensure_engine_for" ,lambda cards ,path ,c ,yes :c ), \ 221unittest .mock .patch .object (setup .subprocess ,"call" ,call ): 222_ ,out = quiet (setup .start ,p ,None ,None ,True ,True ,None ,keep ) 223return call .call_args [0 ][0 ],out ,json .loads (p .read_text ()) 224 225def test_on_a_start (self ): 226cmd ,out ,_ = self .start_cmd ({}) 227self .assertIn ("--open" ,cmd )# the default, as before 228self .assertIn ("the browser opens when it is ready" ,out ) 229cmd ,out ,_ = self .start_cmd ({"open_browser" :False }) 230self .assertNotIn ("--open" ,cmd ) 231self .assertNotIn ("the browser opens" ,out ) 232self .assertIn ("no browser" ,out )# the Settings line says so 233cmd ,out ,saved = self .start_cmd ({}, {"open_browser" :False })# START-HERE --no-browser: kept from now on 234self .assertNotIn ("--open" ,cmd ) 235self .assertIs (saved ["open_browser" ],False ) 236self .assertIn ("saved for this model: no browser" ,out ) 237cmd ,out ,saved = self .start_cmd ({"open_browser" :False }, {"open_browser" :True }) 238self .assertIn ("--open" ,cmd ) 239self .assertIs (saved ["open_browser" ],True ) 240 241 242class VisionTokens (unittest .TestCase ): 243"""#625: --vision-tokens N sets the config's vision.max_tokens (the most image tokens a picture becomes); a setup 244run again keeps it for the same encoder device; without it the device's default, as before.""" 245 246def setUp (self ): 247self .ram ,self .found = PROFILES ["64GB-1x32GB" ] 248 249def run_setup (self ,* extra ,configs = ()): 250return install (self .ram ,self .found , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" ,* extra ], 251configs = configs ) 252 253def test_the_flag (self ): 254code ,out ,cfg ,_ = self .run_setup ("--vision" ,"cpu" ) 255self .assertEqual (code ,0 ,out ) 256self .assertEqual (cfg ["vision" ]["max_tokens" ],300 )# the default, as before 257self .assertNotIn ("--vision-tokens" ,out ) 258code ,out ,cfg ,_ = self .run_setup ("--vision" ,"cpu" ,"--vision-tokens" ,"768" ) 259self .assertEqual (code ,0 ,out ) 260self .assertEqual (cfg ["vision" ]["max_tokens" ],768 ) 261self .assertIn ("up to 768 image tokens" ,out ) 262self .assertIn ("longer to encode" ,out )# a note, not a cap 263code ,out ,cfg ,_ = self .run_setup ("--vision" ,"gpu" ,"--vision-tokens" ,"2048" ) 264self .assertEqual (cfg ["vision" ]["max_tokens" ],2048 ) 265self .assertIn ("more VRAM" ,out ) 266 267def test_kept_when_setup_runs_again (self ): 268code ,out ,first ,_ = self .run_setup ("--vision" ,"cpu" ,"--vision-tokens" ,"768" ) 269old = [("strata-q2_0.json" ,first )] 270code ,out ,cfg ,_ = self .run_setup ("--vision" ,"cpu" ,"--context" ,"65536" ,configs = old ) 271self .assertEqual (cfg ["vision" ]["max_tokens" ],768 ) 272code ,out ,cfg ,_ = self .run_setup ("--vision" ,"cpu" ,"--vision-tokens" ,"300" ,configs = old ) 273self .assertEqual (cfg ["vision" ]["max_tokens" ],300 )# given again: the new value 274code ,out ,cfg ,_ = self .run_setup ("--vision" ,"gpu" ,configs = old ) 275self .assertEqual (cfg ["vision" ]["max_tokens" ],1024 )# another device: its own default 276 277def test_refused_and_unused (self ): 278with contextlib .redirect_stderr (io .StringIO ())as err : 279code ,out ,cfg ,_ = self .run_setup ("--vision-tokens" ,"0" ) 280self .assertEqual (code ,2 ) 281self .assertIn ("--vision-tokens takes a number" ,err .getvalue ()) 282code ,out ,cfg ,_ = self .run_setup ("--vision" ,"no" ,"--vision-tokens" ,"768" ) 283self .assertEqual (code ,0 ,out ) 284self .assertNotIn ("vision" ,cfg ) 285self .assertIn ("images are off" ,out ) 286 287 288if __name__ == "__main__" : 289unittest .main ()