2026-10-14 19:07:27 +08:00
|
|
|
"""`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
|
|
|
|
|
|
|
|
|
|
|
2026-10-16 19:15:32 +08:00
|
|
|
def test_build_system_prompt_instructs_inspect_region():
|
2026-10-16 18:55:31 +08:00
|
|
|
prompt = build_system_prompt(DEFAULT_CHECKLIST, allow_tools=True)
|
2026-10-16 19:15:32 +08:00
|
|
|
# 放大取证走 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
|
2026-10-16 18:55:31 +08:00
|
|
|
|
|
|
|
|
|
2026-10-14 19:53:41 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2026-10-14 19:07:27 +08:00
|
|
|
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
|