"""模型配置解析测试:数据库默认模型与凭据注入、环境变量优先级。""" from __future__ import annotations import asyncio from pathlib import Path from bot042.agent.base import _build_model from bot042.config import Settings from bot042.db import repo from bot042.db.schema import init_db from bot042.flows.model_settings import resolve_model_settings async def _seed(db: Path) -> None: await init_db(db) await repo.update_provider( db, provider_id=1, base_url="https://gw.example.com/v1", api_key="sk-from-db", ) await repo.add_model(db, provider="openai", name="gw-model", is_default=True) def test_db_config_fills_model_and_credentials(tmp_path: Path) -> None: db = tmp_path / "bot.db" async def _run() -> Settings: await _seed(db) return await resolve_model_settings(Settings(home=tmp_path), db) resolved = asyncio.run(_run()) assert resolved.model == "openai:gw-model" assert resolved.api_key == "sk-from-db" assert resolved.base_url == "https://gw.example.com/v1" def test_env_model_wins_and_skips_mismatched_credentials(tmp_path: Path) -> None: db = tmp_path / "bot.db" async def _run() -> Settings: await _seed(db) settings = Settings( home=tmp_path, model="openai:gpt-4o-mini", model_explicit=True, ) return await resolve_model_settings(settings, db) resolved = asyncio.run(_run()) # 模型以环境变量为准;库里的 key 属于另一个模型,不能挪用 assert resolved.model == "openai:gpt-4o-mini" assert resolved.api_key is None assert resolved.base_url is None def test_db_sets_agent_max_steps(tmp_path: Path) -> None: db = tmp_path / "bot.db" async def _run() -> Settings: await _seed(db) await repo.update_setting(db, "agent.max_steps", value="77") return await resolve_model_settings(Settings(home=tmp_path), db) assert asyncio.run(_run()).max_steps == 77 def test_env_max_steps_wins_over_db(tmp_path: Path) -> None: db = tmp_path / "bot.db" async def _run() -> Settings: await _seed(db) await repo.update_setting(db, "agent.max_steps", value="77") settings = Settings(home=tmp_path, max_steps=5, max_steps_explicit=True) return await resolve_model_settings(settings, db) assert asyncio.run(_run()).max_steps == 5 def test_invalid_db_max_steps_keeps_default(tmp_path: Path) -> None: db = tmp_path / "bot.db" async def _run() -> Settings: await _seed(db) await repo.update_setting(db, "agent.max_steps", value="abc") return await resolve_model_settings(Settings(home=tmp_path), db) assert asyncio.run(_run()).max_steps == 200 def test_missing_db_keeps_env_settings(tmp_path: Path) -> None: settings = Settings(home=tmp_path, api_key="sk-env") resolved = asyncio.run(resolve_model_settings(settings, tmp_path / "none.db")) assert resolved == settings def test_build_model_uses_credentials(tmp_path: Path) -> None: settings = Settings( home=tmp_path, model="openai:gw-model", api_key="sk-x", base_url="https://gw.example.com/v1", ) model = _build_model(settings) assert model.model_name == "gw-model" if hasattr(model, "openai_api_base"): assert model.openai_api_base == "https://gw.example.com/v1" def test_build_model_falls_back_to_string(tmp_path: Path) -> None: settings = Settings(home=tmp_path, model="openai:gpt-4o-mini") assert _build_model(settings) == "openai:gpt-4o-mini"