okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd0e3766a
main
1"""Tester report 2026-10-18 (Terminal 0.1.45), T02: `strata launch` with flags opened the screens straight into the run, which crashed 2(IndexError: pop from empty list in switch_screen) and exited 0. The run now opens on its own screen; an error of the screens ends non-zero.""" 3 4from __future__import annotations 5 6import asyncio 7import contextlib 8import json 9import io 10import unittest 11from unittest import mock 12 13from textual .app import App 14 15from strata_cli .launch import app as launch_app 16from strata_cli .launch import settings as S 17from strata_cli .launch .app import Ctx ,LaunchApp 18from strata_cli .launch .widgets import Glyphs 19from tests .terminal .launch_support import HW_12 ,Home ,catalog ,text_of 20from tests .terminal .test_launch_tui import FakeRunner ,SIZE ,make_ctx 21 22 23def flags_settings ()-> S .LaunchSettings : 24"""What `strata launch --model q2_0 --family qwen --context 128k --kv int8 --vision cpu --parallel 1 --tune skip --access local --port 8081` gives.""" 25return S .LaunchSettings (name = "QA flags" ,family = "qwen" ,model = "Q2_0" ,context = 131072 ,kv = "int8" ,vision = "cpu" ,parallel = 1 ,tune = "skip" , 26access = "local" ,port = 8081 ) 27 28 29class FlagsOpenTheRun (unittest .TestCase ): 30def setUp (self ): 31self .home = Home ().__enter__ () 32self .addCleanup (self .home .__exit__ ,None ,None ,None ) 33FakeRunner .instances .clear () 34FakeRunner .behaviour = "ok" 35FakeRunner .download = 0.0 36p = mock .patch ("strata_cli.launch.app.Runner" ,FakeRunner ) 37p .start () 38self .addCleanup (p .stop ) 39 40def test_auto_run_shows_the_run_screen_and_finishes (self ): 41app = LaunchApp (make_ctx (settings = flags_settings ()),auto_run = True ) 42 43async def go (): 44async with app .run_test (size = SIZE )as pilot : 45for _ in range (60 ): 46await pilot .pause (0.05 ) 47if app .result is not None : 48break 49return app .result 50 51result = asyncio .run (go ()) 52self .assertIsNotNone (result ,"the run did not finish: the screens crashed instead of opening the run" ) 53self .assertEqual (result .urls , ["http://127.0.0.1:8081" ]) 54 55def test_auto_run_with_a_big_download_asks_first_and_then_runs (self ): 56FakeRunner .download = 66.0 57app = LaunchApp (make_ctx (settings = flags_settings ()),auto_run = True ) 58 59async def go (): 60async with app .run_test (size = SIZE )as pilot : 61await pilot .pause (0.3 ) 62self .assertIn ("Download about 66 GB?" ,text_of (app )) 63await pilot .press ("enter" )# Download and start 64for _ in range (60 ): 65await pilot .pause (0.05 ) 66if app .result is not None : 67break 68return app .result 69 70self .assertIsNotNone (asyncio .run (go ())) 71 72def headless_run (self ,app ): 73return App .run (app ,headless = True ) 74 75def test_run_tui_with_flags_returns_zero_and_opens_the_monitor (self ): 76calls = [] 77with mock .patch .object (LaunchApp ,"run" ,lambda self ,** kw :App .run (self ,headless = True )), \ 78mock .patch .object (launch_app ,"run_monitor" ,lambda info ,** kw :calls .append (info )or 0 ), \ 79mock .patch .object (launch_app ,"Catalog" ,create = True ): 80cat = catalog (HW_12 ) 81code = launch_app .run_tui (flags_settings (),catalog = cat ,auto_run = True ) 82self .assertEqual (code ,0 ) 83self .assertEqual (len (calls ),1 ) 84 85def test_an_error_of_the_screens_ends_non_zero_with_a_plain_sentence (self ): 86class Broken (FakeRunner ): 87def __init__ (self ,* a ,** kw ): 88raise RuntimeError ("boom" ) 89 90err = io .StringIO () 91with mock .patch .object (LaunchApp ,"run" ,lambda self ,** kw :App .run (self ,headless = True )), \ 92mock .patch .object (launch_app ,"Runner" ,Broken ), \ 93contextlib .redirect_stderr (err ),contextlib .redirect_stdout (io .StringIO ()): 94code = launch_app .run_tui (flags_settings (),catalog = catalog (HW_12 ),auto_run = True ) 95self .assertNotEqual (code ,0 ) 96self .assertIn ("nothing was started" ,err .getvalue ()) 97self .assertIn ("--no-tui" ,err .getvalue ()) 98 99def test_the_command_turns_a_crash_into_exit_one (self ): 100from strata_cli .launch import cmd 101err = io .StringIO () 102with contextlib .redirect_stderr (err ): 103code = cmd ._tui (lambda * a ,** k : (_ for _ in ()).throw (IndexError ("pop from empty list" ))) 104self .assertEqual (code ,1 ) 105self .assertIn ("unexpected problem" ,err .getvalue ()) 106self .assertNotIn ("Traceback" ,err .getvalue ()) 107 108 109if __name__ == "__main__" : 110unittest .main () 111 112 113class NoEngineFlow (unittest .TestCase ): 114"""Owner decision 2026-10-18: no usable engine is a warning, not a refusal; the model downloads and the end message names the problem.""" 115 116def setUp (self ): 117self .home = Home ().__enter__ () 118self .addCleanup (self .home .__exit__ ,None ,None ,None ) 119 120def served_nothing (self ): 121from strata_cli .launch .runner import Result ,no_engine_final 122text = no_engine_final ("The feed has no engine package for this PC." ) 123return Result ("launch-x" , [],None ,None , [text ],"/x" ,serving = False ,engine_problem = "The feed has no engine package for this PC." , 124final_text = text ) 125 126def test_the_command_ends_with_exit_zero_and_the_engine_message (self ): 127from strata_cli .launch .runner import Runner 128from tests .terminal .launch_support import run_cli 129with mock .patch .object (Runner ,"run" ,return_value = self .served_nothing ()): 130code ,out ,err = run_cli (["launch" ,"--no-tui" ,"--model" ,"q2_0" ,"--yes" ]) 131self .assertEqual (code ,0 ,err ) 132self .assertIn ("The model is ready, but Strata can't start it yet" ,out ) 133self .assertIn ("The feed has no engine package for this PC" ,out ) 134self .assertIn ("Next step:" ,out ) 135self .assertNotIn ("Strata is serving" ,out ) 136 137def test_json_says_not_serving (self ): 138from strata_cli .launch .runner import Runner 139from tests .terminal .launch_support import run_cli 140with mock .patch .object (Runner ,"run" ,return_value = self .served_nothing ()): 141code ,out ,err = run_cli (["launch" ,"--no-tui" ,"--model" ,"q2_0" ,"--output" ,"json" ]) 142data = json .loads (out ) 143self .assertEqual (code ,0 ) 144self .assertTrue (data ["ok" ]) 145self .assertFalse (data ["serving" ]) 146self .assertIn ("engine" ,data ["engine_problem" ]) 147 148def test_yes_is_passed_to_the_runner_and_json_never_asks (self ): 149from strata_cli .launch .runner import Runner 150from tests .terminal .launch_support import run_cli 151seen = [] 152real = Runner .__init__ 153 154def spy (self ,* a ,** kw ): 155seen .append (kw .get ("assume_yes" )) 156real (self ,* a ,** kw ) 157 158with mock .patch .object (Runner ,"__init__" ,spy ),mock .patch .object (Runner ,"run" ,return_value = self .served_nothing ()): 159run_cli (["launch" ,"--no-tui" ,"--model" ,"q2_0" ,"--yes" ]) 160run_cli (["launch" ,"--no-tui" ,"--model" ,"q2_0" ,"--output" ,"json" ]) 161run_cli (["launch" ,"--no-tui" ,"--model" ,"q2_0" ]) 162self .assertEqual (seen , [True ,True ,False ]) 163 164def test_the_question_default_is_yes (self ): 165from strata_cli .launch import cmd 166with mock .patch ("builtins.input" ,return_value = "" ),mock .patch .object (cmd .sys ,"stdin" )as sin , \ 167mock .patch .object (cmd .sys ,"stdout" )as sout : 168sin .isatty .return_value = True 169sout .isatty .return_value = True 170self .assertTrue (cmd ._ask_yes ("t" ,"text" )) 171with mock .patch ("builtins.input" ,return_value = "n" ),mock .patch .object (cmd .sys ,"stdin" )as sin , \ 172mock .patch .object (cmd .sys ,"stdout" )as sout : 173sin .isatty .return_value = True 174sout .isatty .return_value = True 175self .assertFalse (cmd ._ask_yes ("t" ,"text" )) 176self .assertTrue (cmd ._ask_yes ("t" ,"text" ))# no keyboard: the person already named the model 177 178def test_the_screens_ask_with_a_dialog_and_enter_means_yes (self ): 179FakeRunner .instances .clear () 180FakeRunner .behaviour = "ok" 181FakeRunner .download = 0.0 182answers = [] 183 184class Asking (FakeRunner ): 185def run (self ,start = True ): 186answers .append (self .confirm ("No engine" ,"Strata couldn't find an engine for this computer (x). Download?" )) 187return super ().run (start ) 188 189def __init__ (self ,* a ,confirm = None ,** kw ): 190super ().__init__ (* a ,** kw ) 191self .confirm = confirm 192 193app = LaunchApp (make_ctx (settings = flags_settings ()),auto_run = True ) 194 195async def go (): 196with mock .patch ("strata_cli.launch.app.Runner" ,Asking ): 197async with app .run_test (size = SIZE )as pilot : 198await pilot .pause (0.4 ) 199self .assertIn ("Strata couldn't find an engine for this computer" ,text_of (app )) 200await pilot .press ("enter" )# Download anyway 201for _ in range (60 ): 202await pilot .pause (0.05 ) 203if app .result is not None : 204break 205return app .result 206 207self .assertIsNotNone (asyncio .run (go ())) 208self .assertEqual (answers , [True ]) 209 210def test_the_screens_show_the_end_message_and_exit_zero (self ): 211from strata_cli .launch .app import LaunchApp as LA 212res = self .served_nothing () 213with mock .patch .object (LA ,"run" ,lambda self ,** kw :res ),mock .patch .object (launch_app ,"run_monitor" ,side_effect = AssertionError ("no monitor" )): 214out = io .StringIO () 215with contextlib .redirect_stdout (out ): 216code = launch_app .run_tui (flags_settings (),catalog = catalog (HW_12 ),auto_run = True ) 217self .assertEqual (code ,0 ) 218self .assertIn ("The model is ready, but Strata can't start it yet" ,out .getvalue ())