test/config: cover model-level preset routing

This commit is contained in:
2026-07-28 21:45:59 +08:00
parent 3b72698e14
commit c7ee9b0063
5 changed files with 64 additions and 6 deletions
+2 -2
View File
@@ -129,10 +129,10 @@ access_key_or_token = "sk"
usage={},
)
monkeypatch.setattr("plyngent.cli.app.create_client", lambda _p: DummyClient())
monkeypatch.setattr("plyngent.cli.app.create_client", lambda _p, **_kw: DummyClient())
monkeypatch.setattr(
"plyngent.cli.state.create_client",
lambda _p: DummyClient(),
lambda _p, **_kw: DummyClient(),
)
from platformdirs import user_data_path
+24
View File
@@ -189,6 +189,30 @@ models = {}
_ = config.promote_provider("hollow", [])
def test_model_level_preset_url_overrides_parse(tmp_path: Path) -> None:
path = tmp_path / "mixed.toml"
_ = path.write_text(
"""
[providers.mixed]
preset = "openai-compatible"
url = "https://gateway.example/v1"
access_key_or_token = "sk-test"
[providers.mixed.models]
"claude" = { text = true, preset = "anthropic", url = "https://api.anthropic.com/v1" }
"qwen" = { text = true }
""",
encoding="utf-8",
)
config = plyngent.config.load(path)
provider = config.providers["mixed"]
assert isinstance(provider, OpenAICompatibleProvider)
assert provider.models["claude"].preset == "anthropic"
assert provider.models["claude"].url == "https://api.anthropic.com/v1"
assert provider.models["qwen"].preset == ""
assert provider.models["qwen"].url == ""
def test_read_invalid_config() -> None:
with pytest.raises(ConfigFormatError):
_ = plyngent.config.load(Path(__file__).parent / "plyngent-invalid.toml")
+32 -3
View File
@@ -5,6 +5,7 @@ import pytest
from plyngent.config.models import (
AnthropicProvider,
DeepseekProvider,
ModelConfig,
OpenAICompatibleProvider,
OpenAIProvider,
)
@@ -65,16 +66,44 @@ def test_deepseek_openai_convention() -> None:
assert "deepseek-v4-pro" in provider.models
def test_deepseek_anthropic_convention_not_implemented() -> None:
def test_deepseek_ignores_legacy_convention_extra() -> None:
provider = DeepseekProvider(
access_key_or_token="sk-test",
extras={"convention": "anthropic"},
)
with pytest.raises(ProviderNotSupportedError, match="anthropic"):
_ = create_client(provider)
client = create_client(provider)
assert isinstance(client, DeepseekOpenAIClient)
def test_anthropic_not_implemented() -> None:
provider = AnthropicProvider(access_key_or_token="sk-test")
with pytest.raises(ProviderNotSupportedError, match="anthropic"):
_ = create_client(provider)
def test_model_level_preset_url_overrides_parent() -> None:
provider = OpenAICompatibleProvider(
access_key_or_token="sk-test",
url="https://gateway.example/v1",
models={
"gpt": ModelConfig(preset="openai", url="https://api.openai.com/v1"),
"qwen": ModelConfig(),
},
)
openai_client = create_client(provider, model="gpt")
assert openai_client.__class__.__name__ == "ResponsesChatClient"
assert provider_to_openai_config(provider, model="gpt").base_url == "https://api.openai.com/v1"
compat_client = create_client(provider, model="qwen")
assert isinstance(compat_client, OpenAICompatibleClient)
assert provider_to_openai_config(provider, model="qwen").base_url == "https://gateway.example/v1"
def test_model_level_anthropic_preset_not_implemented() -> None:
provider = OpenAICompatibleProvider(
access_key_or_token="sk-test",
url="https://gateway.example/v1",
models={"claude": ModelConfig(preset="anthropic", url="https://api.anthropic.com/v1")},
)
with pytest.raises(ProviderNotSupportedError, match="anthropic"):
_ = create_client(provider, model="claude")