117 lines
5.6 KiB
Python
117 lines
5.6 KiB
Python
"""真实 MySQL:意图配置激活后必须被运行期分类链路读到。
|
|
|
|
覆盖两件事(都不改库结构,只写数据):
|
|
1. 状态机可用:`draft --reviews--> approved --activations--> active`,激活写
|
|
`reviewer_id/reviewed_at/effective_at`,并把同 `agent_type:intent_code` 的旧 active
|
|
版本归档(生成列唯一键 `uk_intent_config_active_one` 不允许两个 active);
|
|
2. 运行期真的读它:`load_active_intent_configs`(分类链路的装载器)按 `agent_type`
|
|
返回 active 行,draft 行不返回,归档后不再返回。
|
|
"""
|
|
|
|
import uuid
|
|
from datetime import UTC, datetime
|
|
from decimal import Decimal
|
|
|
|
import pytest
|
|
from sqlalchemy import text
|
|
|
|
from app.core.contracts import RequestContext
|
|
from app.infrastructure.db import SessionFactory
|
|
from app.repository.platform_repository import PlatformRepository
|
|
from app.service.admin_service import AdminService
|
|
from app.service.runtime_config_service import load_active_intent_configs
|
|
|
|
pytestmark = pytest.mark.integration
|
|
|
|
INTENT = "fund_quote"
|
|
|
|
|
|
def now() -> datetime:
|
|
return datetime.now(UTC).replace(tzinfo=None)
|
|
|
|
|
|
async def test_activation_state_machine_feeds_runtime_intent_config() -> None:
|
|
agent_type = f"it_intent_{uuid.uuid4().hex[:12]}"
|
|
actor = uuid.uuid4().int % 10**12 + 10**15
|
|
async with SessionFactory() as session, session.begin():
|
|
await session.execute(text("""
|
|
INSERT INTO sys_user
|
|
(id,user_no,username,password_hash,user_type,professional_investor_status,
|
|
fund_account_status,status,created_at,updated_at)
|
|
VALUES (:id,:name,:name,'test-only','员工','未申请','未开户','正常',
|
|
UTC_TIMESTAMP(),UTC_TIMESTAMP())
|
|
"""), {"id": actor, "name": f"it-intent-{actor}"})
|
|
try:
|
|
context = RequestContext(user_id=str(actor), trace_id="it-intent", roles=("admin",))
|
|
service = AdminService()
|
|
async with SessionFactory() as session:
|
|
repo = PlatformRepository(session)
|
|
draft = await repo.create("agent_intent_config", {
|
|
"agent_type": agent_type, "intent_code": INTENT, "intent_name": "旧版本意图",
|
|
"description": "旧描述", "examples": ["旧示例"],
|
|
"confidence_threshold": Decimal("0.9000"), "version": 1, "created_by": actor,
|
|
})
|
|
# 另建一个只停留在 draft 的意图,用来证明运行期不读非 active 行。
|
|
await repo.create("agent_intent_config", {
|
|
"agent_type": agent_type, "intent_code": "draft_only", "intent_name": "草稿意图",
|
|
"description": "草稿描述", "examples": [], "version": 1, "created_by": actor,
|
|
})
|
|
approved = await service._transition(
|
|
repo, "agent-intent-configs", draft, "reviews", {"decision": "approved"}, context
|
|
)
|
|
assert approved["status"] == "approved"
|
|
assert int(approved["reviewer_id"]) == actor
|
|
activated = await service._transition(
|
|
repo, "agent-intent-configs", approved, "activations", {}, context
|
|
)
|
|
assert activated["status"] == "active"
|
|
assert activated["effective_at"] is not None
|
|
await session.commit()
|
|
|
|
# 运行期装载器走独立会话:只有已提交的 active 行可见,draft 行不可见。
|
|
entries = await load_active_intent_configs(agent_type)
|
|
assert [entry.intent_code for entry in entries] == [INTENT]
|
|
assert entries[0].description == "旧描述"
|
|
assert entries[0].confidence_threshold == 0.9
|
|
|
|
# 新版本激活:旧 active 版本被归档,装载器只看到新版本。
|
|
async with SessionFactory() as session:
|
|
repo = PlatformRepository(session)
|
|
newer = await repo.create("agent_intent_config", {
|
|
"agent_type": agent_type, "intent_code": INTENT, "intent_name": "新版本意图",
|
|
"description": "新描述", "examples": ["新示例"],
|
|
"confidence_threshold": Decimal("0.6000"), "version": 2, "created_by": actor,
|
|
})
|
|
approved_new = await service._transition(
|
|
repo, "agent-intent-configs", newer, "reviews", {"decision": "approved"}, context
|
|
)
|
|
await service._transition(
|
|
repo, "agent-intent-configs", approved_new, "activations", {}, context
|
|
)
|
|
archived = await repo.get("agent_intent_config", draft["id"])
|
|
assert archived is not None and archived["status"] == "archived"
|
|
await session.commit()
|
|
|
|
entries = await load_active_intent_configs(agent_type)
|
|
assert [(entry.description, entry.confidence_threshold) for entry in entries] == [
|
|
("新描述", 0.6)
|
|
]
|
|
|
|
# 归档生效版本后,运行期不再读到它(归档是明确的失效动作)。
|
|
async with SessionFactory() as session:
|
|
repo = PlatformRepository(session)
|
|
current = await repo.get("agent_intent_config", newer["id"])
|
|
assert current is not None
|
|
await service._transition(
|
|
repo, "agent-intent-configs", current, "archivals", {}, context
|
|
)
|
|
await session.commit()
|
|
assert await load_active_intent_configs(agent_type) == ()
|
|
finally:
|
|
async with SessionFactory() as session, session.begin():
|
|
await session.execute(
|
|
text("DELETE FROM agent_intent_config WHERE agent_type=:agent"),
|
|
{"agent": agent_type},
|
|
)
|
|
await session.execute(text("DELETE FROM sys_user WHERE id=:id"), {"id": actor})
|