okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3b1ab2848
main
1"""G04/E01 from the TUI stress test: a second launch while a model serves (same choices, other choices, no screens, --restart, 2another context on the same port).""" 3 4import os 5import unittest 6from unittest import mock 7 8from strata_cli .launch import settings as S 9from strata_cli .launch .catalog import Catalog 10from strata_cli .launch .runner import LaunchError ,Runner 11from tests .terminal .launch_support import HW_12 ,Home ,catalog ,run_cli 12 13 14class SecondLaunchTests (unittest .TestCase ): 15def setUp (self ): 16self .home = Home ().__enter__ () 17self .addCleanup (self .home .__exit__ ,None ,None ,None ) 18p = mock .patch .object (Catalog ,"hardware" ,lambda self ,fresh = False :dict (HW_12 ,disk_free_gb = 500.0 )) 19p .start () 20self .addCleanup (p .stop ) 21 22def runner (self ,** kw ): 23s = S .LaunchSettings (family = "qwen" ,model = "IQ3_XXS" ,context = 32768 ,name = "gyro" ) 24r = Runner (s ,catalog = catalog (),** kw ) 25r .said = [] 26r .emit_cb = lambda e :r .said .append (e .text ) 27return r 28 29@staticmethod 30def row (r ,port = 8080 ,name = None ,choices = None ): 31return [{"profile" :"launch-" + (name or r .name ),"name" :name or r .name ,"port" :port , 32"choices" :r .choices ()if choices is None else choices }] 33 34def test_same_choices_reuse_the_running_server (self ): 35r = self .runner () 36rows = self .row (r ) 37with mock .patch .object (Runner ,"serving_launches" ,lambda self :rows ), \ 38mock .patch .object (Runner ,"ensure_engine" ,side_effect = AssertionError ("must not run" )), \ 39mock .patch ("strata_control.runtime.stop" ,side_effect = AssertionError ("must not stop" )): 40res = r .run () 41self .assertTrue (res .reused ) 42self .assertTrue (res .urls ) 43self .assertTrue (any ("nothing was restarted" in x for x in r .said )) 44 45def test_other_choices_without_screens_refuse_in_one_line (self ): 46r = self .runner () 47rows = self .row (r ,choices = {** r .choices (),"vision" :"cpu" ,"context" :65536 }) 48with mock .patch .object (Runner ,"serving_launches" ,lambda self :rows ): 49with self .assertRaises (LaunchError )as cm : 50r .run () 51self .assertEqual (cm .exception .code ,6 ) 52self .assertIn ("port 8080" ,cm .exception .text ) 53self .assertIn ("context 64K" ,cm .exception .text ) 54self .assertIn ("--restart" ,cm .exception .next ) 55self .assertIn ("strata launch --stop" ,cm .exception .next ) 56 57def test_yes_alone_does_not_restart (self ): 58r = self .runner (assume_yes = True ) 59rows = self .row (r ,choices = {** r .choices (),"context" :65536 }) 60with mock .patch .object (Runner ,"serving_launches" ,lambda self :rows ): 61with self .assertRaises (LaunchError ): 62r .run () 63 64def test_screens_ask_once_and_cancel_leaves_it_running (self ): 65asked = [] 66r = self .runner (confirm_restart = lambda q ,d :asked .append ((q ,d ))or False ) 67rows = self .row (r ,choices = {** r .choices (),"context" :65536 }) 68with mock .patch .object (Runner ,"serving_launches" ,lambda self :rows ), \ 69mock .patch ("strata_control.runtime.stop" )as stop : 70with self .assertRaises (LaunchError )as cm : 71r .run () 72self .assertEqual (len (asked ),1 ) 73self .assertIn ("Restart it with these settings?" ,asked [0 ][0 ]) 74self .assertEqual (asked [0 ][1 ],"Answers in progress stop." ) 75self .assertEqual (cm .exception .code ,130 ) 76stop .assert_not_called () 77 78def test_screens_restart_goes_on (self ): 79r = self .runner (confirm_restart = lambda q ,d :True ) 80rows = self .row (r ,choices = {** r .choices (),"context" :65536 }) 81with mock .patch .object (Runner ,"serving_launches" ,lambda self :rows ), \ 82mock .patch .object (Runner ,"ensure_engine" ,side_effect = RuntimeError ("went on" )): 83with self .assertRaises (RuntimeError ): 84r .run () 85 86def test_restart_flag_goes_on_without_asking (self ): 87r = self .runner (restart = True ) 88rows = self .row (r ,choices = {** r .choices (),"context" :65536 }) 89with mock .patch .object (Runner ,"serving_launches" ,lambda self :rows ), \ 90mock .patch .object (Runner ,"ensure_engine" ,side_effect = RuntimeError ("went on" )): 91with self .assertRaises (RuntimeError ): 92r .run () 93 94def test_a_changed_context_on_the_same_port_stops_the_old_profile_first (self ): 95r = self .runner (restart = True ) 96rows = self .row (r ,name = "gyro-s-32k" ,choices = {"context" :32768 })# another profile name, same port 8080 97order = [] 98with mock .patch .object (Runner ,"serving_launches" ,lambda self :rows ): 99self .assertIsNone (r .check_serving ()) 100self .assertEqual (r ._stop_first , ["launch-gyro-s-32k" ]) 101with mock .patch ("strata_control.runtime.stop" ,side_effect = lambda n ,timeout = 0 :order .append (n )), \ 102mock .patch ("strata_cli.launch.access.stop_tunnel" ): 103r .stop_others () 104self .assertEqual (order , ["launch-gyro-s-32k" ]) 105self .assertEqual (r ._stop_first , []) 106 107def test_a_changed_context_without_restart_names_the_running_one (self ): 108r = self .runner () 109rows = self .row (r ,name = "gyro-s-32k" ,choices = {"context" :32768 }) 110with mock .patch .object (Runner ,"serving_launches" ,lambda self :rows ): 111with self .assertRaises (LaunchError )as cm : 112r .check_serving () 113self .assertIn ("gyro-s-32k" ,cm .exception .text ) 114 115def test_a_different_port_runs_beside_with_a_memory_warning (self ): 116r = self .runner () 117r .s .port = 8081 118rows = self .row (r ,name = "other" ,port = 8080 ,choices = {}) 119with mock .patch .object (Runner ,"serving_launches" ,lambda self :rows ): 120self .assertIsNone (r .check_serving ()) 121self .assertEqual (r ._stop_first , []) 122self .assertTrue (any ("graphics memory" in n for n in r .notices )) 123 124def test_command_line_refuses_restarts_and_yes_alone_does_not (self ): 125rows = [{"profile" :"launch-q2-0-32k" ,"name" :"q2-0-32k" ,"port" :8080 ,"choices" : {"context" :1 }}] 126with mock .patch .object (Runner ,"serving_launches" ,lambda self :rows ): 127code ,out ,err = run_cli (["launch" ,"--no-tui" ,"--model" ,"q2_0" ,"--context" ,"32k" ]) 128self .assertEqual (code ,6 ,err ) 129self .assertIn ("--restart" ,err ) 130self .assertIn ("q2-0-32k" ,err ) 131code ,out ,err = run_cli (["launch" ,"--no-tui" ,"--yes" ,"--model" ,"q2_0" ,"--context" ,"32k" ]) 132self .assertEqual (code ,6 ) 133seen = {} 134 135def go (self ,* a ,** k ): 136seen ["restart" ]= self .restart 137from strata_cli .launch .runner import Result 138return Result ("launch-x" , ["http://127.0.0.1:8080" ],None ,None , [],"/x" ) 139 140with mock .patch .object (Runner ,"run" ,go ): 141code ,out ,err = run_cli (["launch" ,"--no-tui" ,"--yes" ,"--restart" ,"--model" ,"q2_0" ,"--context" ,"32k" ]) 142self .assertEqual (code ,0 ,err ) 143self .assertTrue (seen ["restart" ]) 144 145def test_restart_is_in_help_and_docs (self ): 146code ,out ,_ = run_cli (["launch" ,"--help" ]) 147self .assertIn ("--restart" ,out ) 148root = os .path .dirname (os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))) 149with open (os .path .join (root ,"docs" ,"TERMINAL_LAUNCH.md" ),encoding = "utf-8" )as f : 150self .assertIn ("--restart" ,f .read ()) 151 152 153class ModelPathWins (unittest .TestCase ): 154"""C08: --model-path and --data-dir are honoured even when an installed copy with the same model and context exists.""" 155 156def setUp (self ): 157import json 158import tempfile 159from pathlib import Path 160self .home = Home ().__enter__ () 161self .addCleanup (self .home .__exit__ ,None ,None ,None ) 162p = mock .patch .object (Catalog ,"hardware" ,lambda self ,fresh = False :dict (HW_12 ,disk_free_gb = 500.0 )) 163p .start () 164self .addCleanup (p .stop ) 165self .tmp = tempfile .TemporaryDirectory () 166self .addCleanup (self .tmp .cleanup ) 167self .base = Path (self .tmp .name ) 168self .installed = self .base / "dl-data" 169 (self .installed / "models" / "Q2_0" ).mkdir (parents = True ) 170self .gguf = self .installed / "models" / "Q2_0" / "m.gguf" 171self .gguf .write_bytes (b"x" ) 172self .pack = self .installed / "packs" / "q2_0" 173self .pack .mkdir (parents = True ) 174self .cfg_path = self .base / "strata-q2_0.json" 175self .cfg_path .write_text (json .dumps ({"args" : ["--model" ,str (self .gguf ),"--pack" ,str (self .pack ),"--max-context" ,"32768" ]}),encoding = "utf-8" ) 176self .mine = self .base / "documents" 177self .mine .mkdir () 178 (self .mine / "mine.gguf" ).write_bytes (b"y" ) 179 180def runner (self ,** fields ): 181s = S .LaunchSettings (family = "qwen" ,model = "Q2_0" ,context = 32768 ,** fields ) 182return Runner (s ,catalog = catalog ()) 183 184def test_installed_copy_serves_when_nothing_was_given (self ): 185self .assertTrue (self .runner ().installed_serves_request (self .cfg_path )) 186 187def test_a_model_path_elsewhere_is_not_served_by_the_installed_copy (self ): 188self .assertFalse (self .runner (model_path = str (self .mine )).installed_serves_request (self .cfg_path )) 189self .assertFalse (self .runner (model_path = str (self .mine / "mine.gguf" )).installed_serves_request (self .cfg_path )) 190 191def test_a_model_path_that_is_the_installed_folder_is_the_installed_copy (self ): 192self .assertTrue (self .runner (model_path = str (self .installed / "models" / "Q2_0" )).installed_serves_request (self .cfg_path )) 193 194def test_a_data_dir_elsewhere_is_not_served_by_the_installed_copy (self ): 195self .assertFalse (self .runner (data_dir = str (self .base / "c08-data" )).installed_serves_request (self .cfg_path )) 196self .assertTrue (self .runner (data_dir = str (self .installed )).installed_serves_request (self .cfg_path )) 197 198def test_ensure_model_runs_setup_with_the_given_path_instead_of_reusing_the_profile (self ): 199r = self .runner (model_path = str (self .mine )) 200said = [] 201r .emit_cb = lambda e :said .append (e .text ) 202desc = {"context" :32768 ,"kv" :"int8" ,"images" :"off" } 203with mock .patch .object (Runner ,"check_own_files" ),mock .patch .object (Runner ,"resolved_vision" ),mock .patch .object (Runner ,"installed_state" ,return_value = (self .cfg_path ,desc )),mock .patch .object (Runner ,"_spawn" ,side_effect = RuntimeError ("setup would run" )): 204with self .assertRaises (RuntimeError ): 205r .ensure_model () 206self .assertFalse (any ("already installed with these choices" in x for x in said )) 207self .assertTrue (any ("uses the folder you gave" in x for x in said )) 208self .assertIn (str (self .mine ),r .setup_args ()) 209 210def test_without_a_path_the_installed_copy_is_still_reused (self ): 211r = self .runner () 212said = [] 213r .emit_cb = lambda e :said .append (e .text ) 214desc = {"context" :32768 ,"kv" :"int8" ,"images" :"off" } 215with mock .patch .object (Runner ,"check_own_files" ),mock .patch .object (Runner ,"resolved_vision" ),mock .patch .object (Runner ,"installed_state" ,return_value = (self .cfg_path ,desc )): 216self .assertEqual (r .ensure_model (),self .cfg_path ) 217self .assertTrue (any ("already installed with these choices" in x for x in said )) 218 219 220if __name__ == "__main__" : 221unittest .main ()