Files
cad-agent/tests/test_checklist.py
T
gjm d4a9442e30 feat(review): 几何干涉检查 find_collisions(BVH 粗筛 + 容器过滤)
C++(cad_mcp_plugins):
- 新增 bvh.h:header-only 2D BVH(质心中位分割,叶<=4,SelfPairs 交叉对枚举)
- 新增 find_collisions.cpp:粗筛 AABB 相交实体对,附重叠盒 + 逐轴重叠比 ratio
  (= 逐轴重叠长/较短长 之积,退化轴取 1.0),按 ratio 降序 + 分页;
  narrow=true 只对返回页跑 intersectWith 细筛(附 exact)
- 容器过滤(关键):图框块/分区边界会与一切相交,用重叠【度】判定
  deg >= max(exclude_container_deg=8, exclude_container_frac=0.25 * 其他实体数),
  与图幅/缩放无关;被剔除者在 excluded_containers 里如实报告
  (初版用面积判据对多图框并排失效:3 框只占并集 27/13/7% 全部漏过,
   全模型 163 对噪声;改度判据后图框 deg=35/25/23 vs 其余 deg<=3,分离干净)
- cmakelists.txt + 两个 mcp_config.json 注册(28 工具)

agent 侧(Python):
- cad_tools.READ_ONLY_TOOLS += find_collisions(受控子循环可调用)
- checks.check_collisions + run_deterministic_checks(受 CHECK_COLLISIONS 开关,默认关)
- config += CHECK_COLLISIONS=False / COLLISION_MIN_OVERLAP=0.5
- checklist.py / review_checklist.json += 「干涉/碰撞」类别(high)+ 提示词协议
  (先 find_collisions 拿候选 → inspect_region 目视确认;AABB 相交 != 真干涉)

验证:
- verify_collisions.py 17/17 PASS:全模型候选 163→80(全部 ratio=0.000),
  min_overlap=0.5 → 0 对;逐图框 0 对;narrow top-5 中 4 条 exact=true;
  --inject 注入深重叠矩形报出 ratio=0.902 并清理 2/2
- pytest 355 passed(新增 12 项)
- 端到端:LLM 主动调用 find_collisions{min_overlap:0.2,narrow:true,window:<图框>},未误报
2026-10-16 19:46:44 +08:00

113 lines
4.2 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_instructs_inspect_region():
prompt = build_system_prompt(DEFAULT_CHECKLIST, allow_tools=True)
# 放大取证走 inspect_region 这一个语义化入口(不再让 LLM 自己组合裸视口工具)
assert "inspect_region" in prompt
assert "handle" in prompt and "label" in prompt and "window" in prompt
assert "cx" in prompt and "cy" in prompt
assert "从上到下" in prompt # cy 方向必须写清(图像坐标)
assert "inspect-N" in prompt # 返回标签可继续下钻
assert "不需要判断截图够不够清楚" in prompt
def test_build_system_prompt_mentions_collision_tool():
prompt = build_system_prompt(DEFAULT_CHECKLIST, allow_tools=True)
# 干涉排查入口:find_collisions 粗筛候选 + 强调 AABB 相交 ≠ 真干涉
assert "find_collisions" in prompt
assert "包围盒相交" in prompt
assert "不等于真干涉" in prompt
def test_default_checklist_has_collision_category():
titles = [c["title"] for c in DEFAULT_CHECKLIST["categories"]]
assert "干涉/碰撞" in titles
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