120 lines
3.8 KiB
Python
120 lines
3.8 KiB
Python
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 agent_core.tools.builtin_tools import query_demo_records
|
|
|
|
|
|
def test_create_audit_log_records_success_result(db):
|
|
result = AgentResult(answer="回答", structured_output={"x": 1}, status="success")
|
|
|
|
log = create_audit_log("knowledge_qa", "知识库问答助手", "问题", result)
|
|
|
|
assert AgentAuditLog.objects.count() == 1
|
|
assert log.final_answer == "回答"
|
|
assert log.structured_output == {"x": 1}
|
|
assert log.status == "success"
|
|
|
|
|
|
def test_audit_list_page_shows_log(client, db):
|
|
result = AgentResult(answer="回答", status="success")
|
|
create_audit_log("knowledge_qa", "知识库问答助手", "问题", result)
|
|
|
|
response = client.get(reverse("audit:list"))
|
|
|
|
assert response.status_code == 200
|
|
assert "知识库问答助手" in response.content.decode("utf-8")
|
|
|
|
|
|
def test_audit_list_can_filter_by_scenario(client, db):
|
|
create_audit_log(
|
|
"knowledge_qa",
|
|
"知识库问答助手",
|
|
"制度问题",
|
|
AgentResult(answer="回答一", status="success"),
|
|
)
|
|
create_audit_log(
|
|
"quality_analysis",
|
|
"质量异常分析助手",
|
|
"质量问题",
|
|
AgentResult(answer="回答二", status="success"),
|
|
)
|
|
|
|
response = client.get(reverse("audit:list"), {"scenario_id": "knowledge_qa"})
|
|
|
|
content = response.content.decode("utf-8")
|
|
assert response.status_code == 200
|
|
assert "知识库问答助手" in content
|
|
assert "质量异常分析助手" not in content
|
|
|
|
|
|
def test_audit_list_page_shows_user_input_summary(client, db):
|
|
create_audit_log(
|
|
"knowledge_qa",
|
|
"知识库问答助手",
|
|
"这是一个比较长的用户输入,用于确认列表页会展示输入摘要。",
|
|
AgentResult(answer="回答", status="success"),
|
|
)
|
|
|
|
response = client.get(reverse("audit:list"))
|
|
|
|
assert "这是一个比较长的用户输入" in response.content.decode("utf-8")
|
|
|
|
|
|
def test_audit_detail_page_shows_raw_output(client, db):
|
|
result = AgentResult(
|
|
answer="结构化回答",
|
|
raw_output='{"answer":"结构化回答","confidence":"high"}',
|
|
status="success",
|
|
)
|
|
log = create_audit_log("knowledge_qa", "知识库问答助手", "问题", result)
|
|
|
|
response = client.get(reverse("audit:detail", args=[log.id]))
|
|
|
|
content = response.content.decode("utf-8")
|
|
assert response.status_code == 200
|
|
assert "原始输出" in content
|
|
assert "confidence" in content
|
|
assert "high" in content
|
|
|
|
|
|
def test_create_audit_log_masks_api_keys_from_error_message(db):
|
|
result = AgentResult(
|
|
answer="",
|
|
status="failed",
|
|
error="LLM_API_KEY=sk-secret-value 调用失败",
|
|
)
|
|
|
|
log = create_audit_log("knowledge_qa", "知识库问答助手", "问题", result)
|
|
|
|
assert "sk-secret-value" not in log.error_message
|
|
assert "sk-***" in log.error_message
|
|
|
|
|
|
def test_create_audit_log_masks_embedding_api_keys_from_error_message(db):
|
|
result = AgentResult(
|
|
answer="",
|
|
status="failed",
|
|
error="EMBEDDING_API_KEY=embed-secret 调用失败",
|
|
)
|
|
|
|
log = create_audit_log("knowledge_qa", "知识库问答助手", "问题", result)
|
|
|
|
assert "embed-secret" not in log.error_message
|
|
assert "EMBEDDING_API_KEY=***" in log.error_message
|
|
|
|
|
|
def test_query_demo_records_reads_demo_business_record_table(db):
|
|
DemoBusinessRecord.objects.create(
|
|
scenario_id="quality_analysis",
|
|
record_type="defect",
|
|
title="A线缺陷",
|
|
payload={"rate": 0.12},
|
|
)
|
|
|
|
result = query_demo_records(user_input="quality_analysis defect")
|
|
|
|
assert result["records"][0]["title"] == "A线缺陷"
|
|
assert result["records"][0]["payload"] == {"rate": 0.12}
|