okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f31abf7bd
main
1"""0.2.10: a PC with cards of more than one make chooses the engine with `strata launch --backend` (kept in the settings file), is told that the 2other cards exist, and a person who chose is never refused.""" 3 4from __future__import annotations 5 6import json 7import os 8import tempfile 9import unittest 10from pathlib import Path 11from unittest import mock 12 13from strata_control import engine_component as ec ,gpu_fit 14 15NV = {"vendor" :"nvidia" ,"name" :"NVIDIA GeForce RTX 5060" ,"vram_gb" :8.0 ,"usable" :True ,"index" :0 } 16ARC = [{"vendor" :"intel" ,"name" :"Intel Arc Pro B65" ,"vram_gb" :32.0 ,"usable" :True ,"index" :i }for i in (0 ,1 )] 17 18 19class SavedChoice (unittest .TestCase ): 20def env (self ,d ): 21return {"STRATA_SETTINGS_FILE" :str (Path (d )/ "settings.json" )} 22 23def test_names_people_type (self ): 24self .assertEqual ([ec .clean_backend_name (x )for x in ("Intel" ,"nvidia" ,"AMD" ,"sycl" ,"arc" ,"toaster" ,"" )], 25 ["sycl" ,"cuda" ,"hip" ,"sycl" ,"sycl" ,"" ,"" ]) 26 27def test_a_saved_choice_decides_and_keeps_the_other_settings (self ): 28with tempfile .TemporaryDirectory ()as d : 29env = self .env (d ) 30Path (env ["STRATA_SETTINGS_FILE" ]).write_text (json .dumps ({"data_dir" :"/x" })) 31with mock .patch .object (ec ,"gpu_makes_present" ,lambda : {"cuda" ,"sycl" }): 32ec .save_backend ("intel" ,env ) 33self .assertEqual (json .loads (Path (env ["STRATA_SETTINGS_FILE" ]).read_text ()), {"data_dir" :"/x" ,"engine_backend" :"sycl" }) 34self .assertEqual (ec .detect_backend (env ), ("sycl" ,"saved choice" )) 35self .assertEqual (ec .detect_backend ({** env ,ec .ENV_BACKEND :"cuda" }), ("cuda" ,"environment" ),"the environment still wins" ) 36ec .save_backend (None ,env ) 37self .assertEqual (json .loads (Path (env ["STRATA_SETTINGS_FILE" ]).read_text ()), {"data_dir" :"/x" }) 38 39def test_a_choice_for_a_make_this_pc_does_not_have_is_ignored (self ): 40with tempfile .TemporaryDirectory ()as d : 41env = self .env (d ) 42with mock .patch .object (ec ,"gpu_makes_present" ,lambda : {"cuda" }): 43ec .save_backend ("sycl" ,env ) 44self .assertIsNone (ec .saved_backend (env )) 45 46def test_the_update_make_guard_lets_a_saved_choice_through (self ): 47from strata_control import updater 48ctx = mock .Mock () 49ctx .hardware = {"backend" :"sycl" ,"backend_source" :"saved choice" } 50self .assertIsNone (updater .make_guard (ctx )) 51 52 53class Notes (unittest .TestCase ): 54def test_intel_cards_beside_nvidia_are_always_mentioned (self ): 55note = gpu_fit .other_vendor_note ([NV ,* ARC ],"nvidia" ) 56self .assertIn ("2 Intel Arc cards (64 GB together)" ,note ) 57self .assertIn ("--backend intel" ,note ) 58 59def test_one_intel_card_and_the_reverse_note (self ): 60self .assertIn ("an Intel Arc Pro B65 (32 GB)" ,gpu_fit .other_vendor_note ([NV ,ARC [0 ]],"nvidia" )) 61back = gpu_fit .other_vendor_note ([NV ,* ARC ],"intel" ) 62self .assertIn ("NVIDIA" ,back ) 63self .assertIn ("--backend auto" ,back ) 64 65def test_no_note_without_other_makes (self ): 66self .assertIsNone (gpu_fit .other_vendor_note (ARC ,"intel" )) 67 68 69class BrokenNvidia (unittest .TestCase ): 70def test_says_what_is_wrong_and_the_next_step_only_when_it_is (self ): 71from strata_cli .launch import cmd 72comp = mock .Mock (gpus = list (ARC )) 73with mock .patch .object (ec ,"detect_backend" ,lambda * a ,** k : ("cuda" ,"sysfs pci vendor" )),mock .patch .object (ec ,"gpu_makes_present" ,lambda : {"cuda" ,"sycl" }): 74note = cmd ._broken_nvidia_note (comp ) 75self .assertIn ("driver does not answer" ,note ) 76self .assertIn ("--backend intel" ,note ) 77self .assertIsNone (cmd ._broken_nvidia_note (mock .Mock (gpus = [NV ,* ARC ])),"a working NVIDIA card: nothing to say" ) 78with mock .patch .object (ec ,"detect_backend" ,lambda * a ,** k : ("sycl" ,"saved choice" )),mock .patch .object (ec ,"gpu_makes_present" ,lambda : {"cuda" ,"sycl" }): 79self .assertIsNone (cmd ._broken_nvidia_note (comp ),"the person already chose" ) 80 81 82class SavedChoiceLine (unittest .TestCase ): 83def test_a_launch_after_a_saved_choice_says_so (self ): 84from strata_cli .launch import cmd ,settings as S 85from tests .terminal .launch_support import HW_12 ,Home ,catalog 86with Home (),mock .patch .object (ec ,"detect_backend" ,lambda * a ,** k : ("sycl" ,"saved choice" )): 87_s ,notes = cmd .apply_defaults (S .LaunchSettings (family = "qwen" ,model = "IQ3_XXS" ),catalog (HW_12 )) 88hit = [n for n in notes if "your saved choice" in n .text ] 89self .assertTrue (hit , [n .text for n in notes ]) 90self .assertIn ("Intel cards" ,hit [0 ].text ) 91self .assertIn ("--backend auto" ,hit [0 ].text ) 92 93 94if __name__ == "__main__" : 95unittest .main ()