Files
cad-agent/tests/test_checklist.py
T
gjm f7fa8f14d9 feat(review): 审查结论改为结构化 JSON,移除二次 LLM 抽取
- checklist.py:系统提示加入结构化输出契约(只输出 {summary, issues[]},
  引用 handle 必须真实存在、不得编造)
- review.py:review_frame 返回 {summary, issues, raw};仅在「不再请求工具」的回合
  解析结论,中间工具回合的 content 永不解析;强制收口那次 chat 不传 tools
- report.py:删除 extract_issues 与 _EXTRACT_SYSTEM(不再需要第二次 LLM 调用);
  新增 format_finding(用 .get,顺带修复缺 frame 键 KeyError);save_report 的
  issues 省略时从 findings 拍平
- __main__.py:主流程改为 确定性检查 → review_frame → build_finding(合并+校验),
  issues 由 flatten_issues 派生

顺带修复既有 gap:summarize 硬下标 KeyError、extract_issues 单行围栏解析失败。
2026-10-14 19:53:41 +08:00

89 lines
3.1 KiB
Python

"""`dwg_review.checklist` 单元测试。"""
import json
from dwg_review.checklist import (
DEFAULT_CHECKLIST,
build_system_prompt,
checklist_summary,
load_checklist,
)
def test_load_valid_checklist(tmp_path):
data = {
"name": "自定义清单",
"categories": [{"title": "类别A", "severity": "high", "checks": ["检查1"]}],
}
path = tmp_path / "cl.json"
path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
loaded = load_checklist(path)
assert loaded["name"] == "自定义清单"
assert loaded["categories"][0]["title"] == "类别A"
def test_load_missing_file_falls_back(tmp_path, capsys):
loaded = load_checklist(tmp_path / "nope.json")
assert loaded is DEFAULT_CHECKLIST
assert "无法加载审查清单" in capsys.readouterr().out
def test_load_malformed_structure_falls_back(tmp_path, capsys):
path = tmp_path / "cl.json"
path.write_text(json.dumps({"name": "缺 categories"}), encoding="utf-8")
assert load_checklist(path) is DEFAULT_CHECKLIST
assert "结构异常" in capsys.readouterr().out
def test_load_invalid_json_falls_back(tmp_path, capsys):
path = tmp_path / "cl.json"
path.write_text("{ not json", encoding="utf-8")
assert load_checklist(path) is DEFAULT_CHECKLIST
assert "无法加载审查清单" in capsys.readouterr().out
def test_load_empty_categories_falls_back(tmp_path):
path = tmp_path / "cl.json"
path.write_text(json.dumps({"categories": []}), encoding="utf-8")
assert load_checklist(path) is DEFAULT_CHECKLIST
def test_build_system_prompt_includes_categories_and_checks():
prompt = build_system_prompt(DEFAULT_CHECKLIST)
assert DEFAULT_CHECKLIST["name"] in prompt
for cat in DEFAULT_CHECKLIST["categories"]:
assert cat["title"] in prompt
assert f"严重度:{cat['severity']}" in prompt
for chk in cat["checks"]:
assert chk in prompt
def test_build_system_prompt_tool_hint_toggle():
with_tools = build_system_prompt(DEFAULT_CHECKLIST, allow_tools=True)
without = build_system_prompt(DEFAULT_CHECKLIST, allow_tools=False)
assert "只读" in with_tools and "取证工具" in with_tools
assert "只读" not in without
def test_build_system_prompt_requires_structured_json_output():
# allow_tools=False 以隔离「工具提示块」里的「只读」字样
prompt = build_system_prompt(DEFAULT_CHECKLIST, allow_tools=False)
assert "JSON" in prompt
assert '"summary"' in prompt
assert '"issues"' in prompt
assert '"severity"' in prompt and '"category"' in prompt and '"handle"' in prompt
# 结构化契约本身不得含「只读」,否则会破坏 allow_tools 开关的判别
assert "只读" not in prompt
def test_build_system_prompt_forbids_fabricated_handle():
prompt = build_system_prompt(DEFAULT_CHECKLIST)
assert "不要编造" in prompt
def test_checklist_summary_mentions_name_and_count():
summary = checklist_summary(DEFAULT_CHECKLIST)
assert DEFAULT_CHECKLIST["name"] in summary
assert str(len(DEFAULT_CHECKLIST["categories"])) in summary
assert DEFAULT_CHECKLIST["categories"][0]["title"] in summary