Files
cad-agent/tests/test_checklist.py
T
gjm e8140f2bc5 feat(review): 审查阶段对齐绘图区分辨率 + 放大取证闭环
发现的缺口:maximized_drawing_area 原先只包住取证循环,审查阶段没被包住 →
LLM 在受控子循环里 zoom + 截图拿到的是未最大化的 2110x462(正是上一轮刚解决的
低分辨率问题,在最需要放大看细节的审查阶段又出现了);且审查中的 zoom 不复位视口,
跑完把用户的 CAD 留在最后一次放大处。

改动:
1. __main__.run_review:把 maximized_drawing_area 从「只包取证」扩到「包取证 + 审查」
   (两者都要截图);report/annotate 留在 with 之外。
2. phases/review.py:新增 _VIEW_TOOLS;review_frame 拆成外壳 + _review_frame,
   外壳 try/finally 里仅当确实动过视口才复位到该图框整幅视图
   (zoom_to_window(win, margin=0.02))。纯查结构化数据的路径不受影响。
3. checklist.build_system_prompt:把「可调用只读工具」改写成明确的放大取证协议——
   截图看不清时用 zoom_to_window(坐标由图框/实体 bbox 算)或 zoom_to_entity,
   再 get_viewport_screenshot 复核;并约束不要为看图而反复放大。

次生问题(同轮修复):_MAX_OBS_IMAGES 原为硬编码 3,而模型一轮内会发 8 次截图 →
拍了 8 张只回灌 3 张。改为配置项 REVIEW_OBS_IMAGES(默认 6),并把预算耗尽的占位
文案改成明确告知模型「不要再请求截图,请给出结论」。

实测(真实图纸 1 图框,新增 verify_review_maximized.py):取证与审查两阶段窗口均为
[-4,-2,3444,1442](最大化态),结束精确还原 [68,0,2316,960],三项全 PASS;
子循环里 LLM 连发 8 组 zoom_to_window + get_viewport_screenshot,提示词生效。
pytest 304 passed。
2026-10-16 18:55:31 +08:00

97 lines
3.4 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_zoom_when_unreadable():
prompt = build_system_prompt(DEFAULT_CHECKLIST, allow_tools=True)
assert "看不清" in prompt
assert "zoom_to_window" in prompt
assert "zoom_to_entity" in prompt
assert "get_viewport_screenshot" in prompt
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