okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1"""Release 0.2.4 TUI stability walk: every screen of `strata launch`, every key, for a Qwen pick, at 80x24 and a wide 2terminal, on several computers, with back/escape on each screen and a seeded random-key fuzz. Nothing may raise, hang or leave a blank 3screen; the app must still be alive after every key. The same file runs on Windows and Linux.""" 4 5from __future__import annotations 6 7import asyncio 8import os 9import random 10import unittest 11from unittest import mock 12 13from strata_cli .launch import access ,store 14from strata_cli .launch import settings as S 15from strata_cli .launch .app import LaunchApp 16from tests .terminal .launch_support import HW_12 ,HW_24 ,HW_2X16 ,Home ,text_of 17from tests .terminal .test_launch_tui import FakeRunner ,make_ctx 18 19HW_H100 = {"ram_gb" :1024.0 ,"os" :"Test OS" ,"cpu" : {"avx2" :True ,"name" :"test cpu" }, 20"gpus" : [{"index" :0 ,"name" :"NVIDIA H100 80GB HBM3" ,"vram_gb" :80.0 ,"usable" :True ,"vendor" :"nvidia" }]} 21HW_NOGPU = {"ram_gb" :16.0 ,"os" :"Test OS" ,"cpu" : {"avx2" :True ,"name" :"test cpu" },"gpus" : []} 22HW_AMD = {** HW_12 ,"gpus" : [{"index" :0 ,"name" :"AMD Radeon RX 9070 XT" ,"vram_gb" :16.0 ,"usable" :True ,"vendor" :"amd" }]} 23 24SIZES = {"narrow" : (80 ,24 ),"wide" : (200 ,60 )} 25PICKS = {"qwen" : ("qwen" ,"IQ3_XXS" )} 26# every key a person can press on any screen except q / ctrl+c (they leave on purpose and are tested on their own) 27KEYS = ["enter" ,"escape" ,"up" ,"down" ,"left" ,"right" ,"pageup" ,"pagedown" ,"home" ,"end" ,"tab" ,"delete" ,"backspace" , 28"question_mark" ,"f1" ,"slash" ,"space" ,"plus" ,"minus" ,"1" ,"2" ,"3" ,"a" ,"c" ,"d" ,"e" ,"h" ,"k" ,"m" ,"n" ,"o" ,"p" ,"r" ,"t" ,"x" ] 29 30 31def alive (app )-> bool : 32return app .is_running and app ._exception is None 33 34 35async def settle (pilot ,n = 2 ): 36await pilot .pause (0.05 * n ) 37 38 39async def dismiss_extras (app ,pilot ): 40"""Close a pop-up or an open text field the last key opened.""" 41for _ in range (4 ): 42if len (app .screen_stack )> 2 or type (app .screen ).__name__ == "ConfirmScreen" : 43await pilot .press ("escape" ) 44await settle (pilot ) 45else : 46break 47 48 49class Walk (unittest .TestCase ): 50def setUp (self ): 51self .home = Home ().__enter__ () 52self .addCleanup (self .home .__exit__ ,None ,None ,None ) 53FakeRunner .instances .clear () 54FakeRunner .behaviour = "ok" 55FakeRunner .download = 0.0 56for p in (mock .patch ("strata_cli.launch.app.Runner" ,FakeRunner ), 57mock .patch .object (access ,"lan_ips" ,return_value = ["192.168.1.5" ])): 58p .start () 59self .addCleanup (p .stop ) 60 61def drive (self ,coro ,hw = HW_12 ,size = (80 ,24 )): 62ctx = make_ctx (hw ) 63app = LaunchApp (ctx ) 64 65async def go (): 66async with app .run_test (size = size )as pilot : 67await pilot .pause (0.2 ) 68return await coro (app ,pilot ,ctx ) 69 70return asyncio .run (go ()) 71 72async def to_model (self ,app ,pilot ,pick ): 73app .screen .menu ().select (("new" ,None )) 74await pilot .press ("enter" )# "Set up from the beginning" 75await settle (pilot ,3 ) 76self .assertEqual (app .current ,"model" ) 77menu = app .screen .menu () 78menu .select (PICKS [pick ]) 79app .screen .paint () 80self .assertEqual ((app .ctx .settings .family ,app .ctx .settings .model )if False else menu .current .key ,PICKS [pick ]) 81 82# ---- 1. Enter through every screen, Escape all the way back, forward again 83def _forward_back (self ,pick ,size ,hw ): 84async def case (app ,pilot ,ctx ): 85await self .to_model (app ,pilot ,pick ) 86seen = [] 87for _ in range (20 ): 88self .assertTrue (alive (app ),f"app died on { app . current } " ) 89t = text_of (app ) 90self .assertTrue (t .strip (),f"blank screen on { app . current } " ) 91seen .append (app .current ) 92if app .current == "save" : 93break 94before = app .current 95await pilot .press ("n" if before == "advanced" else "enter" ) 96for _ in range (40 ):# wait for the next screen (a slow terminal needs more than one frame) 97await pilot .pause (0.05 ) 98if app .current != before : 99break 100self .assertEqual (seen [- 1 ],"save" ,seen ) 101for _ in range (len (seen )):# back to the Start screen (Esc on it would leave) 102before = app .current 103await pilot .press ("escape" ) 104for _ in range (40 ): 105await pilot .pause (0.05 ) 106if app .current != before : 107break 108self .assertTrue (alive (app ),f"died going back from { before } ; seen= { seen } exc= { app . _exception !r } " ) 109self .assertEqual (app .current ,"start" ) 110self .assertIn ("Set up from the beginning" ,text_of (app )) 111self .drive (case ,hw = hw ,size = size ) 112 113def test_forward_and_back_every_combination (self ): 114for hw_name ,hw in (("12" ,HW_12 ), ("h100" ,HW_H100 ), ("2x16" ,HW_2X16 ), ("nogpu" ,HW_NOGPU ), ("amd" ,HW_AMD )): 115for pick in PICKS : 116for sz ,size in SIZES .items (): 117with self .subTest (hw = hw_name ,pick = pick ,size = sz ): 118self ._forward_back (pick ,size ,hw ) 119 120# ---- 2. Every key on every screen 121def _key_sweep (self ,pick ,size ,hw ): 122async def case (app ,pilot ,ctx ): 123await self .to_model (app ,pilot ,pick ) 124for step in app .flow (): 125if app .current != step : 126app .goto (step ) 127await settle (pilot ,3 ) 128for key in KEYS : 129if key == "enter" and step == "save" :# starts the launch and leaves; test_run_paths covers it 130continue 131await pilot .press (key ) 132await settle (pilot ) 133await dismiss_extras (app ,pilot ) 134self .assertTrue (alive (app ),f"app died: key { key !r } on { step } ( { pick } , { size } )" ) 135if app .screen_stack and type (app .screen ).__name__ in ("RunScreen" ,): 136app .back_to_start ()if False else None 137if app .current != step :# the key moved on or back: return to this screen 138if app .current == "run" or app .current == "start" : 139app .current = step 140app .goto (step ) 141await settle (pilot ,2 ) 142t = text_of (app ) 143self .assertTrue (t .strip (),f"blank screen: key { key !r } on { step } " ) 144self .drive (case ,hw = hw ,size = size ) 145 146def _sweep_pick (self ,pick ,hw ): 147for sz ,size in SIZES .items (): 148with self .subTest (hw = hw ["gpus" ][0 ]["name" ]if hw ["gpus" ]else "none" ,pick = pick ,size = sz ): 149self ._key_sweep (pick ,size ,hw ) 150 151def test_every_key_on_every_screen_qwen (self ): 152self ._sweep_pick ("qwen" ,HW_12 ) 153 154# ---- 3. Seeded random keys, including the run screen 155def test_random_keys_do_not_crash (self ): 156for seed in range (3 ): 157for pick in PICKS : 158with self .subTest (seed = seed ,pick = pick ): 159rng = random .Random (seed * 31 + len (pick )) 160size = SIZES ["narrow" if seed % 2 else "wide" ] 161 162async def case (app ,pilot ,ctx ): 163await self .to_model (app ,pilot ,pick ) 164for i in range (60 ): 165await pilot .press (rng .choice (KEYS )) 166await pilot .pause (0.03 ) 167if type (app .screen ).__name__ == "ConfirmScreen" and rng .random ()< 0.4 : 168await pilot .press ("enter" ) 169self .assertTrue (alive (app )or app .return_value is None and not app .is_running , 170f"seed { seed } step { i } : exception { app . _exception !r } " ) 171if not app .is_running : 172break 173if app .is_running : 174self .assertTrue (text_of (app ).strip (),f"blank screen at step { i } " ) 175return True 176self .drive (case ,hw = HW_2X16 if seed % 3 == 0 else HW_12 ,size = size ) 177 178# ---- the Save screen says the same tuning length as the Tuning screen 179def test_save_screen_tuning_time_matches_the_model (self ): 180for pick ,want ,never in (("qwen" ,"quick (about 20 min)" ,"5-10" ),): 181with self .subTest (pick = pick ): 182async def case (app ,pilot ,ctx ): 183await self .to_model (app ,pilot ,pick ) 184for _ in range (30 ): 185if app .current == "save" : 186break 187await pilot .press ("n" if app .current == "advanced" else "enter" ) 188await settle (pilot ,3 ) 189t = " " .join (text_of (app ).split ()) 190self .assertIn (want ,t ) 191self .assertNotIn (never ,t ) 192self .drive (case ,hw = HW_12 ,size = (110 ,34 )) 193 194 195# ---- 4. q and ctrl+c leave cleanly from every screen; the Start screen's Escape too 196def test_quit_keys_leave_from_every_screen (self ): 197for key in ("q" ,"ctrl+c" ,"ctrl+q" ): 198for step in ("start" ,"model" ,"cards" ,"folder" ,"context" ,"pictures" ,"parallel" ,"tuning" ,"advanced" ,"access" ,"save" ): 199if key == "q" and step in ("folder" ,"save" ):# a text field has the focus there: q is a letter 200continue 201with self .subTest (key = key ,step = step ): 202async def case (app ,pilot ,ctx ): 203if step != "start" : 204await self .to_model (app ,pilot ,"qwen" ) 205if step in app .flow (): 206app .goto (step ) 207await settle (pilot ,3 ) 208await pilot .press (key ) 209await settle (pilot ,4 ) 210self .assertFalse (app .is_running ,f" { key } did not quit on { step } " ) 211self .assertIsNone (app ._exception ) 212self .drive (case ,hw = HW_2X16 ) 213 214# ---- 5. Resize while on each screen 215def test_resize_on_every_screen (self ): 216async def case (app ,pilot ,ctx ): 217await self .to_model (app ,pilot ,"qwen" ) 218for step in app .flow (): 219app .goto (step ) 220await settle (pilot ,3 ) 221for size in ((60 ,20 ), (200 ,60 ), (80 ,24 ), (40 ,12 ), (120 ,30 )): 222await pilot .resize_terminal (* size ) 223await settle (pilot ,3 ) 224self .assertTrue (alive (app ),f"resize { size } killed { step } " ) 225self .assertTrue (text_of (app ).strip (),f"blank at { size } on { step } " ) 226self .drive (case ,hw = HW_2X16 ) 227 228# ---- 6. The run: success, failure and retry, stop and cancel 229def test_run_paths (self ): 230for pick in ("qwen" ,): 231for behaviour in ("ok" ,"fail" ,"wait" ): 232with self .subTest (pick = pick ,behaviour = behaviour ): 233FakeRunner .behaviour = behaviour 234 235async def case (app ,pilot ,ctx ): 236await self .to_model (app ,pilot ,pick ) 237for _ in range (30 ): 238if app .current == "save" : 239break 240await pilot .press ("n" if app .current == "advanced" else "enter" ) 241await settle (pilot ,3 ) 242self .assertEqual (app .current ,"save" ) 243await pilot .press ("enter" )# Save screen: first option 244for _ in range (40 ): 245await pilot .pause (0.05 ) 246if not app .is_running or app .current == "run" : 247break 248for _ in range (10 ): 249await pilot .pause (0.05 ) 250if behaviour == "fail" and app .is_running : 251self .assertIn ("Not enough disk space" ,text_of (app )) 252await pilot .press ("r" )# retry 253await pilot .pause (0.3 ) 254await pilot .press ("escape" )# back to the start 255await pilot .pause (0.2 ) 256if behaviour == "wait" and app .is_running : 257await pilot .press ("s" ) 258await pilot .pause (0.3 ) 259self .assertIn ("Stop now?" ,text_of (app )) 260await pilot .press ("enter" )# Stop 261await pilot .pause (0.6 ) 262self .assertIn ("Stopped. Nothing was started." ,text_of (app )) 263self .assertIsNone (app ._exception ) 264self .drive (case ,hw = HW_12 ) 265 266 267class ErrorPaths (unittest .TestCase ): 268"""The screens must come up (and say so plainly) when the world is broken.""" 269 270def setUp (self ): 271self .home = Home ().__enter__ () 272self .addCleanup (self .home .__exit__ ,None ,None ,None ) 273FakeRunner .instances .clear () 274FakeRunner .behaviour = "ok" 275FakeRunner .download = 0.0 276p = mock .patch .object (access ,"lan_ips" ,return_value = [])# no network at all 277p .start () 278self .addCleanup (p .stop ) 279 280def run_flow (self ,runner ,hw = HW_12 ,pick = "qwen" ,settle_s = 1.0 ): 281ctx = make_ctx (hw ) 282app = LaunchApp (ctx ) 283 284async def go (): 285async with app .run_test (size = (80 ,24 ))as pilot : 286await pilot .pause (0.2 ) 287app .screen .menu ().select (("new" ,None )) 288await pilot .press ("enter" ) 289await pilot .pause (0.2 ) 290app .screen .menu ().select (PICKS [pick ]) 291for _ in range (30 ): 292if app .current == "save" : 293break 294await pilot .press ("n" if app .current == "advanced" else "enter" ) 295await pilot .pause (0.1 ) 296await pilot .press ("enter" ) 297await pilot .pause (settle_s ) 298return text_of (app ),app ._exception ,app .is_running 299 300with mock .patch ("strata_cli.launch.app.Runner" ,runner ): 301return asyncio .run (go ()) 302 303def test_runner_raising_anything_is_shown_in_words (self ): 304for exc in (OSError (28 ,"No space left on device" ),PermissionError ("denied" ),ConnectionError ("feed unreachable" ), 305KeyError ("x" ),UnicodeEncodeError ("charmap" ,"ı" ,0 ,1 ,"can't encode" ),RuntimeError ("boom" )): 306with self .subTest (exc = type (exc ).__name__ ): 307class Raising (FakeRunner ): 308def run (self ,start = True ): 309raise exc 310 311text ,err ,running = self .run_flow (Raising ) 312self .assertIsNone (err ) 313self .assertTrue (running ,"the screens closed instead of showing the failure" ) 314self .assertIn ("Press r to try again" ,text ) 315self .assertNotIn ("Traceback" ,text ) 316 317 318class RealRunnerErrors (unittest .TestCase ): 319"""The real Runner against an unreachable feed, a port that is busy and a data folder that is full: plain failures, never a traceback.""" 320 321def setUp (self ): 322self .home = Home ().__enter__ () 323self .addCleanup (self .home .__exit__ ,None ,None ,None ) 324 325def test_unreachable_feed_gives_a_plain_failure (self ): 326from tests .terminal .test_launch_tui import make_ctx as mk 327ctx = mk (HW_12 ,S .LaunchSettings (name = "x" ,family = "qwen" ,model = "IQ3_XXS" ,context = 65536 ,tune = "skip" ,access = "local" ,port = 18999 )) 328ctx .feed = "http://127.0.0.1:9" # nothing listens here 329app = LaunchApp (ctx ,auto_run = True ) 330 331async def go (): 332async with app .run_test (size = (80 ,24 ))as pilot : 333for _ in range (100 ): 334await pilot .pause (0.1 ) 335t = text_of (app ) 336if "Press r to try again" in t or "Download about" in t or not app .is_running : 337break 338if "Download about" in text_of (app ): 339await pilot .press ("escape" ) 340await pilot .pause (0.2 ) 341return text_of (app ),app ._exception 342 343text ,err = asyncio .run (go ()) 344self .assertIsNone (err ,text ) 345self .assertNotIn ("Traceback" ,text ) 346 347 348if __name__ == "__main__" : 349unittest .main ()