okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
538acd098
main
1"""Setup can start a component-backed config without treating the reference as a file. 2 3python -m unittest tools.test_setup_component_start -v 4""" 5import contextlib 6import io 7import json 8import os 9import sys 10import tempfile 11import unittest 12from pathlib import Path 13from unittest import mock 14 15sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 16import setup 17 18 19class ComponentStart (unittest .TestCase ): 20def setUp (self ): 21tmp = tempfile .TemporaryDirectory () 22self .addCleanup (tmp .cleanup ) 23self .root = Path (tmp .name ) 24self .config = self .root / "strata-iq3_s.json" 25self .gguf = self .root / "model.gguf" 26self .gguf .write_bytes (b"GGUF" ) 27self .cards = [{"index" :i ,"name" :f"RTX { i } " ,"arch" :"89" ,"vram_gb" :24.0 }for i in range (2 )] 28 29def run_start (self ,exe = "component:strata-engine" ,backend = "cuda" ,gpu = 0 ,win = False ,keep = None ): 30cfg = {"exe" :exe ,"backend" :backend ,"args" : ["--native" ,str (self .gguf )], 31"gpu" :gpu ,"gpus_asked" :True ,"model_name" :"test" ,"port" :8080 } 32self .config .write_text (json .dumps (cfg ),encoding = "utf-8" ) 33self .output = io .StringIO () 34with contextlib .ExitStack ()as st : 35st .enter_context (contextlib .redirect_stdout (self .output )) 36st .enter_context (mock .patch .object (setup ,"WIN" ,win )) 37st .enter_context (mock .patch .dict (os .environ , {"STRATA_EXECV" :"" })) 38st .enter_context (mock .patch .object (setup ,"gpus" ,return_value = self .cards )) 39st .enter_context (mock .patch .object (setup ,"amd_gpus" ,return_value = [ 40 {"index" :i ,"name" :f"Radeon { i } " ,"arch" :"gfx1201" ,"vram_gb" :24.0 }for i in range (2 )])) 41st .enter_context (mock .patch .object (setup ,"check_gpus" )) 42st .enter_context (mock .patch .object (setup ,"split_budget" ,return_value = False )) 43st .enter_context (mock .patch .object (setup ,"split_mmap" ,return_value = False )) 44st .enter_context (mock .patch .object (setup ,"recommend_remote_expert_opt" )) 45st .enter_context (mock .patch .object (setup ,"old_gpus_opt_in" ,return_value = [])) 46for name in ("engine_runs_on" ,"engine_archs_hip" ,"hip_runtime_beside_exe" ): 47st .enter_context (mock .patch .object (setup ,name ,side_effect = AssertionError ("legacy engine lookup: " + name ))) 48self .server = st .enter_context (mock .patch .object (setup .subprocess ,"call" ,return_value = 0 )) 49return setup .start (self .config ,None ,gpu = gpu ,open_browser = False ,yes = True ,keep = keep ) 50 51def test_component_reaches_the_server_and_keeps_its_reference (self ): 52self .assertEqual (self .run_start (),0 ) 53self .server .assert_called_once () 54argv = self .server .call_args .args [0 ] 55self .assertIn (str (setup .ROOT / "serve" / "server.py" ),argv ) 56self .assertEqual (argv [argv .index ("--config" )+ 1 ],str (self .config )) 57self .assertEqual (self .server .call_args .kwargs ["env" ][setup .acquisition .ENV ],"1" ) 58self .assertEqual (json .loads (self .config .read_text ())["exe" ],"component:strata-engine" ) 59 60def test_component_uses_saved_gpu_without_looking_for_a_source_build (self ): 61self .assertEqual (self .run_start (gpu = None ),0 ) 62self .server .assert_called_once () 63 64def test_component_can_save_several_gpus_without_looking_for_a_source_build (self ): 65self .assertEqual (self .run_start (gpu = [0 ,1 ]),0 ) 66self .assertEqual (json .loads (self .config .read_text ())["gpu" ], [0 ,1 ]) 67 68def test_windows_hip_component_does_not_repair_the_legacy_engine_folder (self ): 69self .assertEqual (self .run_start (backend = "hip" ,win = True ),0 ) 70self .server .assert_called_once () 71 72def test_hip_component_does_not_read_source_build_architectures (self ): 73self .assertEqual (self .run_start (backend = "hip" ,gpu = [0 ,1 ]),0 ) 74self .server .assert_called_once () 75 76def test_missing_model_file_is_still_reported (self ): 77self .gguf .unlink () 78with self .assertRaises (SystemExit ): 79self .run_start () 80self .assertIn (str (self .gguf ),self .output .getvalue ()) 81self .server .assert_not_called () 82 83def test_missing_literal_engine_path_is_still_reported (self ): 84missing = str (self .root / "strata" ) 85with self .assertRaises (SystemExit ): 86self .run_start (exe = missing ) 87self .assertIn (missing ,self .output .getvalue ()) 88self .server .assert_not_called () 89 90def test_component_reference_survives_saving_start_settings (self ): 91self .assertEqual (self .run_start (keep = {"host" :"127.0.0.1" ,"vram_reserve_mib" :1024 }),0 ) 92saved = json .loads (self .config .read_text ()) 93self .assertEqual (saved ["exe" ],"component:strata-engine" ) 94self .assertEqual (saved ["args" ][- 2 :], ["--vram-reserve-mib" ,"1024" ]) 95 96 97if __name__ == "__main__" : 98unittest .main ()