okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a10777a67
main
1"""`--data-dir` names a new folder: the model library is never moved without a choice (0.2.1 R). 2 3Everything lives in temporary folders: settings.json (APPDATA / XDG_CONFIG_HOME), the Strata folder (setup.ROOT) and the data folders. 4""" 5from __future__import annotations 6 7import argparse 8import contextlib 9import io 10import json 11import os 12import shutil 13import sys 14import tempfile 15import unittest 16from pathlib import Path 17from unittest import mock 18 19import setup 20from strata_cli .launch import cmd ,library 21from strata_cli .launch import settings as S 22from strata_cli .launch .runner import Runner 23 24from tests .terminal .test_launch import HW_12 ,Home ,catalog 25 26 27def put (path :Path ,size :int = 64 )-> Path : 28path .parent .mkdir (parents = True ,exist_ok = True ) 29path .write_bytes (b"x" * size ) 30return path 31 32 33class DataFolderChoice (unittest .TestCase ): 34def setUp (self ): 35self .tmp = tempfile .TemporaryDirectory () 36self .addCleanup (self .tmp .cleanup ) 37self .base = Path (self .tmp .name ) 38self .root = self .base / "Strata" 39self .root .mkdir () 40self .old = self .base / "old-data" 41self .new = self .base / "new-data" 42env = mock .patch .dict (os .environ , {"APPDATA" :str (self .base / "appdata" ),"XDG_CONFIG_HOME" :str (self .base / "appdata" ), 43"STRATA_SETTINGS_FILE" :"" }) 44env .start () 45self .addCleanup (env .stop ) 46os .environ .pop ("STRATA_SETTINGS_FILE" ,None ) 47for patcher in (mock .patch .object (setup ,"ROOT" ,self .root ),mock .patch .object (setup ,"_CONTROL_HOME" ,None )): 48patcher .start () 49self .addCleanup (patcher .stop ) 50self .assertTrue (str (setup .settings_path ()).startswith (str (self .base )),setup .settings_path ()) 51self .weights = put (self .old / "models" / "Q2_0" / "w-00001-of-00001.gguf" ,200 ) 52self .pack = put (self .old / "packs" / "q2_0" / "tokenizer" / "vocab.json" ,20 ) 53self .mtp = put (self .old / "mtp" / "rt" / "experts.bin" ,30 ) 54setup .settings_path ().parent .mkdir (parents = True ,exist_ok = True ) 55self .write_settings ({"data_dir" :str (self .old ),"installs" : []}) 56 57def write_settings (self ,data :dict )-> None : 58setup .settings_path ().write_text (json .dumps (data ),encoding = "utf-8" ) 59 60def saved (self )-> dict : 61return json .loads (setup .settings_path ().read_text (encoding = "utf-8" )) 62 63def call (self ,* args ,** kw ): 64with contextlib .redirect_stdout (io .StringIO ())as out : 65res = setup .data_folder (* args ,** kw ) 66return res ,out .getvalue () 67 68def assertLibraryUntouched (self ): 69for f in (self .weights ,self .pack ,self .mtp ): 70self .assertTrue (f .is_file (),f ) 71 72def test_a_new_data_dir_with_yes_keeps_the_library_and_the_saved_default (self ): 73 (dest ,elsewhere ),_ = self .call (str (self .new ),yes = True ) 74self .assertEqual (dest .resolve (),self .new .resolve ()) 75self .assertTrue (dest .is_dir ()) 76self .assertEqual ([Path (e ).resolve ()for e in elsewhere ], [self .old .resolve ()])# still found: the old folder is a root 77self .assertLibraryUntouched () 78self .assertFalse ((self .new / "models" ).exists ()) 79self .assertEqual (self .saved ()["data_dir" ],str (self .old ))# the saved default is not rewritten 80 81def test_the_default_answer_with_no_keyboard_keeps_everything (self ): 82with mock .patch ("builtins.input" ,side_effect = EOFError ): 83with self .assertRaises (SystemExit ):# ask() ends a setup with nobody to answer 84self .call (str (self .new ),yes = False ) 85self .assertLibraryUntouched () 86self .assertEqual (self .saved ()["data_dir" ],str (self .old )) 87 88def test_asked_the_answer_keep_moves_nothing (self ): 89with mock .patch ("builtins.input" ,return_value = "" ): 90 (dest ,elsewhere ),said = self .call (str (self .new ),yes = False ) 91self .assertIn ("keep)" ,said ) 92self .assertLibraryUntouched () 93self .assertEqual (self .saved ()["data_dir" ],str (self .old )) 94self .assertEqual ([Path (e ).resolve ()for e in elsewhere ], [self .old .resolve ()]) 95 96def test_asked_the_answer_move_moves_and_remembers_the_new_folder (self ): 97with mock .patch ("builtins.input" ,return_value = "move" ): 98 (dest ,elsewhere ),_ = self .call (str (self .new ),yes = False ) 99self .assertEqual (elsewhere , []) 100self .assertTrue ((self .new / "models" / "Q2_0" / "w-00001-of-00001.gguf" ).is_file ()) 101self .assertTrue ((self .new / "packs" / "q2_0" / "tokenizer" / "vocab.json" ).is_file ()) 102self .assertFalse (self .weights .exists ()) 103self .assertEqual (Path (self .saved ()["data_dir" ]).resolve (),self .new .resolve ()) 104 105def test_move_data_moves_without_asking (self ): 106with mock .patch ("builtins.input" ,side_effect = AssertionError ("must not ask" )): 107 (dest ,elsewhere ),_ = self .call (str (self .new ),move = True ,yes = True ) 108self .assertTrue ((self .new / "mtp" / "rt" / "experts.bin" ).is_file ()) 109self .assertEqual (Path (self .saved ()["data_dir" ]).resolve (),self .new .resolve ()) 110 111def test_set_default_data_dir_remembers_the_folder_and_moves_nothing (self ): 112 (dest ,elsewhere ),_ = self .call (str (self .new ),yes = True ,set_default = True ) 113self .assertLibraryUntouched () 114self .assertEqual (Path (self .saved ()["data_dir" ]).resolve (),self .new .resolve ()) 115self .assertEqual ([Path (e ).resolve ()for e in elsewhere ], [self .old .resolve ()]) 116 117def test_a_pc_with_no_models_anywhere_remembers_the_new_folder (self ): 118for d in ("models" ,"packs" ,"mtp" ): 119shutil .rmtree (self .old / d ) 120 (dest ,elsewhere ),_ = self .call (str (self .new ),yes = True ) 121self .assertEqual (elsewhere , []) 122self .assertEqual (Path (self .saved ()["data_dir" ]).resolve (),self .new .resolve ()) 123 124def test_the_library_in_the_strata_folder_is_asked_about_too (self ): 125# no remembered data folder: the models sit next to the checkout (an old install) 126self .write_settings ({}) 127for d in ("models" ,"packs" ,"mtp" ): 128shutil .rmtree (self .old / d ) 129mine = put (self .root / "Strata-data" / "models" / "Q2_0" / "w-00001-of-00001.gguf" ) 130 (dest ,elsewhere ),_ = self .call (str (self .new ),yes = True ) 131self .assertTrue (mine .is_file ()) 132self .assertEqual ([Path (e ).resolve ()for e in elsewhere ], [(self .root / "Strata-data" ).resolve ()]) 133self .assertNotIn ("data_dir" ,self .saved ()) 134 135def test_no_data_dir_still_gathers_into_the_remembered_folder (self ): 136# the data folder's own job (an earlier Strata folder's models) is not part of the --data-dir question 137other = self .base / "Strata-old" 138other .mkdir () 139 (other / "setup.py" ).write_text ("# old\n" ) 140put (other / "models" / "Q2_0" / "extra.gguf" ) 141self .write_settings ({"data_dir" :str (self .old ),"installs" : [str (other )]}) 142 (dest ,_ ),_ = self .call (None ,yes = True ) 143self .assertEqual (dest .resolve (),self .old .resolve ()) 144self .assertTrue ((self .old / "models" / "Q2_0" / "extra.gguf" ).is_file ()) 145 146def test_the_same_folder_again_is_not_a_change (self ): 147 (dest ,elsewhere ),_ = self .call (str (self .old ),yes = False ) 148self .assertEqual (elsewhere , []) 149self .assertLibraryUntouched () 150 151 152class TerminalQuestion (unittest .TestCase ): 153def setUp (self ): 154self .tmp = tempfile .TemporaryDirectory () 155self .addCleanup (self .tmp .cleanup ) 156self .base = Path (self .tmp .name ) 157self .old = self .base / "old" 158self .new = self .base / "new" 159put (self .old / "models" / "Q2_0" / "w.gguf" ,3_000_000 ) 160self .cat = mock .Mock () 161self .cat .data_dir .return_value = self .old 162 163def args (self ,** kw ): 164ns = dict (data_dir = str (self .new ),move_data = False ,yes = False ,plan = False ) 165ns .update (kw ) 166return argparse .Namespace (** ns ) 167 168def test_no_data_dir_asks_nothing (self ): 169self .assertFalse (cmd ._move_data_choice (self .args (data_dir = None ),S .LaunchSettings (),self .cat ,False )) 170 171def test_move_data_says_yes_without_asking (self ): 172with mock .patch ("builtins.input" ,side_effect = AssertionError ("must not ask" )): 173self .assertTrue (cmd ._move_data_choice (self .args (move_data = True ),S .LaunchSettings (),self .cat ,False )) 174 175def test_yes_machine_and_plan_keep_the_library (self ): 176with mock .patch ("builtins.input" ,side_effect = AssertionError ("must not ask" )): 177for kw ,machine in (({"yes" :True },False ), ({},True ), ({"plan" :True },False )): 178self .assertFalse (cmd ._move_data_choice (self .args (** kw ),S .LaunchSettings (),self .cat ,machine ),kw ) 179 180def test_no_keyboard_keeps_the_library (self ): 181with mock .patch .object (sys .stdin ,"isatty" ,return_value = False ),mock .patch ("builtins.input" ,side_effect = AssertionError ): 182self .assertFalse (cmd ._move_data_choice (self .args (),S .LaunchSettings (),self .cat ,False )) 183 184def test_asked_on_a_terminal (self ): 185out = io .StringIO () 186with mock .patch .object (sys .stdin ,"isatty" ,return_value = True ),contextlib .redirect_stdout (out ): 187with mock .patch ("builtins.input" ,return_value = "m" ): 188self .assertTrue (cmd ._move_data_choice (self .args (),S .LaunchSettings (),self .cat ,False )) 189with mock .patch ("builtins.input" ,return_value = "" ): 190self .assertFalse (cmd ._move_data_choice (self .args (),S .LaunchSettings (),self .cat ,False )) 191self .assertIn ("use the new folder only for new downloads" ,out .getvalue ()) 192 193def test_an_empty_or_same_library_asks_nothing (self ): 194self .cat .data_dir .return_value = self .new 195with mock .patch .object (sys .stdin ,"isatty" ,return_value = True ),mock .patch ("builtins.input" ,side_effect = AssertionError ): 196self .assertFalse (cmd ._move_data_choice (self .args (),S .LaunchSettings (),self .cat ,False )) 197self .assertEqual (library .movable_library (self .base / "nothing" ,self .new ),0.0 ) 198 199def test_setup_gets_move_data_only_when_chosen_and_a_folder_is_named (self ): 200with Home (): 201cat = catalog (HW_12 ) 202s = S .LaunchSettings (family = "qwen" ,model = "IQ3_XXS" ,context = 32768 ,vision = "off" ,data_dir = "/d" ) 203self .assertNotIn ("--move-data" ,Runner (s ,catalog = cat ).setup_args ()) 204self .assertIn ("--move-data" ,Runner (s ,catalog = cat ,move_data = True ).setup_args ()) 205s2 = S .LaunchSettings (family = "qwen" ,model = "IQ3_XXS" ,context = 32768 ,vision = "off" ) 206self .assertNotIn ("--move-data" ,Runner (s2 ,catalog = cat ,move_data = True ).setup_args ()) 207with mock .patch .object (sys ,"platform" ,"darwin" ): 208mac = Runner (s ,catalog = cat ,move_data = True ).setup_args () 209self .assertIn ("--move-data" ,mac ) 210self .assertEqual (mac [mac .index ("--data-dir" )+ 1 ],"/d" ) 211 212 213if __name__ == "__main__" : 214unittest .main ()