okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
64a91dcdc
main
1"""Local profile and component workflows without a GPU or network service.""" 2 3import io 4import json 5import os 6import subprocess 7import tempfile 8import unittest 9from contextlib import redirect_stderr ,redirect_stdout 10from pathlib import Path 11from unittest import mock 12 13from strata_cli .cli import main 14from strata_control import doctor ,profiles ,runtime 15 16 17class OperatorProfileTests (unittest .TestCase ): 18def setUp (self ): 19self .temp = tempfile .TemporaryDirectory () 20self .addCleanup (self .temp .cleanup ) 21self .root = Path (self .temp .name ) 22self .env = mock .patch .dict (os .environ , {"STRATA_PROFILE_DIR" :str (self .root / "profiles" ), 23"STRATA_RUNTIME_DIR" :str (self .root / "runtime" ), 24"STRATA_API_KEY" :"" }) 25self .env .start () 26self .addCleanup (self .env .stop ) 27engine = self .root / "engine" 28engine .write_text ("engine placeholder" ,encoding = "utf-8" ) 29engine .chmod (0o700 ) 30tokenizer = self .root / "tokenizer" 31tokenizer .mkdir () 32for name in ("vocab.json" ,"merges.txt" ,"token_type.json" ): 33 (tokenizer / name ).write_text ("{}" ,encoding = "utf-8" ) 34pack = self .root / "pack" 35pack .mkdir () 36shard = self .root / "model.gguf" 37shard .write_bytes (b"model placeholder" ) 38self .shard = shard 39self .config = self .root / "run.json" 40self .config .write_text (json .dumps ({"exe" :str (engine ),"args" : ["--pack" ,str (pack ),"--native" ,str (shard )], 41"cwd" :str (self .root ),"tokenizer" :str (tokenizer ), 42"model_name" :"test-model" ,"port" :8095 }),encoding = "utf-8" ) 43 44def command (self ,* args ): 45output = io .StringIO () 46with redirect_stdout (output ): 47code = main (list (args )) 48return code ,json .loads (output .getvalue ()) 49 50def test_saved_overrides_drive_serve_plan_without_rewriting_run_config_or_starting (self ): 51before = self .config .read_bytes () 52code ,created = self .command ("profile" ,"create" ,"four-gpu" ,"--from-config" ,str (self .config ), 53"--output" ,"json" ) 54self .assertEqual (code ,0 ) 55self .assertEqual (created ["profile" ]["overrides" ], {}) 56code ,changed = self .command ("profile" ,"set" ,"four-gpu" ,"parallel=8" ,"batch_groups=2" , 57"max_queue=32" ,"queue_timeout=12.5" ,"batch_telemetry=true" , 58"--output" ,"json" ) 59self .assertEqual (code ,0 ) 60self .assertNotEqual (changed ["revision" ],created ["revision" ]) 61with mock .patch ("serve.server.main" ,side_effect = AssertionError ("server started" )): 62code ,planned = self .command ("serve" ,"--profile" ,"four-gpu" ,"--plan" ,"--output" ,"json" ) 63self .assertEqual (code ,0 ) 64plan = planned ["plan" ] 65self .assertEqual (plan ["effective" ]["parallel" ],8 ) 66self .assertEqual (plan ["effective" ]["batch_groups" ],2 ) 67self .assertEqual (plan ["effective" ]["max_queue" ],32 ) 68self .assertEqual (plan ["effective" ]["queue_timeout" ],12.5 ) 69self .assertTrue (plan ["effective" ]["batch_telemetry" ]) 70self .assertEqual (plan ["origin" ]["parallel" ],"saved_profile" ) 71self .assertEqual (plan ["engine_argv" ][- 4 :], ["--batch-groups" ,"2" ,"--batch" ,"8" ]) 72self .assertIn ("--max-queue" ,plan ["server_argv" ]) 73self .assertIn ("--batch-telemetry" ,plan ["server_argv" ]) 74self .assertEqual (plan ["server_argv" ][plan ["server_argv" ].index ("--queue-timeout" )+ 1 ],"12.5" ) 75self .assertEqual (self .config .read_bytes (),before ) 76self .assertEqual (len (list ((self .root / "profiles" / ".history" / "four-gpu" ).glob ("*.json" ))),1 ) 77 78def test_saved_profile_is_used_by_actual_foreground_serve_call (self ): 79profiles .create ("local" ,str (self .config )) 80profiles .set_values ("local" , ["parallel=4" ,"max_queue=7" ]) 81with mock .patch ("serve.server.main" ,return_value = 0 )as server : 82self .assertEqual (main (["serve" ,"--profile" ,"local" ]),0 ) 83argv = server .call_args .args [0 ] 84self .assertEqual (server .call_args .kwargs ["interrupt_exit_code" ],130 ) 85self .assertIn (str (self .config .resolve ()),argv ) 86self .assertEqual (argv [argv .index ("--parallel" )+ 1 ],"4" ) 87self .assertEqual (argv [argv .index ("--max-queue" )+ 1 ],"7" ) 88 89def test_json_alias_is_one_shot_and_keeps_machine_errors (self ): 90code ,listed = self .command ("profile" ,"list" ,"--json" ) 91self .assertEqual (code ,0 ) 92self .assertTrue (listed ["ok" ]) 93self .assertEqual (listed ["profiles" ], []) 94with mock .patch ("serve.server.main" ,side_effect = AssertionError ("server started" )): 95code ,planned = self .command ("serve" ,"--profile" ,str (self .config ),"--plan" ,"--json" ) 96self .assertEqual (code ,0 ) 97self .assertTrue (planned ["ok" ]) 98self .assertIn ("plan" ,planned ) 99code ,no_plan = self .command ("serve" ,"--profile" ,str (self .config ),"--json" ) 100self .assertEqual (code ,2 ) 101self .assertEqual (no_plan ["error" ]["code" ],2 ) 102code ,stream = self .command ("serve" ,"--profile" ,str (self .config ),"--json" , 103"--output" ,"jsonl" ) 104self .assertEqual (code ,2 ) 105self .assertEqual (stream ["event" ],"failed" ) 106code ,harness = self .command ("code" ,"sessions" ,"list" ,"--json" ) 107self .assertEqual (code ,5 ) 108self .assertEqual (harness ["error" ]["code" ],5 ) 109 110def test_quiet_suppresses_human_success_but_preserves_errors_and_json (self ): 111output ,diagnostics = io .StringIO (),io .StringIO () 112with redirect_stdout (output ),redirect_stderr (diagnostics ): 113self .assertEqual (main (["profile" ,"list" ,"--quiet" ]),0 ) 114self .assertEqual (main (["serve" ,"--profile" ,str (self .config ),"--plan" ,"--quiet" ]),0 ) 115self .assertEqual ((output .getvalue (),diagnostics .getvalue ()), ("" ,"" )) 116with redirect_stdout (output ),redirect_stderr (diagnostics ): 117self .assertEqual (main (["profile" ,"list" ,"--quiet" ,"--json" ]),0 ) 118self .assertTrue (json .loads (output .getvalue ())["ok" ]) 119output ,diagnostics = io .StringIO (),io .StringIO () 120with redirect_stdout (output ),redirect_stderr (diagnostics ): 121self .assertNotEqual (main (["profile" ,"show" ,"missing" ,"--quiet" ]),0 ) 122self .assertEqual (output .getvalue (),"" ) 123self .assertIn ("strata:" ,diagnostics .getvalue ()) 124output ,diagnostics = io .StringIO (),io .StringIO () 125def failed_operation (_a ): 126"x" * 70_000 ) 127"failure detail" ) 128return 5 129with mock .patch ("strata_cli.cli._profile_command" ,side_effect = failed_operation ): 130with redirect_stdout (output ),redirect_stderr (diagnostics ): 131self .assertEqual (main (["profile" ,"list" ,"--quiet" ]),5 ) 132self .assertEqual (output .getvalue (),"" ) 133self .assertLess (len (diagnostics .getvalue ()),66_000 ) 134self .assertIn ("earlier human output omitted" ,diagnostics .getvalue ()) 135self .assertIn ("failure detail" ,diagnostics .getvalue ()) 136 137def test_quiet_foreground_serve_hides_human_banner (self ): 138output = io .StringIO () 139with mock .patch ("serve.server.main" ,side_effect = lambda * _args ,** _kwargs :"ready banner" )or 0 ): 140with redirect_stdout (output ): 141self .assertEqual (main (["serve" ,"--profile" ,str (self .config ),"--quiet" ]),0 ) 142self .assertEqual (output .getvalue (),"" ) 143output ,diagnostics = io .StringIO (),io .StringIO () 144with mock .patch ("serve.server.main" ,return_value = 8 ): 145with redirect_stdout (output ),redirect_stderr (diagnostics ): 146self .assertEqual (main (["serve" ,"--profile" ,str (self .config ),"--quiet" ]),8 ) 147self .assertEqual (output .getvalue (),"" ) 148self .assertIn ("drain deadline expired" ,diagnostics .getvalue ()) 149 150def test_foreground_jsonl_rejects_plan_and_bad_profile_as_events (self ): 151code ,invalid = self .command ("serve" ,"--profile" ,str (self .config ),"--plan" , 152"--output" ,"jsonl" ) 153self .assertEqual (code ,2 ) 154self .assertEqual (invalid ["event" ],"failed" ) 155self .assertEqual (invalid ["exit_code" ],2 ) 156code ,missing = self .command ("serve" ,"--profile" ,"missing-profile" , 157"--output" ,"jsonl" ) 158self .assertNotEqual (code ,0 ) 159self .assertEqual (missing ["event" ],"failed" ) 160self .assertEqual (missing ["exit_code" ],code ) 161 162def test_foreground_jsonl_reports_startup_failure_without_human_stdout (self ): 163output ,diagnostics = io .StringIO (),io .StringIO () 164with mock .patch ("serve.server.main" ,side_effect = SystemExit ("engine refused to start" )): 165with redirect_stdout (output ),redirect_stderr (diagnostics ): 166code = main (["serve" ,"--profile" ,str (self .config ),"--output" ,"jsonl" ]) 167rows = [json .loads (line )for line in output .getvalue ().splitlines ()] 168self .assertEqual (code ,6 ) 169self .assertEqual ([row ["event" ]for row in rows ], ["starting" ,"failed" ]) 170self .assertEqual (rows [- 1 ]["phase" ],"startup" ) 171self .assertNotIn ("engine refused to start" ,output .getvalue ()) 172self .assertIn ("startup failed" ,diagnostics .getvalue ()) 173 174@ unittest . skipUnless ( os . name == "posix" , "managed activity lease is POSIX-only" ) 175def test_foreground_serve_holds_shared_lease_until_server_returns (self ): 176profiles .create ("local" ,str (self .config )) 177 178def running_server (argv ,** _kwargs ): 179with self .assertRaisesRegex (profiles .ProfileError ,"engine lease" ): 180runtime .acquire_activity_lease (shared = False ) 181return 0 182 183with mock .patch ("serve.server.main" ,side_effect = running_server ): 184self .assertEqual (main (["serve" ,"--profile" ,"local" ]),0 ) 185exclusive = runtime .acquire_activity_lease (shared = False ) 186os .close (exclusive ) 187 188def test_batch_telemetry_precedence_can_disable_a_run_config (self ): 189cfg = json .loads (self .config .read_text ()) 190cfg ["batch_telemetry" ]= True 191self .config .write_text (json .dumps (cfg )) 192profiles .create ("local" ,str (self .config )) 193code ,inherited = self .command ("serve" ,"--profile" ,"local" ,"--plan" ,"--output" ,"json" ) 194self .assertEqual (code ,0 ) 195self .assertTrue (inherited ["plan" ]["effective" ]["batch_telemetry" ]) 196self .assertEqual (inherited ["plan" ]["origin" ]["batch_telemetry" ],"run_config" ) 197profiles .set_values ("local" , ["batch_telemetry=false" ]) 198code ,disabled = self .command ("serve" ,"--profile" ,"local" ,"--plan" ,"--output" ,"json" ) 199self .assertEqual (code ,0 ) 200self .assertFalse (disabled ["plan" ]["effective" ]["batch_telemetry" ]) 201self .assertIn ("--no-batch-telemetry" ,disabled ["plan" ]["server_argv" ]) 202code ,enabled = self .command ("serve" ,"--profile" ,"local" ,"--batch-telemetry" , 203"--plan" ,"--output" ,"json" ) 204self .assertEqual (code ,0 ) 205self .assertEqual (enabled ["plan" ]["origin" ]["batch_telemetry" ],"command" ) 206self .assertIn ("--batch-telemetry" ,enabled ["plan" ]["server_argv" ]) 207 208def test_serve_plan_validates_ready_file_without_writing_it (self ): 209profiles .create ("local" ,str (self .config )) 210ready = self .root / "ready.json" 211code ,result = self .command ("serve" ,"--profile" ,"local" ,"--ready-file" ,str (ready ), 212"--plan" ,"--output" ,"json" ) 213self .assertEqual (code ,0 ) 214self .assertEqual (result ["plan" ]["ready_file" ],str (ready )) 215self .assertFalse (ready .exists ()) 216ready .write_text ("stale" ,encoding = "utf-8" ) 217code ,result = self .command ("serve" ,"--profile" ,"local" ,"--ready-file" ,str (ready ), 218"--plan" ,"--output" ,"json" ) 219self .assertEqual (code ,2 ) 220self .assertFalse (result ["ok" ]) 221 222def test_stale_edit_rejected_and_bad_editor_content_keeps_saved_revision (self ): 223profiles .create ("local" ,str (self .config )) 224current ,old_revision = profiles .load ("local" ) 225profiles .set_values ("local" , ["parallel=4" ]) 226with self .assertRaisesRegex (profiles .ProfileError ,"changed during edit" ): 227profiles .save ("local" ,current ,expected_revision = old_revision ) 228_ ,current_revision = profiles .load ("local" ) 229 230def corrupt (command ,check ): 231Path (command [- 1 ]).write_text ("{invalid" ,encoding = "utf-8" ) 232return mock .Mock (returncode = 0 ) 233 234with mock .patch .dict (os .environ , {"EDITOR" :"fake-editor" ,"VISUAL" :"" }), \ 235mock .patch ("strata_control.profiles.subprocess.run" ,side_effect = corrupt ): 236code ,result = self .command ("profile" ,"edit" ,"local" ,"--output" ,"json" ) 237self .assertEqual (code ,2 ) 238self .assertFalse (result ["ok" ]) 239draft_path = Path (result ["error" ]["message" ].split ("draft kept at " ,1 )[1 ]) 240self .assertTrue (draft_path .is_file ()) 241self .assertEqual (draft_path .read_text (encoding = "utf-8" ),"{invalid" ) 242self .assertEqual (profiles .load ("local" )[1 ],current_revision ) 243 244def test_plan_then_history_and_rollback_restore_saved_settings_only (self ): 245before = self .config .read_bytes () 246profiles .create ("local" ,str (self .config )) 247_ ,first = profiles .load ("local" ) 248code ,planned = self .command ("profile" ,"set" ,"local" ,"parallel=8" ,"--plan" ,"--output" ,"json" ) 249self .assertEqual (code ,0 ) 250self .assertTrue (planned ["would_save" ]) 251self .assertEqual (planned ["changes" ]["parallel" ], {"before" :None ,"after" :8 }) 252self .assertEqual (profiles .load ("local" )[1 ],first ) 253code ,changed = self .command ("profile" ,"set" ,"local" ,"parallel=8" ,"--output" ,"json" ) 254self .assertEqual (code ,0 ) 255code ,listing = self .command ("profile" ,"history" ,"local" ,"--output" ,"json" ) 256self .assertEqual (code ,0 ) 257self .assertEqual (listing ["history" ][0 ]["revision" ],changed ["revision" ]) 258self .assertIn (first , [item ["revision" ]for item in listing ["history" ]]) 259code ,restored = self .command ("profile" ,"rollback" ,"local" ,"--to" ,first ,"--output" ,"json" ) 260self .assertEqual (code ,0 ) 261self .assertEqual (restored ["revision" ],first ) 262self .assertEqual (restored ["profile" ]["overrides" ], {}) 263self .assertEqual (self .config .read_bytes (),before ) 264 265def test_use_config_previews_and_saves_a_new_model_source_without_editing_either_config (self ): 266profiles .create ("local" ,str (self .config )) 267old_profile ,old_revision = profiles .load ("local" ) 268next_config = self .root / "next.json" 269next_value = json .loads (self .config .read_text (encoding = "utf-8" )) 270next_value ["model_name" ]= "next-model" 271next_config .write_text (json .dumps (next_value ),encoding = "utf-8" ) 272before = (self .config .read_bytes (),next_config .read_bytes ()) 273code ,preview = self .command ("profile" ,"use-config" ,"local" ,"--from-config" , 274str (next_config ),"--plan" ,"--output" ,"json" ) 275self .assertEqual (code ,0 ) 276self .assertTrue (preview ["would_save" ]) 277self .assertEqual (preview ["revision" ],old_revision ) 278self .assertEqual (preview ["model_name" ],"next-model" ) 279self .assertEqual (profiles .load ("local" ), (old_profile ,old_revision )) 280code ,unavailable = self .command ("profile" ,"use-config" ,"local" ,"--from-config" , 281str (self .root / "missing.json" ),"--output" ,"json" ) 282self .assertEqual (code ,5 ) 283self .assertFalse (unavailable ["ok" ]) 284self .assertEqual (profiles .load ("local" ), (old_profile ,old_revision )) 285code ,stale = self .command ("profile" ,"use-config" ,"local" ,"--from-config" , 286str (next_config ),"--if-revision" ,"0" * 16 ,"--output" ,"json" ) 287self .assertEqual (code ,6 ) 288self .assertFalse (stale ["ok" ]) 289code ,saved = self .command ("profile" ,"use-config" ,"local" ,"--from-config" , 290str (next_config ),"--if-revision" ,old_revision ,"--output" ,"json" ) 291self .assertEqual (code ,0 ) 292self .assertEqual (saved ["profile" ]["source_config" ],str (next_config .resolve ())) 293self .assertNotEqual (saved ["revision" ],old_revision ) 294self .assertEqual ((self .config .read_bytes (),next_config .read_bytes ()),before ) 295self .assertIn (old_revision , [row ["revision" ]for row in profiles .history ("local" )]) 296 297def test_doctor_distinguishes_required_missing_from_unverified_optional (self ): 298profiles .create ("local" ,str (self .config )) 299code ,result = self .command ("doctor" ,"--profile" ,"local" ,"--components" ,"--output" ,"json" ) 300self .assertEqual (code ,0 ) 301self .assertTrue (result ["report" ]["ok" ]) 302self .assertEqual (result ["report" ]["integrity" ],"unverified_no_manifest_hashes" ) 303self .shard .unlink () 304code ,result = self .command ("components" ,"verify" ,"--profile" ,"local" ,"--output" ,"json" ) 305self .assertEqual (code ,5 ) 306self .assertFalse (result ["report" ]["ok" ]) 307self .assertTrue (any (i ["component" ]== "native" for i in result ["report" ]["issues" ])) 308 309def test_monitor_inventory_checks_the_optional_pinned_version (self ): 310pinned = (Path (__file__ ).resolve ().parents [2 ]/ "requirements-monitor.txt" ).read_text () 311self .assertIn (f"textual== { doctor . MONITOR_TEXTUAL_VERSION } " ,pinned ) 312with mock .patch .object (doctor .util ,"find_spec" ,return_value = None ): 313missing = doctor ._monitor_component () 314self .assertEqual (missing ["status" ],"optional_missing" ) 315with mock .patch .object (doctor .util ,"find_spec" ,return_value = object ()), \ 316mock .patch .object (doctor .metadata ,"version" ,return_value = "8.2.8" ): 317present = doctor ._monitor_component () 318self .assertEqual (present ["status" ],"ok" ) 319self .assertEqual (present ["check" ],"package_version_only" ) 320with mock .patch .object (doctor .util ,"find_spec" ,return_value = object ()), \ 321mock .patch .object (doctor .metadata ,"version" ,return_value = "9.0.0" ): 322changed = doctor ._monitor_component () 323self .assertEqual (changed ["status" ],"unverified" ) 324self .assertEqual (changed ["version" ],"9.0.0" ) 325with mock .patch .object (doctor .util ,"find_spec" ,return_value = object ()), \ 326mock .patch .object (doctor .metadata ,"version" , 327side_effect = doctor .metadata .PackageNotFoundError ("textual" )): 328unknown = doctor ._monitor_component () 329self .assertEqual (unknown ["status" ],"unverified" ) 330 331output = io .StringIO () 332with mock .patch .object (doctor ,"_monitor_component" ,return_value = changed ),redirect_stdout (output ): 333code = main (["doctor" ]) 334self .assertEqual (code ,0 )# an optional version does not stop serving 335self .assertIn ("interactive monitor was qualified with Textual 8.2.8" ,output .getvalue ()) 336 337def test_live_doctor_checks_four_selected_nvidia_cards_without_starting_engine (self ): 338config = json .loads (self .config .read_text (encoding = "utf-8" )) 339config ["gpu" ]= [0 ,1 ,2 ,3 ] 340config ["backend" ]= "cuda" 341self .config .write_text (json .dumps (config ),encoding = "utf-8" ) 342profiles .create ("four-gpu" ,str (self .config )) 343cards = "\n" .join (f" { i } , RTX 6000 test { i } , GPU-test- { i } , 49140, 580.95" for i in range (4 )) 344with mock .patch ("strata_control.doctor.shutil.which" ,return_value = "/fake/nvidia-smi" ), \ 345mock .patch ("strata_control.doctor.subprocess.run" , 346return_value = mock .Mock (returncode = 0 ,stdout = cards ))as probe : 347code ,result = self .command ("doctor" ,"--profile" ,"four-gpu" ,"--live" ,"--output" ,"json" ) 348self .assertEqual (code ,0 ) 349self .assertEqual (result ["report" ]["live_gpu_probe" ]["status" ],"ok" ) 350self .assertEqual (result ["report" ]["live_gpu_probe" ]["selected_indices" ], [0 ,1 ,2 ,3 ]) 351self .assertEqual (len (result ["report" ]["live_gpu_probe" ]["cards" ]),4 ) 352self .assertEqual (probe .call_args .kwargs ["timeout" ],5 ) 353self .assertFalse (probe .call_args .kwargs .get ("shell" ,False )) 354 355with mock .patch ("strata_control.doctor.shutil.which" ,return_value = "/fake/nvidia-smi" ), \ 356mock .patch ("strata_control.doctor.subprocess.run" , 357return_value = mock .Mock (returncode = 0 ,stdout = cards .rsplit ("\n" ,1 )[0 ])): 358code ,result = self .command ("components" ,"verify" ,"--profile" ,"four-gpu" ,"--live" , 359"--output" ,"json" ) 360self .assertEqual (code ,5 ) 361self .assertEqual (result ["report" ]["live_gpu_probe" ]["status" ],"missing_selected" ) 362self .assertTrue (any (i ["code" ]== "GPU_NOT_VISIBLE" for i in result ["report" ]["issues" ])) 363 364def test_live_probe_failure_and_unsupported_hip_are_explicit (self ): 365config = json .loads (self .config .read_text (encoding = "utf-8" )) 366config ["gpu" ]= [0 ,1 ] 367config ["backend" ]= "cuda" 368self .config .write_text (json .dumps (config ),encoding = "utf-8" ) 369profiles .create ("local" ,str (self .config )) 370with mock .patch ("strata_control.doctor.shutil.which" ,return_value = None ): 371code ,result = self .command ("doctor" ,"--profile" ,"local" ,"--live" ,"--output" ,"json" ) 372self .assertEqual (code ,5 ) 373self .assertEqual (result ["report" ]["live_gpu_probe" ]["status" ],"unavailable" ) 374self .assertTrue (any (i ["code" ]== "LIVE_GPU_PROBE_UNAVAILABLE" for i in result ["report" ]["issues" ])) 375 376config ["backend" ]= "hip" 377self .config .write_text (json .dumps (config ),encoding = "utf-8" ) 378with mock .patch ("strata_control.doctor.subprocess.run" , 379side_effect = AssertionError ("NVIDIA probe must not run for HIP" )): 380code ,result = self .command ("doctor" ,"--profile" ,"local" ,"--live" ,"--output" ,"json" ) 381self .assertEqual (code ,5 ) 382self .assertEqual (result ["report" ]["live_gpu_probe" ]["status" ],"unverified" ) 383 384def test_doctor_default_does_not_run_host_probe (self ): 385profiles .create ("local" ,str (self .config )) 386with mock .patch ("strata_control.doctor.subprocess.run" , 387side_effect = AssertionError ("default doctor must be offline" )): 388code ,result = self .command ("doctor" ,"--profile" ,"local" ,"--output" ,"json" ) 389self .assertEqual (code ,0 ) 390self .assertIsNone (result ["report" ]["live_gpu_probe" ]) 391with mock .patch ("strata_control.doctor.subprocess.run" , 392side_effect = AssertionError ("a profile is required for live probes" )): 393code ,missing = self .command ("doctor" ,"--live" ,"--output" ,"json" ) 394self .assertEqual (code ,5 ) 395self .assertTrue (any (i ["code" ]== "PROFILE_NOT_SELECTED_FOR_LIVE_PROBE" 396for i in missing ["report" ]["issues" ])) 397 398def test_live_probe_rejects_untrusted_card_output_and_timeouts (self ): 399config = json .loads (self .config .read_text (encoding = "utf-8" )) 400config ["gpu" ]= 0 401config ["backend" ]= "cuda" 402self .config .write_text (json .dumps (config ),encoding = "utf-8" ) 403profiles .create ("local" ,str (self .config )) 404for output in ("0, GPU\x1b[31m, GPU-0, 1000, 580.95" , 405"0, GPU, GPU-0, 1000, 580.95\n0, GPU, GPU-0, 1000, 580.95" , 406"0, GPU, GPU-0, not-a-number, 580.95" ): 407with self .subTest (output = output ), \ 408mock .patch ("strata_control.doctor.shutil.which" ,return_value = "/fake/nvidia-smi" ), \ 409mock .patch ("strata_control.doctor.subprocess.run" , 410return_value = mock .Mock (returncode = 0 ,stdout = output )): 411code ,result = self .command ("doctor" ,"--profile" ,"local" ,"--live" ,"--output" ,"json" ) 412self .assertEqual (code ,5 ) 413self .assertEqual (result ["report" ]["live_gpu_probe" ]["status" ],"unavailable" ) 414with mock .patch ("strata_control.doctor.shutil.which" ,return_value = "/fake/nvidia-smi" ), \ 415mock .patch ("strata_control.doctor.subprocess.run" , 416side_effect = subprocess .TimeoutExpired ("nvidia-smi" ,5 )): 417code ,result = self .command ("doctor" ,"--profile" ,"local" ,"--live" ,"--output" ,"json" ) 418self .assertEqual (code ,5 ) 419self .assertEqual (result ["report" ]["live_gpu_probe" ]["status" ],"unavailable" ) 420 421def test_calibration_preview_has_no_engine_or_config_side_effects (self ): 422profiles .create ("local" ,str (self .config )) 423before = self .config .read_bytes () 424with mock .patch ("serve.server.StrataEngine" ,side_effect = AssertionError ("engine started" )), \ 425mock .patch ("tools.calibrate.run" ,side_effect = AssertionError ("measurement started" )): 426code ,result = self .command ("engine" ,"calibrate" ,"--profile" ,"local" ,"--plan" ,"--output" ,"json" ) 427self .assertEqual (code ,0 ) 428self .assertFalse (result ["plan" ]["would_start_engine" ]) 429self .assertFalse (result ["plan" ]["would_write_config" ]) 430self .assertEqual (self .config .read_bytes (),before ) 431 432def test_invalid_profile_names_and_types_do_not_escape_store (self ): 433for name in ("../outside" ,"UPPER" ,"a/b" ,".hidden" ): 434with self .assertRaises (profiles .ProfileError ): 435profiles .create (name ,str (self .config )) 436profiles .create ("local" ,str (self .config )) 437for assignment in ("parallel=0" ,"max_queue=99999" ,"queue_timeout=nan" , 438"queue_timeout=0" ,"offline=yes" ,"batch_telemetry=yes" ,"host=bad host" ): 439with self .assertRaises (profiles .ProfileError ): 440profiles .set_values ("local" , [assignment ]) 441profiles .set_values ("local" , ["queue_timeout=5" ]) 442with self .assertRaisesRegex (profiles .ProfileError ,"requires max_queue" ): 443from strata_cli .cli import _launch_plan 444_launch_plan ("local" ) 445 446 447if __name__ == "__main__" : 448unittest .main ()