|
| 1 | +"""Promoted config knobs (the 2026-09-01 extensions burn-down). |
| 2 | +
|
| 3 | +service_tier / user_id / store graduated from provider-syntax smuggling |
| 4 | +in config.extensions to canonical Config fields. Wire equality for the |
| 5 | +promoted spellings is pinned by the contract's request direction (the |
| 6 | +rewritten cases build the same frozen bodies); these tests pin the |
| 7 | +mapping table and the no-silent-drop raises. |
| 8 | +""" |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from lm15 import Config, Message, Request, UnsupportedFeatureError |
| 14 | +from lm15.providers import AnthropicLM, GeminiLM, OpenAILM |
| 15 | +from lm15.providers.openai_chat import OpenAIChatLM |
| 16 | +from lm15.serde import config_from_dict, config_to_dict |
| 17 | +from lm15.testing import FakeTransport |
| 18 | + |
| 19 | + |
| 20 | +def req(**config) -> Request: |
| 21 | + return Request(model="m", messages=(Message.user("hi"),), config=Config(**config)) |
| 22 | + |
| 23 | + |
| 24 | +def payload(lm, request): |
| 25 | + return lm._payload(request, stream=False) if not isinstance(lm, GeminiLM) else lm._payload(request) |
| 26 | + |
| 27 | + |
| 28 | +# ─── Mapping table ─────────────────────────────────────────────────── |
| 29 | + |
| 30 | +def test_openai_spellings() -> None: |
| 31 | + lm = OpenAILM(api_key="k", transport=FakeTransport([])) |
| 32 | + body = payload(lm, req(service_tier="flex", user_id="end-user-7", store=False)) |
| 33 | + assert body["service_tier"] == "flex" |
| 34 | + assert body["safety_identifier"] == "end-user-7" # current field; `user` is the deprecated spelling |
| 35 | + assert body["store"] is False |
| 36 | + assert "user" not in body and "user_id" not in body |
| 37 | + |
| 38 | + |
| 39 | +def test_openai_chat_spellings() -> None: |
| 40 | + lm = OpenAIChatLM(api_key="k", transport=FakeTransport([])) |
| 41 | + body = lm._payload(req(service_tier="default", user_id="end-user-7", store=True), stream=False) |
| 42 | + assert body["service_tier"] == "default" |
| 43 | + assert body["user"] == "end-user-7" # Chat Completions dialect spelling |
| 44 | + assert body["store"] is True |
| 45 | + |
| 46 | + |
| 47 | +def test_anthropic_spellings_and_store_raises() -> None: |
| 48 | + lm = AnthropicLM(api_key="k", transport=FakeTransport([])) |
| 49 | + body = lm._payload(req(service_tier="standard_only", user_id="u-1"), stream=False) |
| 50 | + assert body["service_tier"] == "standard_only" |
| 51 | + assert body["metadata"] == {"user_id": "u-1"} |
| 52 | + with pytest.raises(UnsupportedFeatureError, match="store"): |
| 53 | + lm._payload(req(store=False), stream=False) |
| 54 | + |
| 55 | + |
| 56 | +def test_gemini_store_maps_and_others_raise() -> None: |
| 57 | + lm = GeminiLM(api_key="k", transport=FakeTransport([])) |
| 58 | + assert lm._payload(req(store=False))["store"] is False |
| 59 | + with pytest.raises(UnsupportedFeatureError, match="service_tier"): |
| 60 | + lm._payload(req(service_tier="auto")) |
| 61 | + with pytest.raises(UnsupportedFeatureError, match="user_id"): |
| 62 | + lm._payload(req(user_id="u-1")) |
| 63 | + |
| 64 | + |
| 65 | +def test_extensions_still_win_over_promoted_fields() -> None: |
| 66 | + # Passthrough precedence is the existing rule: extensions apply last. |
| 67 | + lm = OpenAILM(api_key="k", transport=FakeTransport([])) |
| 68 | + body = payload(lm, req(service_tier="flex", extensions={"service_tier": "priority"})) |
| 69 | + assert body["service_tier"] == "priority" |
| 70 | + |
| 71 | + |
| 72 | +# ─── Type + serde ──────────────────────────────────────────────────── |
| 73 | + |
| 74 | +def test_config_validation() -> None: |
| 75 | + with pytest.raises(ValueError): |
| 76 | + Config(service_tier="") |
| 77 | + with pytest.raises(ValueError): |
| 78 | + Config(user_id="") |
| 79 | + with pytest.raises(TypeError): |
| 80 | + Config(store="yes") |
| 81 | + |
| 82 | + |
| 83 | +def test_serde_roundtrip_and_false_survives() -> None: |
| 84 | + cfg = Config(service_tier="flex", user_id="u-1", store=False) |
| 85 | + d = config_to_dict(cfg) |
| 86 | + assert d == {"service_tier": "flex", "user_id": "u-1", "store": False} |
| 87 | + assert config_from_dict(d) == cfg |
| 88 | + assert config_to_dict(Config()) == {} # all-default still collapses |
0 commit comments