后端新增风险图谱算法模块、风险观察与反馈服务、规则 DSL 校验器和可解释性引擎,完善系统仪表盘和财务仪表盘统计, 优化 agent 运行和编排执行链路,清理旧开发文档,前端新增 系统趋势、负载热力图等多种仪表盘图表组件,完善操作反馈 对话框和工作台日期选择器,优化报销创建和审批详情交互, 补充单元测试覆盖。
124 lines
4.9 KiB
Python
124 lines
4.9 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
from sqlalchemy.pool import StaticPool
|
|
|
|
from app.core.agent_enums import AgentAssetDomain, AgentAssetStatus
|
|
from app.db.base import Base
|
|
from app.models.agent_asset import AgentAsset, AgentAssetVersion
|
|
from app.schemas.agent_asset import (
|
|
AgentAssetRiskRuleDraftUpdate,
|
|
AgentAssetRiskRuleGenerateRequest,
|
|
AgentAssetRiskRuleRevisionCreate,
|
|
)
|
|
from app.services.agent_asset_rule_library import AgentAssetRuleLibraryManager
|
|
from app.services.risk_rule_generation import RiskRuleGenerationService
|
|
from app.services.agent_asset_risk_rule_revision import AgentAssetRiskRuleRevisionService
|
|
|
|
|
|
class NullRuntimeChatService:
|
|
def complete(self, *args, **kwargs) -> None:
|
|
return None
|
|
|
|
|
|
def build_session() -> Session:
|
|
engine = create_engine(
|
|
"sqlite+pysqlite:///:memory:",
|
|
connect_args={"check_same_thread": False},
|
|
poolclass=StaticPool,
|
|
)
|
|
Base.metadata.create_all(bind=engine)
|
|
return sessionmaker(bind=engine, autoflush=False, autocommit=False)()
|
|
|
|
|
|
def test_update_unpublished_risk_rule_draft_updates_business_fields(tmp_path) -> None:
|
|
with build_session() as db:
|
|
asset_id = _create_rule(db, tmp_path)
|
|
updated = AgentAssetRiskRuleRevisionService(db).update_unpublished_draft(
|
|
asset_id,
|
|
AgentAssetRiskRuleDraftUpdate(
|
|
rule_title="差旅绕行说明校验",
|
|
expense_category="travel",
|
|
natural_language="差旅报销存在绕行但未说明原因时,进入风险复核。",
|
|
requires_attachment=True,
|
|
),
|
|
actor="finance",
|
|
)
|
|
|
|
assert updated.name == "差旅绕行说明校验"
|
|
assert updated.description == "差旅报销存在绕行但未说明原因时,进入风险复核。"
|
|
assert updated.scenario_json == ["差旅费"]
|
|
assert updated.config_json["requires_attachment"] is True
|
|
assert updated.config_json["generation_request"]["natural_language"] == updated.description
|
|
assert updated.config_json["last_operation"]["action"] == "update_draft"
|
|
|
|
|
|
def test_update_published_rule_requires_revision(tmp_path) -> None:
|
|
with build_session() as db:
|
|
asset_id = _create_rule(db, tmp_path)
|
|
asset = db.get(AgentAsset, asset_id)
|
|
assert asset is not None
|
|
asset.status = AgentAssetStatus.ACTIVE.value
|
|
asset.published_version = asset.current_version
|
|
db.add(asset)
|
|
db.flush()
|
|
|
|
with pytest.raises(PermissionError):
|
|
AgentAssetRiskRuleRevisionService(db).update_unpublished_draft(
|
|
asset_id,
|
|
AgentAssetRiskRuleDraftUpdate(natural_language="已上线规则不能直接覆盖。"),
|
|
actor="finance",
|
|
)
|
|
|
|
|
|
def test_create_revision_draft_for_published_rule_does_not_overwrite_active_version(tmp_path) -> None:
|
|
with build_session() as db:
|
|
asset_id = _create_rule(db, tmp_path)
|
|
asset = db.get(AgentAsset, asset_id)
|
|
assert asset is not None
|
|
asset.status = AgentAssetStatus.ACTIVE.value
|
|
asset.published_version = "v0.1.0"
|
|
asset.current_version = "v0.1.0"
|
|
asset.working_version = "v0.1.0"
|
|
db.add(asset)
|
|
db.flush()
|
|
|
|
updated = AgentAssetRiskRuleRevisionService(db).create_revision_draft(
|
|
asset_id,
|
|
AgentAssetRiskRuleRevisionCreate(
|
|
rule_title="差旅票据城市复核",
|
|
natural_language="票据城市与申报目的地不一致时,要求补充说明。",
|
|
requires_attachment=True,
|
|
change_reason="补充城市一致性判断。",
|
|
),
|
|
actor="manager",
|
|
)
|
|
|
|
revision = updated.config_json["revision_draft"]
|
|
assert updated.status == AgentAssetStatus.ACTIVE.value
|
|
assert updated.published_version == "v0.1.0"
|
|
assert updated.working_version == "v0.1.1"
|
|
assert revision["version"] == "v0.1.1"
|
|
assert revision["base_version"] == "v0.1.0"
|
|
assert revision["generation_request"]["natural_language"] == "票据城市与申报目的地不一致时,要求补充说明。"
|
|
assert updated.config_json["last_operation"]["action"] == "create_revision"
|
|
assert db.query(AgentAssetVersion).filter_by(asset_id=asset_id, version="v0.1.1").one()
|
|
|
|
|
|
def _create_rule(db: Session, tmp_path) -> str:
|
|
return RiskRuleGenerationService(
|
|
db,
|
|
rule_library_manager=AgentAssetRuleLibraryManager(rule_root=tmp_path / "rules"),
|
|
runtime_chat_service=NullRuntimeChatService(),
|
|
).generate_rule_asset(
|
|
AgentAssetRiskRuleGenerateRequest(
|
|
business_domain=AgentAssetDomain.EXPENSE,
|
|
expense_category="travel",
|
|
rule_title="差旅规则草稿",
|
|
natural_language="差旅报销事由缺失时,提示补充说明。",
|
|
),
|
|
actor="pytest",
|
|
)
|