okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
67a48427b
main
1"""TUI stress test fixes on the screens: the Folder step (B05), the Save summary (B12), ctrl+c and Escape (B13), PermissionError words, setup guard.""" 2 3import contextlib 4import io 5import json 6import os 7import tempfile 8import unittest 9from pathlib import Path 10from unittest import mock 11 12import setup 13from strata_control import folders 14from strata_cli .launch import settings as S 15from strata_cli .launch .runner import setup_failure 16from tests .terminal .launch_support import run_cli 17from tests .terminal .test_launch_tui import Base ,FakeRunner ,flat ,press ,text_of ,typed 18 19 20class FolderRules (unittest .TestCase ): 21def setUp (self ): 22self .tmp = tempfile .TemporaryDirectory () 23self .addCleanup (self .tmp .cleanup ) 24self .base = Path (self .tmp .name ) 25 26def test_usable_folders_pass_and_nothing_is_left_behind (self ): 27self .assertIsNone (folders .problem ("" )) 28self .assertIsNone (folders .problem (self .base )) 29self .assertIsNone (folders .problem (self .base / "new" / "deeper" ))# the parent exists and can be written: it can be created 30self .assertEqual (os .listdir (self .base ), [])# nothing was created, the test file is gone 31 32def test_a_file_is_not_a_folder (self ): 33f = self .base / "a.txt" 34f .write_text ("x" ) 35what ,nxt = folders .problem (f ) 36self .assertIn ("is a file" ,what ) 37self .assertTrue (nxt ) 38 39def test_a_missing_parent_that_cannot_be_created_is_refused (self ): 40f = self .base / "a.txt" 41f .write_text ("x" ) 42self .assertIn ("is a file" ,folders .problem (f / "inside" )[0 ]) 43with mock .patch .object (folders ,"_can_write" ,return_value = False ): 44what ,nxt = folders .problem (self .base / "x" / "y" ) 45self .assertIn ("cannot be created" ,what ) 46 47def test_a_folder_that_cannot_be_written_is_refused (self ): 48with mock .patch .object (folders ,"_can_write" ,return_value = False ): 49what ,nxt = folders .problem (self .base ) 50self .assertIn ("cannot write" ,what ) 51self .assertIn ("Pick another folder" ,nxt ) 52 53def test_the_write_test_creates_and_deletes_a_file (self ): 54seen = [] 55real = open 56 57def spy (path ,* a ,** k ): 58seen .append (str (path )) 59return real (path ,* a ,** k ) 60 61with mock .patch ("builtins.open" ,spy ): 62self .assertTrue (folders ._can_write (self .base )) 63self .assertTrue (any (".strata-write-test-" in s for s in seen )) 64self .assertEqual (os .listdir (self .base ), []) 65 66@ unittest . skipUnless ( os . name == "nt" , "Windows system folders" ) 67def test_system_folders_are_refused (self ): 68for p in (r"C:\Windows\System32" ,r"C:\Windows" ,r"C:\Program Files\Strata" ,r"C:\Program Files (x86)\x" ): 69with self .subTest (p = p ): 70what ,nxt = folders .problem (p ) 71self .assertIn ("system folder" ,what ) 72 73@ unittest . skipUnless ( os . name == "nt" , "Windows path rules" ) 74def test_malformed_and_relative_paths_are_refused (self ): 75for p in (r"C:\a C:\b" ,r"C:\models|x" ,r"D:\what?" ,"models" ,r"..\x" ): 76with self .subTest (p = p ): 77self .assertIsNotNone (folders .problem (p ),p ) 78 79def test_the_plain_words_carry_a_next_step (self ): 80text = folders .words (("This is bad." ,"Do that." )) 81self .assertEqual (text ,"This is bad. Do that." ) 82 83 84class FolderScreen (Base ): 85def test_a_bad_folder_stays_on_the_screen_with_one_plain_line_and_is_not_kept (self ): 86async def case (app ,pilot ,ctx ): 87await press (pilot ,"enter" ,"enter" ) 88f = app .screen .query_one ("#folderpath" ) 89f .value = "" 90await typed (pilot ,str (self .home .base / "afile.txt" )) 91 (self .home .base / "afile.txt" ).write_text ("x" ) 92await press (pilot ,"enter" ) 93t = flat (app ) 94self .assertIn ("Where should the model files be stored?" ,t ) 95self .assertIn ("is a file, not a folder" ,t ) 96self .assertIn ("Pick another folder" ,t ) 97self .assertIsNone (ctx .settings .data_dir ) 98f .value = "" 99await typed (pilot ,str (self .home .base / "good" )) 100await press (pilot ,"enter" ) 101self .assertIn ("How much text should it remember at once?" ,text_of (app )) 102self .assertEqual (ctx .settings .data_dir ,str (self .home .base / "good" )) 103self .run_app (case ) 104 105def test_the_save_summary_shows_the_chosen_storage_path (self ): 106async def case (app ,pilot ,ctx ): 107await press (pilot ,* ("enter" ,)* 7 ,"n" ,"enter" ) 108t = flat (app ) 109self .assertIn ("Storage" ,t ) 110self .assertIn (str (ctx .default_folder ()),t ) 111self .run_app (case ) 112 113def test_the_save_summary_shows_a_typed_folder (self ): 114async def case (app ,pilot ,ctx ): 115await press (pilot ,"enter" ,"enter" ) 116f = app .screen .query_one ("#folderpath" ) 117f .value = "" 118await typed (pilot ,str (self .home .base / "mine" )) 119await press (pilot ,* ("enter" ,)* 5 ,"n" ,"enter" ) 120self .assertIn ("Step 9 of 9" ,flat (app )) 121self .assertIn ("mine" ,flat (app )) 122self .run_app (case ) 123 124 125class QuitKeys (Base ): 126def test_ctrl_c_quits_the_setup_screens_like_ctrl_q (self ): 127for key in ("ctrl+c" ,"ctrl+q" ): 128with self .subTest (key = key ): 129async def case (app ,pilot ,ctx ): 130await press (pilot ,"enter" )# the Model screen 131await press (pilot ,key ) 132await pilot .pause (0.3 ) 133self .assertTrue (app ._exit )# the app is closing 134self .run_app (case ) 135 136def test_ctrl_c_in_the_folder_field_still_quits (self ): 137async def case (app ,pilot ,ctx ): 138await press (pilot ,"enter" ,"enter" ) 139await press (pilot ,"ctrl+c" ) 140await pilot .pause (0.3 ) 141self .assertTrue (app ._exit ) 142self .run_app (case ) 143 144def test_escape_in_the_stop_dialog_only_closes_it (self ): 145from strata_cli .launch import store 146async def case (app ,pilot ,ctx ): 147store .save (S .LaunchSettings (name = "T" ,family = "qwen" ,model = "IQ3_XXS" ,context = 32768 )) 148FakeRunner .behaviour = "wait" 149app .start_preset ("T" ) 150await pilot .pause (0.4 ) 151await press (pilot ,"ctrl+c" )# work would be lost: the dialog stays 152self .assertIn ("Stop now?" ,text_of (app )) 153await press (pilot ,"escape" ) 154self .assertNotIn ("Stop now?" ,text_of (app )) 155self .assertTrue (app .running )# still running, and no Back happened 156self .assertEqual (app .current ,"run" ) 157app .cancel .set () 158await pilot .pause (0.5 ) 159self .run_app (case ) 160 161 162class PermissionWords (unittest .TestCase ): 163def test_a_permission_error_names_the_folder_and_what_to_do (self ): 164lines = ["Traceback (most recent call last):" ," f" , 165r"PermissionError: [WinError 5] Access is denied: 'C:\Windows\System32\models'" ] 166e = setup_failure (lines ,"" ,"" ,r"C:\x\a.setup.log" ,False ) 167self .assertIn (r"C:\Windows\System32" ,e .text ) 168self .assertIn ("access denied" ,e .text ) 169self .assertNotIn ("WinError" ,e .text + e .next ) 170self .assertNotIn ("Run it again" ,e .next ) 171self .assertIn ("--data-dir" ,e .next ) 172 173def test_other_errors_keep_their_words (self ): 174e = setup_failure (["Traceback (most recent call last):" ," f" ,"KeyError: 'cuda'" ],"" ,"" ,None ,True ) 175self .assertIn ("KeyError" ,e .text ) 176 177 178class SetupGuard (unittest .TestCase ): 179def setUp (self ): 180self .tmp = tempfile .TemporaryDirectory () 181self .addCleanup (self .tmp .cleanup ) 182self .base = Path (self .tmp .name ) 183self .root = self .base / "Strata" 184self .root .mkdir () 185env = mock .patch .dict (os .environ , {"APPDATA" :str (self .base / "appdata" ),"XDG_CONFIG_HOME" :str (self .base / "appdata" )}) 186env .start () 187self .addCleanup (env .stop ) 188os .environ .pop ("STRATA_SETTINGS_FILE" ,None ) 189for patcher in (mock .patch .object (setup ,"ROOT" ,self .root ),mock .patch .object (setup ,"_CONTROL_HOME" ,None )): 190patcher .start () 191self .addCleanup (patcher .stop ) 192setup .settings_path ().parent .mkdir (parents = True ,exist_ok = True ) 193 194def test_a_bad_requested_folder_stops_with_plain_words_and_is_never_saved (self ): 195setup .settings_path ().write_text (json .dumps ({"installs" : []}),encoding = "utf-8" ) 196afile = self .base / "afile" 197afile .write_text ("x" ) 198with contextlib .redirect_stdout (io .StringIO ())as out ,self .assertRaises (SystemExit ): 199setup .data_folder (str (afile ),yes = True ) 200self .assertIn ("is a file, not a folder" ,out .getvalue ()) 201self .assertNotIn ("data_dir" ,json .loads (setup .settings_path ().read_text (encoding = "utf-8" ))) 202 203def test_a_bad_saved_folder_is_dropped_not_kept (self ): 204afile = self .base / "afile" 205afile .write_text ("x" ) 206setup .settings_path ().write_text (json .dumps ({"data_dir" :str (afile ),"installs" : []}),encoding = "utf-8" ) 207with contextlib .redirect_stdout (io .StringIO ()): 208dest ,_ = setup .data_folder (None ,yes = True ) 209self .assertNotEqual (str (dest .resolve ()),str (afile .resolve ())) 210self .assertNotEqual (json .loads (setup .settings_path ().read_text (encoding = "utf-8" )).get ("data_dir" ),str (afile )) 211 212 213class DataDirFlag (unittest .TestCase ): 214def test_launch_data_dir_with_a_bad_path_refuses_before_anything (self ): 215with tempfile .TemporaryDirectory ()as tmp : 216afile = Path (tmp )/ "afile" 217afile .write_text ("x" ) 218code ,out ,err = run_cli (["launch" ,"--plan" ,"--model" ,"q2_0" ,"--data-dir" ,str (afile )]) 219self .assertEqual (code ,2 ) 220self .assertIn ("is a file, not a folder" ,err ) 221 222 223if __name__ == "__main__" : 224unittest .main ()