okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7f5b34d1d
main
1"""Drafting Off (or MTP), then Auto again, in one scratch world: Auto starts the engine exactly as Auto did before (the same drafter, --spec, --draft-conf and 2batch), on every machine class and model set. Found in the shipped 0.2.13.1 by the real-use rehearsal on an RTX PRO 5000 (REAL-USE-1, block (b)): after one 3`strata launch --drafting off`, `strata launch --drafting auto` started with no drafter (the Off line, --spec 4 --batch 4) and stayed so. There the launch named 4--model-path, so setup ran at every launch: the Off launch's "--mtp off" left "mtp": false in the installed config, and Auto read that as "the draft layer is off". 5 6The Auto line compared with is the second Auto launch of the world: on the UD sets the first launch fetches the plain DFlash2 pack and the next one converts it 7for the selector (the same with Auto, Auto and nothing else), which is not what this test is about. An explicit Off or MTP stays what it is, and a start 8without a --drafting word after Off stays Off. Nothing real is used (tests/snapshot/README.md: scratch HOME, data folder, install folder). 9 10python -m pytest tests/snapshot/test_drafting_round_trip.py -q -p no:cacheprovider 11""" 12 13from __future__import annotations 14 15import json 16import sys 17import unittest 18from pathlib import Path 19 20ROOT = Path (__file__ ).resolve ().parents [2 ] 21if str (ROOT )not in sys .path : 22sys .path .insert (0 ,str (ROOT )) 23 24from tests .snapshot .driver import drive_launch # noqa: E402 25from tests .snapshot .matrix import MACHINES ,MODEL_SETS # noqa: E402 26from tests .snapshot .normalize import normalize # noqa: E402 27from tests .snapshot .world import World # noqa: E402 28 29 30TUI = "tui" 31 32 33def tui_drafting (world ,pick ): 34"""The TUI's Drafting step on this world's installed config, in a fresh launch (no choice in the settings yet): (the row it preselects, what its 35commit leaves in the settings). `pick` None: the person leaves the preselected row as it is.""" 36from types import SimpleNamespace 37from strata_cli .launch import settings as S 38from strata_cli .launch .screens import DraftingScreen 39base = json .loads (world .config_path .read_text (encoding = "utf-8" )) 40s = S .LaunchSettings (family = world .mset .family ,model = world .mset .model ) 41menu = SimpleNamespace (columns = None ,sel = None ,current = None ) 42menu .set_items = lambda items ,sel :setattr (menu ,"sel" ,sel ) 43fake = SimpleNamespace (ctx = SimpleNamespace (settings = s ,base_config = lambda :base ),CHOICES = DraftingScreen .CHOICES , 44query_one = lambda _cls :menu ,paint = lambda :None ,menu = lambda :menu ) 45with world .patched (): 46DraftingScreen .on_mount (fake ) 47shown = DraftingScreen .CHOICES [menu .sel ][0 ] 48menu .current = SimpleNamespace (key = pick or shown ) 49DraftingScreen .commit (fake ) 50return shown ,s .drafting 51 52 53def launches (machine ,mset ,steps ,prepare = None ): 54"""One world, one `strata launch` per (choice, setup_runs) step (choice None: no --drafting word; (TUI, pick): the TUI's Drafting step first, then the launch 55with what it saved), in order; the normalized records (a TUI step's record also has "tui": (shown, saved)). `prepare(world)` runs before the first step.""" 56world = World (machine ,mset ).build () 57try : 58if prepare is not None : 59prepare (world ) 60out = [] 61for choice ,runs in steps : 62tui = None 63if isinstance (choice ,tuple )and choice [0 ]== TUI : 64tui = tui_drafting (world ,choice [1 ]) 65choice = tui [1 ] 66with world .patched (): 67rec = drive_launch (world , ()if choice is None else ("--drafting" ,choice ),setup_runs = runs ) 68assert "failed" not in rec ,rec .get ("failed" ) 69rec = normalize (rec ,world ) 70if tui is not None : 71rec ["tui" ]= tui 72out .append (rec ) 73return out 74finally : 75world .close () 76 77 78def argv (rec :dict )-> list [str ]: 79return rec ["engine" ]["argv" ] 80 81 82def drafter (rec :dict )-> str : 83a = argv (rec ) 84return "dflash2" if "--draft" in a else "mtp" if "--mtp" in a else "off" 85 86 87class AutoAgainIsAuto (unittest .TestCase ): 88maxDiff = None 89 90def cells (self ,setup_runs = (False ,True )): 91for runs in setup_runs : 92for m in MACHINES : 93for s in MODEL_SETS : 94yield m ,s ,runs 95 96def round_trip (self ,middle :str ,back :str = "auto" ): 97for m ,s ,runs in self .cells (): 98with self .subTest (machine = m .key ,set = s .key ,setup_runs = runs ): 99recs = launches (m ,s , [("auto" ,runs ), (back ,runs ), (middle ,runs ), (back ,runs )]) 100self .assertEqual (drafter (recs [2 ]),middle ,f"the explicit { middle } stays { middle } " ) 101self .assertEqual (argv (recs [3 ]),argv (recs [1 ]),f" { back } after { middle } starts as { back } did before" ) 102self .assertEqual (recs [3 ]["config_file" ].get ("drafting" ),back ) 103 104def test_off_then_auto_is_auto (self ): 105self .round_trip ("off" ) 106 107def test_mtp_then_auto_is_auto (self ): 108self .round_trip ("mtp" ) 109 110def test_off_then_mtp_is_mtp (self ): 111self .round_trip ("off" ,back = "mtp" ) 112 113def test_auto_after_off_runs_setup_again_when_the_install_would_be_reused (self ): 114# setup ran at the Off launch only (a change of context, a --model-path), the Auto launch after it would reuse the installed copy as it is 115for m ,s ,_runs in self .cells (setup_runs = (True ,)): 116with self .subTest (machine = m .key ,set = s .key ): 117recs = launches (m ,s , [("auto" ,False ), ("auto" ,False ), ("off" ,True ), ("auto" ,"when_needed" )]) 118self .assertEqual (argv (recs [3 ]),argv (recs [1 ]),"Auto after Off starts as Auto did before" ) 119 120def test_off_stays_off_at_a_start_without_a_drafting_word (self ): 121for m ,s ,runs in self .cells (setup_runs = (True ,)): 122if m .os == "macos" : 123continue # a Mac's setup takes no --mtp word (runner.setup_args): Off is not written there, as before 124with self .subTest (machine = m .key ,set = s .key ): 125recs = launches (m ,s , [("auto" ,runs ), ("off" ,runs ), (None ,runs )]) 126self .assertEqual (argv (recs [2 ]),argv (recs [1 ]),"the Off of the launch before is kept (setup's \"mtp\": false)" ) 127 128# the TUI (`strata launch` without --no-tui): its Drafting step shows Off where the installed config's draft layer is off and no Drafting choice is saved 129def test_tui_off_then_auto_is_auto (self ): 130for m ,s ,runs in self .cells (setup_runs = (True ,)): 131if m .os == "macos" : 132continue 133with self .subTest (machine = m .key ,set = s .key ): 134recs = launches (m ,s , [("auto" ,runs ), ("auto" ,runs ), ("off" ,runs ), ((TUI ,"auto" ),runs )]) 135self .assertEqual (recs [3 ]["tui" ], ("off" ,"auto" ),"the step shows Off, and Auto picked is saved" ) 136self .assertEqual (argv (recs [3 ]),argv (recs [1 ]),"Auto picked in the TUI after Off starts as Auto did before" ) 137 138def test_tui_off_left_as_it_is_stays_off (self ): 139for m ,s ,runs in self .cells (setup_runs = (True ,)): 140if m .os == "macos" : 141continue 142with self .subTest (machine = m .key ,set = s .key ): 143recs = launches (m ,s , [("auto" ,runs ), ("off" ,runs ), ((TUI ,None ),runs )]) 144self .assertEqual (recs [2 ]["tui" ], ("off" ,None ),"Off is shown, and leaving it writes nothing new" ) 145self .assertEqual (argv (recs [2 ]),argv (recs [1 ]),"the Off line" ) 146 147def test_tui_app_draft_layer_off_left_as_it_is_stays_off (self ): 148def app_turned_the_layer_off (world ):# the app's "Draft layer (MTP)" switch: "mtp": false in the run config, no "drafting" key 149from strata_control import draft_layer 150cfg = json .loads (world .config_path .read_text (encoding = "utf-8" )) 151cfg ["mtp" ]= False 152draft_layer .apply (cfg ) 153world .config_path .write_text (json .dumps (cfg ,indent = 1 ),encoding = "utf-8" ) 154for m ,s ,_runs in self .cells (setup_runs = (True ,)): 155with self .subTest (machine = m .key ,set = s .key ): 156recs = launches (m ,s , [((TUI ,None ),"when_needed" )],prepare = app_turned_the_layer_off ) 157self .assertEqual (recs [0 ]["tui" ], ("off" ,None ),"Off is shown, and leaving it writes nothing new" ) 158self .assertEqual (drafter (recs [0 ]),"off" ,"the draft layer stays off" ) 159 160 161if __name__ == "__main__" : 162unittest .main ()