okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1"""`strata launch` settles the engine before it touches the model. With no usable engine and none to get it does NOT refuse (owner decision, 22026-10-18): it warns in plain words (the reason from `strata update`, and the next step), asks whether to download the model anyway (the 3answer is yes unless the person says no; --yes skips the question), downloads, and ends with a message that names the engine problem and the 4next step, exit code 0. Nothing is started without an engine.""" 5 6from __future__import annotations 7 8import io 9import json 10import os 11import unittest 12from pathlib import Path 13from unittest import mock 14 15from strata_cli .launch import settings as S 16from strata_cli .launch .runner import LaunchError ,NO_ENGINE_STEP ,Runner ,no_engine_final ,no_engine_warning 17from strata_control import engine_component as ec 18 19ROOT = Path (__file__ ).resolve ().parents [2 ] 20 21WRONG_FAMILY = ec .arch_notice (("gfx1100" ,"gfx1101" ,"gfx1102" ), ["gfx1201" ]) 22USABLE = {"usable" :True ,"engine" : {"version" :"0.1.45" ,"backend" :"hip" },"notice" :None } 23UNUSABLE = {"usable" :False ,"engine" :None ,"notice" :"No engine for this PC was found in /x: the component folder holds no engine package. " 24"The app never downloads an engine while it starts or serves." } 25 26 27def qwen_catalog (): 28"""A catalog stand-in for a Qwen model: since 0.2.4 the runner asks it whether the family is DeepSeek (which has its own engine and skips this step); 29a bare Mock answers yes to everything.""" 30cat = mock .Mock () 31cat .strata .multi_family .return_value = False 32return cat 33 34 35class FakeProc : 36def __init__ (self ,events :list [dict ],code :int = 0 ): 37self .stdout = iter (json .dumps (e )+ "\n" for e in events ) 38self ._code = code 39 40def wait (self )-> int : 41return self ._code 42 43 44def update_events (status :str ,note :str = "" ,code :int = 0 )-> list [dict ]: 45res = {"component" :"engine" ,"status" :status ,"changed" :status == "updated" ,"installed_before" :None } 46if note : 47res ["note" ]= note 48return [{"event" :"phase" ,"phase" :"feed" ,"message" :"Reading the signed update feed" }, 49 {"event" :"result" ,"results" : [res ],"exit_code" :code }] 50 51 52def runner (spawned :list )-> Runner : 53r = Runner (S .LaunchSettings (family = "qwen" ,model = "Q2_0" ,context = 32768 ),catalog = qwen_catalog (),emit = lambda e :None ) 54r .check_cancel = lambda :None 55return r 56 57 58class EngineBeforeModel (unittest .TestCase ): 59def run_ensure (self ,statuses :list [dict ],events :list [dict ],code :int = 0 ): 60"""ensure_engine() with a fake `strata update` and a sequence of engine reports. Returns (spawn count, the error or None).""" 61spawns ,err ,_r = self .run_ensure_runner (statuses ,events ,code ) 62return spawns ,err 63 64def run_ensure_runner (self ,statuses :list [dict ],events :list [dict ],code :int = 0 ,confirm = None ,gb :float = 0.0 , 65assume_yes :bool = False ): 66"""ensure_engine() like run_ensure, returning (spawns, error or None, the runner with .events).""" 67spawns :list = [] 68seen :list = [] 69r = Runner (S .LaunchSettings (family = "qwen" ,model = "Q2_0" ,context = 32768 ),catalog = qwen_catalog (),emit = seen .append , 70confirm = confirm ,assume_yes = assume_yes ) 71r .events = seen 72r .check_cancel = lambda :None 73err = None 74with mock .patch .object (ec ,"status" ,side_effect = statuses ), \ 75mock .patch .object (Runner ,"_spawn" ,side_effect = lambda argv ,** kw :spawns .append (argv )or FakeProc (events ,code )), \ 76mock .patch .object (Runner ,"download_gb" ,return_value = gb ): 77try : 78r .ensure_engine () 79except LaunchError as e : 80err = e 81return spawns ,err ,r 82 83def test_engine_present_runs_nothing (self ): 84spawns ,err = self .run_ensure ([USABLE ], []) 85self .assertIsNone (err ) 86self .assertEqual (spawns , []) 87 88def test_no_engine_and_the_feed_has_one (self ): 89spawns ,err = self .run_ensure ([UNUSABLE ,USABLE ],update_events ("updated" )) 90self .assertIsNone (err ) 91self .assertEqual (len (spawns ),1 ) 92self .assertIn ("update" ,spawns [0 ]) 93 94def test_no_engine_and_the_feed_has_none_is_a_warning_not_a_refusal (self ): 95note = "The feed has no engine package for this PC." 96spawns ,err ,r = self .run_ensure_runner ([UNUSABLE ,UNUSABLE ],update_events ("none-for-this-pc" ,note )) 97self .assertIsNone (err ) 98self .assertEqual (r .engine_problem ,note ) 99warning = no_engine_warning (note ) 100self .assertEqual (r .events [- 1 ].text ,warning ) 101self .assertEqual (r .events [- 1 ].level ,"warn" ) 102self .assertIn ("Strata couldn't find an engine for this computer (The feed has no engine package for this PC)" ,warning ) 103self .assertIn ("The model will download, but it may not run until you install the right engine (next step:" ,warning ) 104self .assertIn (NO_ENGINE_STEP ,warning ) 105self .assertNotIn ("The app never downloads" ,warning )# not the generic line 106 107def test_no_graphics_card_is_said_plainly_with_no_feed_address (self ): 108report = dict (UNUSABLE ,backend = None ) 109spawns ,err ,r = self .run_ensure_runner ([report ,report ],update_events ("none-for-this-pc" )) 110self .assertIsNone (err ) 111self .assertTrue (r .engine_problem .startswith ("no graphics card that Strata can use was found" )) 112text = r .events [- 1 ].text 113self .assertIn ("Strata needs an NVIDIA or AMD graphics card" ,text ) 114self .assertNotIn ("--feed" ,text ) 115self .assertNotIn ("<address>" ,text ) 116self .assertNotIn ("--feed" ,no_engine_final (r .engine_problem )) 117 118def test_the_other_no_engine_texts_do_not_ask_for_a_feed_address_either (self ): 119self .assertNotIn ("<address>" ,NO_ENGINE_STEP ) 120self .assertNotIn ("--feed" ,no_engine_warning ("The feed has no engine package for this PC." )) 121 122def test_wrong_family_names_the_plain_reason_in_the_warning (self ): 123spawns ,err ,r = self .run_ensure_runner ([UNUSABLE ,UNUSABLE ],update_events ("none-for-this-pc" ,WRONG_FAMILY )) 124self .assertIsNone (err ) 125self .assertEqual (r .engine_problem ,WRONG_FAMILY ) 126text = r .events [- 1 ].text 127self .assertIn ("This build of Strata is for AMD RX 7000 cards. This computer has an AMD RX 9000 card (gfx1201)." ,text ) 128self .assertIn ("Ask for the RX 9000 build)" ,text ) 129 130def test_installed_wrong_family_package_uses_its_plain_notice (self ): 131report = {"usable" :False ,"engine" :None ,"notice" :WRONG_FAMILY } 132spawns ,err ,r = self .run_ensure_runner ([report ,report ],update_events ("none-for-this-pc" )) 133self .assertEqual (r .engine_problem ,WRONG_FAMILY ) 134 135def test_the_question_is_asked_with_the_warning_and_no_stops_before_the_download (self ): 136asked = [] 137spawns ,err ,r = self .run_ensure_runner ([UNUSABLE ,UNUSABLE ],update_events ("none-for-this-pc" ,"No engine here." ), 138confirm = lambda title ,text :asked .append ((title ,text ))or False ,gb = 66.0 ) 139self .assertEqual (len (asked ),1 ) 140self .assertIn ("Strata couldn't find an engine for this computer (No engine here)" ,asked [0 ][1 ]) 141self .assertIn ("Download the model now (about 66 GB)?" ,asked [0 ][1 ]) 142self .assertIsInstance (err ,LaunchError ) 143self .assertEqual (err .code ,130 ) 144self .assertIn ("Nothing was downloaded" ,err .text ) 145 146def test_yes_goes_on_and_the_yes_flag_asks_nothing (self ): 147asked = [] 148spawns ,err ,r = self .run_ensure_runner ([UNUSABLE ,UNUSABLE ],update_events ("none-for-this-pc" ,"No engine here." ), 149confirm = lambda t ,x :asked .append (1 )or True ,gb = 66.0 ) 150self .assertIsNone (err ) 151self .assertEqual (len (asked ),1 ) 152asked .clear () 153spawns ,err ,r = self .run_ensure_runner ([UNUSABLE ,UNUSABLE ],update_events ("none-for-this-pc" ,"No engine here." ), 154confirm = lambda t ,x :asked .append (1 )or False ,gb = 66.0 ,assume_yes = True ) 155self .assertIsNone (err ) 156self .assertEqual (asked , []) 157self .assertEqual (r .engine_problem ,"No engine here." ) 158 159def test_nothing_to_download_asks_nothing (self ): 160asked = [] 161spawns ,err ,r = self .run_ensure_runner ([UNUSABLE ,UNUSABLE ],update_events ("none-for-this-pc" ,"No engine here." ), 162confirm = lambda t ,x :asked .append (1 )or False ,gb = 0.0 ) 163self .assertIsNone (err ) 164self .assertEqual (asked , []) 165 166def test_the_model_is_downloaded_and_nothing_is_started_without_an_engine (self ): 167r = runner ([]) 168started = [] 169with mock .patch .object (ec ,"status" ,return_value = UNUSABLE ), \ 170mock .patch .object (Runner ,"_spawn" ,return_value = FakeProc (update_events ("none-for-this-pc" ,WRONG_FAMILY ))), \ 171mock .patch .object (Runner ,"ensure_model" ,return_value = Path ("/x/strata-q.json" ))as model , \ 172mock .patch .object (Runner ,"build_config" ,return_value = Path ("/x/launch.json" )), \ 173mock .patch .object (Runner ,"ensure_profile" ),mock .patch .object (Runner ,"tune" )as tune , \ 174mock .patch .object (Runner ,"start" ,side_effect = lambda :started .append (1 )): 175result = r .run () 176model .assert_called_once () 177tune .assert_not_called () 178self .assertEqual (started , []) 179self .assertFalse (result .serving ) 180self .assertEqual (result .urls , []) 181self .assertEqual (result .engine_problem ,WRONG_FAMILY ) 182self .assertEqual (result .final_text ,no_engine_final (WRONG_FAMILY )) 183self .assertIn ("The model is ready, but Strata can't start it yet" ,result .final_text ) 184self .assertIn ("Ask for the RX 9000 build)" ,result .final_text ) 185self .assertIn (NO_ENGINE_STEP ,result .final_text ) 186 187def test_the_setup_child_is_told_to_go_on_without_an_engine (self ): 188r = runner ([]) 189r .engine_problem = "No engine here." 190seen = {} 191 192class Boom (Exception ): 193pass 194 195def spawn (self ,argv ,env = None ,cwd = None ): 196seen ["env" ]= env 197raise Boom () 198 199cat = mock .MagicMock () 200cat .src = ROOT 201r ._catalog = cat 202r .s = S .LaunchSettings (family = "qwen" ,model = "Q2_0" ,context = 32768 ) 203# (not an Intel Arc launch: on a box with an Arc card intel_chosen() is true and the model would be adopted, not set up; the 204# MagicMock catalog used to answer multi_file() with a truthy mock, which hid that before DeepSeek's branch left intel_chosen) 205with mock .patch .object (Runner ,"installed_state" ,return_value = (None ,None )),mock .patch .object (Runner ,"_spawn" ,spawn ),mock .patch .object (Runner ,"intel_chosen" ,return_value = False ): 206with self .assertRaises (Boom ): 207r .ensure_model () 208self .assertEqual (seen ["env" ].get ("STRATA_ALLOW_NO_ENGINE" ),"1" ) 209self .assertNotIn ("STRATA_NEED_ENGINE" ,seen ["env" ]) 210 211def test_a_failed_update_still_reports_the_old_message (self ): 212spawns ,err = self .run_ensure ([UNUSABLE ], [{"event" :"error" ,"error" : {"message" :"cannot reach the feed" }}],code = 6 ) 213self .assertEqual (err .code ,6 ) 214self .assertIn ("Cannot reach the feed" ,err .text )# (the reason is capitalised where it joins the sentence) 215 216 217class SetupControlStep (unittest .TestCase ): 218"""setup.py in the app's mode (and under `strata launch`): the engine step comes before the download step.""" 219 220def load_setup (self ): 221"""A private copy of setup.py read in the app's mode; the shared `setup` module (other tests) is left as it is.""" 222import importlib .util 223spec = importlib .util .spec_from_file_location ("setup_control_copy" ,ROOT / "setup.py" ) 224module = importlib .util .module_from_spec (spec ) 225with mock .patch .dict (os .environ , {"STRATA_CONTROL_INSTALL" :"1" }): 226spec .loader .exec_module (module ) 227return module 228 229def step (self ,exc :Exception ,env :dict )-> tuple [object ,str ]: 230setup = self .load_setup () 231out = io .StringIO () 232with mock .patch .dict (os .environ ,env ),mock .patch .object (ec ,"resolve" ,side_effect = exc ), \ 233mock .patch .object (setup ,"get_llama_cpp" ,return_value = Path ("llama" )), \ 234mock .patch .object (setup ,"say" ,lambda * a ,** k :out .write (" " .join (map (str ,a ))+ "\n" )), \ 235mock .patch .object (setup ,"warn" ,lambda * a ,** k :out .write ("WARN " + " " .join (map (str ,a ))+ "\n" )), \ 236mock .patch .object (setup ,"ok" ,lambda * a ,** k :None ): 237try : 238return setup .control_engine_step ({},"none" ),out .getvalue () 239except SystemExit as e : 240return e ,out .getvalue () 241 242def test_wrong_family_stops_before_the_download_for_launch (self ): 243exc = ec .EngineMissing ("nothing matched" ,Path ("/c" ),notice = WRONG_FAMILY ) 244res ,out = self .step (exc , {"STRATA_NEED_ENGINE" :"1" }) 245self .assertIsInstance (res ,SystemExit ) 246self .assertIn ("Ask for the RX 9000 build." ,out ) 247self .assertIn ("nothing was downloaded" ,out ) 248 249def test_wrong_family_goes_on_with_a_warning_when_launch_said_go_on (self ): 250exc = ec .EngineMissing ("nothing matched" ,Path ("/c" ),notice = WRONG_FAMILY ) 251res ,out = self .step (exc , {"STRATA_ALLOW_NO_ENGINE" :"1" }) 252self .assertNotIsInstance (res ,SystemExit ) 253self .assertIn ("no engine is installed for this PC yet" ,out ) 254self .assertIn ("The model is downloaded and prepared now" ,out ) 255 256def test_no_engine_for_launch_still_stops_setup_when_run_on_its_own (self ): 257res ,out = self .step (ec .EngineMissing ("the component folder holds no engine package" ,Path ("/c" )), {"STRATA_NEED_ENGINE" :"1" }) 258self .assertIsInstance (res ,SystemExit ) 259 260def test_the_app_with_a_wrong_family_engine_warns_and_still_downloads (self ): 261"""Owner decision 2026-10-18: the app downloads the model anyway and warns (its card says so).""" 262env = {k :v for k ,v in os .environ .items ()if k != "STRATA_NEED_ENGINE" } 263exc = ec .EngineMissing ("nothing matched" ,Path ("/c" ),notice = WRONG_FAMILY ) 264with mock .patch .dict (os .environ ,env ,clear = True ): 265res ,out = self .step (exc , {}) 266self .assertNotIsInstance (res ,SystemExit ) 267self .assertIn ("Ask for the RX 9000 build." ,out ) 268self .assertIn ("The model is downloaded and prepared now" ,out ) 269 270def test_no_engine_stops_before_the_download_for_launch (self ): 271res ,out = self .step (ec .EngineMissing ("the component folder holds no engine package" ,Path ("/c" )), {"STRATA_NEED_ENGINE" :"1" }) 272self .assertIsInstance (res ,SystemExit ) 273 274def test_the_app_without_an_engine_keeps_its_order (self ): 275env = {k :v for k ,v in os .environ .items ()if k != "STRATA_NEED_ENGINE" } 276with mock .patch .dict (os .environ ,env ,clear = True ): 277res ,out = self .step (ec .EngineMissing ("the component folder holds no engine package" ,Path ("/c" )), {}) 278self .assertNotIsInstance (res ,SystemExit ) 279self .assertIn ("The model is downloaded and prepared now" ,out ) 280 281 282if __name__ == "__main__" : 283unittest .main ()