okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
c77572806
main
1"""Small fixes from the 0.2.2 issue sweep, one test class per issue number.""" 2 3from __future__import annotations 4 5import json 6import os 7import sys 8import tempfile 9import unittest 10from pathlib import Path 11 12sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [2 ])) 13 14 15class Issue3298MtpDepth (unittest .TestCase ): 16"""Qwen3.6 passes `--mtp 2` (a draft depth); it is not a folder.""" 17 18def setUp (self ): 19self .tmp = tempfile .TemporaryDirectory () 20self .addCleanup (self .tmp .cleanup ) 21self .root = Path (self .tmp .name ) 22 23def test_doctor_does_not_look_for_a_folder_named_2 (self ): 24from strata_control import doctor 25engine = self .root / "engine" 26engine .write_text ("x" ,encoding = "utf-8" ) 27engine .chmod (0o700 ) 28tok = self .root / "tok" 29tok .mkdir () 30for n in ("vocab.json" ,"merges.txt" ,"token_type.json" ): 31 (tok / n ).write_text ("{}" ,encoding = "utf-8" ) 32model = self .root / "m.gguf" 33model .write_bytes (b"x" ) 34cfg = self .root / "run.json" 35cfg .write_text (json .dumps ({"exe" :str (engine ),"args" : ["--native" ,str (model ),"--mtp" ,"2" ,"--vision" ], 36"cwd" :str (self .root ),"tokenizer" :str (tok ),"model_name" :"q36" ,"port" :8095 }), 37encoding = "utf-8" ) 38report = doctor .inspect (cfg ) 39self .assertNotIn ("mtp" , [c ["name" ]for c in report ["components" ]]) 40self .assertEqual ([c ["name" ]for c in report ["components" ]if c ["status" ]not in ("ok" ,) 41and c .get ("required" )and c ["name" ]in ("native" ,"mtp" ,"pack" )], []) 42 43def test_doctor_still_checks_an_mtp_folder (self ): 44from strata_control import doctor 45engine = self .root / "engine" 46engine .write_text ("x" ,encoding = "utf-8" ) 47engine .chmod (0o700 ) 48tok = self .root / "tok" 49tok .mkdir () 50for n in ("vocab.json" ,"merges.txt" ,"token_type.json" ): 51 (tok / n ).write_text ("{}" ,encoding = "utf-8" ) 52cfg = self .root / "run.json" 53cfg .write_text (json .dumps ({"exe" :str (engine ),"args" : ["--mtp" ,str (self .root / "gone" )], 54"cwd" :str (self .root ),"tokenizer" :str (tok ),"model_name" :"x" ,"port" :8095 }), 55encoding = "utf-8" ) 56report = doctor .inspect (cfg ) 57mtp = [c for c in report ["components" ]if c ["name" ]== "mtp" ] 58self .assertEqual (len (mtp ),1 ) 59self .assertEqual (mtp [0 ]["status" ],"missing" ) 60 61def test_launch_files_present_ignores_a_depth_number (self ): 62from strata_cli .launch .runner import files_present 63model = self .root / "m.gguf" 64model .write_bytes (b"x" ) 65self .assertEqual (files_present ({"args" : ["--native" ,str (model ),"--mtp" ,"2" ]}), []) 66self .assertEqual (files_present ({"args" : ["--mtp" ,str (self .root / "gone" )]}), [str (self .root / "gone" )]) 67 68 69class Issue3403MacBuildTools (unittest .TestCase ): 70"""The Mac setup compiles without setup.cmake_build(), so cmake and ninja must be installed for it too.""" 71 72def test_missing_build_tools_are_installed_before_the_mac_compile (self ): 73from unittest import mock 74import setup 75sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [2 ]/ "metal" )) 76import setup_mac as M 77installed = {"cmake" :None ,"ninja" :None } 78 79def find (name ): 80return installed [name ] 81 82def pip (packages ,what ): 83self .assertTrue (all (p .split ("==" )[0 ]in ("cmake" ,"ninja" )for p in packages ),packages ) 84for p in packages : 85installed [p .split ("==" )[0 ]]= "/venv/bin/" + p .split ("==" )[0 ] 86 87with mock .patch .object (setup ,"find_tool" ,side_effect = find ),mock .patch .object (setup ,"pip_install" ,side_effect = pip )as pi ,mock .patch .object (setup ,"_tool_version" ,return_value = (True , (3 ,31 ,0 ))): 88self .assertEqual (setup .ensure_build_tools (), ("/venv/bin/cmake" ,"/venv/bin/ninja" )) 89self .assertEqual (pi .call_count ,1 ) 90setup .ensure_build_tools ()# present now: nothing more is installed 91self .assertEqual (pi .call_count ,1 ) 92 93def test_both_mac_builds_ask_for_the_tools_first (self ): 94import inspect 95sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [2 ]/ "metal" )) 96import setup_mac as M 97for fn in (M .build_engine ,M .build_vision ): 98self .assertIn ("S.ensure_build_tools()" ,inspect .getsource (fn ),fn .__name__ ) 99 100 101@ unittest . skipUnless ( sys . platform == "win32" , "ACLs are Windows only" ) 102class WindowsRuntimeFolderRefused (unittest .TestCase ): 103"""The owner's fresh Windows PC: `strata launch` stopped with "runtime directory must be owner-only" on a folder that was already 104there with another account in its ACL. Fake SDDL strings for each way an ACL can look.""" 105 106def setUp (self ): 107from unittest import mock 108from strata_control import private_files as pf 109self .pf ,self .mock = pf ,mock 110self .me = "S-1-5-21-1-2-3-1001" 111patch = mock .patch .object (pf ,"current_user_sid" ,return_value = self .me ) 112patch .start () 113self .addCleanup (patch .stop ) 114 115def offenders (self ,sddl ): 116return self .pf .unshared_offenders (sddl ) 117 118def test_the_usual_acls_are_fine (self ): 119for sddl in ("D:PAI(A;OICI;FA;;;S-1-5-21-1-2-3-1001)" , 120"D:PAI(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;FA;;;S-1-5-21-1-2-3-1001)" , 121"D:PAI(A;OICI;FA;;;LA)(A;OICI;FA;;;SY)" ,# the built-in Administrator's alias 122"D:P(A;OICI;FA;;;S-1-5-18)(A;OICI;FA;;;S-1-5-32-544)(A;OICIIO;GA;;;CO)(A;OICI;FA;;;OW)" ): 123self .assertEqual (self .offenders (sddl ), [],sddl ) 124 125def test_another_account_is_named (self ): 126self .assertEqual (self .offenders ("D:PAI(A;OICI;FA;;;S-1-5-21-1-2-3-1001)(A;OICI;FR;;;BU)" ), ["BU" ]) 127self .assertEqual (self .offenders ("D:PAI(A;;FA;;;WD)(A;;FA;;;AU)" ), ["WD" ,"AU" ]) 128self .assertEqual (self .offenders ("D:PAI(A;OICI;FA;;;S-1-5-21-1-2-3-1002)" ), ["S-1-5-21-1-2-3-1002" ]) 129 130def test_an_inherit_only_entry_is_not_for_the_folder_itself (self ): 131self .assertEqual (self .offenders ("D:PAI(A;OICI;FA;;;S-1-5-21-1-2-3-1001)(A;OICIIO;FR;;;BU)(A;CIIO;GR;;;WD)" ), []) 132# the same entry without the IO flag does apply 133self .assertEqual (self .offenders ("D:PAI(A;OICI;FR;;;BU)" ), ["BU" ]) 134 135def test_deny_entries_do_not_make_it_reachable (self ): 136self .assertEqual (self .offenders ("D:PAI(D;;FA;;;BU)(A;OICI;FA;;;S-1-5-21-1-2-3-1001)" ), []) 137 138def test_plain_names (self ): 139self .assertEqual (self .pf .account_name ("BU" ),"Users" ) 140self .assertEqual (self .pf .account_name ("WD" ),"Everyone" ) 141self .assertEqual (self .pf .account_name ("S-1-5-32-545" ),"Users" ) 142 143def test_runtime_dir_repairs_a_folder_that_names_other_accounts (self ): 144from strata_control import runtime 145pf = self .pf 146with tempfile .TemporaryDirectory ()as tmp : 147base = Path (tmp )/ "runtime" 148base .mkdir () 149state = {"fixed" :False } 150 151def sddl (_path ): 152return "D:PAI(A;OICI;FA;;;S-1-5-21-1-2-3-1001)" if state ["fixed" ]else \ 153"D:PAI(A;OICI;FA;;;S-1-5-21-1-2-3-1001)(A;OICI;0x1200a9;;;BU)" 154 155def restrict (path ,directory = None ): 156state ["fixed" ]= True 157 158env = {"STRATA_RUNTIME_DIR" :str (base )} 159with self .mock .patch .dict (os .environ ,env ),self .mock .patch .object (pf ,"dacl_sddl" ,side_effect = sddl ), \ 160self .mock .patch .object (pf ,"restrict_to_current_user" ,side_effect = restrict )as r , \ 161self .mock .patch .object (pf ,"private_mkdir" ),self .mock .patch .object (runtime ,"_private_mkdir" ): 162self .assertEqual (runtime .runtime_dir (),base ) 163self .assertEqual (r .call_count ,1 ) 164 165def test_runtime_dir_still_refuses_and_says_who_when_the_repair_fails (self ): 166from strata_control import profiles ,runtime 167pf = self .pf 168with tempfile .TemporaryDirectory ()as tmp : 169base = Path (tmp )/ "runtime" 170base .mkdir () 171with self .mock .patch .dict (os .environ , {"STRATA_RUNTIME_DIR" :str (base )}), \ 172self .mock .patch .object (pf ,"dacl_sddl" ,return_value = "D:PAI(A;OICI;FA;;;S-1-5-21-1-2-3-1001)(A;OICI;FR;;;BU)" ), \ 173self .mock .patch .object (pf ,"restrict_to_current_user" ,side_effect = OSError ("denied" )), \ 174self .mock .patch .object (runtime ,"_private_mkdir" ): 175with self .assertRaises (profiles .ProfileError )as cm : 176runtime .runtime_dir () 177self .assertIn ("Users" ,str (cm .exception )) 178self .assertIn (str (base ),str (cm .exception )) 179self .assertIn ("Properties" ,str (cm .exception )) 180 181 182@ unittest . skipUnless ( sys . platform == "win32" , "ACLs are Windows only" ) 183class WindowsRuntimeFolderMoreCases (unittest .TestCase ): 184"""The cases beyond the first repair (audit-refusals-022.md item 5): a data drive's inherited Users entries, an ACL that cannot be 185read, a folder made by an elevated run, a drive without permissions, and a message in plain words.""" 186 187ME = "S-1-5-21-1-2-3-1001" 188 189def run_dir (self ,* ,sddl ,restrict = None ,repair = None ,acls = True ): 190from unittest import mock 191from strata_control import private_files as pf ,runtime 192with tempfile .TemporaryDirectory ()as tmp : 193base = Path (tmp )/ "runtime" 194base .mkdir () 195calls = {"restrict" :0 ,"repair" :0 } 196 197def r (path ,directory = None ): 198calls ["restrict" ]+= 1 199if restrict : 200return restrict (calls ) 201def rp (path ,tree = True ): 202calls ["repair" ]+= 1 203return repair (calls )if repair else False 204with mock .patch .dict (os .environ , {"STRATA_RUNTIME_DIR" :str (base )}), \ 205mock .patch .object (pf ,"current_user_sid" ,return_value = self .ME ), \ 206mock .patch .object (pf ,"dacl_sddl" ,side_effect = sddl ), \ 207mock .patch .object (pf ,"restrict_to_current_user" ,side_effect = r ), \ 208mock .patch .object (pf ,"repair_access" ,side_effect = rp ), \ 209mock .patch .object (pf ,"volume_has_acls" ,return_value = acls ), \ 210mock .patch .object (runtime ,"_private_mkdir" ),mock .patch .object (runtime ,"_NO_ACL_WARNED" ,set ()), \ 211mock .patch ("sys.stderr" ,new_callable = lambda :__import__ ("io" ).StringIO ())as err : 212try : 213return runtime .runtime_dir (),calls ,None ,err .getvalue () 214except Exception as exc :# noqa: BLE001 - the test looks at the ProfileError text 215return None ,calls ,exc ,err .getvalue () 216 217def test_a_data_drive_folder_with_users_and_authenticated_users_is_repaired_once (self ): 218state = {"fixed" :False } 219def sddl (_ ): 220return f"D:PAI(A;OICI;FA;;; { self . ME } )" if state ["fixed" ]else \ 221f"D:PAI(A;OICI;FA;;; { self . ME } )(A;OICI;0x1301bf;;;AU)(A;OICI;0x1200a9;;;BU)" 222base ,calls ,exc ,_ = self .run_dir (sddl = sddl ,restrict = lambda c :state .update (fixed = True )) 223self .assertIsNone (exc ) 224self .assertEqual (calls ["restrict" ],1 ) 225 226def test_an_acl_that_cannot_be_read_is_repaired_then_refused_in_plain_words (self ): 227def sddl (_ ): 228raise OSError ("access denied" ) 229base ,calls ,exc ,_ = self .run_dir (sddl = sddl ) 230self .assertIsNotNone (exc ) 231self .assertEqual ((calls ["restrict" ],calls ["repair" ]), (1 ,1 ))# tried both, once each 232self .assertIn ("cannot read the permissions" ,str (exc )) 233self .assertNotIn ("0700" ,str (exc )) 234 235def test_a_folder_made_by_an_elevated_run_gets_ownership_taken_back (self ): 236state = {"fixed" :False } 237def sddl (_ ): 238if not state ["fixed" ]: 239raise OSError ("access denied" ) 240return f"D:PAI(A;OICI;FA;;; { self . ME } )" 241def restrict (calls ): 242raise PermissionError ("denied" ) 243base ,calls ,exc ,_ = self .run_dir (sddl = sddl ,restrict = restrict ,repair = lambda c :state .update (fixed = True )or True ) 244self .assertIsNone (exc ) 245self .assertEqual ((calls ["restrict" ],calls ["repair" ]), (1 ,1 )) 246 247def test_a_drive_without_permissions_is_used_with_one_warning (self ): 248def sddl (_ ): 249raise AssertionError ("FAT has no ACL to read" ) 250base ,calls ,exc ,err = self .run_dir (sddl = sddl ,acls = False ) 251self .assertIsNone (exc ) 252self .assertEqual ((calls ["restrict" ],calls ["repair" ]), (0 ,0 )) 253self .assertIn ("no file permissions" ,err ) 254 255def test_the_refusal_names_the_accounts_and_never_says_mode_0700 (self ): 256sddl = lambda _ :f"D:PAI(A;OICI;FA;;; { self . ME } )(A;OICI;FR;;;AU)(A;OICI;FR;;;WD)" # noqa: E731 257base ,calls ,exc ,_ = self .run_dir (sddl = sddl ) 258text = str (exc ) 259self .assertIn ("Authenticated Users" ,text ) 260self .assertIn ("Everyone" ,text ) 261self .assertNotIn ("0700" ,text ) 262self .assertEqual (calls ["restrict" ],1 ) 263 264def test_the_real_system_drive_has_permissions (self ): 265from strata_control import private_files as pf 266self .assertTrue (pf .volume_has_acls (os .environ .get ("SystemRoot" ,"C:\\Windows" ))) 267 268 269class Issue3410TrashIsNotAnInstall (unittest .TestCase ): 270def test_trash_folders_are_recognised (self ): 271from strata_control .discovery import in_trash 272for p in ("/home/u/.local/share/Trash/files/strata" ,"/mnt/d/.Trash-1000/files/Strata" ,"/Users/u/.Trash/Strata" , 273"C:\\$Recycle.Bin\\S-1-5-21\\$R123\\Strata" ,"/home/u/.local/share/trash/files/strata/x" ): 274self .assertTrue (in_trash (p ),p ) 275for p in ("/home/u/bin/strata" ,"/home/u/Trash-talk/strata" ,"/home/u/trash/strata" ,"/opt/Strata" , 276"C:\\Users\\u\\Strata" ): 277self .assertFalse (in_trash (p ),p ) 278 279def test_remembered_installs_in_the_trash_are_not_listed (self ): 280from strata_control import discovery 281with tempfile .TemporaryDirectory ()as tmp : 282good = Path (tmp )/ "bin" / "strata" 283trash = Path (tmp )/ ".local" / "share" / "Trash" / "files" / "strata" 284got = discovery ._installs_in ({"installs" : [str (trash ),str (good )]}) 285self .assertEqual (got , [good ]) 286 287def test_setup_does_not_pass_a_trash_folder_on (self ): 288import setup 289with tempfile .TemporaryDirectory ()as tmp : 290trash = Path (tmp )/ ".local" / "share" / "Trash" / "files" / "strata" 291trash .mkdir (parents = True ) 292 (trash / "setup.py" ).write_text ("x" ) 293from unittest import mock 294nowhere = Path (tmp )/ "none" / "none" / "home" # no Strata* folders beside it on this PC 295with mock .patch .object (setup ,"home" ,return_value = nowhere ): 296self .assertEqual (setup .other_installs ({"installs" : [str (trash )]}), []) 297self .assertEqual (setup .other_installs ({"installs" : [str (trash )]+ [str (trash .parent .parent )]}), []) 298 299 300class Issue2971WebToolRowFitsPhones (unittest .TestCase ): 301"""A tool row widened the whole page on a phone (752 px page at a 412 px viewport, measured in Chrome). The three rules 302that keep it inside the viewport; a long tool name is cut with an ellipsis instead of pushing the page.""" 303 304def test_the_rules_are_in_the_stylesheet (self ): 305import re 306css = (Path (__file__ ).resolve ().parents [2 ]/ "serve" / "web" / "app.css" ).read_text (encoding = "utf-8" ) 307 308def rule (selector ): 309m = re .search (r"(?m)^" + re .escape (selector )+ r"\s*\{([^}]*)\}" ,css ) 310self .assertIsNotNone (m ,selector ) 311return m .group (1 ) 312 313self .assertIn ("min-width: 0" ,rule (".view" )) 314self .assertIn ("overflow: hidden" ,rule (".tool-call > summary" )) 315name = rule (".tool-call__name" ) 316for part in ("min-width: 0" ,"overflow: hidden" ,"text-overflow: ellipsis" ): 317self .assertIn (part ,name ) 318 319 320class Issue3188BenchStartsItsOwnServer (unittest .TestCase ): 321"""tools/strata_bench.py named `port` in the start-a-server branch without ever setting it.""" 322 323def test_the_start_branch_runs_with_the_config_port (self ): 324from unittest import mock 325from tools import strata_bench as sb 326import io 327from contextlib import redirect_stdout 328with tempfile .TemporaryDirectory ()as tmp : 329cfg = Path (tmp )/ "run.json" 330cfg .write_text (json .dumps ({"port" :18999 ,"args" : []}),encoding = "utf-8" ) 331started = [] 332with mock .patch .object (sb ,"is_strata" ,return_value = False ), \ 333mock .patch .object (sb ,"try_json" ,return_value = None ), \ 334mock .patch .object (sb ,"start_server" ,side_effect = lambda c ,p ,g ,log :started .append (p )or object ()), \ 335mock .patch .object (sb ,"wait_ready" ,return_value = False ), \ 336mock .patch .object (sb ,"stop_server" ): 337out = io .StringIO () 338with redirect_stdout (out ),mock .patch ("sys.stderr" ,io .StringIO ()): 339code = sb .main (["--config" ,str (cfg )]) 340self .assertEqual (code ,1 ) 341self .assertEqual (started , [18999 ]) 342self .assertIn ("on port 18999" ,out .getvalue ()) 343started .clear () 344with redirect_stdout (io .StringIO ()),mock .patch ("sys.stderr" ,io .StringIO ()): 345sb .main (["--config" ,str (cfg ),"--port" ,"18777" ]) 346self .assertEqual (started , [18777 ]) 347 348 349@ unittest . skipUnless ( sys . platform == "win32" , "SDDL aliases exist on Windows only" ) 350class Issue3302AdministratorAlias (unittest .TestCase ): 351"""SDDL prints the built-in Administrator (RID 500) as `LA`; the owner-only checks must still accept it.""" 352 353def setUp (self ): 354from unittest import mock 355from strata_control import private_files as pf 356self .pf = pf 357self .mock = mock 358# the built-in Administrator's string SID on this machine: resolve the alias the same way the fix does 359import ctypes 360from ctypes import wintypes 361pointer = ctypes .c_void_p () 362pf ._advapi .ConvertStringSidToSidW ("LA" ,ctypes .byref (pointer )) 363text = wintypes .LPWSTR () 364pf ._advapi .ConvertSidToStringSidW (pointer .value ,ctypes .byref (text )) 365self .admin_sid = text .value 366self .assertTrue (self .admin_sid .endswith ("-500" )) 367 368def test_alias_matches_the_account_it_names (self ): 369self .assertTrue (self .pf ._same_sid ("LA" ,self .admin_sid )) 370self .assertTrue (self .pf ._same_sid (self .admin_sid ,self .admin_sid )) 371self .assertFalse (self .pf ._same_sid ("LA" ,"S-1-5-21-1-2-3-1001" )) 372self .assertFalse (self .pf ._same_sid ("WD" ,self .admin_sid )) 373self .assertFalse (self .pf ._same_sid ("S-1-5-21-1-2-3-1001" ,self .admin_sid )) 374 375def test_private_and_unshared_hold_for_the_built_in_administrator (self ): 376pf = self .pf 377with self .mock .patch .object (pf ,"current_user_sid" ,return_value = self .admin_sid ), \ 378self .mock .patch .object (pf ,"dacl_sddl" ,return_value = "D:PAI(A;OICI;FA;;;LA)" ): 379self .assertTrue (pf .is_private ("x" )) 380self .assertTrue (pf .is_unshared ("x" )) 381with self .mock .patch .object (pf ,"current_user_sid" ,return_value = self .admin_sid ), \ 382self .mock .patch .object (pf ,"dacl_sddl" ,return_value = "D:PAI(A;OICI;FA;;;S-1-5-21-1-2-3-1001)" ): 383self .assertFalse (pf .is_private ("x" )) 384self .assertFalse (pf .is_unshared ("x" )) 385with self .mock .patch .object (pf ,"current_user_sid" ,return_value = self .admin_sid ), \ 386self .mock .patch .object (pf ,"dacl_sddl" ,return_value = "D:PAI(A;OICI;FA;;;LA)(A;OICI;FR;;;WD)" ): 387self .assertFalse (pf .is_private ("x" )) 388self .assertFalse (pf .is_unshared ("x" )) 389 390 391 392class Issue3488IntegrityRetry (unittest .TestCase ): 393"""A hash that reads wrong once (file just unpacked, or held by a virus scanner) must not stick.""" 394 395def test_second_read_decides (self ): 396from unittest import mock 397from strata_control import engine_component as ec 398path = Path ("x" ) 399with mock .patch .object (ec ,"_sha256" ,side_effect = ["bad" ,"good" ])as sha ,mock .patch .object (ec .time ,"sleep" ): 400self .assertEqual (ec ._hash_settled (path ,"good" ),"good" ) 401self .assertEqual (sha .call_count ,2 ) 402with mock .patch .object (ec ,"_sha256" ,side_effect = [PermissionError (),"good" ]),mock .patch .object (ec .time ,"sleep" ): 403self .assertEqual (ec ._hash_settled (path ,"good" ),"good" ) 404with mock .patch .object (ec ,"_sha256" ,return_value = "bad" ),mock .patch .object (ec .time ,"sleep" ): 405self .assertEqual (ec ._hash_settled (path ,"good" ),"bad" )# still wrong after the retry: refuse 406 407 408 409class Issue3501TrashOnPosix (unittest .TestCase ): 410"""A Windows Recycle Bin path read on Linux: Path does not split backslashes there.""" 411 412def test_windows_path_on_a_posix_path_class (self ): 413from pathlib import PurePosixPath 414from unittest import mock 415from strata_control import discovery 416with mock .patch .object (discovery ,"Path" ,PurePosixPath ): 417self .assertTrue (discovery .in_trash (r"C:\$Recycle.Bin\S-1-5-21\$R123\Strata" )) 418self .assertTrue (discovery .in_trash ("/mnt/d/$RECYCLE.BIN/S-1/Strata" )) 419self .assertFalse (discovery .in_trash (r"C:\Users\a\Strata" )) 420 421 422 423class Issue3504MultiValueArgs (unittest .TestCase ): 424"""Advanced settings: a two-value option is two argv entries, `off` removes all of its values, an empty value is no bare flag.""" 425 426ARGS = ["--max-context" ,"131072" ,"--control-vector-layer-range" ,"4" ,"44" ,"--cvec-dir" ,"per-layer" , 427"--conversation-cache-mib" ,"8192" ] 428 429def test_two_values_are_two_entries (self ): 430from strata_control import tune_knobs 431for typed in ("4,44" ,"4 44" ,"4-44" ): 432out = tune_knobs .apply_to_argv (self .ARGS , {"--control-vector-layer-range" :typed }) 433i = out .index ("--control-vector-layer-range" ) 434self .assertEqual (out [i + 1 :i + 3 ], ["4" ,"44" ],typed ) 435self .assertEqual (out .count ("--control-vector-layer-range" ),1 ) 436 437def test_off_removes_the_option_and_every_value (self ): 438from strata_cli .launch import settings as S 439from strata_control import tune_knobs 440extra = {"--control-vector-layer-range" :"off" ,"--cvec-dir" :"off" } 441a ,_ = S .as_argv_changes (extra ,tune_knobs .BOOL_FLAGS ) 442out = tune_knobs .apply_to_argv (self .ARGS ,a ) 443self .assertEqual (out , ["--max-context" ,"131072" ,"--conversation-cache-mib" ,"8192" ]) 444 445def test_an_empty_value_is_no_bare_flag (self ): 446from strata_cli .launch import settings as S 447from strata_control import tune_knobs 448a ,_ = S .as_argv_changes ({"--cvec-dir" :"" ,"--kv-grow" :"" },tune_knobs .BOOL_FLAGS ) 449self .assertEqual (a , {"--kv-grow" :True }) 450self .assertEqual (tune_knobs .apply_to_argv (self .ARGS ,a )[:- 1 ].count ("--cvec-dir" ),1 ) 451 452def test_set_flag_and_the_table_keep_both_values (self ): 453from strata_cli .launch import params 454from strata_control import user_args 455self .assertEqual (user_args .set_flag ([],"--control-vector-layer-range" ,"4,44" ), 456 ["--control-vector-layer-range" ,"4" ,"44" ]) 457self .assertEqual (params .argv_to_dict (self .ARGS )["--control-vector-layer-range" ],"4 44" ) 458 459 460if __name__ == "__main__" : 461unittest .main () 462 463 464class DoctorAndTheDrafter (unittest .TestCase ): 465"""One drafter per run: a model whose drafter is DFlash2 has no MTP layer on disk and is still usable; with the MTP chosen the folder is required.""" 466 467def report (self ,** extra ): 468import tempfile 469from pathlib import Path 470from strata_control import doctor 471root = Path (tempfile .mkdtemp ()) 472engine = root / "engine" 473engine .write_text ("x" ,encoding = "utf-8" ) 474engine .chmod (0o700 ) 475tok = root / "tok" 476tok .mkdir () 477for n in ("vocab.json" ,"merges.txt" ,"token_type.json" ): 478 (tok / n ).write_text ("{}" ,encoding = "utf-8" ) 479cfg = root / "run.json" 480cfg .write_text (json .dumps ({"exe" :str (engine ),"args" : ["--mtp" ,str (root / "gone" )],"cwd" :str (root ),"tokenizer" :str (tok ), 481"model_name" :"qwen3.8-flash-next-iq3_s" ,"port" :8095 ,** extra }),encoding = "utf-8" ) 482return [c for c in doctor .inspect (cfg )["components" ]if c ["name" ]== "mtp" ][0 ] 483 484def test_the_layer_is_optional_only_when_the_drafter_is_dflash2 (self ): 485from unittest import mock 486from strata_control import drafting 487with mock .patch .object (drafting ,"detect_hardware" ,return_value = ("cuda" , ["sm_120" ])): 488self .assertEqual (self .report ()["status" ],"optional_missing" ,"Auto is DFlash2 here" ) 489self .assertEqual (self .report (drafting = "dflash2" )["status" ],"optional_missing" ) 490self .assertEqual (self .report (drafting = "mtp" )["status" ],"missing" ,"the MTP is chosen: its layer is required" ) 491self .assertEqual (self .report (mtp = False )["status" ],"missing" ,"unchanged for a config that switched the layer off by hand" ) 492with mock .patch .object (drafting ,"detect_hardware" ,return_value = ("sycl" , [])): 493self .assertEqual (self .report ()["status" ],"optional_missing" ,"Intel is DFlash2 too" ) 494with mock .patch .object (drafting ,"detect_hardware" ,return_value = ("cpu" , [])): 495self .assertEqual (self .report ()["status" ],"missing" ,"an engine without the code stays on the MTP" ) 496 497def test_a_model_saved_with_off_needs_no_mtp_folder (self ): 498"""0.2.15 release test (the app, after a restart): a model saved with Drafting Off and no MTP folder was "missing rt" and could not load.""" 499from unittest import mock 500from strata_control import drafting 501for hardware in (("cuda" , ["sm_120" ]), ("cuda" , ["sm_60" ]), ("hip" , ["gfx1201" ]), ("cpu" , [])): 502with mock .patch .object (drafting ,"detect_hardware" ,return_value = hardware ): 503self .assertEqual (self .report (drafting = "off" )["status" ],"optional_missing" ,hardware ) 504self .assertEqual (self .report (drafting = "OFF" )["status" ],"optional_missing" ,hardware ) 505self .assertEqual (self .report (drafting = "mtp" )["status" ],"missing" ,"the MTP chosen still needs its layer" )