"""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)}) @pytest.mark.parametrize("var, value", [ ("SEMIF_VRAM_CAP_GIB", "0"), ("SEMIF_VRAM_CAP_GIB", "-4"), ("SEMIF_VRAM_CAP_GIB", "nan"), ("SEMIF_VRAM_CAP_GIB", "inf"), ("SEMIF_MAX_TOKENS", "0"), ("SEMIF_MAX_DECISIONS", "-1"), ("SEMIF_MAX_BODY_BYTES", "0"), ("SEMIF_MAX_QUEUE", "0"), ("SEMIF_MAX_TOKENS", "lots"), ("SEMIF_VRAM_CAP_GIB", "twelve"), ]) def test_out_of_range_or_unparseable_values_are_refused_naming_the_variable(var, value): with pytest.raises(ValueError, match=var): Settings.from_env({"SEMIF_API_TOKEN": TOKEN, var: value}) @pytest.mark.parametrize("token", ["x" * 31 + "\n", "x" * 30 + "\r\n", "x" * 32 + "\x00", "x" * 16 + " " + "x" * 16, "é" * 32]) def test_a_token_with_non_visible_ascii_is_refused(token): with pytest.raises(ValueError, match="SEMIF_API_TOKEN"): Settings.from_env({"SEMIF_API_TOKEN": token}) @pytest.mark.parametrize("table", [{"w": True}, {"w": 1e-300}, {"w": 0.04}, {"w": 21}]) def test_a_temperature_must_be_a_real_number_in_range(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)}) @pytest.mark.parametrize("content", [None, "{not json"]) def test_a_missing_or_malformed_calibration_file_is_a_named_startup_error(tmp_path, content): cal = tmp_path / "cal.json" if content is not None: cal.write_text(content) with pytest.raises(ValueError, match="SEMIF_CALIBRATION"): Settings.from_env({"SEMIF_API_TOKEN": TOKEN, "SEMIF_CALIBRATION": str(cal)}) def test_in_range_edges_are_accepted(tmp_path): cal = tmp_path / "cal.json" cal.write_text(json.dumps({"lo": 0.05, "hi": 20})) s = Settings.from_env({"SEMIF_API_TOKEN": "!" + "~" * 31, "SEMIF_VRAM_CAP_GIB": "0.5", "SEMIF_MAX_QUEUE": "1", "SEMIF_CALIBRATION": str(cal)}) assert (s.vram_cap_gib, s.max_queue, s.calibration) == (0.5, 1, {"lo": 0.05, "hi": 20.0})