Files
strata/tools/test_setup_mac.py
T
Niko1221 93d7ceb91d setup on macOS: IQ3_S offered (recommended from 110 GB), streaming Macs configured without MTP
IQ3_S runs on Metal since mac-kern's IQ4_XS kernels: recommended from 110 GB (Apple's 128 GB Macs), IQ3_XXS where it
fits resident, then Q2_0. A pack that does not fit Metal's share of the memory streams its experts from the SSD (the
engine decides, f-mac-ssd): setup says so with the M4 numbers, writes --suffix-draft 0 and no --mtp (one token per
step, measured faster there) and does not fetch the draft layer.
2026-10-12 20:02:39 +02:00

130 lines
6.0 KiB
Python

"""Tests for setup on a Mac (metal/setup_mac.py and setup.py's hand-over): the Mac check, the pack recommended by
unified memory, what a bigger choice risks, the engine arguments with the Metal defaults and the config. Runs on any
PC: sysctl / sw_vers / xcode-select are mocked. No GPU, no network.
python -m unittest tools.test_setup_mac
"""
from __future__ import annotations
import subprocess
import sys
import unittest
from pathlib import Path
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "metal"))
import setup # noqa: E402
import setup_mac as M # noqa: E402
def fake_run(values: dict, clt_rc=0):
def run(cmd, **kw):
if cmd[0] == "sysctl":
return subprocess.CompletedProcess(cmd, 0, values.get(cmd[2], ""), "")
if cmd[0] == "sw_vers":
return subprocess.CompletedProcess(cmd, 0, values.get("macos", ""), "")
if cmd[0] == "xcode-select":
return subprocess.CompletedProcess(cmd, clt_rc, "/Library/Developer/CommandLineTools" if clt_rc == 0 else "",
"")
raise AssertionError(cmd)
return run
M5 = {"machdep.cpu.brand_string": "Apple M5 Max", "hw.memsize": str(128 * 2 ** 30), "macos": "27.2",
"hw.perflevel0.physicalcpu": "12"}
class MacCheck(unittest.TestCase):
def test_reads_chip_memory_macos_and_tools(self):
with mock.patch.object(M.platform, "machine", return_value="arm64"):
info = M.mac_info(fake_run(M5))
self.assertEqual(info["chip"], "Apple M5 Max")
self.assertAlmostEqual(info["ram_gb"], 128.0)
self.assertEqual(info["macos"], "27.2")
self.assertTrue(info["arm64"])
self.assertTrue(info["clt"])
self.assertEqual(M.mac_problems(info), [])
def test_intel_old_macos_and_missing_tools_are_named(self):
with mock.patch.object(M.platform, "machine", return_value="x86_64"):
info = M.mac_info(fake_run({**M5, "macos": "14.6", "machdep.cpu.brand_string": "Intel Core i9"}, clt_rc=2))
p = " | ".join(M.mac_problems(info))
self.assertIn("Apple Silicon", p)
self.assertIn("macOS 15", p)
self.assertIn("xcode-select --install", p)
class Recommendation(unittest.TestCase):
def test_by_memory(self):
self.assertEqual(M.recommend_model(128)[0], "IQ3_S")
self.assertEqual(M.recommend_model(96)[0], "IQ3_XXS")
self.assertEqual(M.recommend_model(64)[0], "Q2_0")
rec, notes = M.recommend_model(32)
self.assertEqual(rec, "Q2_0")
self.assertTrue(notes and "stream" in notes[0] and "~14 tok/s" in notes[0])
self.assertTrue(M.streams("Q2_0", 32) and M.streams("Q2_0", 48) and not M.streams("Q2_0", 64))
def test_a_bigger_choice_is_kept_with_a_note(self):
notes = M.model_note("IQ3_XXS", 64)
self.assertTrue(any("stream from the SSD" in n for n in notes))
self.assertEqual(M.model_note("Q2_0", 128), [])
self.assertEqual(M.model_note("IQ3_S", 128), [])
def test_context(self):
self.assertEqual(M.recommend_context(64), 32768)
self.assertEqual(M.recommend_context(128), 65536)
class Config(unittest.TestCase):
def test_engine_args_carry_the_metal_defaults(self):
args = M.engine_args(Path("/p/packs/q2_0"), [Path("/m/a-00001.gguf"), Path("/m/a-00002.gguf")],
Path("/d/mtp/rt"), 32768)
self.assertEqual(args[args.index("--spec") + 1], "2")
self.assertEqual(args[args.index("--native") + 1], str(Path("/m/a-00001.gguf")))
self.assertEqual(args[args.index("--ple-gguf") + 1], str(Path("/m/a-00002.gguf")))
self.assertEqual(args[args.index("--mtp") + 1], str(Path("/d/mtp/rt")))
self.assertIn("--expert-profile", args)
self.assertNotIn("--hc-q8", args) # Q8_0 HC is the engine's own default on Metal
st = M.engine_args(Path("/p"), [Path("/a1.gguf"), Path("/a2.gguf")], Path("/d/mtp/rt"), 32768, stream=True)
self.assertNotIn("--mtp", st) # streaming: one token per step, no draft layer
self.assertEqual(st[st.index("--suffix-draft") + 1], "0")
def test_config_is_a_model_config_with_the_metal_backend(self):
cfg = M.mac_config(Path("/r/build-metal/strata"), ["--pack", "x"], Path("/p"), "qwen3.8-flash-next-q2_0",
"Q2_0", 8080)
self.assertEqual(cfg["backend"], "metal")
self.assertEqual(cfg["tokenizer"], str(Path("/p") / "tokenizer"))
self.assertTrue(cfg["exe"] and isinstance(cfg["args"], list))
class HandOver(unittest.TestCase):
def test_darwin_hands_the_run_to_setup_mac(self):
with mock.patch.object(setup.sys, "platform", "darwin"), \
mock.patch.object(setup.subprocess, "call", return_value=0) as call, \
mock.patch.object(setup.sys, "argv", ["setup.py", "--model", "Q2_0", "--yes"]):
self.assertEqual(setup.main(), 0)
cmd = call.call_args[0][0]
self.assertTrue(cmd[1].endswith(str(Path("metal") / "setup_mac.py")))
self.assertEqual(cmd[2:], ["--model", "Q2_0", "--yes"])
def test_other_platforms_never_reach_it(self):
src = (ROOT / "setup.py").read_text(encoding="utf-8")
self.assertIn('if sys.platform == "darwin":', src)
with mock.patch.object(setup, "mac_setup", side_effect=AssertionError("called")), \
mock.patch.object(setup.sys, "platform", "linux"), \
mock.patch.object(setup.sys, "argv", ["setup.py", "--inspect", "x"]), \
mock.patch.object(setup.subprocess, "run", return_value=subprocess.CompletedProcess([], 0)):
with self.assertRaises(SystemExit):
setup.main()
def test_setup_sh_fetches_a_pinned_python_on_macos_only(self):
sh = (ROOT / "setup.sh").read_text(encoding="utf-8")
self.assertIn('[ "$(uname -s)" = Darwin ]', sh)
self.assertIn("83dcb04777c798bd95f3687acf9ae84b09860272ee6bdfb05be8bcf9447f75b8", sh)
if __name__ == "__main__":
unittest.main()