feat(s41): PORTFOLIO_MODEL_MAP override + placeholder fail-fast in resolve_model

This commit is contained in:
Kjell Tore Guttormsen 2026-07-15 11:17:06 +02:00
commit eb552f854e
3 changed files with 97 additions and 8 deletions

View file

@ -1,5 +1,8 @@
"""Tests for the backend profiles (D2) — now wired (Fase 2), no longer skeletons."""
import json
from pathlib import Path
import pytest
from agent_framework import BaseChatClient
@ -12,6 +15,19 @@ from portfolio_optimiser.backends import (
resolve_model,
)
# A complete, non-placeholder map for the PORTFOLIO_MODEL_MAP override (S4.1). Both blocks are
# required — resolve_model does no structural validation, so an azure-only map would break local.
_VALID_MAP = {
"local": {"default": "qwen3:4b", "proposer": "qwen3:4b", "checker": "qwen3:4b"},
"azure": {"default": "gpt-4o-mini", "proposer": "gpt-4o-mini", "checker": "gpt-4o-mini"},
}
def _write_map(tmp_path: Path, data: dict) -> Path:
p = tmp_path / "model_map.json"
p.write_text(json.dumps(data), encoding="utf-8")
return p
def test_get_backend_by_string() -> None:
assert isinstance(get_backend("azure"), AzureFoundryBackend)
@ -48,7 +64,34 @@ def test_azure_backend_fails_fast_without_endpoint(monkeypatch: pytest.MonkeyPat
get_backend("azure").create_chat_client(model="dummy-deployment")
def test_model_map_resolves_role_to_model() -> None:
def test_model_map_resolves_role_to_model(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("PORTFOLIO_MODEL_MAP", raising=False)
assert resolve_model("local", "proposer") == "qwen3:4b"
# Unknown role falls back to the profile default (still a non-empty id).
assert resolve_model(Profile.LOCAL, "no-such-role")
def test_resolve_model_azure_placeholder_fails_fast(monkeypatch: pytest.MonkeyPatch) -> None:
# SC4: the bundled azure block ships REPLACE-WITH-* placeholders → never build a client from
# one. Detach point: remove the _PLACEHOLDER_PREFIX guard in resolve_model → this goes GREEN
# (returns the placeholder) → RED here.
monkeypatch.delenv("PORTFOLIO_MODEL_MAP", raising=False)
with pytest.raises(ValueError, match="placeholder"):
resolve_model("azure", "proposer")
def test_resolve_model_override_redirects_azure(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# SC5: PORTFOLIO_MODEL_MAP points at an out-of-tree valid map → resolves the real id (control:
# the raise fires only on the placeholder, not always). The committed model_map.json is never
# touched.
monkeypatch.setenv("PORTFOLIO_MODEL_MAP", str(_write_map(tmp_path, _VALID_MAP)))
assert resolve_model("azure", "proposer") == "gpt-4o-mini"
assert resolve_model("local", "proposer") == "qwen3:4b"
def test_resolve_model_override_missing_file_fails_fast(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("PORTFOLIO_MODEL_MAP", "/nonexistent/model_map.json")
with pytest.raises(FileNotFoundError):
resolve_model("azure", "proposer")