ForcePilot/backend/test/test_chat_router_configs.py
copilot-swe-agent[bot] fb05161f10
fix: 修复智能体配置默认创建缺少agent_id参数
Agent-Logs-Url: https://github.com/xerrors/Yuxi/sessions/933d6368-5424-4327-99cf-3722768afa57

Co-authored-by: xerrors <35524243+xerrors@users.noreply.github.com>
2026-03-30 07:18:33 +00:00

71 lines
2.1 KiB
Python

from __future__ import annotations
from fastapi import FastAPI
from fastapi.testclient import TestClient
from server.routers.chat_router import chat
from server.utils.auth_middleware import get_db, get_required_user
from yuxi.storage.postgres.models_business import User
def _build_app() -> FastAPI:
app = FastAPI()
app.include_router(chat, prefix="/api")
async def fake_db():
return None
async def fake_required_user():
return User(
username="admin",
user_id="admin",
password_hash="x",
role="admin",
department_id=1,
id=1,
)
app.dependency_overrides[get_db] = fake_db
app.dependency_overrides[get_required_user] = fake_required_user
return app
def test_list_agent_configs_creates_default_with_agent_id(monkeypatch):
agent_id = "demo-agent"
captured: dict[str, object] = {}
class DummyConfig:
id = 1
name = "初始配置"
description = None
icon = None
pics = []
examples = []
is_default = True
class DummyRepo:
def __init__(self, _db):
self.calls = 0
async def list_by_department_agent(self, *, department_id, agent_id):
self.calls += 1
if self.calls == 1:
return []
return [DummyConfig()]
async def get_or_create_default(self, *, department_id, agent_id, created_by=None):
captured["department_id"] = department_id
captured["agent_id"] = agent_id
captured["created_by"] = created_by
return None
monkeypatch.setattr("server.routers.chat_router.agent_manager.get_agent", lambda _agent_id: object())
monkeypatch.setattr("server.routers.chat_router.AgentConfigRepository", DummyRepo)
client = TestClient(_build_app())
resp = client.get(f"/api/chat/agent/{agent_id}/configs")
assert resp.status_code == 200, resp.text
assert captured["agent_id"] == agent_id
assert captured["department_id"] == 1
assert captured["created_by"] == "1"