okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1"""The `strata launch` screens, driven with Textual's pilot: every screen, its keys and what it writes into the settings.""" 2 3from __future__import annotations 4 5import asyncio 6import json 7import os 8import threading 9import time 10import unittest 11from pathlib import Path 12from unittest import mock 13 14from strata_cli .launch import access ,settings as S ,store 15from strata_cli .launch .app import Ctx ,LaunchApp ,run_monitor 16from strata_cli .launch .runner import Cancelled ,Event ,LaunchError ,Result 17from strata_cli .launch .widgets import Glyphs 18from tests .terminal .launch_support import HW_12 ,HW_24 ,HW_2X16 ,Home ,catalog ,text_of 19 20SIZE = (110 ,60 )# (round 0214: the list has seven more rows (Unsloth's v3.1 sets); the tests that read the whole list need the room) 21 22 23def flat (app )-> str : 24return " " .join (text_of (app ).split ()) 25 26 27def make_ctx (hw = None ,settings = None ): 28cat = catalog (hw or HW_12 ) 29comp = cat .computer () 30return Ctx (catalog = cat ,computer = comp ,rows = cat .rows (comp .raw ),settings = settings or S .LaunchSettings (),glyphs = Glyphs (True )) 31 32 33class FakeRunner : 34"""Stands in for the real Runner: emits the same events, installs nothing, starts nothing.""" 35 36instances :list = [] 37behaviour = "ok" 38hold = 0 39download = 0.0 40 41def __init__ (self ,s ,* ,catalog = None ,emit = None ,cancel = None ,feed = None ,** kw ): 42self .s ,self .emit_cb ,self .cancel ,self .feed = s ,emit ,cancel ,feed 43FakeRunner .instances .append (self ) 44 45def download_gb (self ): 46return FakeRunner .download 47 48def run (self ,start = True ): 49e = self .emit_cb 50e (Event ("engine" ,"Engine 0.1.45 (cuda) is installed." ,100 )) 51e (Event ("model" ,"Downloading 12.0 of 75.8 GB" ,15.8 )) 52if FakeRunner .behaviour == "wait" : 53while not self .cancel .is_set (): 54time .sleep (0.02 ) 55raise Cancelled () 56if FakeRunner .behaviour == "fail" : 57raise LaunchError ("The model could not be installed. Not enough disk space." ,"Free some space and run it again." ,6 ) 58if getattr (FakeRunner ,"hold" ,0 ): 59time .sleep (FakeRunner .hold ) 60e (Event ("model" ,"Model is ready." ,100 )) 61e (Event ("config" ,"Settings are saved." ,100 )) 62if self .s .tune != "skip" : 63e (Event ("tune" ,"Tuning" ,40.0 ,data = {"round" : (2 ,5 ),"minutes_left" :12 , 64"speeds" : [{"label" :"Default settings" ,"decode_tok_s" :83.0 }, 65 {"label" :"Longer prompt chunks" ,"decode_tok_s" :85.1 }]})) 66if getattr (FakeRunner ,"hold" ,0 ): 67time .sleep (FakeRunner .hold ) 68e (Event ("tune" ,"Done: answers are about 3% faster with the tuned settings." ,100 )) 69e (Event ("start" ,"The model is loaded and answering." ,100 )) 70urls = [f"http://127.0.0.1: { self . s . port } " ] 71return Result ("launch-x" ,urls ,self .s .api_key ,"faster" , [],"/nonexistent.json" ) 72 73 74async def press (pilot ,* keys ,pause = 0.12 ): 75for k in keys : 76await pilot .press (k ) 77await pilot .pause (pause ) 78 79 80async def typed (pilot ,text ): 81for ch in text : 82await pilot .press (ch ) 83await pilot .pause (0.1 ) 84 85 86class Base (unittest .TestCase ): 87def setUp (self ): 88self .home = Home ().__enter__ () 89self .addCleanup (self .home .__exit__ ,None ,None ,None ) 90FakeRunner .instances .clear () 91FakeRunner .behaviour = "ok" 92FakeRunner .download = 0.0 93p = mock .patch ("strata_cli.launch.app.Runner" ,FakeRunner ) 94p .start () 95self .addCleanup (p .stop ) 96p = mock .patch .object (access ,"lan_ips" ,return_value = ["192.168.1.5" ]) 97p .start () 98self .addCleanup (p .stop ) 99 100def run_app (self ,coro ,ctx = None ,auto_run = False ): 101ctx = ctx or make_ctx () 102app = LaunchApp (ctx ,auto_run = auto_run ) 103 104async def go (): 105async with app .run_test (size = SIZE )as pilot : 106await pilot .pause (0.2 ) 107return await coro (app ,pilot ,ctx ) 108 109return asyncio .run (go ()) 110 111 112class StartScreenTests (Base ): 113def test_no_presets_offers_the_two_ways_in (self ): 114async def case (app ,pilot ,ctx ): 115t = text_of (app ) 116self .assertIn ("No saved presets yet" ,t ) 117self .assertIn ("Set up from the beginning" ,t ) 118self .assertIn ("Use model files I already have" ,t ) 119self .assertIn ("RTX 5070 12 GB" ,t )# the computer, top right 120self .assertIn ("q quit" ,t ) 121self .run_app (case ) 122 123def test_presets_are_listed_with_their_columns (self ): 124store .save (S .LaunchSettings (name = "IQ3_XXS 128K" ,family = "qwen" ,model = "IQ3_XXS" ,context = 131072 ,parallel = 1 ,tuned_preset = "x" ,tuned_at = "2026-10-16" )) 125store .save (S .LaunchSettings (name = "Coder 64K" ,family = "coder" ,model = "IQ1_M" ,context = 65536 ,parallel = 2 )) 126 127async def case (app ,pilot ,ctx ): 128t = text_of (app ) 129self .assertIn ("Start from a saved preset" ,t ) 130self .assertIn ("IQ3_XXS 128K" ,t ) 131self .assertIn ("128K" ,t ) 132self .assertIn ("1 request" ,t ) 133self .assertIn ("2 requests" ,t ) 134self .assertIn ("tuned 10-16" ,t ) 135self .assertIn ("not tuned" ,t ) 136self .assertIn ("e edit preset" ,t ) 137self .run_app (case ) 138 139def test_enter_on_a_preset_launches_it_and_opens_the_run_screen (self ): 140store .save (S .LaunchSettings (name = "Office" ,family = "qwen" ,model = "IQ3_XXS" ,context = 65536 ,tune = "quick" )) 141 142async def case (app ,pilot ,ctx ): 143await press (pilot ,"enter" ) 144await pilot .pause (0.6 ) 145self .assertEqual (FakeRunner .instances [0 ].s .name ,"Office" ) 146self .assertEqual (app .result .profile ,"launch-x" ) 147self .run_app (case ) 148 149def test_a_model_that_is_not_here_asks_before_downloading (self ): 150store .save (S .LaunchSettings (name = "Big" ,family = "qwen" ,model = "IQ3_XXS" ,context = 65536 )) 151FakeRunner .download = 75.8 152 153async def case (app ,pilot ,ctx ): 154await press (pilot ,"enter" ) 155t = flat (app ) 156self .assertIn ("Download about 76 GB?" ,t ) 157self .assertIn ("not on this computer yet" ,t ) 158self .assertIn ("Download and start" ,t ) 159self .assertEqual (FakeRunner .instances [- 1 ].behaviour ,"ok" ) 160await press (pilot ,"down" ,"enter" )# Not now 161await pilot .pause (0.3 ) 162self .assertIsNone (app .result ) 163await press (pilot ,"enter" ) 164await press (pilot ,"enter" )# Download and start 165await pilot .pause (0.8 ) 166self .assertIsNotNone (app .result ) 167self .run_app (case ) 168 169def test_delete_asks_then_removes_only_the_preset (self ): 170store .save (S .LaunchSettings (name = "Old" ,family = "qwen" ,model = "Q2_0" ,context = 32768 )) 171 172async def case (app ,pilot ,ctx ): 173await press (pilot ,"d" ) 174t = text_of (app ) 175self .assertIn ("Delete the preset 'Old'?" ,t ) 176self .assertIn ("Only the saved choices go" ,t ) 177await press (pilot ,"enter" )# first option: Delete it 178self .assertEqual (store .list_presets (), []) 179self .assertIn ("No saved presets yet" ,text_of (app )) 180self .run_app (case ) 181 182def test_delete_can_be_cancelled (self ): 183store .save (S .LaunchSettings (name = "Keep" ,family = "qwen" ,model = "Q2_0" ,context = 32768 )) 184 185async def case (app ,pilot ,ctx ): 186await press (pilot ,"d" ,"escape" ) 187self .assertEqual ([p .name for p in store .list_presets ()], ["Keep" ]) 188self .run_app (case ) 189 190def test_edit_opens_setup_with_the_preset_loaded (self ): 191store .save (S .LaunchSettings (name = "E" ,family = "swift" ,model = "IQ2_XS" ,context = 65536 ,parallel = 2 )) 192 193async def case (app ,pilot ,ctx ): 194await press (pilot ,"e" ) 195t = text_of (app ) 196self .assertIn ("Which model?" ,t ) 197self .assertEqual ((ctx .settings .family ,ctx .settings .model ), ("swift" ,"IQ2_XS" )) 198self .run_app (case ) 199 200def test_q_leaves (self ): 201async def case (app ,pilot ,ctx ): 202await press (pilot ,"q" ) 203await pilot .pause (0.2 ) 204self .assertFalse (app .is_running ) 205self .run_app (case ) 206 207def test_help_is_in_words (self ): 208async def case (app ,pilot ,ctx ): 209await press (pilot ,"question_mark" ) 210self .assertIn ("A preset remembers" ,text_of (app )) 211self .run_app (case ) 212 213 214class ModelStep (Base ): 215def go (self ): 216async def start (app ,pilot ,ctx ): 217await press (pilot ,"enter" ) 218return start 219 220def test_the_list_shows_real_figures_and_the_recommendation (self ): 221async def case (app ,pilot ,ctx ): 222await press (pilot ,"enter" ) 223t = text_of (app ) 224self .assertIn ("Which model?" ,t ) 225for col in ("Model" ,"Download" ,"Needs (RAM + VRAM)" ,"On this computer" ): 226self .assertIn (col ,t ) 227self .assertIn ("IQ3_XXS" ,t ) 228line = next (l for l in t .splitlines ()if "Qwen3.8-Flash-Next IQ3_S" in l )# 64 GB + 12 GB: the 64 GB tier 229self .assertIn ("84 GB" ,line )# setup.py's download_gb 83.6 230self .assertIn ("55 GB" ,line )# the files without the lookup table: 83.6 GB download - 28.8 GB (the owner's rule) 231self .assertIn ("(recommended)" ,line ) 232self .assertIn ("› " ,line )# the cursor starts on the recommendation 233big = next (l for l in t .splitlines ()if "UD-Q5_K_XL" in l ) 234self .assertTrue (big .rstrip ().endswith ("may run slower" ),big )# 99 GB of weights against 64 GB + 12 GB: a warning, not a block 235self .assertNotIn ("SSD " ,big )# (no "SSD N GB" suffix any more) 236nv = next (l for l in t .splitlines ()if "(NVFP4) NVFP4 " in l ) 237self .assertTrue (nv .rstrip ().endswith ("may run slower" ),nv )# 76.8 GB of files (128 - 51.2) against 64 GB + 11.9 GB 238for old in ("too big" ,"fits, a bit slower" ,"does not fit" ): 239self .assertNotIn (old ,t ) 240self .assertIn ("The n-gram table stays on your SSD" ,t ) 241self .run_app (case ) 242 243def test_the_verdict_column_is_not_cut_off_at_80_columns (self ):# 0.2.5 list 244ctx = make_ctx () 245app = LaunchApp (ctx ) 246 247async def go (): 248async with app .run_test (size = (80 ,60 ))as pilot : 249await pilot .pause (0.2 ) 250await press (pilot ,"enter" ) 251t = text_of (app ) 252self .assertIn ("Fits?" ,t ) 253rec = next (l for l in t .splitlines ()if "Qwen3.8-Flash-Next IQ3_S " in l ) 254self .assertIn ("fits (recommended)" ,rec ) 255for model in ("Q2_0" ,"NVFP4 " ,"UD-Q5_K_XL" ): 256row = next (l for l in t .splitlines ()if model in l and "GB" in l ) 257self .assertTrue (any (w in row for w in ("fits" ,"may run slower" ,"tight" )),row ) 258self .assertTrue (all (len (l )<= 80 for l in t .splitlines ())) 259asyncio .run (go ()) 260 261def test_choosing_a_model_and_going_on (self ): 262async def case (app ,pilot ,ctx ): 263await press (pilot ,"enter" ,"up" ,"up" ,"up" ,"enter" ) 264self .assertEqual ((ctx .settings .family ,ctx .settings .model ), ("qwen" ,"Q2_0" )) 265self .assertIn ("Where should the model files be stored?" ,text_of (app )) 266self .run_app (case ) 267 268def test_a_model_that_does_not_fit_can_still_be_chosen (self ): 269async def case (app ,pilot ,ctx ): 270await press (pilot ,"enter" ,"end" ,"enter" ) 271last = ctx .rows [- 1 ] 272self .assertIn (last .verdict , ("slower" ,"tight" ))# the last row does not fit 64 GB + a 12 GB card: said, not refused 273self .assertEqual ((ctx .settings .family ,ctx .settings .model ), (last .family ,last .model )) 274self .assertIn ("Where should the model files be stored?" ,text_of (app )) 275self .run_app (case ) 276 277def test_own_files_are_checked_with_the_fingerprint_table (self ): 278plan = {"candidate_selection" : {"family" :"qwen" ,"model" :"IQ3_XXS" },"issues" : [],"file_count" :2 ,"total_bytes" :44_000_000_000 } 279 280async def case (app ,pilot ,ctx ): 281await press (pilot ,"enter" ,"o" ) 282self .assertIn ("Type or paste the folder" ,text_of (app )) 283with mock .patch ("strata_control.models.plan_import" ,return_value = plan )as pi ,mock .patch ("strata_control.models.own_path_problem" ,return_value = None ): 284await typed (pilot ,"/data/models/IQ3_XXS" ) 285await press (pilot ,"enter" ) 286self .assertEqual (ctx .settings .model_path ,os .path .abspath ("/data/models/IQ3_XXS" )) 287self .assertEqual ((ctx .settings .family ,ctx .settings .model ), ("qwen" ,"IQ3_XXS" )) 288self .assertIn ("These files are qwen IQ3_XXS (2 file(s), 44 GB)" ,text_of (app )) 289await press (pilot ,"enter" ) 290self .assertIn ("Your model files stay where they are" ,text_of (app )) 291self .run_app (case ) 292 293def test_own_files_that_are_not_a_known_model_say_why (self ): 294plan = {"candidate_selection" :None ,"issues" : ["incomplete_or_invalid_shard_set" ],"file_count" :1 ,"total_bytes" :1 } 295 296async def case (app ,pilot ,ctx ): 297await press (pilot ,"enter" ,"o" ) 298with mock .patch ("strata_control.models.plan_import" ,return_value = plan ),mock .patch ("strata_control.models.own_path_problem" ,return_value = None ): 299await typed (pilot ,"/x" ) 300await press (pilot ,"enter" ) 301t = flat (app ) 302self .assertIn ("some parts of the model are missing" ,t ) 303self .assertIn ("pick a model from the list" ,t ) 304self .assertIsNone (ctx .settings .model_path ) 305self .run_app (case ) 306 307def test_own_files_folder_that_is_not_there_is_named (self ): 308async def case (app ,pilot ,ctx ): 309await press (pilot ,"enter" ,"o" ) 310await typed (pilot ,"/no/such/folder" ) 311await press (pilot ,"enter" ) 312t = flat (app ) 313self .assertIn ("does not exist" ,t ) 314self .assertIn ("Nothing was downloaded" ,t ) 315self .assertIsNone (ctx .settings .model_path ) 316self .run_app (case ) 317 318def test_own_files_path_from_the_start_screen (self ): 319async def case (app ,pilot ,ctx ): 320await press (pilot ,"down" ,"enter" ) 321self .assertIn ("Type or paste the folder" ,text_of (app )) 322await press (pilot ,"escape" ) 323self .assertIn ("Which model?" ,text_of (app )) 324self .run_app (case ) 325 326def test_escape_goes_back_to_the_start (self ): 327async def case (app ,pilot ,ctx ): 328await press (pilot ,"enter" ,"escape" ) 329self .assertIn ("Set up from the beginning" ,text_of (app )) 330self .run_app (case ) 331 332 333class FolderStep (Base ): 334def test_default_folder_and_free_space_and_change (self ): 335async def case (app ,pilot ,ctx ): 336await press (pilot ,"enter" ,"enter" ) 337t = text_of (app ) 338self .assertIn ("Where should the model files be stored?" ,t ) 339self .assertIn ("Not downloaded yet: about 90 GB" ,t ) 340self .assertIn ("Free space in that folder's drive" ,t ) 341f = app .screen .query_one ("#folderpath" ) 342f .value = "" 343self .assertNotEqual (str (ctx .default_folder ()),str (self .home .base / "Elsewhere-data" ))# (on Linux the default is Strata-data under the home: typing it again is no change) 344await typed (pilot ,str (self .home .base / "Elsewhere-data" )) 345self .assertEqual (f .value ,str (self .home .base / "Elsewhere-data" )) 346await press (pilot ,"enter" ) 347self .assertEqual (ctx .settings .data_dir ,str (self .home .base / "Elsewhere-data" )) 348self .assertIn ("How much text should it remember at once?" ,text_of (app )) 349self .run_app (case ) 350 351def test_a_new_folder_says_that_the_models_already_stored_will_move (self ): 352async def case (app ,pilot ,ctx ): 353old = self .home .base / "old-data" 354 (old / "models" / "Q2_0" ).mkdir (parents = True ) 355 (old / "models" / "Q2_0" / "x.gguf" ).write_text ("x" ) 356with mock .patch .object (type (ctx .catalog ),"data_dir" ,return_value = old ): 357await press (pilot ,"enter" ,"enter" ) 358self .assertNotIn ("stay there and keep working" ,text_of (app )) 359f = app .screen .query_one ("#folderpath" ) 360f .value = "" 361await typed (pilot ,str (self .home .base / "new-data" )) 362t = " " .join (text_of (app ).replace ("\n" ," " ).split ()) 363self .assertIn ("Model files Strata already keeps in" ,t ) 364self .assertIn ("stay there and keep working" ,t ) 365self .assertNotIn ("are moved into this folder" ,t ) 366self .run_app (case ) 367 368def test_keeping_the_default_stores_nothing (self ): 369async def case (app ,pilot ,ctx ): 370await press (pilot ,"enter" ,"enter" ,"enter" ) 371self .assertIsNone (ctx .settings .data_dir ) 372self .run_app (case ) 373 374def test_not_enough_space_is_a_warning_you_can_go_past (self ): 375async def case (app ,pilot ,ctx ): 376with mock .patch .object (type (ctx .catalog ),"disk_free" ,return_value = 10.0 ): 377await press (pilot ,"enter" ,"enter" ) 378t = text_of (app ) 379self .assertIn ("Not enough for" ,t ) 380await press (pilot ,"enter" ) 381self .assertIn ("How much text" ,text_of (app )) 382self .run_app (case ) 383 384 385class ContextStep (Base ): 386def reach (self ): 387async def f (app ,pilot ,ctx ): 388await press (pilot ,"enter" ,"enter" ,"enter" ) 389return f 390 391def test_sizes_words_memory_and_recommendation (self ): 392async def case (app ,pilot ,ctx ): 393await press (pilot ,"enter" ,"enter" ,"enter" ) 394t = text_of (app ) 395for size in ("8K" ,"32K" ,"64K" ,"128K" ,"256K" ,"512K" ,"Custom" ): 396self .assertIn (size ,t ) 397self .assertIn ("about 98,000 words" ,t ) 398self .assertIn ("recommended" ,t ) 399self .assertIn ("+1.8 GB" ,t )# 128K at 8-bit: 131072 x 13 x 1056 bytes 400self .assertIn ("experimental: rope scaling" ,t ) 401self .assertIn ("Conversation memory: 8-bit" ,t ) 402self .run_app (case ) 403 404def test_choose_128k (self ): 405async def case (app ,pilot ,ctx ): 406await press (pilot ,"enter" ,"enter" ,"enter" ,"enter" )# the cursor starts on 128K now (the default on every card) 407self .assertEqual (ctx .settings .context ,131072 ) 408self .assertIn ("Should the model read pictures?" ,text_of (app )) 409self .run_app (case ) 410 411def test_memory_format_toggle_changes_the_figures (self ): 412async def case (app ,pilot ,ctx ): 413await press (pilot ,"enter" ,"enter" ,"enter" ,"m" ) 414self .assertEqual (ctx .settings .kv ,"q4_0" ) 415t = text_of (app ) 416self .assertIn ("4-bit" ,t ) 417self .assertIn ("+0.2 GB" ,t )# 32K at 4-bit 418await press (pilot ,"m" ) 419self .assertEqual (ctx .settings .kv ,"int8" ) 420self .run_app (case ) 421 422def test_custom_size (self ): 423async def case (app ,pilot ,ctx ): 424await press (pilot ,"enter" ,"enter" ,"enter" ,"c" ) 425await typed (pilot ,"100k" ) 426await press (pilot ,"enter" ) 427self .assertEqual (ctx .settings .context ,102400 ) 428self .assertIn ("100K" ,text_of (app )) 429await press (pilot ,"enter" ) 430self .assertEqual (ctx .settings .context ,102400 ) 431self .run_app (case ) 432 433def test_bad_custom_size_is_explained (self ): 434async def case (app ,pilot ,ctx ): 435await press (pilot ,"enter" ,"enter" ,"enter" ,"c" ) 436await typed (pilot ,"lots" ) 437await press (pilot ,"enter" ) 438self .assertIn ("is not a context size" ,text_of (app )) 439self .run_app (case ) 440 441def test_bigger_card_recommends_more (self ): 442async def case (app ,pilot ,ctx ): 443await press (pilot ,"enter" ,"enter" ,"enter" ) 444t = text_of (app ) 445line = next (l for l in t .splitlines ()if "128K" in l ) 446self .assertIn ("recommended" ,line ) 447self .run_app (case ,ctx = make_ctx (HW_24 )) 448 449 450class PicturesParallelTuning (Base ): 451def reach_pictures (self ): 452return ("enter" ,"enter" ,"enter" ,"enter" ) 453 454def test_pictures_default_off_and_on (self ): 455async def case (app ,pilot ,ctx ): 456await press (pilot ,* self .reach_pictures ()) 457t = text_of (app ) 458self .assertIn ("No pictures" ,t ) 459self .assertIn ("Pictures are on by default" ,flat (app )) 460self .assertIn ("0.9 GB download" ,t ) 461self .assertIn ("runs on the CPU" ,flat (app ))# a 12 GB card: the CPU reader (the GPU one from 24 GB) 462self .assertEqual (app .screen .menu ().index ,1 )# "Yes" is preselected 463await press (pilot ,"enter" ) 464self .assertEqual (ctx .settings .vision ,"cpu" ) 465self .assertIn ("How many requests at the same time?" ,text_of (app )) 466self .run_app (case ) 467 468def test_pictures_no_is_one_key_away (self ): 469async def case (app ,pilot ,ctx ): 470await press (pilot ,* self .reach_pictures ()) 471await press (pilot ,"up" ,"enter" ) 472self .assertEqual (ctx .settings .vision ,"off" ) 473self .run_app (case ) 474 475def test_pictures_not_offered_for_a_model_without_them (self ): 476store .save (S .LaunchSettings (name = "G" ,family = "gyro" ,model = "Gyro-S" ,context = 32768 )) 477 478async def case (app ,pilot ,ctx ): 479await press (pilot ,"e" ,"enter" ,"enter" ,"enter" ) 480self .assertEqual ((ctx .settings .family ,ctx .settings .model ), ("gyro" ,"Gyro-S" )) 481t = flat (app ) 482self .assertIn ("not available for this model yet" ,t ) 483self .assertIn ("This model cannot read pictures yet" ,t ) 484await press (pilot ,"down" ,"enter" )# the greyed row is skipped 485self .assertEqual (ctx .settings .vision ,"off" ) 486# every catalog model reads pictures now: a row that does not stands in for a future one 487import dataclasses 488from strata_cli .launch .catalog import Catalog 489real_rows = Catalog .rows 490 491def rows (self ,* args ,** kwargs ): 492return [dataclasses .replace (r ,images = False )if r .model == "Gyro-S" else r for r in real_rows (self ,* args ,** kwargs )] 493 494with mock .patch .object (Catalog ,"rows" ,rows ): 495self .run_app (case ) 496 497def test_parallel_fit_to_card_and_warnings (self ): 498async def case (app ,pilot ,ctx ): 499await press (pilot ,* self .reach_pictures (),"enter" ) 500t = text_of (app ) 501self .assertIn ("Fit to my graphics card (1)" ,t ) 502self .assertIn ("may not fit: slower answers" ,t ) 503self .assertIn ("12 GB: 1, 20 GB: 2, 24 GB: 3, 32 GB or more: 4" ," " .join (t .split ())) 504await press (pilot ,"down" ,"down" ,"down" ,"enter" ) 505self .assertEqual (ctx .settings .parallel ,3 ) 506self .assertIn ("Tune for the best speed" ,text_of (app )) 507self .run_app (case ) 508 509def test_parallel_default_is_fit_to_card (self ): 510async def case (app ,pilot ,ctx ): 511await press (pilot ,* self .reach_pictures (),"enter" ,"enter" ) 512self .assertIsNone (ctx .settings .parallel ) 513self .run_app (case ) 514 515def test_bigger_card_fits_more (self ): 516async def case (app ,pilot ,ctx ): 517await press (pilot ,* self .reach_pictures (),"enter" ) 518self .assertIn ("Fit to my graphics card (3)" ,text_of (app )) 519self .run_app (case ,ctx = make_ctx (HW_24 )) 520 521def test_tuning_choices_and_the_answer_changing_tick (self ): 522async def case (app ,pilot ,ctx ): 523await press (pilot ,* self .reach_pictures (),"enter" ,"enter" ) 524t = text_of (app ) 525self .assertIn ("Quick" ,t ) 526self .assertIn ("about 20 minutes" ,t ) 527self .assertIn ("Thorough" ,t ) 528self .assertIn ("up to 2 hours" ,t ) 529self .assertIn ("Skip" ,t ) 530self .assertIn ("[ ] Allow settings that change answers very slightly" ,t ) 531line = next (l for l in t .splitlines ()if "›" in l ) 532self .assertIn ("Quick" ,line )# a first setup recommends tuning 533await press (pilot ,"a" ) 534self .assertIn ("[x] Allow settings" ,text_of (app )) 535self .assertTrue (ctx .settings .tune_allow_small ) 536await press (pilot ,"down" ,"enter" ) 537self .assertEqual (ctx .settings .tune ,"thorough" ) 538self .run_app (case ) 539 540def test_tuning_skip (self ): 541async def case (app ,pilot ,ctx ): 542await press (pilot ,* self .reach_pictures (),"enter" ,"enter" ,"down" ,"down" ,"enter" ) 543self .assertEqual (ctx .settings .tune ,"skip" ) 544self .run_app (case ) 545 546 547class AdvancedStep (Base ): 548HELP = " --prefill N prompt chunk size (default 2048)\n --kv fp16|int8 KV storage\n --native-rope experimental pinned rotary\n" 549 550def reach (self ): 551return ("enter" ,"enter" ,"enter" ,"enter" ,"enter" ,"enter" ,"enter" ) 552 553def ctx_with_engine (self ): 554ctx = make_ctx () 555ctx .settings .family ,ctx .settings .model = "qwen" ,"IQ3_XXS" 556base = {"exe" :"component:strata-engine" ,"args" : ["--max-context" ,"32768" ,"--kv" ,"int8" ,"--prefill" ,"auto" ]} 557p1 = mock .patch .object (Ctx ,"base_config" ,lambda self :base ) 558p2 = mock .patch .object (Ctx ,"engine_exe" ,lambda self :"/engine/strata" ) 559p3 = mock .patch ("strata_cli.launch.params.engine_help_text" ,return_value = self .HELP ) 560for p in (p1 ,p2 ,p3 ): 561p .start () 562self .addCleanup (p .stop ) 563return ctx 564 565def test_table_columns_command_and_same_as_line (self ): 566ctx = self .ctx_with_engine () 567 568async def case (app ,pilot ,ctx ): 569await press (pilot ,* self .reach ()) 570t = text_of (app ) 571for col in ("Setting" ,"Default" ,"Tuned" ,"Yours" ,"What it does" ): 572self .assertIn (col ,t ) 573self .assertIn ("--max-context" ,t ) 574self .assertIn ("--prefill" ,t ) 575self .assertIn ("--native-rope" ,t ) 576self .assertIn ("[experimental]" ,t ) 577self .assertIn ("+ Add a setting by name" ,t ) 578self .assertIn ("Command that will run" ,t ) 579self .assertIn ("Same as: strata launch --model iq3_s" ,t ) 580self .run_app (case ,ctx = ctx ) 581 582def test_edit_a_value_then_reset_it (self ): 583ctx = self .ctx_with_engine () 584 585async def case (app ,pilot ,ctx ): 586await press (pilot ,* self .reach ()) 587names = [it .key for it in app .screen .menu ().items ] 588idx = names .index ("--prefill" ) 589for _ in range (idx ): 590await press (pilot ,"down" ) 591await press (pilot ,"enter" ) 592self .assertIn ("Type the value for --prefill" ,text_of (app )) 593await typed (pilot ,"65536" ) 594await press (pilot ,"enter" ) 595self .assertEqual (ctx .settings .extra , {"--prefill" :"65536" }) 596self .assertIn ("65536" ,text_of (app )) 597self .assertIn ("--set prefill=65536" ,text_of (app )) 598await press (pilot ,"delete" ) 599self .assertEqual (ctx .settings .extra , {}) 600self .assertIn ("back to the tuned value" ,text_of (app )) 601self .run_app (case ,ctx = ctx ) 602 603def test_add_by_name_warns_about_an_unknown_flag_but_keeps_it (self ): 604ctx = self .ctx_with_engine () 605 606async def case (app ,pilot ,ctx ): 607await press (pilot ,* self .reach ()) 608await press (pilot ,"a" ) 609await typed (pilot ,"prefil=1" ) 610await press (pilot ,"enter" ) 611self .assertEqual (ctx .settings .extra , {"--prefil" :"1" }) 612t = text_of (app ) 613self .assertIn ("does not list --prefil" ,t ) 614self .assertIn ("Did you mean --prefill" ,t ) 615self .assertIn ("--prefil" ,t ) 616self .run_app (case ,ctx = ctx ) 617 618def test_add_an_environment_setting (self ): 619ctx = self .ctx_with_engine () 620 621async def case (app ,pilot ,ctx ): 622await press (pilot ,* self .reach (),"a" ) 623await typed (pilot ,"STRATA_PIN_BY_NEED=1" ) 624await press (pilot ,"enter" ) 625self .assertEqual (ctx .settings .extra , {"STRATA_PIN_BY_NEED" :"1" }) 626self .run_app (case ,ctx = ctx ) 627 628def test_experimental_filter_and_text_filter (self ): 629ctx = self .ctx_with_engine () 630 631async def case (app ,pilot ,ctx ): 632await press (pilot ,* self .reach (),"x" ) 633keys = [it .key for it in app .screen .menu ().items ] 634self .assertIn ("--native-rope" ,keys ) 635self .assertNotIn ("--max-context" ,keys ) 636await press (pilot ,"x" ,"slash" ) 637await typed (pilot ,"kv" ) 638await press (pilot ,"enter" ) 639keys = [it .key for it in app .screen .menu ().items ] 640self .assertIn ("--kv" ,keys ) 641self .assertNotIn ("--native-rope" ,keys ) 642self .run_app (case ,ctx = ctx ) 643 644def test_next_and_back (self ): 645async def case (app ,pilot ,ctx ): 646await press (pilot ,* self .reach (),"n" ) 647self .assertIn ("Who can use it?" ,text_of (app )) 648await press (pilot ,"escape" ) 649self .assertIn ("All settings" ,text_of (app )) 650self .run_app (case ) 651 652def test_works_before_the_engine_is_installed (self ): 653async def case (app ,pilot ,ctx ): 654await press (pilot ,* self .reach ()) 655t = text_of (app ) 656self .assertIn ("The engine is not installed yet" ,t ) 657self .assertIn ("Command that will run" ,t ) 658self .run_app (case ) 659 660def test_copy_does_not_crash (self ): 661async def case (app ,pilot ,ctx ): 662await press (pilot ,* self .reach (),"c" ) 663self .assertTrue (any (w in text_of (app )for w in ("Copied" ,"does not allow copying" ))) 664self .run_app (case ) 665 666 667class AccessStep (Base ): 668REACH = ("enter" ,)* 8 669 670def test_four_ways_and_what_each_needs (self ): 671async def case (app ,pilot ,ctx ): 672with mock .patch .object (access ,"find_cloudflared" ,return_value = None ), \ 673mock .patch .object (access ,"tailscale_status" ,return_value = {"installed" :False ,"running" :False ,"ip" :None ,"name" :None }): 674await press (pilot ,* self .REACH [:7 ],"n" ) 675t = text_of (app ) 676self .assertIn ("This computer only" ,t ) 677self .assertIn ("http://127.0.0.1:8080" ,t ) 678self .assertIn ("http://192.168.1.5:8080" ,t ) 679self .assertIn ("cloudflared is not installed" ,t ) 680self .assertIn ("Tailscale is not installed" ,t ) 681self .assertIn ("Port [ 8080 ]" ,t ) 682self .assertIn ("API key [ off ]" ,t ) 683self .run_app (case ) 684 685def reach (self ): 686return self .REACH [:7 ]+ ("n" ,) 687 688def test_lan_makes_a_key_and_keeps_the_port (self ): 689async def case (app ,pilot ,ctx ): 690await press (pilot ,* self .reach (),"down" ) 691t = text_of (app ) 692self .assertTrue (ctx .settings .api_key .startswith ("sk-strata-" )) 693self .assertIn ("API key [ on ]" ,t ) 694self .assertIn ("An API key was made for you" ,t ) 695await press (pilot ,"enter" ) 696self .assertEqual (ctx .settings .access ,"lan" ) 697self .assertIn ("Save as preset, then launch" ,text_of (app )) 698self .run_app (case ) 699 700def test_cloudflare_without_the_helper_says_how_to_install_it_and_does_not_block (self ): 701async def case (app ,pilot ,ctx ): 702with mock .patch .object (access ,"find_cloudflared" ,return_value = None ): 703await press (pilot ,* self .reach (),"down" ,"down" ) 704t = text_of (app ) 705self .assertIn ("cloudflared is not installed on this computer" ,t ) 706self .assertIn ("quick tunnel" ,flat (app )) 707await press (pilot ,"enter" ) 708self .assertTrue (any ("cloudflared" in w for w in ctx .warnings ()))# inside the mock: a box with a system cloudflared finds one 709self .assertEqual (ctx .settings .access ,"cloudflare" ) 710self .assertIn ("Save as preset, then launch" ,text_of (app )) 711self .run_app (case ) 712 713def test_tailscale_found (self ): 714async def case (app ,pilot ,ctx ): 715with mock .patch .object (access ,"tailscale_status" ,return_value = {"installed" :True ,"running" :True ,"ip" :"100.64.0.1" ,"name" :"pc.tail.ts.net" }): 716await press (pilot ,* self .reach (),"down" ,"down" ,"down" ) 717self .assertIn ("http://pc.tail.ts.net:8080" ,text_of (app )) 718self .run_app (case ) 719 720def test_port_change_and_bad_port (self ): 721async def case (app ,pilot ,ctx ): 722await press (pilot ,* self .reach (),"p" ) 723app .screen .query_one ("#portfield" ).value = "" 724await typed (pilot ,"abc" ) 725await press (pilot ,"enter" ) 726self .assertIn ("is not a port number" ,text_of (app )) 727app .screen .query_one ("#portfield" ).value = "" 728await typed (pilot ,"9090" ) 729await press (pilot ,"enter" ) 730self .assertEqual (ctx .settings .port ,9090 ) 731self .assertIn ("http://127.0.0.1:9090" ,text_of (app )) 732self .run_app (case ) 733 734def test_windows_firewall_line_shows_on_lan_only (self ): 735async def case (app ,pilot ,ctx ): 736with mock .patch .object (access ,"WINDOWS" ,True ): 737await press (pilot ,* self .reach ()) 738self .assertNotIn ("Windows may ask" ,flat (app )) 739await press (pilot ,"down" ) 740self .assertIn ("Windows may ask whether to allow Strata" ,flat (app )) 741self .assertIn ("choose Allow for private networks" ,flat (app )) 742self .run_app (case ) 743 744def test_key_toggle_and_new_key (self ): 745async def case (app ,pilot ,ctx ): 746await press (pilot ,* self .reach (),"down" ) 747first = ctx .settings .api_key 748await press (pilot ,"k" ) 749self .assertNotEqual (ctx .settings .api_key ,first ) 750await press (pilot ,"t" ) 751self .assertIsNone (ctx .settings .api_key ) 752self .assertTrue (ctx .settings .no_key ) 753self .assertIn ("anyone who can reach this port can use the model and its tools" ,text_of (app )) 754await press (pilot ,"t" ) 755self .assertTrue (ctx .settings .api_key ) 756self .assertFalse (ctx .settings .no_key ) 757self .run_app (case ) 758 759def test_default_is_a_key_and_no_key_survives_moving_on (self ): 760async def case (app ,pilot ,ctx ): 761await press (pilot ,* self .reach (),"down" ) 762self .assertTrue (ctx .settings .api_key )# default: Strata makes a key 763await press (pilot ,"t" )# one keypress: no key 764await press (pilot ,"down" )# moving the cursor does not bring the key back 765await press (pilot ,"up" ) 766self .assertIsNone (ctx .settings .api_key ) 767self .assertTrue (ctx .settings .no_key ) 768self .run_app (case ) 769 770def test_local_never_asks_for_a_key (self ): 771async def case (app ,pilot ,ctx ): 772await press (pilot ,* self .reach ()) 773self .assertEqual (ctx .settings .access ,"local" ) 774self .assertIsNone (ctx .settings .api_key ) 775self .assertNotIn ("An API key was made" ,text_of (app )) 776self .run_app (case ) 777 778 779class SaveAndRun (Base ): 780def reach (self ): 781return ("enter" ,)* 8 + ("n" ,"enter" ) 782 783def test_summary_and_save_then_launch (self ): 784async def case (app ,pilot ,ctx ): 785await press (pilot ,* ("enter" ,)* 7 ,"n" ,"enter" ) 786t = text_of (app ) 787self .assertIn ("Save as preset, then launch" ,t ) 788self .assertIn ("Qwen3.8-Flash-Next IQ3_S" ,t ) 789self .assertIn ("128K, memory int8" ,t ) 790self .assertIn ("quick (about 20 min)" ,t ) 791self .assertIn ("This computer only, port 8080" ,t ) 792await press (pilot ,"enter" )# Save as a preset and launch 793await pilot .pause (0.8 ) 794self .assertEqual ([p .name for p in store .list_presets ()], ["IQ3_S 128K" ]) 795self .assertEqual (FakeRunner .instances [0 ].s .name ,"IQ3_S 128K" ) 796self .assertIsNotNone (app .result ) 797self .run_app (case ) 798 799def test_launch_without_saving (self ): 800async def case (app ,pilot ,ctx ): 801await press (pilot ,* ("enter" ,)* 7 ,"n" ,"enter" ,"down" ,"enter" ) 802await pilot .pause (0.8 ) 803self .assertEqual (store .list_presets (), []) 804self .assertIsNotNone (app .result ) 805self .run_app (case ) 806 807def test_save_and_stop_here (self ): 808async def case (app ,pilot ,ctx ): 809await press (pilot ,* ("enter" ,)* 7 ,"n" ,"enter" ,"down" ,"down" ,"enter" ) 810await pilot .pause (0.3 ) 811self .assertEqual (len (store .list_presets ()),1 ) 812self .assertEqual (FakeRunner .instances , []) 813self .assertTrue (app .stopped_after_save ) 814self .run_app (case ) 815 816def test_the_name_can_be_typed (self ): 817async def case (app ,pilot ,ctx ): 818await press (pilot ,* ("enter" ,)* 7 ,"n" ,"enter" ) 819f = app .screen .query_one ("#name" ) 820f .value = "" 821f .focus () 822await typed (pilot ,"My preset" ) 823app .screen .set_focus (None ) 824await press (pilot ,"enter" ) 825await pilot .pause (0.5 ) 826self .assertEqual ([p .name for p in store .list_presets ()], ["My preset" ]) 827self .run_app (case ) 828 829def test_run_screen_shows_the_stages_and_the_tuning_table (self ): 830shown = {} 831 832async def case (app ,pilot ,ctx ): 833store .save (S .LaunchSettings (name = "T" ,family = "qwen" ,model = "IQ3_XXS" ,context = 32768 ,tune = "quick" )) 834FakeRunner .behaviour = "ok" 835FakeRunner .hold = 1.5 836self .addCleanup (setattr ,FakeRunner ,"hold" ,0 ) 837app .start_preset ("T" ) 838await pilot .pause (0.05 ) 839for _ in range (100 ): 840t = text_of (app ) 841if "Default settings" in t : 842shown ["t" ]= t 843break 844await pilot .pause (0.02 ) 845await pilot .pause (0.5 ) 846self .run_app (case ) 847t = shown .get ("t" ,"" ) 848self .assertIn ("Tuning" ,t ) 849self .assertIn ("measurement 2 of 5" ,t ) 850self .assertIn ("Default settings" ,t ) 851self .assertIn ("83.0 tokens/s" ,t ) 852self .assertIn ("Close other heavy programs while it measures" ,t ) 853 854def test_failure_says_what_it_means_and_retry_works (self ): 855async def case (app ,pilot ,ctx ): 856store .save (S .LaunchSettings (name = "T" ,family = "qwen" ,model = "IQ3_XXS" ,context = 32768 )) 857FakeRunner .behaviour = "fail" 858app .start_preset ("T" ) 859await pilot .pause (0.6 ) 860t = flat (app ) 861self .assertIn ("Not enough disk space" ,t ) 862self .assertIn ("Press r to try again" ,t ) 863FakeRunner .behaviour = "ok" 864await press (pilot ,"r" ) 865await pilot .pause (0.8 ) 866self .assertIsNotNone (app .result ) 867self .run_app (case ) 868 869def test_stop_asks_and_cancels (self ): 870async def case (app ,pilot ,ctx ): 871store .save (S .LaunchSettings (name = "T" ,family = "qwen" ,model = "IQ3_XXS" ,context = 32768 )) 872FakeRunner .behaviour = "wait" 873app .start_preset ("T" ) 874await pilot .pause (0.4 ) 875await press (pilot ,"s" ) 876self .assertIn ("Stop now?" ,text_of (app )) 877self .assertIn ("Files already downloaded are kept" ,flat (app )) 878await press (pilot ,"enter" )# Stop 879await pilot .pause (0.6 ) 880self .assertIn ("Stopped. Nothing was started." ,text_of (app )) 881self .run_app (case ) 882 883 884class CardsStep (Base ): 885def test_cards_step_shows_a_role_for_every_card_and_the_recommendation_is_selected (self ): 886async def case (app ,pilot ,ctx ): 887await press (pilot ,"enter" ,"enter" ) 888t = text_of (app ) 889self .assertIn ("Which graphics cards should it use?" ,t ) 890self .assertIn ("Step 2 of 10" ,t ) 891flat = " " .join (t .split ()) 892self .assertIn ("[ Main ]" ,flat )# equal cards: the highest number is Main (the engine order stays [0, 1]) 893self .assertIn ("[ Helper ]" ,flat ) 894self .assertIn ("recommended" ,flat ) 895self .assertIn ("M Main" ,flat ) 896self .assertNotIn ("slower" ,flat )# no explaining warning for a recommendation 897await press (pilot ,"enter" )# keeping the recommendation stores "not set": a card added later is re-recommended 898self .assertIsNone (ctx .settings .gpu_roles ) 899self .assertIsNone (ctx .settings .gpus ) 900self .assertIn ("Where should the model files be stored?" ,text_of (app )) 901self .run_app (case ,ctx = make_ctx (HW_2X16 )) 902 903def test_m_h_o_set_the_role_of_the_card_under_the_cursor_and_r_goes_back (self ): 904async def case (app ,pilot ,ctx ): 905await press (pilot ,"enter" ,"enter" ) 906await press (pilot ,"o" )# card 1 (a Helper) Off 907flat = " " .join (text_of (app ).split ()) 908self .assertIn ("recommended: helper" ,flat )# the muted tag says what was recommended 909await press (pilot ,"m" )# Main on card 1: card 2 becomes a Helper, one quiet line says so 910flat = " " .join (text_of (app ).split ()) 911self .assertIn ("is now a Helper." ,flat ) 912await press (pilot ,"enter" ) 913self .assertEqual (ctx .settings .gpu_roles , {"0" :"main" ,"1" :"helper" }) 914self .assertIsNone (ctx .settings .gpus ) 915self .run_app (case ,ctx = make_ctx (HW_2X16 )) 916 917def test_r_restores_the_recommendation (self ): 918async def case (app ,pilot ,ctx ): 919await press (pilot ,"enter" ,"enter" ,"down" ,"o" ,"r" ,"enter" ) 920self .assertIsNone (ctx .settings .gpu_roles ) 921self .run_app (case ,ctx = make_ctx (HW_2X16 )) 922 923def test_every_card_off_is_allowed_with_one_short_line (self ): 924async def case (app ,pilot ,ctx ): 925await press (pilot ,"enter" ,"enter" ,"down" ,"o" ,"up" ,"o" ) 926flat = " " .join (text_of (app ).split ()) 927self .assertIn ("! This engine needs a graphics card to start. Turn one on." ,flat ) 928await press (pilot ,"enter" ) 929self .assertEqual (ctx .settings .gpu_roles , {"0" :"off" ,"1" :"off" })# allowed; the server says plainly what the engine can do 930self .run_app (case ,ctx = make_ctx (HW_2X16 )) 931 932def test_d_shows_what_the_roles_mean (self ): 933async def case (app ,pilot ,ctx ): 934await press (pilot ,"enter" ,"enter" ,"d" ) 935self .assertIn ("Helper: holds a share of the model" ," " .join (text_of (app ).split ())) 936self .run_app (case ,ctx = make_ctx (HW_2X16 )) 937 938def test_an_integrated_chip_beside_one_card_gives_no_cards_step (self ): 939async def case (app ,pilot ,ctx ): 940await press (pilot ,"enter" ) 941self .assertIn ("Step 1 of 9" ,text_of (app )) 942hw = dict (HW_2X16 ) 943hw ["gpus" ]= [dict (HW_2X16 ["gpus" ][0 ]), {"index" :1 ,"name" :"AMD Radeon Graphics" ,"vram_gb" :16.0 ,"usable" :True ,"integrated" :True }] 944self .run_app (case ,ctx = make_ctx (hw )) 945 946def test_one_card_has_nine_steps (self ): 947async def case (app ,pilot ,ctx ): 948await press (pilot ,"enter" ) 949self .assertIn ("Step 1 of 9" ,text_of (app )) 950self .run_app (case ) 951 952 953class NarrowTerminal (Base ): 954def test_80_columns_still_readable (self ): 955ctx = make_ctx () 956app = LaunchApp (ctx ) 957 958async def go (): 959async with app .run_test (size = (80 ,24 ))as pilot : 960await pilot .pause (0.2 ) 961await press (pilot ,"enter" ) 962t = text_of (app ) 963self .assertIn ("Which model?" ,t ) 964self .assertIn ("Step 1 of 9" ,t ) 965self .assertIn ("Enter next" ,t ) 966await press (pilot ,"enter" ,"enter" ) 967self .assertIn ("How much text" ,text_of (app )) 968asyncio .run (go ()) 969 970 971if __name__ == "__main__" : 972unittest .main ()