feat: 重构处理历史与通知留痕追踪

This commit is contained in:
2026-06-04 00:49:33 +08:00
parent d0841e533f
commit 77d9420d43
13 changed files with 431 additions and 96 deletions

View File

@@ -1,8 +1,8 @@
from django.urls import reverse
from agent_core.results import AgentResult
from apps.audit.models import AgentAuditLog, DemoBusinessRecord
from apps.audit.services import create_audit_log
from apps.audit.models import AgentAuditLog, DemoBusinessRecord, NotificationRecord
from apps.audit.services import create_audit_log, create_notification_record
from agent_core.tools.builtin_tools import query_demo_records
@@ -117,3 +117,80 @@ def test_query_demo_records_reads_demo_business_record_table(db):
assert result["records"][0]["title"] == "A线缺陷"
assert result["records"][0]["payload"] == {"rate": 0.12}
def test_audit_log_records_batch_conversation_and_product_context(db):
result = AgentResult(answer="回答", status="success")
log = create_audit_log(
"document_review",
"注册审核智能体",
"开始审核",
result,
batch_id="SUB-20260604-001",
conversation_id="conv-001",
product_name="新型冠状病毒 2019-nCoV 核酸检测试剂盒",
)
assert log.batch_id == "SUB-20260604-001"
assert log.conversation_id == "conv-001"
assert log.product_name == "新型冠状病毒 2019-nCoV 核酸检测试剂盒"
def test_create_notification_record_persists_task_completed_and_task_failed(db):
completed = create_notification_record(
batch_id="SUB-20260604-001",
conversation_id="conv-001",
product_name="产品A",
trigger_source="risk_report",
notify_reason="task_completed",
owner_role="注册资料负责人",
feishu_user_id="ou_demo_1",
message_status="sent",
web_detail_url="https://example.com/detail/1",
receipt={"message_id": "msg-1"},
)
failed = create_notification_record(
batch_id="SUB-20260604-001",
conversation_id="conv-001",
product_name="产品A",
trigger_source="risk_report",
notify_reason="task_failed",
owner_role="注册资料负责人",
feishu_user_id="ou_demo_1",
message_status="failed",
web_detail_url="https://example.com/detail/1",
receipt={"message_id": "msg-2"},
)
assert NotificationRecord.objects.count() == 2
assert completed.notify_reason == "task_completed"
assert failed.notify_reason == "task_failed"
def test_audit_list_supports_batch_and_product_filters(client, db):
create_audit_log(
"document_review",
"注册审核智能体",
"问题一",
AgentResult(answer="回答一", status="success"),
batch_id="SUB-20260604-001",
conversation_id="conv-001",
product_name="产品A",
)
create_audit_log(
"document_review",
"注册审核智能体",
"问题二",
AgentResult(answer="回答二", status="success"),
batch_id="SUB-20260604-002",
conversation_id="conv-002",
product_name="产品B",
)
response = client.get(reverse("audit:list"), {"keyword": "产品A"})
content = response.content.decode("utf-8")
assert response.status_code == 200
assert "产品A" in content
assert "产品B" not in content