AgentEvalTool/tests/unit/test_intelligent_eval_report.py
sinohqb eb4944a8bd feat(intelligent-eval): terminal-state discipline watchdogs (ADR-0011)
常见故障自愈有上限,超限收敛终态且可见:任务 attempts 上限、会话过期、
planning 双闸、executing 超窗兜底、触发失败计数判死、孤儿 agent 双管、
fire-and-forget 触发;open_session 预算硬闸门、settle 按终态区分、报告
scores 归一化;cron 池遗留面全删。
2026-08-20 14:34:17 +08:00

117 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Unit tests for the pure Markdown report renderer (票据 04)."""
import pytest
from agenteval.intelligent_eval.report import render_report_markdown
from pydantic import ValidationError
def _report() -> dict:
return {
"summary": "整体表现良好,退货流程存在确认缺失。",
"scores": {"退货流程": 0.7, "投诉处理": 0.85},
"findings": [
{
"issue": "未主动确认订单号",
"severity": "high",
"dimension": "退货流程",
"evidence": [
{
"session_id": "s-1",
"turn_index": 3,
"user_said": "我要退货",
"assistant_replied": "好的,已为您发起",
}
],
"suggestion": "增加订单号确认步骤",
"related_sop": "退货处理流程 §3.2",
},
{
"issue": "投诉共情不足",
"severity": "low",
"dimension": "投诉处理",
},
],
"highlights": [{"description": "多轮追问保持上下文连贯", "dimension": "多轮追问"}],
"priority_recommendations": ["先修退货确认", "再优化投诉共情"],
}
def test_renders_title_and_summary():
md = render_report_markdown(_report(), name="客服评估", eval_id="e-1")
assert "# 客服评估" in md
assert "整体表现良好" in md
assert "`e-1`" in md
def test_renders_scores_table():
md = render_report_markdown(_report())
assert "| 维度 | 分数 |" in md
assert "| 退货流程 | 0.7 |" in md
def test_sorts_findings_by_severity():
md = render_report_markdown(_report())
high_index = md.index("未主动确认订单号")
low_index = md.index("投诉共情不足")
assert high_index < low_index
def test_renders_evidence_block():
md = render_report_markdown(_report())
assert "会话 `s-1`" in md
assert "第 3 轮" in md
assert "**用户**:我要退货" in md
assert "**对象**:好的,已为您发起" in md
def test_renders_highlights_and_recommendations():
md = render_report_markdown(_report())
assert "多轮追问保持上下文连贯(多轮追问)" in md
assert "- 先修退货确认" in md
def test_minimal_report():
md = render_report_markdown({"summary": "只有概述", "findings": [{"issue": "i", "severity": "s", "dimension": "d"}]})
assert "只有概述" in md
assert "## 维度评分" not in md
assert "## 亮点" not in md
def test_rejects_missing_summary():
with pytest.raises(ValidationError):
render_report_markdown({"findings": [{"issue": "i", "severity": "s", "dimension": "d"}]})
def test_rejects_empty_findings():
with pytest.raises(ValidationError):
render_report_markdown({"summary": "x", "findings": []})
def test_rejects_finding_missing_required_fields():
with pytest.raises(ValidationError):
render_report_markdown({"summary": "x", "findings": [{"issue": "i"}]})
def test_scores_normalized_from_flat_to_canonical():
"""ADR-0011扁平 scores 归一为 {overall, dimensions}overall 取维度平均。"""
from agenteval.intelligent_eval.report import normalize_scores
normalized = normalize_scores({"退货流程": 0.6, "投诉处理": 0.8})
assert normalized == {"overall": 0.7, "dimensions": {"退货流程": 0.6, "投诉处理": 0.8}}
def test_scores_normalized_nested_preserved():
from agenteval.intelligent_eval.report import normalize_scores
normalized = normalize_scores({"overall": 0.9, "dimensions": {"退货流程": 0.9}})
assert normalized == {"overall": 0.9, "dimensions": {"退货流程": 0.9}}
def test_scores_drops_non_numeric_and_nan():
from agenteval.intelligent_eval.report import normalize_scores
normalized = normalize_scores({"退货流程": 0.7, "备注": "", "overall": "", "": float("nan")})
assert normalized == {"overall": 0.7, "dimensions": {"退货流程": 0.7}}
assert normalize_scores(None) is None
assert normalize_scores({}) == {}