okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0c53372a0
main
1"""Tests for `setup.py --update` (#475, UPDATE.bat / update.sh): it refreshes what a start would - the Python packages, 2the engine, each model's config and draft subset - and never starts the model. Every outside effect is mocked: no 3GPU, no downloads, nothing written outside a temp folder. 4 5python -m unittest tools.test_setup_update 6""" 7from __future__import annotations 8 9import contextlib 10import io 11import json 12import sys 13import tempfile 14import unittest 15from pathlib import Path 16from unittest import mock 17 18ROOT = Path (__file__ ).resolve ().parents [1 ] 19sys .path .insert (0 ,str (ROOT )) 20import setup # noqa: E402 21 22 23class Update (unittest .TestCase ): 24def config (self ,d :Path ,** extra )-> Path : 25rt = d / "mtp" / "rt" 26rt .mkdir (parents = True ) 27p = d / "strata-iq3_s.json" 28p .write_text (json .dumps ({"model_name" :"Qwen IQ3_S" ,"exe" :str (d / "engine" / setup .EXE ), 29"args" : ["--native" ,"x" ,"--mtp" ,str (rt )],** extra }),encoding = "utf-8" ) 30return p 31 32def run_update (self ,have ,argv = ()): 33a = mock .Mock (** {"build" :False ,"prebuilt" :"URL" ,** dict (argv )}) 34out = io .StringIO () 35with mock .patch .object (setup ,"pip_install" )as pip , \ 36mock .patch .object (setup ,"update_installed_engine" )as eng , \ 37mock .patch .object (setup ,"refresh_draft_vocab" )as dv , \ 38mock .patch .object (setup ,"start" )as start , \ 39mock .patch ("subprocess.call" )as call , \ 40contextlib .redirect_stdout (out ): 41rc = setup .update_install (have ,a ) 42return rc ,pip ,eng ,dv ,start ,call ,out .getvalue () 43 44def test_refreshes_without_starting (self ): 45with tempfile .TemporaryDirectory ()as d : 46p = self .config (Path (d ),draft_vocab = "en" ) 47rc ,pip ,eng ,dv ,start ,call ,out = self .run_update ([p ]) 48self .assertEqual (rc ,0 ) 49pip .assert_called_once () 50eng .assert_called_once_with ("URL" ) 51dv .assert_called_once () 52self .assertEqual (dv .call_args [0 ][1 ],"en" )# the model's own subset is kept 53start .assert_not_called () 54call .assert_not_called () 55self .assertIn ("Strata is updated" ,out ) 56 57def test_an_engine_that_stayed_old_is_not_called_updated (self ):# #3653 58with tempfile .TemporaryDirectory ()as d : 59p = self .config (Path (d )) 60with mock .patch .object (setup ,"engine_version" ,return_value = (0 ,1 ,44 ,1 )): 61rc ,pip ,eng ,dv ,start ,call ,out = self .run_update ([p ]) 62self .assertIn ("the engine was NOT" ,out ) 63self .assertIn ("Next step:" ,out ) 64self .assertNotIn ("[ok] Strata is updated" ,out ) 65with mock .patch .object (setup ,"engine_version" ,return_value = (0 ,1 ,49 ,2 )): 66rc ,pip ,eng ,dv ,start ,call ,out = self .run_update ([p ]) 67self .assertIn ("Strata is updated (engine 0.1.49.2)" ,out ) 68 69def test_build_keeps_the_compiled_engine_path (self ): 70with tempfile .TemporaryDirectory ()as d : 71p = self .config (Path (d )) 72rc ,pip ,eng ,dv ,* _ = self .run_update ([p ], {"build" :True }) 73self .assertEqual (rc ,0 ) 74eng .assert_not_called () 75self .assertEqual (dv .call_args [0 ][1 ],"cjk" ) 76 77def test_nothing_installed (self ): 78rc ,pip ,eng ,dv ,start ,call ,out = self .run_update ([]) 79self .assertEqual (rc ,0 ) 80for m in (pip ,eng ,dv ,start ,call ): 81m .assert_not_called () 82self .assertIn ("START-HERE.bat" ,out ) 83 84def test_a_json_that_is_no_model_config_is_skipped (self ): 85"""#549: a strata-*.json without "args" (not written by setup) stopped update.sh with KeyError: 'args'.""" 86with tempfile .TemporaryDirectory ()as d : 87p = self .config (Path (d )) 88other = Path (d )/ "strata-notes.json" 89other .write_text (json .dumps ({"note" :"mine" }),encoding = "utf-8" ) 90broken = Path (d )/ "strata-cut.json" 91broken .write_text ("{\"args\": [" ,encoding = "utf-8" ) 92rc ,pip ,eng ,dv ,start ,call ,out = self .run_update ([other ,broken ,p ]) 93self .assertEqual (rc ,0 ,out ) 94self .assertIn ('skipped strata-notes.json (no "exe" or "args"): it is not a Strata model config' ,out ) 95self .assertIn ("skipped strata-cut.json (not valid JSON)" ,out ) 96dv .assert_called_once ()# the real model is still refreshed 97self .assertIn ("Qwen IQ3_S: up to date" ,out ) 98self .assertIn ("Strata is updated" ,out ) 99 100def test_installed_configs_lists_only_model_configs (self ): 101with tempfile .TemporaryDirectory ()as d : 102p = self .config (Path (d )) 103 (Path (d )/ "strata-notes.json" ).write_text ("{}" ,encoding = "utf-8" ) 104with mock .patch .object (setup ,"ROOT" ,Path (d )),contextlib .redirect_stdout (io .StringIO ()): 105self .assertEqual (setup .installed_configs (), [p ]) 106 107def test_main_update_never_starts (self ): 108with tempfile .TemporaryDirectory ()as d : 109p = self .config (Path (d )) 110with mock .patch .object (sys ,"argv" , ["setup.py" ,"--update" ]), \ 111mock .patch .object (setup ,"data_folder" ,return_value = (Path (d ), [])), \ 112mock .patch .object (setup ,"installed_configs" ,return_value = [p ]), \ 113mock .patch .object (setup ,"update_install" ,return_value = 0 )as up , \ 114mock .patch .object (setup ,"start" )as start , \ 115contextlib .redirect_stdout (io .StringIO ()): 116self .assertEqual (setup .main (),0 ) 117up .assert_called_once () 118start .assert_not_called () 119 120 121class SettingsLine (unittest .TestCase ): 122"""#564: a start prints the settings it uses (the engine options without the model's paths, and the server's 123fields), so a change made by hand to strata-<model>.json shows without reading the log.""" 124 125CFG = {"exe" :"x" ,"host" :"0.0.0.0" ,"port" :8081 ,"api_key" :"secret" ,"gpu" : [0 ,1 ],"fit_max_tokens" :True , 126"args" : ["--native" ,"E:\\Strata\\packs\\iq3_s" ,"--mtp" ,"/s/mtp/rt" ,"m.gguf" ,"--kv" ,"int8" , 127"--kv-resident" ,"32768" ,"--spec-min-p" ,"0.5" ,"--vram-reserve-mib" ,"2048" ,"--mmap-experts" , 128"--prefill" ,"auto" ]} 129 130def test_summary (self ): 131s = setup .settings_summary (self .CFG ) 132self .assertEqual (s ,"--kv int8 --kv-resident 32768 --spec-min-p 0.5 --vram-reserve-mib 2048 --mmap-experts " 133"--prefill auto; server 0.0.0.0:8081, api key set, gpu 0,1, fit_max_tokens true" ) 134self .assertNotIn ("secret" ,s ) 135self .assertIn ("127.0.0.1:9000" ,setup .settings_summary ({"args" : []},9000 )) 136 137def test_a_start_prints_it (self ): 138with tempfile .TemporaryDirectory ()as d : 139exe = Path (d )/ "strata.exe" 140exe .write_bytes (b"" ) 141p = Path (d )/ "strata-iq3_s.json" 142args = [x for x in self .CFG ["args" ]if x != "m.gguf" ]# no model file here 143p .write_text (json .dumps ({** self .CFG ,"exe" :str (exe ),"gpu" :0 ,"args" :args }),encoding = "utf-8" ) 144out = io .StringIO () 145with mock .patch .object (setup ,"gpus" ,lambda : []), \ 146mock .patch .object (setup ,"refresh_draft_vocab" ), \ 147mock .patch .object (setup ,"is_wsl" ,lambda :False ), \ 148mock .patch .object (setup .subprocess ,"call" ,return_value = 0 ), \ 149contextlib .redirect_stdout (out ): 150setup .start (p ,None ,open_browser = False ,yes = True ) 151text = " " .join (out .getvalue ().split ()) 152self .assertIn ("Settings (strata-iq3_s.json): --kv int8 --kv-resident 32768" ,text ) 153self .assertIn ("--vram-reserve-mib 2048" ,text ) 154 155 156if __name__ == "__main__" : 157unittest .main ()