okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0918cba0a
main
1"""Tests for setup.py's experimental older-CPU build (#394 #595 #623): which floor a CPU without AVX2 gets, the CMake 2definition, a build folder configured afresh when the floor changes, and the MCP server's note instead of a refusal. 3No compiler, no GPU. 4 5python -m unittest tools.test_setup_oldcpu 6""" 7from __future__import annotations 8 9import io 10import os 11import sys 12import tempfile 13import unittest 14from pathlib import Path 15from unittest import mock 16 17ROOT = Path (__file__ ).resolve ().parents [1 ] 18sys .path .insert (0 ,str (ROOT )) 19sys .path .insert (0 ,str (ROOT / "tools" )) 20import setup # noqa: E402 21 22 23def cpuinfo (flags :str ): 24return mock .patch ("builtins.open" ,lambda * a ,** k :io .StringIO (f"model name\t: Old Xeon\nflags\t\t: { flags } \n" )) 25 26 27class FloorTest (unittest .TestCase ): 28def setUp (self ): 29self .env = mock .patch .dict (os .environ , {"STRATA_ISA_FLOOR" :"" }) 30self .env .start () 31 32def tearDown (self ): 33self .env .stop () 34 35def test_avx2_cpu_gets_the_normal_engine (self ): 36self .assertEqual (setup .cpu_floor (True ),"" ) 37 38def test_linux_flags (self ): 39with mock .patch .object (setup ,"WIN" ,False ): 40with cpuinfo ("fpu sse4_1 sse4_2 popcnt avx xsave" ): 41self .assertEqual (setup .cpu_floor (False ),"avx" ) 42with cpuinfo ("fpu sse4_1 sse4_2 popcnt" ): 43self .assertEqual (setup .cpu_floor (False ),"none" ) 44with cpuinfo ("fpu sse3 ssse3" ): 45self .assertEqual (setup .cpu_floor (False ),"unsupported" ) 46 47def test_windows_probe (self ): 48with mock .patch .object (setup ,"WIN" ,True ): 49for probe ,want in (("avx" ,"avx" ), ("sse4.2" ,"none" ), ("" ,"unsupported" )): 50with mock .patch .object (setup ,"_cpuid_floor" ,lambda p = probe :p ): 51self .assertEqual (setup .cpu_floor (False ),want ) 52 53def test_env_asks_for_a_floor_build_on_any_cpu (self ): 54for v in ("avx" ,"none" ,"AVX" ): 55with mock .patch .dict (os .environ , {"STRATA_ISA_FLOOR" :v }): 56self .assertEqual (setup .cpu_floor (True ),v .lower ()) 57with mock .patch .dict (os .environ , {"STRATA_ISA_FLOOR" :"bogus" }): 58self .assertEqual (setup .cpu_floor (True ),"" ) 59 60@ unittest . skipUnless ( os . name == "nt" , "the CPUID stub runs on Windows" ) 61def test_this_pc_probe (self ): 62self .assertIn (setup ._cpuid_floor (), ("avx" ,"sse4.2" ))# any x86-64 PC that runs the tests 63 64 65class DefsTest (unittest .TestCase ): 66def test_defs_and_fresh_configure (self ): 67with tempfile .TemporaryDirectory ()as d : 68b = Path (d ) 69cache = b / "CMakeCache.txt" 70self .assertEqual (setup .isa_floor_defs ("" ,b , {}), [])# the normal build: nothing added 71cache .write_text ("x" ) 72self .assertEqual (setup .isa_floor_defs ("avx" ,b , {"isa_floor" :"avx" }), ["-DSTRATA_ISA_FLOOR=avx" ]) 73self .assertTrue (cache .exists ())# same floor: the folder is kept 74self .assertEqual (setup .isa_floor_defs ("none" ,b , {"isa_floor" :"avx" }), ["-DSTRATA_ISA_FLOOR=none" ]) 75self .assertFalse (cache .exists ())# another floor: configured afresh 76cache .write_text ("x" ) 77self .assertEqual (setup .isa_floor_defs ("" ,b , {"isa_floor" :"none" }), []) 78self .assertFalse (cache .exists ())# back to the normal build 79cache .write_text ("x" ) 80setup .isa_floor_defs ("" ,b , {}) 81self .assertTrue (cache .exists ())# the normal build's folder untouched 82 83 84class McpTest (unittest .TestCase ): 85def test_no_avx2_is_a_note_not_a_refusal (self ): 86import strata_mcp as M 87s = M .Strata (ROOT ) 88gpu = [{"index" :0 ,"name" :"x" ,"vram_gb" :24.0 ,"usable" :True ,"vendor" :"nvidia" }] 89r = s .recommend ({"ram_gb" :64 ,"gpus" :gpu ,"cpu" : {"avx2" :False }}) 90self .assertIsNotNone (r ["model" ]) 91self .assertTrue (any ("no AVX2" in n and "EXPERIMENTAL" in n for n in r ["notes" ])) 92 93 94if __name__ == "__main__" : 95unittest .main ()