"""Settings.from_env. Contract: semif-serve.contract.md ยง Configuration, INV-6.""" import json import pytest from semif_serve.config import DEFAULT_REVISION, Settings TOKEN = "t" * 40 def test_defaults_from_a_minimal_env(): s = Settings.from_env({"SEMIF_API_TOKEN": TOKEN}) assert (s.api_token, s.revision, s.device, s.max_tokens, s.max_decisions) == (TOKEN, DEFAULT_REVISION, "cuda", 4096, 64) assert s.vram_cap_gib is None and s.calibration == {} @pytest.mark.parametrize("env", [{}, {"SEMIF_API_TOKEN": "short"}, {"SEMIF_API_TOKEN": "x" * 31}]) def test_a_missing_or_short_token_is_refused_at_startup(env): with pytest.raises(ValueError, match="SEMIF_API_TOKEN"): Settings.from_env(env) def test_numbers_and_calibration_file_are_parsed(tmp_path): cal = tmp_path / "cal.json" cal.write_text(json.dumps({"triage": 2.5})) s = Settings.from_env({"SEMIF_API_TOKEN": TOKEN, "SEMIF_VRAM_CAP_GIB": "12", "SEMIF_MAX_DECISIONS": "8", "SEMIF_CALIBRATION": str(cal)}) assert (s.vram_cap_gib, s.max_decisions, s.calibration) == (12.0, 8, {"triage": 2.5}) @pytest.mark.parametrize("table", [{"w": 0}, {"w": -1.0}, {"w": "2"}, {"w": float("inf")}, ["w", 2.0]]) def test_a_calibration_table_needs_positive_finite_numbers(tmp_path, table): cal = tmp_path / "cal.json" cal.write_text(json.dumps(table)) with pytest.raises(ValueError, match="SEMIF_CALIBRATION"): Settings.from_env({"SEMIF_API_TOKEN": TOKEN, "SEMIF_CALIBRATION": str(cal)})